SQL injection (SQLi) happens when user input is concatenated directly into a database query instead of being treated strictly as data. It has topped vulnerability classifications for over a decade, not because it is hard to prevent, but because it is easy to introduce by accident. This article covers how testers confirm it responsibly and, more importantly, the specific fix that removes the vulnerability class entirely.
The vulnerable pattern
This PHP example is representative of how SQLi is introduced in real codebases:
// VULNERABLE: user input concatenated into the query
$id = $_GET['id'];
$query = "SELECT name, email FROM users WHERE id = " . $id;
$result = $mysqli->query($query);
An attacker who controls the id parameter can alter the structure of the query itself, not just its value, potentially extracting unrelated tables or bypassing logic entirely.
Confirming it manually first
Before reaching for an automated tool, testers confirm the signal manually in Burp Suite’s Repeater by sending a single quote and observing the response:
GET /product?id=1' HTTP/1.1
Host: app.example.com
A database error, a blank page where content normally appears, or a measurable time delay when a boolean or time-based payload is sent are all signals worth investigating further.
Confirming and characterizing with sqlmap
sqlmap automates detection and, with authorization, extraction, across many injection techniques and database engines:
sqlmap -u "https://app.example.com/product?id=1" \
--cookie="session=abc123" \
--batch --level=3 --risk=2 \
--dbms=mysql
--level and --risk control how many injection points and payload types sqlmap tries; higher values are more thorough but more intrusive, and should be chosen deliberately within the agreed rules of engagement, not maxed out by default. To enumerate databases once injection is confirmed:
sqlmap -u "https://app.example.com/product?id=1" --cookie="session=abc123" --batch --dbs
The real fix: parameterized queries
Input validation and escaping help, but the structural fix is a parameterized (prepared) statement, which separates the query structure from the data entirely so the database never interprets input as code:
// FIXED: parameterized query
$id = $_GET['id'];
$stmt = $mysqli->prepare("SELECT name, email FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
The same principle applies in every stack, for example Python with psycopg2:
# FIXED: parameterized query
cur.execute("SELECT name, email FROM users WHERE id = %s", (user_id,))
Every modern ORM (Eloquent, SQLAlchemy, Entity Framework) uses parameterization by default when used correctly; the risk reappears when developers drop down to raw query strings for “just this one case.”
Defense in depth
- Run the database account used by the application with least privilege; it should not be able to read tables it never needs.
- Disable detailed database error messages in production responses.
- Add a web application firewall as a compensating control, never as a substitute for fixing the code.
- Add static analysis (e.g. Semgrep rules for raw query concatenation) to CI so the pattern cannot silently return.
How Egaros can help
We test applications for injection flaws across SQL, NoSQL and command contexts, and provide developers with the exact remediated code, not just a finding title. Contact us to schedule a test.


اترك تعليقاً