Bookmarks

SQLi — SQL injection

An attack in which malicious SQL reaches a database query through user input and is executed as part of that query.

Also: SQL code injection, SQLi

checked 9 Nov

All terms

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:

sqlA query built by string concatenation
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:

sqlThe comment discards the check; a second statement is attached to the first
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

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • XSScross-site scripting

    SQL injection is malicious code in a database query; XSS is code that runs in the victim’s browser.

  • SSRFserver-side request forgery

    SSRF makes the server call an address of the attacker’s choosing, whereas SQL injection makes it run arbitrary SQL in its database.

  • CVECVE identifier

    A CVE is the number of a specific vulnerability in the MITRE catalogue; SQLi is a class of vulnerabilities, and each instance has its own number.

In depth in the references

where the topic is covered in detail