SQL injection: how input becomes a command
SQL injection is when user input becomes part of an SQL command, because the query is built as a string and the boundary between code and data disappears. The result is seen through errors, UNION or blind conditions and delays. It is tested with safe probes: a quote, a true and a false condition, a controlled delay, no destructive commands. The defence is parameterized queries.
Pentest · Injections
The search field fails with a database error when you type an apostrophe. Just a bug or already a hole?
A tester types a single quote into the catalogue search and instead of a product list sees an error page with syntax error at or near. It is not necessarily SQL injection, but it is a strong signal: the typed text reached the database as part of a command. The lesson shows how it works, which kinds exist (including "blind" ones, when no errors are visible) and how to check them without breaking anything.
Mechanism. The application builds SQL as a string and inserts the user’s input into it:
const sql =
"SELECT * FROM products WHERE name = '" + term + "'";
For term = laptop this is an ordinary query. For term = ' OR '1'='1 the database receives WHERE name = '' OR '1'='1': the condition is always true, the whole catalogue is returned. The user did not pass data, he changed the structure of the command. This is what separates injection from incorrect input: the border between code and data is gone.
Building a query: a string or parameters
Kinds.
- Error-based: the database error is shown in the response and reveals the query structure;
- UNION-based:
UNION SELECT …is appended to the query and the result of another table appears in the same response. First the number of columns of the original query is found; - Blind boolean: data is not visible, but a condition changes the response (a product is there or not, the page differs);
- Blind time-based: the response is the same, but the injected command delays it (
pg_sleep(5)in PostgreSQL,SLEEP(5)in MySQL); - Out-of-band: the database itself contacts an external address; used when there are no other channels.
For "blind" kinds the check is like an experiment with a control. Two responses differ only in the condition:
GET /products?name=laptop' AND '1'='1 -> list of laptops
GET /products?name=laptop' AND '1'='2 -> empty list
GET /products?name=laptop'; SELECT pg_sleep(5)-- -> 5 s delay
If the first and the second responses differ consistently while the request format and the network do not, the input reaches an SQL condition. A delay of exactly five seconds on repeats also points to command execution rather than a slow network: repeat with 2 and 8 seconds and compare. Note that a tester uses no DROP, UPDATE or data extraction, even on a test environment: a condition and a delay are enough as proof.
Where input goes. Not only in a search string: filters and sorting, numeric parameters (?id=5), JSON fields, cookies, headers (User-Agent, X-Forwarded-For), previously stored values (second-order). One place where a query is built by string is enough.
A real case. In May 2023 the web application MOVEit Transfer (Progress Software, a file transfer system) turned out to have the SQL injection CVE-2023-34362. According to CISA, exploitation began around 27 May, before a fix existed. The CL0P group used the injection to install the web shell LEMURLOOT: through it they ran commands on the server, read settings and files and stole data. On 2 June CISA added the vulnerability to the KEV catalogue and on 7 June published the advisory AA23-158A. The lesson: SQL injection is not only "showing extra rows": it leads to command execution and data theft from many customers of a product.
How QA checks it. Only on an authorized environment or in a training lab (PortSwigger Web Security Academy, OWASP Juice Shop, DVWA). Start with the harmless: a single quote, a pair of "true/false conditions", a delay. Extra caution with requests that change data (UPDATE behind a form, deletion): OR 1=1 in such a query would affect every row. sqlmap can automate this, but with low risk and only within the scope.
Common mistakes.
- Considering only the situation with a visible error as injection. Blind kinds stay silent;
- Considering a query behind a login safe: after authentication the input reaches SQL just the same;
- Checking only GET parameters and forgetting JSON, headers and cookies.
Check yourself
The request ?name=laptop' AND '1'='1 returns laptops and ?name=laptop' AND '1'='2 returns an empty list. There are no errors. What does it mean?
Answer
The input reaches an SQL condition: the response depends on the truth of the added expression. This is boolean-based blind SQL injection. It is confirmed further with a delay or another pair of conditions and fixed with parameterization.
Why does a tester not add DROP TABLE to the injection even on a test environment?
Answer
A safe condition or delay is enough to prove the flaw, while a destructive command would destroy data, spoil the environment for other people and add no evidence. The pentest principle: the minimal action that proves the impact, within the permission.