diff --git a/nxc/connection.py b/nxc/connection.py index 062b91c139..3a929ae55e 100755 --- a/nxc/connection.py +++ b/nxc/connection.py @@ -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): diff --git a/nxc/protocols/mssql.py b/nxc/protocols/mssql.py index 66395530e4..105d97fefa 100755 --- a/nxc/protocols/mssql.py +++ b/nxc/protocols/mssql.py @@ -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) @@ -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 diff --git a/nxc/protocols/mssql/proto_args.py b/nxc/protocols/mssql/proto_args.py index 9d494ed44e..1c77740891 100644 --- a/nxc/protocols/mssql/proto_args.py +++ b/nxc/protocols/mssql/proto_args.py @@ -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") diff --git a/tests/e2e_commands.txt b/tests/e2e_commands.txt index 944b44e65a..47855b4283 100644 --- a/tests/e2e_commands.txt +++ b/tests/e2e_commands.txt @@ -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