SQL injection (SQLi) is over 25 years old and still appears in real applications every year. It happens when user input is pasted into a database query instead of being passed separately as data.
How it happens
The developer writes:
query = "SELECT * FROM products
WHERE category = '" + input + "'"Normal input Gifts gives:
SELECT * FROM products WHERE category = 'Gifts'Input Gifts' OR '1'='1 gives:
SELECT * FROM products WHERE category = 'Gifts' OR '1'='1'The quote closed the string and the rest became SQL code. Every row is returned.
input ──► string concatenation ──► SQL
(data becomes code ✗)
input ──► parameter (?) ──────────► SQL
(data stays data ✓)Step 1: find candidate inputs
Anything that might reach a database: search boxes, IDs, filters, sort orders, login fields, cookies, headers, JSON fields.
Step 2: probe
Send these one at a time in Repeater and watch the response:
| Probe | What you're looking for |
|---|---|
' |
SQL error or 500 |
'' |
Error disappears again |
' AND '1'='1 |
Same as normal |
' AND '1'='2 |
Different (fewer results) |
1 AND 1=1 / 1 AND 1=2 |
Same for numeric inputs |
ORDER BY column (id vs 9) |
Error on invalid column number |
True vs false behaving differently is the strongest signal. Errors are a bonus, not a requirement.
Step 3: identify the type
SQL injection
┌──────────┼───────────┐
In-band Blind Out-of-band
│ │ (DNS / HTTP
├ error ├ boolean callbacks)
└ union └ timeUnion-based (in-band)
When results appear on the page, UNION appends rows from another query.
- Find the number of columns:
' ORDER BY 1--,' ORDER BY 2--, ... until it errors. - Find which columns display text:
' UNION SELECT NULL,'a',NULL--. - Read harmless proof data, such as the database version:
' UNION SELECT NULL, @@version, NULL--Version syntax differs: @@version (MySQL, SQL Server), version() (PostgreSQL), sqlite_version() (SQLite).
Boolean-based blind
No results or errors shown, but the page changes between true and false. You can ask yes/no questions:
' AND SUBSTRING(@@version,1,1)='8'--Page normal → yes. Page different → no.
Time-based blind
Nothing visible changes at all, so make the database wait:
' AND SLEEP(5)-- -- MySQL
'; SELECT pg_sleep(5)-- -- PostgreSQL
'; WAITFOR DELAY '0:0:5'-- -- SQL ServerA 5-second delay that appears only with your payload confirms injection. Repeat it a few times to rule out network lag.
Second-order SQL injection
Input is stored safely, then used unsafely later. Register the username alice'--, and a later password-change feature might build a query with it. Test stored values in every feature that reuses them.
NoSQL injection
Document databases such as MongoDB are injectable too, with operators instead of quotes:
{"username": "admin", "password": {"$ne": null}}"Password not equal to null" is true for every account. Test JSON inputs with operators like $ne, $gt and $regex.
Using sqlmap responsibly
sqlmap automates detection and exploitation. Use it to confirm what you found by hand, not to spray a target.
sqlmap -r request.txt -p id --batch --level 2 --risk 1-rreads a request saved from Burp;-ptargets one parameter.- Keep
--risk 1on real engagements: higher levels can modify data. - Never dump real tables. Prove the bug with the version or current user, and stop.
Proving impact in a report
Good proof is minimal: the database version, the current database user, or the number of rows in a table. That proves an attacker could read everything without you reading everything.
Severity is usually critical when unauthenticated, because SQLi often exposes the entire database, and sometimes allows file access or command execution.
The fix
| Defence | Verdict |
|---|---|
| Parameterised queries / prepared statements | ✅ The fix |
| ORM used correctly | ✅ Good (watch raw-query helpers) |
| Allow-lists for sort columns and table names | ✅ Needed where parameters can't be used |
| Least-privilege database account | ✅ Limits damage |
| Escaping or blocking quotes | ⚠️ Fragile, often bypassed |
| WAF only | ⚠️ A speed bump, not a fix |
Example of the fix (any language has an equivalent):
db.query("SELECT * FROM products
WHERE category = ?", [input])Lab
- DVWA (SQL Injection, low): find the number of columns, then display the database version with a UNION query.
- DVWA (SQL Injection (Blind), low): confirm injection with a time delay.
- Juice Shop: log in as the admin without knowing the password using the login form.
- PortSwigger Academy: complete SQL injection vulnerability in WHERE clause allowing retrieval of hidden data and SQL injection UNION attack, determining the number of columns.
- Save one request from Burp and confirm the DVWA finding with
sqlmap -r, then write the finding with the fix.
Checkpoint
- Why does a single quote break a vulnerable query?
- What's the strongest signal of SQL injection when there are no errors?
- When would you use a time-based technique?
- What is second-order SQL injection?
- Why is escaping quotes not a reliable fix?