Skip to content
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
5 changes: 2 additions & 3 deletions nxc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,12 @@ def proto_flow(self):

self.print_host_info()
if self.login() or (self.username == "" and self.password == ""):
self.logger.debug("Calling command arguments")
self.call_cmd_args()
if hasattr(self.args, "module") and self.args.module:
self.load_modules()
self.logger.debug("Calling modules")
self.call_modules()
else:
self.logger.debug("Calling command arguments")
self.call_cmd_args()
self.disconnect()

def call_cmd_args(self):
Expand Down
29 changes: 29 additions & 0 deletions nxc/protocols/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, args, db, host):
self.lmhash = ""
self.nthash = ""
self.is_mssql = False
self.impersonation_applied = False

connection.__init__(self, args, db, host)

Expand Down Expand Up @@ -276,6 +277,34 @@ def hash_login(self, domain, username, ntlm_hash):
self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(self.nthash)} {error_msg if error_msg else ''}")
return False

def impersonate(self):
mode = "LOGIN"
target = self.args.impersonate[0].strip()
if target.lower().startswith("login:"):
target = target.split(":", 1)[1].strip()
mode = "LOGIN"
elif target.lower().startswith("user:"):
target = target.split(":", 1)[1].strip()
mode = "USER"

if not target:
self.logger.error("Impersonation target is empty")
return False

try:
self.conn.sql_query(f"EXECUTE AS {mode} = N'{target}';")
if self.conn.lastError:
self.logger.fail(self.conn.lastError)
return False
self.logger.success(f"Impersonation set: {mode}={target}")
self.impersonation_applied = True
res = self.conn.sql_query("SELECT SYSTEM_USER, SUSER_SNAME();")
self.logger.debug(f"Impersonated context: {res}")
except Exception as e:
self.logger.fail(f"Failed to impersonate {mode} {target}: {e}")
return False
return True

def query(self):
if self.conn.lastError:
# Invalid connection
Expand Down
1 change: 1 addition & 0 deletions nxc/protocols/mssql/proto_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def proto_args(parser, parents):
mssql_parser.add_argument("-H", "--hash", metavar="HASH", dest="hash", nargs="+", default=[], help="NTLM hash(es) or file(s) containing NTLM hashes")
mssql_parser.add_argument("--port", default=1433, type=int, metavar="PORT", help="MSSQL port")
mssql_parser.add_argument("--mssql-timeout", help="SQL server connection timeout", type=int, default=5)
mssql_parser.add_argument("--impersonate", nargs=1, metavar="LOGIN|USER", type=str, help="impersonate a login or user (prefix with login: or user:, default: login)")
mssql_parser.add_argument("-q", "--query", metavar="QUERY", type=str, help="execute the specified query against the mssql db")
mssql_parser.add_argument("--database", nargs="?", const=True, metavar="NAME", help="list databases or list tables for NAME")

Expand Down
1 change: 1 addition & 0 deletions tests/e2e_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ netexec winrm TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -M aws-cr
##### MSSQL
netexec mssql TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS # Need a space at the end for kerb regex
netexec {DNS} mssql TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS # Need a space at the end for kerb regex
netexec mssql TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS --impersonate LOGIN_USERNAME
netexec mssql TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS --rid-brute
netexec mssql TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS --database
netexec mssql TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS --sam
Expand Down