MSSQL RCE via xp_cmdshell
xp_cmdshell is an extended stored procedure that passes a string to the Windows command shell and returns output. When enabled, it executes as the SQL Server service account. On systems where that account is NT AUTHORITY\SYSTEM or a highly privileged domain account, xp_cmdshell is a direct path from SQL authentication to full host compromise.
Sysadmin membership is required to enable or use xp_cmdshell. If you have sysadmin rights but xp_cmdshell is disabled, enabling it takes two configuration commands.
Enable xp_cmdshell
If xp_cmdshell is disabled, enable it through the advanced options. This requires sysadmin:
Configuration option 'show advanced options' changed from 0 to 1. Configuration option 'xp_cmdshell' changed from 0 to 1.
Confirm execution context before running payloads. This tells you what OS user commands will run as:
output -------------------- nt authority\system
Execute Commands via xp_cmdshell
Basic command execution for situational awareness before getting a shell:
Reverse Shell via PowerShell
Set up the listener before executing the payload:
Use a PowerShell reverse shell through xp_cmdshell. The command runs through cmd /c to handle the PowerShell invocation:
Generate the base64 encoded PowerShell payload on Kali first and paste the encoded string into the command. Use the standard PowerShell TCP reverse shell and encode it:
Execute via nxc Without Interactive Session
nxc can run xp_cmdshell commands directly without opening an interactive mssqlclient session, which is faster for one-off command execution:
MSSQL $TARGET_IP 1433 SQL-01 [+] CORP\sqlsvc (Pwn3d!) MSSQL $TARGET_IP 1433 SQL-01 [+] Executed command via xp_cmdshell MSSQL $TARGET_IP 1433 SQL-01 nt authority\system
The -x flag runs the command through xp_cmdshell and returns output. If xp_cmdshell is disabled, nxc will attempt to enable it automatically. Use this for quick checks before committing to an interactive session or a reverse shell.
Ole Automation Alternative
When xp_cmdshell is disabled and cannot be enabled, Ole Automation Procedures provide an equivalent path if they are active. This uses WScript.Shell to execute commands:
Once command execution is confirmed, loot the database itself for credentials and sensitive data , see MSSQL Database Pillaging and MSSQL Credential Dump. For lateral movement to other SQL Server instances through the current connection, see Lateral Movement via MSSQL Linked Servers.
References
-
Microsoft — xp_cmdshelllearn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql (opens in new tab)
xp_cmdshell syntax, permission requirements, and enable procedure
-
NetExec Wiki — MSSQL Command Executionwww.netexec.wiki/mssql-protocol/command-execution (opens in new tab)
nxc -x flag behavior and automatic xp_cmdshell enable
Was this helpful?
Your feedback helps improve this page.