Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Put File Functions [SMB, MSSQL, FTP] #457

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions nxc/protocols/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,35 @@ def get_file(self, filename):
self.logger.fail(f"Failed to download: {filename}")

def put_file(self, local_file, remote_file):
try:
# Check if the remote file already exists
self.conn.voidcmd("TYPE I") # Switch to binary mode. necessary for size command.
if self.conn.size(remote_file) is not None:
ans = input(highlight(f"[!] {remote_file} already exists. Do you want to overwrite it? [Y/n] ", "red"))
if ans.lower() not in ["y", "yes", ""]:
self.logger.fail(f"Uploading was not successful. The file {remote_file} already exists.")
return False
self.logger.display(f"Trying to overwrite {remote_file}...")

except error_perm:
# If file does not exist, an error_perm will be raised, continue with upload
pass
except Exception as e:
self.logger.fail(f"Error checking if remote file exists: {e}")
return False

try:
# Attempt to upload the file
self.conn.storbinary(f"STOR {remote_file}", open(local_file, "rb")) # noqa: SIM115
with open(local_file, "rb") as file:
self.conn.storbinary(f"STOR {remote_file}", file)
except error_perm as error_message:
self.logger.fail(f"Failed to upload file. Response: ({error_message})")
return False
except FileNotFoundError:
self.logger.fail(f"Failed to upload file. {local_file} does not exist locally.")
return False
# Check if the file was uploaded

# Check if the file was uploaded successfully
if self.conn.size(remote_file) > 0:
self.logger.success(f"Uploaded: {local_file} to {remote_file}")
else:
Expand Down
25 changes: 19 additions & 6 deletions nxc/protocols/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from nxc.helpers.bloodhound import add_user_bh
from nxc.helpers.ntlm_parser import parse_challenge
from nxc.helpers.powershell import create_ps_command
from nxc.helpers.logger import highlight
from nxc.protocols.mssql.mssqlexec import MSSQLEXEC

from impacket import tds, ntlm
Expand Down Expand Up @@ -353,19 +354,31 @@ def ps_execute(self, payload=None, get_output=False, methods=None, force_ps32=Fa

@requires_admin
def put_file(self):
self.logger.display(f"Copy {self.args.put_file[0]} to {self.args.put_file[1]}")
with open(self.args.put_file[0], "rb") as f:
try:
self.logger.display(f"Checking if {self.args.put_file[1]} exists on the remote machine.")

# Check if the destination file exists before uploading
exec_method = MSSQLEXEC(self.conn, self.logger)
if exec_method.file_exists(self.args.put_file[1]):
ans = input(highlight(f"[!] {self.args.put_file[1]} already exists. Do you want to overwrite it? [Y/n] ", "red"))
if ans.lower() not in ["y", "yes", ""]:
self.logger.fail(f"Uploading was not successful. The file {self.args.put_file[1]} already exists.")
return False
self.logger.display(f"Trying to overwrite {self.args.put_file[1]}...")

self.logger.display(f"Copying {self.args.put_file[0]} to {self.args.put_file[1]}")

try:
with open(self.args.put_file[0], "rb") as f:
data = f.read()
self.logger.display(f"Size is {len(data)} bytes")
exec_method = MSSQLEXEC(self.conn, self.logger)
exec_method.put_file(data, self.args.put_file[1])

if exec_method.file_exists(self.args.put_file[1]):
self.logger.success("File has been uploaded on the remote machine")
else:
self.logger.fail("File does not exist on the remote system... error during upload")
except Exception as e:
self.logger.fail(f"Error during upload : {e}")
except Exception as e:
self.logger.fail(f"Error during upload: {e}")

@requires_admin
def get_file(self):
Expand Down
27 changes: 22 additions & 5 deletions nxc/protocols/smb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,12 +1434,29 @@ def rid_brute(self, max_rid=None):

def put_file_single(self, src, dst):
self.logger.display(f"Copying {src} to {dst}")
with open(src, "rb") as file:
try:

try:
self.conn.getFile(self.args.share, dst, lambda data: None)
file_exists = True
except FileNotFoundError:
file_exists = False
except Exception as e:
self.logger.debug(f"Error checking if file exists on {self.args.share}: {e}")
file_exists = False

if file_exists:
ans = input(highlight(f"[!] {src} already exists on \\\\{self.args.share}\\{dst}. Do you want to overwrite it? [Y/n] ", "red"))
if ans.lower() not in ["y", "yes", ""]:
self.logger.fail(f"Uploading was not successful. The {src} exists on \\\\{self.args.share}\\{dst}")
return
self.logger.display(f"Trying to overwrite {src} on \\\\{self.args.share}\\{dst}...")

try:
with open(src, "rb") as file:
self.conn.putFile(self.args.share, dst, file.read)
self.logger.success(f"Created file {src} on \\\\{self.args.share}\\{dst}")
except Exception as e:
self.logger.fail(f"Error writing file to share {self.args.share}: {e}")
self.logger.success(f"Created file {src} on \\\\{self.args.share}\\{dst}")
except Exception as e:
self.logger.fail(f"Error writing file to share {self.args.share}: {e}")

def put_file(self):
for src, dest in self.args.put_file:
Expand Down