Skip to content

Commit e551d3e

Browse files
authored
Update enable_cmdshell.py
Signed-off-by: crosscutsaw <73831924+crosscutsaw@users.noreply.github.com>
1 parent 619e477 commit e551d3e

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

nxc/modules/enable_cmdshell.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class NXCModule:
22
"""Enables or disables xp_cmdshell in MSSQL Server."""
33

44
name = "enable_cmdshell"
5-
description = "Enables or disables xp_cmdshell in MSSQL Server"
5+
description = "Enable or disable xp_cmdshell in MSSQL Server"
66
supported_protocols = ["mssql"]
77
opsec_safe = False
88
multiple_hosts = True
@@ -11,6 +11,7 @@ def __init__(self):
1111
self.mssql_conn = None
1212
self.context = None
1313
self.action = None
14+
self.advanced_options_backup = None # Stores original value of 'show advanced options'
1415

1516
def options(self, context, module_options):
1617
"""
@@ -36,22 +37,35 @@ def on_login(self, context, connection):
3637
else:
3738
self.context.log.error("Invalid ACTION. Use 'enable' or 'disable'.")
3839

40+
def backup_show_advanced_options(self):
41+
"""Backs up the current state of 'show advanced options'."""
42+
query = "SELECT CAST(value AS INT) AS value FROM sys.configurations WHERE name = 'show advanced options'"
43+
res = self.mssql_conn.sql_query(query)
44+
if res:
45+
self.advanced_options_backup = int(res[0]["value"]) # Convert to integer
46+
47+
def restore_show_advanced_options(self):
48+
"""Restores the original state of 'show advanced options' if needed."""
49+
if self.advanced_options_backup is not None and self.advanced_options_backup == 0:
50+
self.mssql_conn.sql_query("EXEC sp_configure 'show advanced options', '0'; RECONFIGURE")
51+
3952
def toggle_xp_cmdshell(self, enable: bool):
40-
"""Enables or disables xp_cmdshell."""
53+
"""Enables or disables xp_cmdshell while preserving 'show advanced options' state."""
4154
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}.")
55+
56+
# Backup 'show advanced options' state
57+
self.backup_show_advanced_options()
58+
59+
# Enable 'show advanced options' if it was disabled
60+
self.mssql_conn.sql_query("EXEC sp_configure 'show advanced options', '1'; RECONFIGURE")
61+
62+
try:
63+
# Enable or disable xp_cmdshell
64+
self.mssql_conn.sql_query(f"EXEC sp_configure 'xp_cmdshell', '{state}'; RECONFIGURE")
65+
action_text = "enabled" if enable else "disabled"
66+
self.context.log.success(f"xp_cmdshell successfully {action_text}.")
67+
except Exception as e:
68+
self.context.log.error(f"Failed to execute command: {e}")
69+
70+
# Restore 'show advanced options' to its original state if needed
71+
self.restore_show_advanced_options()

0 commit comments

Comments
 (0)