From 6d143111802d5f58873a8e6acc01fb043fd7ed8b Mon Sep 17 00:00:00 2001 From: Bruno Rocha Moura Date: Wed, 23 Sep 2026 10:02:29 +0100 Subject: [PATCH 1/3] feat(mssql): support login impersonation on linked servers --- nxc/modules/exec_on_link.py | 74 ++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/nxc/modules/exec_on_link.py b/nxc/modules/exec_on_link.py index 5a672093f3..f3d1d7265e 100644 --- a/nxc/modules/exec_on_link.py +++ b/nxc/modules/exec_on_link.py @@ -17,20 +17,23 @@ def __init__(self): self.context = None self.linked_server = None self.command = None + self.as_login = None + self.admin_privs = False def options(self, context, module_options): """ LINKED_SERVER: The name of the linked server to execute the command on. COMMAND: The command to execute on the linked server. + AS_LOGIN: Execute the command as this local SQL Server login. """ - if "LINKED_SERVER" in module_options: - self.linked_server = module_options["LINKED_SERVER"] - if "COMMAND" in module_options: - self.command = module_options["COMMAND"] + self.linked_server = module_options.get("LINKED_SERVER") + self.command = module_options.get("COMMAND") + self.as_login = module_options.get("AS_LOGIN") def on_login(self, context, connection): self.context = context self.mssql_conn = connection.conn + self.admin_privs = connection.admin_privs if not self.linked_server or not self.command: self.context.log.fail("Please specify both LINKED_SERVER and COMMAND options.") return @@ -39,6 +42,65 @@ def on_login(self, context, connection): def execute_on_link(self): """Executes the specified command on the linked server.""" - query = f"EXEC ('{self.command}') AT [{self.linked_server}];" + command = self.escape_sql_literal(self.command) + linked_server = self.escape_sql_identifier(self.linked_server) + query = f"EXEC (N'{command}')" + if self.as_login: + query += f" AS LOGIN = N'{self.escape_sql_literal(self.as_login)}'" + query += f" AT [{linked_server}];" result = self.mssql_conn.sql_query(query) - self.context.log.display(f"Command output: {result}") + if self.mssql_conn.lastError: + self.handle_execution_error(self.mssql_conn.lastError) + elif result: + self.context.log.display(f"Command output: {result}") + else: + self.context.log.display("Command executed but returned no output") + + def handle_execution_error(self, error): + error_text = str(error) + if self.is_impersonation_error(error_text): + self.context.log.fail(f"Unable to execute as login '{self.as_login}': {error_text}") + self.context.log.display("The login may not exist, may not be impersonatable, or the current login lacks IMPERSONATE permission") + return + + if self.is_no_login_mapping_error(error_text): + current_login = f"'{self.as_login}'" if self.as_login else "the current login" + self.context.log.fail(f"No login mapping exists for {current_login} on linked server {self.linked_server}") + self.log_linked_login_mappings() + return + + self.context.log.fail(f"Linked-server execution failed: {error_text}") + + def log_linked_login_mappings(self): + if not self.admin_privs: + self.context.log.display("Unable to enumerate mappings with the current privileges") + self.context.log.display("Run the enum_links module with sufficient privileges, then retry with AS_LOGIN=") + return + + query = f"EXEC sp_helplinkedsrvlogin @rmtsrvname = N'{self.escape_sql_literal(self.linked_server)}';" + mappings = self.mssql_conn.sql_query(query) + if self.mssql_conn.lastError: + self.context.log.display("Unable to enumerate linked-server login mappings") + return + mappings = [mapping for mapping in mappings if mapping["Local Login"] is not None] + if not mappings: + self.context.log.display("No explicit local-login mappings were found for this linked server") + return + + for mapping in mappings: + remote_login = mapping["Remote Login"] or "" + self.context.log.display(f"Mapped local login: {mapping['Local Login']} -> {remote_login}") + if mapping["Local Login"] != self.as_login: + self.context.log.display(f"Retry with AS_LOGIN={mapping['Local Login']}") + + def escape_sql_literal(self, value): + return value.replace("'", "''") + + def escape_sql_identifier(self, value): + return value.replace("]", "]]") + + def is_impersonation_error(self, error): + return self.as_login and "cannot execute as the login" in error.lower() + + def is_no_login_mapping_error(self, error): + return "no login-mapping exists" in error.lower() From ec43882933643b7054f9fa8eaac85f0759c600d7 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Moura Date: Wed, 23 Sep 2026 11:26:01 +0100 Subject: [PATCH 2/3] fix(mssql): format linked-server query output --- nxc/modules/exec_on_link.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nxc/modules/exec_on_link.py b/nxc/modules/exec_on_link.py index f3d1d7265e..92e61b3c2d 100644 --- a/nxc/modules/exec_on_link.py +++ b/nxc/modules/exec_on_link.py @@ -52,7 +52,10 @@ def execute_on_link(self): if self.mssql_conn.lastError: self.handle_execution_error(self.mssql_conn.lastError) elif result: - self.context.log.display(f"Command output: {result}") + self.context.log.display("Command output:") + for row in result: + for key, value in row.items(): + self.context.log.highlight(f"{key}:{value}" if key else str(value)) else: self.context.log.display("Command executed but returned no output") From 9413d2513be428553d099d9a8a1b6525a458c714 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Moura Date: Wed, 23 Sep 2026 11:47:09 +0100 Subject: [PATCH 3/3] docs(mssql): explain linked-server query construction --- nxc/modules/exec_on_link.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nxc/modules/exec_on_link.py b/nxc/modules/exec_on_link.py index 92e61b3c2d..c77ce39664 100644 --- a/nxc/modules/exec_on_link.py +++ b/nxc/modules/exec_on_link.py @@ -46,6 +46,7 @@ def execute_on_link(self): linked_server = self.escape_sql_identifier(self.linked_server) query = f"EXEC (N'{command}')" if self.as_login: + # Keep impersonation scoped to this statement so the session does not need a REVERT. query += f" AS LOGIN = N'{self.escape_sql_literal(self.as_login)}'" query += f" AT [{linked_server}];" result = self.mssql_conn.sql_query(query) @@ -97,6 +98,7 @@ def log_linked_login_mappings(self): self.context.log.display(f"Retry with AS_LOGIN={mapping['Local Login']}") def escape_sql_literal(self, value): + # T-SQL string literals escape apostrophes by doubling them; identifiers escape closing brackets. return value.replace("'", "''") def escape_sql_identifier(self, value):