Blind SQL Injection

Difficulty: Advanced

Question

What is blind SQL injection, how do the boolean and time-based variants differ, and how do you prevent it?

Answer

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.

Boolean-based blind injection

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.

Time-based blind injection

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.

Out-of-band variant

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.

Why it exists and how attackers automate it

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.

Prevention

ControlWhat it does
Parameterized queries / prepared statementsSends the query and the data separately so input can never become SQL. This is the real fix.
ORM with bound parametersMost query builders parameterize automatically; avoid raw string interpolation.
Allowlist for dynamic identifiersTable and column names cannot be parameterized, so validate them against a fixed list.
Least-privilege DB userThe app account should not read system tables or other schemas.
WAF and monitoringDefence 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.

Common mistakes

  • Parameterizing most queries but leaving one dynamic ORDER BY or LIMIT built from a string.
  • Assuming an ORM is safe while still calling a raw-query escape hatch with user input.
  • Hiding errors and calling the app secure; blind injection specifically targets error-free apps.
  • Relying on client-side validation, which an attacker simply skips.
Boolean-based blind injection. The attacker reads data one bit at a time by comparing the app's responses to true and false conditions.
  1. Attacker → App: id=5 AND substring(pw,1,1)>'m'
  2. App → Database: Runs the concatenated query
  3. Database → App: Rows match or no rows
  4. App → Attacker: Normal page = true, empty page = false
  5. Attacker → App: Narrow the range and repeat

Code examples

Vulnerable concatenation vs parameterized query (Node.js)

// 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.

Key points

  • Blind SQLi infers data from app behaviour because results and errors are hidden.
  • Boolean-based reads the true/false of a condition from page differences.
  • Time-based uses database sleep functions when responses look identical.
  • Out-of-band exfiltration uses DNS or HTTP from the database when allowed.
  • Parameterized queries are the fix; WAFs and hidden errors are not.

Common follow-up questions

  • How would you binary-search a password hash with boolean-based injection?
  • Why can you not parameterize a table or column name, and what do you do instead?
  • How does least-privilege database access limit the damage of SQLi?
  • What makes out-of-band exfiltration faster than boolean-based inference?

Interview tips

  • Clearly separate boolean-based from time-based; mixing them up is a common giveaway.
  • State that parameterized queries are the fix before mentioning WAFs, so you do not sound like you treat the symptom.
  • Mention the dynamic ORDER BY gap; it shows you know parameterization has limits.

Concepts covered

SQLi, Boolean-based Inference, Time-based Inference, Parameterized Queries

More Web Security (OWASP, JWT, XSS, CSRF) questions