In short
xp_cmdshell is a Microsoft SQL Server extended stored procedure that starts a Windows command shell, runs a command string and returns the output as rows of text. It runs with the rights of the SQL Server service account. It is disabled by default, but attackers who gain sysadmin access often enable it to run commands on the database server.
Key takeaways
- What it is
- A built-in SQL Server extended stored procedure that runs operating system commands from T-SQL. Microsoft disables it by default on new installations.
- How it works
- A sysadmin enables it with sp_configure. Each call spawns cmd.exe with the SQL Server service account's rights and returns the output to the query.
- Why it matters
- It turns database access into code execution on the server. Sophos and Guardicore Labs both describe campaigns that brute-forced SQL Server and then enabled xp_cmdshell.
- What Odin Research saw
- Staged toolkits that enable xp_cmdshell on internal SQL Server hosts, pivot through linked servers, and then switch it off again in a rollback script.
- What to do first
- Keep xp_cmdshell off, keep SQL Server off the internet, alert on Event ID 15457 and on sqlservr.exe starting cmd.exe or PowerShell.
What is xp_cmdshell?
xp_cmdshell is a Microsoft SQL Server extended stored procedure that starts a Windows command shell, passes it a command string and returns any output as rows of text. It runs with the rights of the SQL Server service account. Microsoft disables it by default because it can be used to escalate privileges.
Microsoft's documentation calls xp_cmdshell a powerful feature. It says new code should not use it and that it should generally stay disabled. If a legacy application needs it, Microsoft recommends enabling it only for the duration of the task.
For attackers, xp_cmdshell is a bridge. It turns a database login into commands on the server's operating system. That makes it a common step after SQL injection, password guessing or stolen credentials.
How xp_cmdshell works
According to Microsoft Learn:
- Turning it on. An administrator enables it through Policy-Based Management or with
sp_configure. The usual steps turn onshow advanced options, set thexp_cmdshelloption to 1 and runRECONFIGURE. - Calling it. The procedure takes one command string and an optional
NO_OUTPUTflag. It returns 0 for success or 1 for failure. - Whose rights. The Windows process it spawns has the same security rights as the SQL Server service account. That account often has more permissions than the task needs.
- Who can call it. When first enabled, it needs
CONTROL SERVERpermission. Non-sysadmin users can run it only through a proxy credential named##xp_cmdshell_proxy_account##. - Blocking behavior. It runs synchronously. Control does not return until the command finishes.
Microsoft also warns that using xp_cmdshell can trigger security audit tools. That is good news for defenders. On a modern server, xp_cmdshell activity should be rare, so any use stands out.
MITRE ATT&CK covers this as SQL Stored Procedures (T1505.001). It notes that adversaries may need to enable xp_cmdshell to run operating system commands through SQL. MITRE lists Stuxnet as one malware family that used it.
How attackers use xp_cmdshell
Two vendor reports show the common pattern of breaking in, enabling the procedure and running tools:
- Sophos, STAC6451. Sophos MDR tracked a cluster, first seen in late March 2024, that brute-forced internet-facing SQL Server instances on port 1433, often with weak credentials. The attackers enabled xp_cmdshell, ran built-in Windows tools under the SQL service and staged AnyDesk, Cobalt Strike, PrintSpoofer and Mimic ransomware. Sophos says xp_cmdshell should not be enabled on exposed servers.
- Guardicore Labs, Vollgar. In 2020, Akamai's Guardicore Labs described a campaign, first seen in May 2018, that brute-forced MS-SQL servers. After login, the attacker enabled xp_cmdshell, Ad Hoc Distributed Queries and OLE Automation Procedures, created backdoor accounts and installed a Monero miner.
Typical goals after enabling xp_cmdshell include privilege escalation, new admin accounts, remote access tools and lateral movement. Because the commands run as the SQL service, they blend with database activity unless you watch the process tree.
What Odin Research observed
xp_cmdshell appears in four Odin Research studies. In each, the evidence is staged files, not confirmed activity on a victim network.
- Enable, use, roll back. In Monograph 01, PowerShell wrappers were written to turn on xp_cmdshell on internal SQL Server hosts and use an RPC-enabled linked server to reach other segments. A rollback script would switch xp_cmdshell off and delete two temporary admin accounts. A later posture scan would see a clean SQL configuration. The script left out a hidden local admin.
- Moving away from xp_cmdshell. In Monograph 05, one script enabled xp_cmdshell under a SQL Agent job. The others moved persistence into SQL Server Agent
CmdExecjob steps, becausesqlservr.exestartingcmd.exeis a pattern many tools watch. A further script registered a directory provider as a linked server to query Active Directory from inside SQL. - Into privilege escalation. In Monograph 03, scripts used xp_cmdshell to launch reflective .NET PrintSpoofer loaders and create a hidden local administrator.
- Claimed by an AI swarm. In Monograph 30, worker logs from an autonomous red-team framework claimed a SQL injection path was used to enable xp_cmdshell on a linked SQL Server. The report treats this as an unverified claim from the attacker's own records.
How to detect xp_cmdshell abuse
- Configuration history, not only state. SQL Server logs Event ID 15457 each time a configuration option changes. Monograph 01 recommends searching all rotated error logs for xp_cmdshell going from 0 to 1 and back to 0. A server that looks clean today may have been opened and closed.
- Confirm with a second source. Event 15457 records a configured value, not proof the change took effect. Pair a hit with SQL Server Audit or with
sqlservr.exespawningcmd.exe. - Process trees. Alert when
sqlservr.exeorsqlagent.exestartscmd.exe, PowerShell,curl.exeor files fromProgramData. - Agent jobs. Flag SQL Agent steps that use the CmdExec, PowerShell or ActiveScripting subsystems, and T-SQL steps that call xp_cmdshell,
sp_OACreateorOPENROWSET(BULK ...). - Short-lived admins. Join account creation and deletion events within 72 hours to catch admins that existed briefly.
Monograph 01 publishes a T-SQL audit script that finds xp_cmdshell enable-then-disable sequences and risky linked servers. Monograph 05 adds a Sentinel query for SQL Agent child processes and a YARA rule for SQL Agent and ADSI scripts. All are in the Defense Kit.
How to lock down xp_cmdshell
- Keep it disabled. Check
sys.configurationson every instance, and enforce the setting with Policy-Based Management. - Get SQL Server off the internet. Both Sophos and Guardicore Labs saw attackers start by brute-forcing exposed servers. Use VPNs and allowlists, and strong, unique passwords. See brute force attack.
- Run with least privilege. Give the SQL Server service account only the rights it needs. If non-admins must use xp_cmdshell, configure a low-privilege proxy account.
- Restrict SQL Agent. Limit CmdExec and PowerShell job steps to least-privilege proxies and require signed scripts, as Monograph 05 advises.
- Audit linked servers. Review linked-server logins with RPC-out enabled as you would domain trusts.
xp_cmdshell compared with other SQL Server execution paths
| Path | How it runs commands | Seen in | What to watch |
|---|---|---|---|
| xp_cmdshell | Spawns cmd.exe from the SQL Server process | Monographs 01, 03, 05, 30; Sophos; Guardicore Labs | Event 15457; sqlservr.exe child processes |
| SQL Agent CmdExec steps | Scheduled job steps run OS commands | Monograph 05 | New jobs with CmdExec or PowerShell subsystems |
| OLE Automation | Creates COM objects from T-SQL | Guardicore Labs (Vollgar) | sp_OACreate calls |
| ADSI linked server | Queries Active Directory from inside SQL | Monograph 05 | New linked servers using the ADSDSOObject provider |
Frequently asked questions about xp_cmdshell
What is xp_cmdshell?
xp_cmdshell is a Microsoft SQL Server extended stored procedure that starts a Windows command shell, runs a command string and returns the output as rows of text, with the rights of the SQL Server service account.
Is xp_cmdshell enabled by default?
No. Microsoft disables it by default on new installations because malicious users sometimes try to elevate privileges with it.
How do I check if xp_cmdshell is enabled?
Query sys.configurations for the xp_cmdshell option or run sp_configure. A value of 1 means enabled. Also check Event ID 15457 history, because attackers may have switched it back off.
Is xp_cmdshell a vulnerability?
No. It is a documented feature. It becomes dangerous when attackers reach a sysadmin login through weak passwords, stolen credentials or SQL injection and then turn it on.
What account does xp_cmdshell run as?
For sysadmin callers, the SQL Server service account. For other users, the account stored in the ##xp_cmdshell_proxy_account## credential. If that proxy does not exist, the call fails.
How do attackers use xp_cmdshell?
After gaining sysadmin access, they enable it, then run commands to add accounts, download tools, install remote access software, escalate privileges or deploy ransomware and miners.
How do I detect xp_cmdshell abuse?
Alert on Event ID 15457 changes, on sqlservr.exe or sqlagent.exe starting cmd.exe or PowerShell, on new SQL Agent CmdExec steps and on short-lived admin accounts.
Which MITRE ATT&CK technique covers xp_cmdshell?
Server Software Component: SQL Stored Procedures, T1505.001. MITRE notes adversaries may need to enable xp_cmdshell to run operating system commands through SQL.
Sources
- Microsoft Learn, xp_cmdshell (Transact-SQL). learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql (checked 21 November 2026)
- Microsoft Learn, Server Configuration: xp_cmdshell. learn.microsoft.com/en-us/sql/database-engine/configure-windows/xp-cmdshell-server-configuration-option (checked 21 November 2026)
- MITRE ATT&CK, Server Software Component: SQL Stored Procedures (T1505.001). attack.mitre.org/techniques/T1505/001/ (checked 21 November 2026)
- Sophos, "Sophos MDR hunt tracks Mimic ransomware campaign against organizations in India". www.sophos.com/blog/sophos-mdr-hunt-tracks-mimic-ransomware-campaign-against-organizations-in-india (checked 21 November 2026)
- Akamai (Guardicore Labs), "The Vollgar Campaign: MS-SQL Servers Under Attack", 1 April 2020. akamai.com/blog/security/vollgar-ms-sql-servers-under-attack (checked 21 November 2026)
Related tools and terms
What is OAST?
Out-of-band application security testing (OAST) detects blind vulnerabilities by making a target call back to a server the tester controls, usually over DNS or HTTP.
TechniqueWhat is JNDI injection?
JNDI injection makes a Java application look up a remote object, usually over LDAP or RMI, and load attacker code. Log4Shell is the best-known example.
TechniqueWhat is HVNC?
Hidden VNC (HVNC) creates a second, invisible desktop on the victim's computer so an attacker can use the victim's browser sessions unseen.
VulnerabilityWhat is Ghostcat?
Ghostcat (CVE-2020-1938) is a flaw in the Apache Tomcat AJP connector that lets an attacker read files and, in some setups, run code.
TechniqueWhat is JARM?
JARM is an open TLS server fingerprinting method from Salesforce. It hashes how a server answers a set of crafted TLS handshakes.
TechniqueWhat is DCSync?
DCSync abuses the Active Directory replication protocol to ask a domain controller for password hashes, as if the caller were another domain controller.
Odin Research
Get the rules, not just the definition
Every Odin Research report ships with verified indicators and detection rules you can load today.