SQL injection is one of the oldest web application vulnerabilities, and it is still found today: the database executes data supplied by an attacker as SQL. The consequences are reading other people’s records, changing and deleting data, and sometimes reading files and running operating system commands. The defence is simple, but it must not be forgotten.
How the vulnerability works
It appears wherever a query is built by concatenating strings: part of the text is SQL written by the developer, part is user data. A single apostrophe is enough to close the string early and add your own code:
SELECT * FROM users WHERE login = '$login'Instead of a login, the value admin' -- arrives: the apostrophe closes the string, and the comment discards the rest of the query. If the DBMS allows several statements in a row, a second one is attached to the query, for example an UPDATE:
SELECT * FROM users WHERE login = 'admin' --'SELECT * FROM users WHERE login = '' ; UPDATE prices SET amount = 0 --'What an attacker can gain:
- Reading any table in the database, not only those shown on screen: user and internal data in full.
- Changing and deleting records, through an “always true” condition (
OR '1'='1') or an attached second statement. - Bypassing login: instead of a password check, a condition that is true in advance.
- In badly configured systems, reading files and running operating system commands through DBMS interfaces.
The technique was described in detail in Phrack magazine back in December 1998; in the OWASP Top 10 2025, injection ranks fifth.
How to defend against it
The main defence is parameterised queries: the application code defines the structure of the SQL, and values are passed separately, so the database treats them as data rather than code. Such APIs (prepared statements) exist in all common languages, and ORMs and frameworks use them by default.
- Table names, column names and sort direction must never come from user data, only from a fixed set in the application (an allowlist).
- Manual escaping is a fallback: the rules depend on the DBMS dialect and settings, and a mistake easily becomes a vulnerability.
- Least privilege for the application’s account and network isolation of the database: this does not prevent injection, but it limits the damage.
- A web application firewall (WAF) and testing, SAST and DAST, add further lines of defence but do not replace parameterisation.
Sources
the term is written from these pages- OWASP — SQL Injectionowasp.org/www-community/attacks/SQL_Injection(opens in a new tab)
- OWASP SQL Injection Prevention Cheat Sheetcheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html(opens in a new tab)
- MITRE — CWE-89: Improper Neutralization of Special Elements used in an SQL Commandcwe.mitre.org/data/definitions/89.html(opens in a new tab)
Verified 9 Nov