11import json
22import errno
3- import os
3+ from os .path import abspath , join , split , exists , splitext , getsize , sep
4+ from os import makedirs , remove , stat
45import time
56import traceback
7+ from nxc .paths import TMP_PATH
68from nxc .protocols .smb .remotefile import RemoteFile
79from impacket .smb3structs import FILE_READ_DATA
810from impacket .smbconnection import SessionError
@@ -36,7 +38,7 @@ def human_time(timestamp):
3638def 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:" )
0 commit comments