From eac107382049f9ee35a94468498e5c890cd0b51e Mon Sep 17 00:00:00 2001 From: jessevz Date: Tue, 23 Dec 2025 16:09:47 +0100 Subject: [PATCH 1/2] Made it possible to let the agent use system binaries if they already exists --- htpclient/binarydownload.py | 26 +++++++++----------------- htpclient/files.py | 9 +++------ htpclient/helpers.py | 20 +++++++++++++++++++- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/htpclient/binarydownload.py b/htpclient/binarydownload.py index d6a487e..031b70d 100644 --- a/htpclient/binarydownload.py +++ b/htpclient/binarydownload.py @@ -7,6 +7,7 @@ from htpclient.config import Config from htpclient.download import Download +from htpclient.helpers import retrieveBinary from htpclient.initialize import Initialize from htpclient.jsonRequest import JsonRequest from htpclient.dicts import * @@ -63,7 +64,7 @@ def check_client_version(self): def __check_utils(self): path = '7zr' + Initialize.get_os_extension() - if not os.path.isfile(path): + if not retrieveBinary(path): query = copy_and_set_token(dict_downloadBinary, self.config.get_value('token')) query['type'] = '7zr' req = JsonRequest(query) @@ -80,7 +81,7 @@ def __check_utils(self): Download.download(ans['executable'], path) os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC) path = 'uftpd' + Initialize.get_os_extension() - if not os.path.isfile(path) and self.config.get_value('multicast'): + if not retrieveBinary(path) and self.config.get_value('multicast'): query = copy_and_set_token(dict_downloadBinary, self.config.get_value('token')) query['type'] = 'uftpd' req = JsonRequest(query) @@ -121,10 +122,8 @@ def check_prince(self): logging.error("Download of prince failed!") sleep(5) return False - if Initialize.get_os() == 1: - os.system("7zr" + Initialize.get_os_extension() + " x -otemp prince.7z") - else: - os.system("./7zr" + Initialize.get_os_extension() + " x -otemp prince.7z") + zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension()) + os.system(zr_bin + " x -otemp prince.7z") for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince if os.path.isdir("temp/" + name): os.rename("temp/" + name, "prince") @@ -160,10 +159,8 @@ def check_preprocessor(self, task): logging.error("Download of preprocessor failed!") sleep(5) return False - if Initialize.get_os() == 1: - os.system(f"7zr{Initialize.get_os_extension()} x -otemp temp.7z") - else: - os.system(f"./7zr{Initialize.get_os_extension()} x -otemp temp.7z") + zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension()) + os.system(f"{zr_bin} x -otemp temp.7z") for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince if os.path.isdir(Path('temp', name)): os.rename(Path('temp', name), path) @@ -200,13 +197,8 @@ def check_version(self, cracker_id): # we need to extract the 7zip temp_folder = Path(self.config.get_value('crackers-path'), 'temp') zip_file = Path(self.config.get_value('crackers-path'), f'{cracker_id}.7z') - - if Initialize.get_os() == 1: - # Windows - cmd = f'7zr{Initialize.get_os_extension()} x -o"{temp_folder}" "{zip_file}"' - else: - # Linux - cmd = f"./7zr{Initialize.get_os_extension()} x -o'{temp_folder}' '{zip_file}'" + zrbinary = retrieveBinary("7zr" + Initialize.get_os_extension()) + cmd = f'{zrbinary} x -o"{temp_folder}" "{zip_file}"' os.system(cmd) # Clean up 7zip diff --git a/htpclient/files.py b/htpclient/files.py index 4ac12f9..deef1e1 100644 --- a/htpclient/files.py +++ b/htpclient/files.py @@ -7,6 +7,7 @@ from htpclient.config import Config from htpclient.download import Download +from htpclient.helpers import retrieveBinary from htpclient.initialize import Initialize from htpclient.jsonRequest import JsonRequest from htpclient.dicts import * @@ -106,11 +107,7 @@ def check_files(self, files, task_id): if os.path.splitext(file_localpath)[1] == '.7z' and not os.path.isfile(txt_file): # extract if needed files_path = Path(self.config.get_value('files-path')) - if Initialize.get_os() == 1: - # Windows - cmd = f'7zr{Initialize.get_os_extension()} x -aoa -o"{files_path}" -y "{file_localpath}"' - else: - # Linux - cmd = f"./7zr{Initialize.get_os_extension()} x -aoa -o'{files_path}' -y '{file_localpath}'" + zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension()) + cmd = f'{zr_bin} x -aoa -o"{files_path}" -y "{file_localpath}"' os.system(cmd) return True diff --git a/htpclient/helpers.py b/htpclient/helpers.py index eb4d1df..c5187c7 100644 --- a/htpclient/helpers.py +++ b/htpclient/helpers.py @@ -8,6 +8,7 @@ from pathlib import Path import os +import shutil import subprocess from htpclient.dicts import copy_and_set_token, dict_clientError @@ -66,7 +67,7 @@ def start_uftpd(os_extension, config): subprocess.check_output("killall -s 9 uftpd", shell=True) # stop running service to make sure we can start it again except subprocess.CalledProcessError: pass # ignore in case uftpd was not running - path = './uftpd' + os_extension + path = retrieveBinary('uftpd' + os_extension) cmd = path + ' ' if config.get_value('multicast-device'): cmd += "-I " + config.get_value('multicast-device') + ' ' @@ -132,3 +133,20 @@ def update_files(command, prince=False): def escape_ansi(line): ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]') return ansi_escape.sub('', line) + +# function to retrieve a system or local binary. +def retrieveBinary(binary): + cwd = Path.cwd() + # use full path so that it works on Windows and Linux + local_binary = cwd / binary + + # First check if there is a local binary and use that if it is there + if local_binary.exists() and local_binary.is_file() and os.access(local_binary, os.X_OK): + return str(local_binary) + + # Fall back on sytem binary + systemBinary = shutil.which(binary) + + if systemBinary: + return systemBinary + return None \ No newline at end of file From 676bf2b7eca31b3f2e5a7444fa3484eae2d2ab89 Mon Sep 17 00:00:00 2001 From: jessevz Date: Tue, 23 Dec 2025 16:41:36 +0100 Subject: [PATCH 2/2] Fixed copilot suggestions --- htpclient/binarydownload.py | 10 ++++++++++ htpclient/files.py | 3 +++ htpclient/helpers.py | 13 ++++++++----- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/htpclient/binarydownload.py b/htpclient/binarydownload.py index 031b70d..6c6f3d8 100644 --- a/htpclient/binarydownload.py +++ b/htpclient/binarydownload.py @@ -123,6 +123,9 @@ def check_prince(self): sleep(5) return False zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension()) + if not zr_bin: + logging.error("7zr not found, cannot extract archive") + return False os.system(zr_bin + " x -otemp prince.7z") for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince if os.path.isdir("temp/" + name): @@ -160,6 +163,9 @@ def check_preprocessor(self, task): sleep(5) return False zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension()) + if not zr_bin: + logging.error("7zr not found, cannot extract archive") + return False os.system(f"{zr_bin} x -otemp temp.7z") for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince if os.path.isdir(Path('temp', name)): @@ -198,6 +204,10 @@ def check_version(self, cracker_id): temp_folder = Path(self.config.get_value('crackers-path'), 'temp') zip_file = Path(self.config.get_value('crackers-path'), f'{cracker_id}.7z') zrbinary = retrieveBinary("7zr" + Initialize.get_os_extension()) + if not zrbinary: + logging.error("7zr not found, cannot extract archive") + sleep(5) + return False cmd = f'{zrbinary} x -o"{temp_folder}" "{zip_file}"' os.system(cmd) diff --git a/htpclient/files.py b/htpclient/files.py index deef1e1..430e226 100644 --- a/htpclient/files.py +++ b/htpclient/files.py @@ -108,6 +108,9 @@ def check_files(self, files, task_id): # extract if needed files_path = Path(self.config.get_value('files-path')) zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension()) + if not zr_bin: + logging.error("7zr not found, cannot extract archive") + return False cmd = f'{zr_bin} x -aoa -o"{files_path}" -y "{file_localpath}"' os.system(cmd) return True diff --git a/htpclient/helpers.py b/htpclient/helpers.py index c5187c7..7f6dff9 100644 --- a/htpclient/helpers.py +++ b/htpclient/helpers.py @@ -68,6 +68,9 @@ def start_uftpd(os_extension, config): except subprocess.CalledProcessError: pass # ignore in case uftpd was not running path = retrieveBinary('uftpd' + os_extension) + if not path: + logging.error("uftpd binary not found, cant do multicast") + return cmd = path + ' ' if config.get_value('multicast-device'): cmd += "-I " + config.get_value('multicast-device') + ' ' @@ -141,12 +144,12 @@ def retrieveBinary(binary): local_binary = cwd / binary # First check if there is a local binary and use that if it is there - if local_binary.exists() and local_binary.is_file() and os.access(local_binary, os.X_OK): + if local_binary.exists() and local_binary.is_file(): return str(local_binary) - # Fall back on sytem binary - systemBinary = shutil.which(binary) + # Fall back on system binary + system_binary = shutil.which(binary) - if systemBinary: - return systemBinary + if system_binary + return system_binary return None \ No newline at end of file