Difficulty: Advanced
What is blind SQL injection, how do the boolean and time-based variants differ, and how do you prevent it?
Blind SQL injection is SQL injection where the application never shows query results or database errors, so the attacker extracts data by asking yes/no questions and watching how the app responds. A different page, a different HTTP status or a different response time answers each question one bit at a time. It is slower than classic injection but just as dangerous, because the whole database can still be read.
The attacker injects a condition and observes whether the page renders normally or differently. If a product page loads for id=5 AND 1=1 but shows an empty page for id=5 AND 1=2, the response is leaking the truth value of a condition. The attacker then replaces the condition with a question about the data, such as whether the first character of the admin password hash is greater than 'm', and binary-searches each character. No data is ever printed; the page's shape is the oracle.
When the page looks identical whether the condition is true or false, the attacker makes the database pause. A payload that says 'if this condition is true, sleep for 5 seconds' turns a timing difference into the answer. A slow response means true, a fast one means false. Databases expose different sleep primitives, for example SLEEP() on MySQL, pg_sleep() on PostgreSQL and a WAITFOR DELAY on SQL Server. Time-based attacks are the last resort because they are slow and noisy, but they work even when nothing is reflected.
If the database can make network requests, the attacker can force a DNS or HTTP lookup to a server they control, embedding stolen data in the hostname. This exfiltrates data in bulk and sidesteps the one-bit-at-a-time bottleneck, though it needs specific database features and outbound access.
The root cause is always the same as any injection: untrusted input concatenated into a query so the data is parsed as code. Tools such as sqlmap automate the binary search across thousands of requests, so 'blind' does not mean safe. Rate limiting slows a scanner but does not fix the flaw.
| Control | What it does |
|---|---|
| Parameterized queries / prepared statements | Sends the query and the data separately so input can never become SQL. This is the real fix. |
| ORM with bound parameters | Most query builders parameterize automatically; avoid raw string interpolation. |
| Allowlist for dynamic identifiers | Table and column names cannot be parameterized, so validate them against a fixed list. |
| Least-privilege DB user | The app account should not read system tables or other schemas. |
| WAF and monitoring | Defence in depth and detection, never the primary control. |
Input escaping and blocklists are fragile and routinely bypassed. The durable fix is to never build SQL by string concatenation.
ORDER BY or LIMIT built from a string.// VULNERABLE: user input becomes part of the SQL text
const id = req.query.id;
const rows = await db.query(
`SELECT name, price FROM products WHERE id = ${id}`
);
// SAFE: the driver sends SQL and values separately
const rows = await db.query(
'SELECT name, price FROM products WHERE id = $1',
[req.query.id]
);
// Dynamic column names cannot be bound, so allowlist them
const sortable = { name: 'name', price: 'price' };
const column = sortable[req.query.sort] ?? 'name';
const rows2 = await db.query(
`SELECT * FROM products ORDER BY ${column}`
);
Placeholders keep user input as data, so no payload can change the query structure. Identifiers like ORDER BY columns must be checked against a fixed allowlist instead.
SQLi, Boolean-based Inference, Time-based Inference, Parameterized Queries