Skip to content

Commit dc39e0e

Browse files
committed
ssh: allow for putting and getting files
1 parent 32b20cd commit dc39e0e

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

nxc/protocols/ssh.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import paramiko
2+
import os
23
import re
34
import uuid
45
import logging
@@ -280,6 +281,36 @@ def plaintext_login(self, username, password, private_key=""):
280281

281282
return True
282283

284+
def put_file_single(self, sftp_conn, src, dst):
285+
self.logger.display(f'Copying "{src}" to "{dst}"')
286+
try:
287+
sftp_conn.put(src, dst)
288+
self.logger.success(f'Created file "{src}" on "{dst}"')
289+
except Exception as e:
290+
self.logger.fail(f'Error writing file to "{dst}": {e}')
291+
292+
def put_file(self):
293+
sftp_conn = self.conn.open_sftp()
294+
for src, dest in self.args.put_file:
295+
self.put_file_single(sftp_conn, src, dest)
296+
sftp_conn.close()
297+
298+
def get_file_single(self, sftp_conn, remote_path, download_path):
299+
self.logger.display(f'Copying "{remote_path}" to "{download_path}"')
300+
try:
301+
sftp_conn.get(remote_path, download_path)
302+
self.logger.success(f'File "{remote_path}" was downloaded to "{download_path}"')
303+
except Exception as e:
304+
self.logger.fail(f'Error getting file "{remote_path}": {e}')
305+
if os.path.getsize(download_path) == 0:
306+
os.remove(download_path)
307+
308+
def get_file(self):
309+
sftp_conn = self.conn.open_sftp()
310+
for src, dest in self.args.get_file:
311+
self.get_file_single(sftp_conn, src, dest)
312+
sftp_conn.close()
313+
283314
def execute(self, payload=None, get_output=False):
284315
if not payload and self.args.execute:
285316
payload = self.args.execute

nxc/protocols/ssh/proto_args.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def proto_args(parser, parents):
1212
ssh_parser.add_argument("--get-output-tries", type=int, default=5, help="Number of times with sudo command tries to get results")
1313
sudo_check_method_arg.make_required.append(sudo_check_arg)
1414

15+
files_group = ssh_parser.add_argument_group("Files", "Options for remote file interaction")
16+
files_group.add_argument("--put-file", action="append", nargs=2, metavar="FILE", help="Put a local file into remote target, ex: whoami.txt /tmp/whoami.txt")
17+
files_group.add_argument("--get-file", action="append", nargs=2, metavar="FILE", help="Get a remote file, ex: /tmp/whoami.txt whoami.txt")
18+
1519
cgroup = ssh_parser.add_argument_group("Command Execution", "Options for executing commands")
1620
cgroup.add_argument("--codec", default="utf-8", help="Set encoding used (codec) from the target's output. If errors are detected, run chcp.com at the target, map the result with https://docs.python.org/3/library/codecs.html#standard-encodings and then execute again with --codec and the corresponding codec")
1721
cgroup.add_argument("--no-output", action="store_true", help="do not retrieve command output")

tests/e2e_commands.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ netexec ssh TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --sudo-check --sudo-
256256
netexec ssh TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --sudo-check --sudo-check-method sudo-stdin --get-output-tries 10
257257
netexec ssh TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --sudo-check --sudo-check-method mkfifo
258258
netexec ssh TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --sudo-check --sudo-check-method mkfifo --get-output-tries 10
259+
netexec ssh TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --put-file TEST_NORMAL_FILE /tmp/test_file.txt --put-file TEST_NORMAL_FILE /tmp/test_file2.txt
260+
netexec ssh TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --get-file /tmp/test_file.txt /tmp/test_file.txt --get-file /tmp/test_file.txt /tmp/test_file2.txt
259261
##### FTP- Default test passwords and random key; switch these out if you want correct authentication
260262
netexec ftp TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD
261263
netexec ftp TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD --ls

0 commit comments

Comments
 (0)