Skip to content

Commit a5b345d

Browse files
jessevzjessevz
andauthored
Made it possible to let the agent use system binaries if exists (#64)
* Made it possible to let the agent use system binaries if they already exists --------- Co-authored-by: jessevz <jesse.van.zutphen@nfi.nl>
1 parent fd0607a commit a5b345d

3 files changed

Lines changed: 47 additions & 24 deletions

File tree

htpclient/binarydownload.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from htpclient.config import Config
99
from htpclient.download import Download
10+
from htpclient.helpers import retrieve_binary
1011
from htpclient.initialize import Initialize
1112
from htpclient.jsonRequest import JsonRequest
1213
from htpclient.dicts import *
@@ -63,7 +64,7 @@ def check_client_version(self):
6364

6465
def __check_utils(self):
6566
path = '7zr' + Initialize.get_os_extension()
66-
if not os.path.isfile(path):
67+
if not retrieve_binary(path):
6768
query = copy_and_set_token(dict_downloadBinary, self.config.get_value('token'))
6869
query['type'] = '7zr'
6970
req = JsonRequest(query)
@@ -80,7 +81,7 @@ def __check_utils(self):
8081
Download.download(ans['executable'], path)
8182
os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)
8283
path = 'uftpd' + Initialize.get_os_extension()
83-
if not os.path.isfile(path) and self.config.get_value('multicast'):
84+
if not retrieve_binary(path) and self.config.get_value('multicast'):
8485
query = copy_and_set_token(dict_downloadBinary, self.config.get_value('token'))
8586
query['type'] = 'uftpd'
8687
req = JsonRequest(query)
@@ -121,10 +122,11 @@ def check_prince(self):
121122
logging.error("Download of prince failed!")
122123
sleep(5)
123124
return False
124-
if Initialize.get_os() == 1:
125-
os.system("7zr" + Initialize.get_os_extension() + " x -otemp prince.7z")
126-
else:
127-
os.system("./7zr" + Initialize.get_os_extension() + " x -otemp prince.7z")
125+
zr_bin = retrieve_binary("7zr" + Initialize.get_os_extension())
126+
if not zr_bin:
127+
logging.error("7zr not found, cannot extract archive")
128+
return False
129+
os.system(zr_bin + " x -otemp prince.7z")
128130
for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince
129131
if os.path.isdir("temp/" + name):
130132
os.rename("temp/" + name, "prince")
@@ -160,10 +162,11 @@ def check_preprocessor(self, task):
160162
logging.error("Download of preprocessor failed!")
161163
sleep(5)
162164
return False
163-
if Initialize.get_os() == 1:
164-
os.system(f"7zr{Initialize.get_os_extension()} x -otemp temp.7z")
165-
else:
166-
os.system(f"./7zr{Initialize.get_os_extension()} x -otemp temp.7z")
165+
zr_bin = retrieve_binary("7zr" + Initialize.get_os_extension())
166+
if not zr_bin:
167+
logging.error("7zr not found, cannot extract archive")
168+
return False
169+
os.system(f"{zr_bin} x -otemp temp.7z")
167170
for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince
168171
if os.path.isdir(Path('temp', name)):
169172
os.rename(Path('temp', name), path)
@@ -200,13 +203,12 @@ def check_version(self, cracker_id):
200203
# we need to extract the 7zip
201204
temp_folder = Path(self.config.get_value('crackers-path'), 'temp')
202205
zip_file = Path(self.config.get_value('crackers-path'), f'{cracker_id}.7z')
203-
204-
if Initialize.get_os() == 1:
205-
# Windows
206-
cmd = f'7zr{Initialize.get_os_extension()} x -o"{temp_folder}" "{zip_file}"'
207-
else:
208-
# Linux
209-
cmd = f"./7zr{Initialize.get_os_extension()} x -o'{temp_folder}' '{zip_file}'"
206+
zr_bin = retrieve_binary("7zr" + Initialize.get_os_extension())
207+
if not zr_bin:
208+
logging.error("7zr not found, cannot extract archive")
209+
sleep(5)
210+
return False
211+
cmd = f'{zr_bin} x -o"{temp_folder}" "{zip_file}"'
210212
os.system(cmd)
211213

212214
# Clean up 7zip

htpclient/files.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from htpclient.config import Config
99
from htpclient.download import Download
10+
from htpclient.helpers import retrieve_binary
1011
from htpclient.initialize import Initialize
1112
from htpclient.jsonRequest import JsonRequest
1213
from htpclient.dicts import *
@@ -106,11 +107,10 @@ def check_files(self, files, task_id):
106107
if os.path.splitext(file_localpath)[1] == '.7z' and not os.path.isfile(txt_file):
107108
# extract if needed
108109
files_path = Path(self.config.get_value('files-path'))
109-
if Initialize.get_os() == 1:
110-
# Windows
111-
cmd = f'7zr{Initialize.get_os_extension()} x -aoa -o"{files_path}" -y "{file_localpath}"'
112-
else:
113-
# Linux
114-
cmd = f"./7zr{Initialize.get_os_extension()} x -aoa -o'{files_path}' -y '{file_localpath}'"
110+
zr_bin = retrieve_binary("7zr" + Initialize.get_os_extension())
111+
if not zr_bin:
112+
logging.error("7zr not found, cannot extract archive")
113+
return False
114+
cmd = f'{zr_bin} x -aoa -o"{files_path}" -y "{file_localpath}"'
115115
os.system(cmd)
116116
return True

htpclient/helpers.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathlib import Path
99

1010
import os
11+
import shutil
1112
import subprocess
1213

1314
from htpclient.dicts import copy_and_set_token, dict_clientError
@@ -66,7 +67,10 @@ def start_uftpd(os_extension, config):
6667
subprocess.check_output("killall -s 9 uftpd", shell=True) # stop running service to make sure we can start it again
6768
except subprocess.CalledProcessError:
6869
pass # ignore in case uftpd was not running
69-
path = './uftpd' + os_extension
70+
path = retrieve_binary('uftpd' + os_extension)
71+
if not path:
72+
logging.error("uftpd binary not found, cant do multicast")
73+
return
7074
cmd = path + ' '
7175
if config.get_value('multicast-device'):
7276
cmd += "-I " + config.get_value('multicast-device') + ' '
@@ -132,3 +136,20 @@ def update_files(command, prince=False):
132136
def escape_ansi(line):
133137
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
134138
return ansi_escape.sub('', line)
139+
140+
# function to retrieve a system or local binary.
141+
def retrieve_binary(binary):
142+
cwd = Path.cwd()
143+
# use full path so that it works on Windows and Linux
144+
local_binary = cwd / binary
145+
146+
# First check if there is a local binary and use that if it is there
147+
if local_binary.exists() and local_binary.is_file():
148+
return str(local_binary)
149+
150+
# Fall back on system binary
151+
system_binary = shutil.which(binary)
152+
153+
if system_binary
154+
return system_binary
155+
return None

0 commit comments

Comments
 (0)