Skip to content

Commit 363691a

Browse files
committed
added is_xp_cmdshell_enabled() function to check mssql if xp_cmdshell is already enabled, to avoid altering its state
1 parent 83514bf commit 363691a

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

nxc/protocols/mssql/mssqlexec.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ def __init__(self, connection, logger):
88

99
def execute(self, command):
1010
result = None
11+
xp_cmdshell_was_enabled = False
12+
1113
try:
12-
self.logger.debug("Attempting to enable xp cmd shell")
13-
self.enable_xp_cmdshell()
14+
xp_cmdshell_was_enabled = self.is_xp_cmdshell_enabled()
15+
if not xp_cmdshell_was_enabled:
16+
self.logger.debug("xp_cmdshell is disabled, attempting to enable it.")
17+
self.enable_xp_cmdshell()
18+
else:
19+
self.logger.debug("xp_cmdshell is already enabled.")
20+
1421
except Exception as e:
15-
self.logger.error(f"Error when attempting to enable x_cmdshell: {e}")
22+
self.logger.error(f"Error when checking/enabling xp_cmdshell: {e}")
23+
1624
try:
1725
cmd = f"exec master..xp_cmdshell '{command}'"
1826
self.logger.debug(f"Attempting to execute query: {cmd}")
@@ -21,19 +29,32 @@ def execute(self, command):
2129
if result:
2230
result = "\n".join(line["output"] for line in result if line["output"] != "NULL")
2331
self.logger.debug(f"Concatenated result together for easier parsing: {result}")
24-
# if you prepend SilentlyContinue it will still output the error, but it will still continue on (so it's not silent...)
2532
if "Preparing modules for first use" in result and "Completed" not in result:
2633
self.logger.error("Error when executing PowerShell (received 'preparing modules for first use'), try prepending $ProgressPreference = 'SilentlyContinue'; to your command")
2734
except Exception as e:
2835
self.logger.error(f"Error when attempting to execute command via xp_cmdshell: {e}")
2936

3037
try:
31-
self.logger.debug("Attempting to disable xp cmd shell")
32-
self.disable_xp_cmdshell()
38+
if not xp_cmdshell_was_enabled:
39+
self.logger.debug("xp_cmdshell was not enabled originally, attempting to disable it.")
40+
self.disable_xp_cmdshell()
41+
else:
42+
self.logger.debug("xp_cmdshell was originally enabled, leaving it enabled.")
3343
except Exception as e:
3444
self.logger.error(f"[OPSEC] Error when attempting to disable xp_cmdshell: {e}")
45+
3546
return result
3647

48+
def is_xp_cmdshell_enabled(self):
49+
query = "EXEC sp_configure 'xp_cmdshell';"
50+
self.logger.debug(f"Checking if xp_cmdshell is enabled: {query}")
51+
result = self.mssql_conn.sql_query(query)
52+
# Assuming the query returns a list of dictionaries with 'config_value' as the key
53+
self.logger.debug(f"xp_cmdshell check result: {result}")
54+
if result and result[0]["config_value"] == 1:
55+
return True
56+
return False
57+
3758
def enable_xp_cmdshell(self):
3859
query = "exec master.dbo.sp_configure 'show advanced options',1;RECONFIGURE;exec master.dbo.sp_configure 'xp_cmdshell', 1;RECONFIGURE;"
3960
self.logger.debug(f"Executing query: {query}")

0 commit comments

Comments
 (0)