SQL injection: when input becomes part of the query

SQL injection (SQLi) is an attack in which input sent to an application ends up inside a database query and changes what the query does. It can read data the user should not see, change or delete records, and sometimes reach the server behind the database. It is preventable with one well-known technique, and still common.

Section Web attacks Updated Weakness CWE-89

Quick facts

Weakness
CWE-89: improper neutralization of special elements in an SQL command
OWASP category
A05:2025 Injection
Impact
Reading, changing or deleting data; bypassing sign-in; in some setups, commands on the database server
Main fix
Parameterised queries (prepared statements)
Does a WAF stop it?
Mostly. Query syntax in the request is recognisable; the vulnerable query stays in the code until fixed

Definitions follow OWASP and CWE-89.

How SQL injection works

An application builds a query by joining strings: the fixed part written by the developer, and a value taken from the request, such as an account number or a search term. If the value is joined in as text, the database cannot tell where the developer's query ends and the user's input begins.

An attacker sends input that contains SQL syntax: a quote that closes the string, an operator that changes the condition, or a clause that adds a second query. The database runs the result as one command. OWASP's standard example turns a lookup for one account into a lookup that returns every account by adding a condition that is always true.

Types of SQL injection

  • In-band SQL injection. Results come back in the application's own response, either as data (union-based) or in database error messages (error-based).
  • Blind SQL injection. Nothing is shown; the attacker infers answers from whether a page changes (boolean-based) or how long it takes to respond (time-based).
  • Out-of-band SQL injection. The database is made to send data through another channel, such as a DNS or HTTP request to a server the attacker controls.
  • Second-order SQL injection. Input is stored safely first and only becomes part of a query later, in a different feature that trusts stored data.

The types differ in how data leaves, not in the cause: in every case input was joined into a query as text.

WHAT EACH TYPE LEAVES BEHIND SYNTAX IN THE REQUEST RESULT IN THE RESPONSE FIXED BY PARAMETERS IN-BAND (UNION, ERROR) BLIND (BOOLEAN, TIME) OUT-OF-BAND SECOND-ORDER // every row ends in the same fix
// four types, one cause and one fix

What it looks like in a request

Most SQL injection attempts carry characters that have no business in the field: quotes in a numeric identifier, comment markers, keywords such as UNION or SELECT in a search box, or operators that build a condition. Automated scanners send many of these in a short time, which also shows in the request rate.

Legitimate text can contain the same characters. A support ticket quoting a query or a name with an apostrophe looks similar to a filter, which is why rules for SQL injection are scored and tuned per site.

What a WAF catches, and what it misses

A request filter matches the shapes above and can refuse them before they reach the code; rule sets such as the OWASP Core Rule Set keep a dedicated group of rules for it. That protects an injectable query while it is being fixed.

It misses what does not look like SQL in the request: second-order injection, input encoded in a way the rules do not decode, and injection through channels the filter does not read. And it leaves the query vulnerable, so the next variation may get through.

How to prevent SQL injection

  1. Use parameterised queries. Prepared statements send the query and the values separately, so input can never become syntax. The OWASP SQL Injection Prevention Cheat Sheet puts this first.
  2. Use stored procedures carefully. They help only if they do not build SQL from strings inside.
  3. Validate input against what is allowed. An allow-list for things that cannot be parameters, such as column names or sort order.
  4. Give the database account the least privilege it needs. So an injection that slips through can read less and change nothing it should not.
  5. Do not show database errors to users. Error text helps attackers map the query.

Questions

What is SQL injection?

SQL injection is an attack in which input sent to an application becomes part of a database query and changes what it does, letting an attacker read, change or delete data.

How do you prevent SQL injection?

Use parameterised queries, also called prepared statements, so input is never treated as SQL. Add allow-list validation for values that cannot be parameters, least-privilege database accounts, and generic error messages.

What is blind SQL injection?

Blind SQL injection is SQL injection where the application shows no query results or errors, so the attacker infers answers from whether a page changes or how long it takes to respond.

Can a WAF prevent SQL injection?

A WAF can refuse many SQL injection attempts by matching query syntax in requests, which protects vulnerable code while it is fixed. It does not make the query safe; parameterised queries do.