From 7034d1821ee9f0ccd160be422f054a77cb3f8d96 Mon Sep 17 00:00:00 2001 From: "Remi GASCOU (Podalirius)" <79218792+p0dalirius@users.noreply.github.com> Date: Fri, 31 May 2024 13:57:10 +0200 Subject: [PATCH] Fixed -download option of find module --- smbclientng/core/LocalFileIO.py | 16 +++++++++------- smbclientng/core/SMBSession.py | 12 +++++++++--- smbclientng/modules/Find.py | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/smbclientng/core/LocalFileIO.py b/smbclientng/core/LocalFileIO.py index 177b978..77acf15 100644 --- a/smbclientng/core/LocalFileIO.py +++ b/smbclientng/core/LocalFileIO.py @@ -27,18 +27,21 @@ class LocalFileIO(object): read(self, size): Reads data from the file up to the specified size and updates the progress bar if expected size is provided. """ - def __init__(self, mode, path=None, expected_size=None, debug=False): + def __init__(self, mode, path=None, expected_size=None, keepRemotePath=False, debug=False): super(LocalFileIO, self).__init__() - self.mode = mode - self.path = path.replace(ntpath.sep, '/') + # Convert remote path format to local operating system path format + self.path = path.replace(ntpath.sep, os.path.sep) self.dir = None self.debug = False self.expected_size = expected_size + self.keepRemotePath = keepRemotePath # Write to local (read remote) if self.mode in ["wb"]: - self.dir = './' + os.path.dirname(self.path) + self.dir = '.' + os.path.sep + if keepRemotePath: + self.dir += os.path.dirname(self.path) if not os.path.exists(self.dir): if self.debug: @@ -47,8 +50,8 @@ def __init__(self, mode, path=None, expected_size=None, debug=False): if self.debug: print("[debug] Openning local '%s' with mode '%s'" % (self.path, self.mode)) - - self.fd = open(self.path, self.mode) + + self.fd = open(self.dir + os.path.sep + os.path.basename(self.path), self.mode) # Write to remote (read local) elif self.mode in ["rb"]: @@ -57,7 +60,6 @@ def __init__(self, mode, path=None, expected_size=None, debug=False): if self.debug: print("[debug] Openning local '%s' with mode '%s'" % (self.path, self.mode)) - self.fd = open(self.path, self.mode) if self.expected_size is None: diff --git a/smbclientng/core/SMBSession.py b/smbclientng/core/SMBSession.py index 10428a2..843edfe 100644 --- a/smbclientng/core/SMBSession.py +++ b/smbclientng/core/SMBSession.py @@ -150,7 +150,7 @@ def close_smb_session(self): # Operations - def get_file(self, path=None): + def get_file(self, path=None, keepRemotePath=False): """ Retrieves a file from the specified path on the SMB share. @@ -170,16 +170,22 @@ def get_file(self, path=None): shareName=self.smb_share, path=tmp_file_path ) + for entry in matches: if entry.is_directory(): print("[>] Skipping '%s' because it is a directory." % tmp_file_path) else: try: + if ntpath.sep in path: + outputfile = ntpath.dirname(path) + ntpath.sep + entry.get_longname() + else: + outputfile = entry.get_longname() f = LocalFileIO( mode="wb", - path=entry.get_longname(), + path=outputfile, expected_size=entry.get_filesize(), - debug=self.config.debug + debug=self.config.debug, + keepRemotePath=keepRemotePath ) self.smbClient.getFile( shareName=self.smb_share, diff --git a/smbclientng/modules/Find.py b/smbclientng/modules/Find.py index 21e8c5e..f84c813 100644 --- a/smbclientng/modules/Find.py +++ b/smbclientng/modules/Find.py @@ -182,7 +182,7 @@ def __recurse_action(self, paths=[], depth=0): if entry.is_directory(): self.smbSession.get_file_recursively(path=(path + entry.get_longname() + ntpath.sep)) else: - self.smbSession.get_file(path=(path + entry.get_longname() + ntpath.sep)) + self.smbSession.get_file(path=(path + entry.get_longname() + ntpath.sep), keepRemotePath=True) # Output formats if self.options.ls: if entry.is_directory():