|
| 1 | +class NXCModule: |
| 2 | + """ |
| 3 | + Enables or disables xp_cmdshell in MSSQL Server. |
| 4 | + Module by crosscutsaw |
| 5 | + """ |
| 6 | + |
| 7 | + name = "enable_cmdshell" |
| 8 | + description = "Enable or disable xp_cmdshell in MSSQL Server" |
| 9 | + supported_protocols = ["mssql"] |
| 10 | + opsec_safe = False |
| 11 | + multiple_hosts = True |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.mssql_conn = None |
| 15 | + self.context = None |
| 16 | + self.action = None |
| 17 | + self.advanced_options_backup = None # Stores original value of 'show advanced options' |
| 18 | + |
| 19 | + def options(self, context, module_options): |
| 20 | + """ |
| 21 | + ACTION enable or disable xp_cmdshell |
| 22 | +
|
| 23 | + Examples |
| 24 | + -------- |
| 25 | + netexec mssql $TARGET -u $username -p $password -M enable_cmdshell -o ACTION=enable |
| 26 | + netexec mssql $TARGET -u $username -p $password -M enable_cmdshell -o ACTION=disable |
| 27 | + """ |
| 28 | + if "ACTION" in module_options: |
| 29 | + self.action = module_options["ACTION"].lower() |
| 30 | + else: |
| 31 | + context.log.fail("Missing required option: ACTION (enable/disable)") |
| 32 | + exit(1) |
| 33 | + |
| 34 | + def on_login(self, context, connection): |
| 35 | + self.context = context |
| 36 | + self.mssql_conn = connection.conn |
| 37 | + |
| 38 | + if self.action == "enable": |
| 39 | + self.toggle_xp_cmdshell(enable=True) |
| 40 | + elif self.action == "disable": |
| 41 | + self.toggle_xp_cmdshell(enable=False) |
| 42 | + else: |
| 43 | + self.context.log.fail("Invalid ACTION. Use 'enable' or 'disable'.") |
| 44 | + |
| 45 | + def backup_show_advanced_options(self): |
| 46 | + """Backs up the current state of 'show advanced options'.""" |
| 47 | + query = "SELECT CAST(value AS INT) AS value FROM sys.configurations WHERE name = 'show advanced options'" |
| 48 | + res = self.mssql_conn.sql_query(query) |
| 49 | + if res: |
| 50 | + self.advanced_options_backup = int(res[0]["value"]) # Convert to integer |
| 51 | + |
| 52 | + def restore_show_advanced_options(self): |
| 53 | + """Restores the original state of 'show advanced options' if needed.""" |
| 54 | + if self.advanced_options_backup is not None and self.advanced_options_backup == 0: |
| 55 | + self.mssql_conn.sql_query("EXEC sp_configure 'show advanced options', '0'; RECONFIGURE;") |
| 56 | + |
| 57 | + def toggle_xp_cmdshell(self, enable: bool): |
| 58 | + """Enables or disables xp_cmdshell while preserving 'show advanced options' state.""" |
| 59 | + state = "1" if enable else "0" |
| 60 | + |
| 61 | + # Backup 'show advanced options' state |
| 62 | + self.backup_show_advanced_options() |
| 63 | + |
| 64 | + # Enable 'show advanced options' if it was disabled |
| 65 | + self.mssql_conn.sql_query("EXEC sp_configure 'show advanced options', '1'; RECONFIGURE;") |
| 66 | + |
| 67 | + try: |
| 68 | + # Enable or disable xp_cmdshell |
| 69 | + self.mssql_conn.sql_query(f"EXEC sp_configure 'xp_cmdshell', '{state}'; RECONFIGURE;") |
| 70 | + action_text = "enabled" if enable else "disabled" |
| 71 | + self.context.log.success(f"xp_cmdshell successfully {action_text}.") |
| 72 | + except Exception as e: |
| 73 | + self.context.log.fail(f"Failed to execute command: {e}") |
| 74 | + |
| 75 | + # Restore 'show advanced options' to its original state if needed |
| 76 | + self.restore_show_advanced_options() |
0 commit comments