Skip to content

Commit eac1073

Browse files
author
jessevz
committed
Made it possible to let the agent use system binaries if they already exists
1 parent ed90a59 commit eac1073

3 files changed

Lines changed: 31 additions & 24 deletions

File tree

htpclient/binarydownload.py

Lines changed: 9 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 retrieveBinary
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 retrieveBinary(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 retrieveBinary(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,8 @@ 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 = retrieveBinary("7zr" + Initialize.get_os_extension())
126+
os.system(zr_bin + " x -otemp prince.7z")
128127
for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince
129128
if os.path.isdir("temp/" + name):
130129
os.rename("temp/" + name, "prince")
@@ -160,10 +159,8 @@ def check_preprocessor(self, task):
160159
logging.error("Download of preprocessor failed!")
161160
sleep(5)
162161
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")
162+
zr_bin = retrieveBinary("7zr" + Initialize.get_os_extension())
163+
os.system(f"{zr_bin} x -otemp temp.7z")
167164
for name in os.listdir("temp"): # this part needs to be done because it is compressed with the main subfolder of prince
168165
if os.path.isdir(Path('temp', name)):
169166
os.rename(Path('temp', name), path)
@@ -200,13 +197,8 @@ def check_version(self, cracker_id):
200197
# we need to extract the 7zip
201198
temp_folder = Path(self.config.get_value('crackers-path'), 'temp')
202199
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}'"
200+
zrbinary = retrieveBinary("7zr" + Initialize.get_os_extension())
201+
cmd = f'{zrbinary} x -o"{temp_folder}" "{zip_file}"'
210202
os.system(cmd)
211203

212204
# Clean up 7zip

htpclient/files.py

Lines changed: 3 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 retrieveBinary
1011
from htpclient.initialize import Initialize
1112
from htpclient.jsonRequest import JsonRequest
1213
from htpclient.dicts import *
@@ -106,11 +107,7 @@ 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 = retrieveBinary("7zr" + Initialize.get_os_extension())
111+
cmd = f'{zr_bin} x -aoa -o"{files_path}" -y "{file_localpath}"'
115112
os.system(cmd)
116113
return True

htpclient/helpers.py

Lines changed: 19 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,7 @@ 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 = retrieveBinary('uftpd' + os_extension)
7071
cmd = path + ' '
7172
if config.get_value('multicast-device'):
7273
cmd += "-I " + config.get_value('multicast-device') + ' '
@@ -132,3 +133,20 @@ def update_files(command, prince=False):
132133
def escape_ansi(line):
133134
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
134135
return ansi_escape.sub('', line)
136+
137+
# function to retrieve a system or local binary.
138+
def retrieveBinary(binary):
139+
cwd = Path.cwd()
140+
# use full path so that it works on Windows and Linux
141+
local_binary = cwd / binary
142+
143+
# First check if there is a local binary and use that if it is there
144+
if local_binary.exists() and local_binary.is_file() and os.access(local_binary, os.X_OK):
145+
return str(local_binary)
146+
147+
# Fall back on sytem binary
148+
systemBinary = shutil.which(binary)
149+
150+
if systemBinary:
151+
return systemBinary
152+
return None

0 commit comments

Comments
 (0)