|
| 1 | +class NXCModule: |
| 2 | + """Enables or disables xp_cmdshell in MSSQL Server.""" |
| 3 | + |
| 4 | + name = "enable_cmdshell" |
| 5 | + description = "Enables or disables xp_cmdshell in MSSQL Server" |
| 6 | + supported_protocols = ["mssql"] |
| 7 | + opsec_safe = False |
| 8 | + multiple_hosts = True |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self.mssql_conn = None |
| 12 | + self.context = None |
| 13 | + self.action = None |
| 14 | + |
| 15 | + def options(self, context, module_options): |
| 16 | + """ |
| 17 | + Available options: |
| 18 | + - ACTION: enable or disable xp_cmdshell |
| 19 | + Example usage: |
| 20 | + netexec mssql $TARGET -u $username -p $password -M enable_cmdshell -o ACTION=enable |
| 21 | + netexec mssql $TARGET -u $username -p $password -M enable_cmdshell -o ACTION=disable |
| 22 | + """ |
| 23 | + if "ACTION" in module_options: |
| 24 | + self.action = module_options["ACTION"].lower() |
| 25 | + else: |
| 26 | + context.log.error("Missing required option: ACTION (enable/disable)") |
| 27 | + |
| 28 | + def on_login(self, context, connection): |
| 29 | + self.context = context |
| 30 | + self.mssql_conn = connection.conn |
| 31 | + |
| 32 | + if self.action == "enable": |
| 33 | + self.toggle_xp_cmdshell(enable=True) |
| 34 | + elif self.action == "disable": |
| 35 | + self.toggle_xp_cmdshell(enable=False) |
| 36 | + else: |
| 37 | + self.context.log.error("Invalid ACTION. Use 'enable' or 'disable'.") |
| 38 | + |
| 39 | + def toggle_xp_cmdshell(self, enable: bool): |
| 40 | + """Enables or disables xp_cmdshell.""" |
| 41 | + state = "1" if enable else "0" |
| 42 | + commands = [ |
| 43 | + "EXEC sp_configure 'show advanced options', '1'", |
| 44 | + "RECONFIGURE", |
| 45 | + f"EXEC sp_configure 'xp_cmdshell', '{state}'", |
| 46 | + "RECONFIGURE" |
| 47 | + ] |
| 48 | + |
| 49 | + for cmd in commands: |
| 50 | + try: |
| 51 | + self.mssql_conn.sql_query(cmd) |
| 52 | + except Exception as e: |
| 53 | + self.context.log.error(f"Failed to execute command: {e}") |
| 54 | + return |
| 55 | + |
| 56 | + action_text = "enabled" if enable else "disabled" |
| 57 | + self.context.log.success(f"xp_cmdshell successfully {action_text}.") |
0 commit comments