MSSQL xp_cmdshell Stored Procedure Abuse for Persistence
Attackers may leverage the xp_cmdshell stored procedure in Microsoft SQL Server to execute arbitrary commands for privilege escalation and persistence, often bypassing default security configurations.
The xp_cmdshell extended stored procedure in Microsoft SQL Server allows execution of operating system commands from within the SQL Server environment. Although disabled by default, its use can provide a direct pathway for attackers to run arbitrary commands on the underlying system with the privileges of the SQL Server service account. This account often has elevated privileges, allowing attackers to escalate their access and establish persistence mechanisms. This activity has been observed in intrusions where attackers seek to maintain control over compromised systems. Defenders should closely monitor for the enabling and use of xp_cmdshell, especially when combined with other suspicious activity.
Attack Chain
- An attacker gains initial access to a vulnerable SQL Server instance, possibly through SQL injection or compromised credentials.
- The attacker attempts to enable the xp_cmdshell stored procedure using
sp_configure 'xp_cmdshell', 1; RECONFIGURE;. - The attacker uses xp_cmdshell to execute reconnaissance commands, such as
xp_cmdshell 'whoami'orxp_cmdshell 'net user'to gather information about the system and user context. - The attacker uses xp_cmdshell to download and execute a malicious payload (e.g., using
certutil.exeto download a file). - The attacker establishes persistence by creating a scheduled task via xp_cmdshell executing the
schtaskscommand. For example:xp_cmdshell 'schtasks /create /tn "Malicious Task" /tr "C:\\Windows\\Temp\\evil.exe" /sc ONLOGON /ru SYSTEM'. - The scheduled task executes upon system logon, providing persistent access for the attacker.
- The attacker uses the persistent access to deploy additional tools or exfiltrate data.
Impact
Successful exploitation enables attackers to execute arbitrary commands with elevated privileges on the SQL Server host. This can lead to data theft, system compromise, and the establishment of persistent backdoors. Lateral movement within the network is also possible, leveraging the compromised SQL Server as a pivot point. While specific victim counts and sectors are not provided, any organization using MSSQL Server is potentially vulnerable.
Recommendation
- Deploy the Sigma rule “Detect Suspicious xp_cmdshell Usage” to your SIEM to detect attempts to use xp_cmdshell for command execution.
- Disable the xp_cmdshell stored procedure unless absolutely necessary. If required, implement strict monitoring and auditing of its usage (reference: rule description).
- Monitor for process creation events with a parent process of
sqlservr.exe, specifically looking for command-line arguments indicative of exploitation (reference: Sigma rule). - Ensure SQL servers are not directly exposed to the internet and implement strict access controls, using allowlists to restrict connections to legitimate sources (reference: the “Response and remediation” section).
Detection coverage 2
Detect Suspicious xp_cmdshell Usage
mediumDetects the execution of commands via the xp_cmdshell extended stored procedure in SQL Server, which can indicate malicious activity.
Detect xp_cmdshell Enabling via Stored Procedure
mediumDetects attempts to enable the xp_cmdshell stored procedure in SQL Server, a prerequisite for its abuse.
Detection queries are kept inside the platform. Get full rules →