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.
Requirements and execution paths:
Sysadmin role required to enable and execute xp_cmdshell
Commands run as the SQL Server service account (often SYSTEM)
Ole Automation Procedures as fallback when xp_cmdshell is blocked
nxc can auto-enable xp_cmdshell and execute without an interactive session
MSSQL xp_cmdshell RCE: enable and execute
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_cmdshellDOCS: Microsoft: xp_cmdshell, learn.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 ExecutionLINK: NetExec Wiki: MSSQL Command Execution, www.netexec.wiki/mssql-protocol/command-execution (opens in new tab)
nxc -x flag behavior and automatic xp_cmdshell enable