Stacked Queries and Multiple Statements
You'll learn how attackers can execute multiple SQL commands in a single injection to go far beyond data theft—modifying, deleting, or even taking control of the database.
What Are Stacked Queries?
Until now, we've focused on SQL injection techniques that extract data using SELECT statements. But what if an attacker could run multiple SQL commands at once? Stacked queries allow an attacker to append additional SQL statements to an existing query using the semicolon (;) separator—turning a simple read operation into a full-blown database manipulation attack.
Think of it like sneaking multiple instructions into a single order: instead of just asking "show me users," you also slip in "and delete all logs" in the same breath.
How It Works
Vulnerable code might execute:
SELECT * FROM products WHERE id = 1
An attacker injects: 1; DROP TABLE users--
The database receives:
SELECT * FROM products WHERE id = 1; DROP TABLE users--
The semicolon ends the first statement, and the second executes immediately after. The comment (--) neutralizes anything that follows.
Why It's Dangerous
Stacked queries enable attackers to:
- Modify data:
UPDATE users SET role='admin' WHERE id=5 - Delete records:
DELETE FROM audit_logs - Create backdoors:
INSERT INTO users VALUES('hacker','password123','admin') - Execute stored procedures:
EXEC xp_cmdshell('net user hacker password /ADD')
Not all databases or database drivers support stacked queries (some MySQL configurations disable them), but when available, they transform SQL injection from reconnaissance into full compromise.
Key Takeaway: Stacked queries allow attackers to execute multiple SQL statements in a single injection point, escalating attacks from data theft to data manipulation, deletion, and potentially complete database takeover.