Glossary · S

SQL Injection

Attack in which untrusted input is interpreted as part of an SQL statement, letting an attacker read, change or delete data in the database.

By Obseria TeamUpdated

Definition

SQL injection (SQLi) is a vulnerability and attack technique in which input from a user is inserted into an SQL statement without proper separation, so that the database executes it as code. Attackers use it to bypass logins, read or modify data, and in some setups run commands on the database server. It is listed as CWE-89 and belongs to the Injection category of the OWASP Top 10. The main defense is parameterized queries, complemented by least privilege database accounts and monitoring.

What is SQL injection?

SQL injection happens when an application builds an SQL statement by concatenating strings that contain user input. If the input contains SQL syntax, such as a quote followed by additional conditions, the database cannot tell data and code apart and executes the attacker's version of the query. The flaw has been known since the late 1990s and is still found regularly, because it only takes one unsafe query in a large codebase.

A classic vulnerable example in Python:

# vulnerable: input becomes part of the SQL text
query = "SELECT * FROM users WHERE email = '" + email + "'"
cursor.execute(query)

# input:  ' OR '1'='1
# result: SELECT * FROM users WHERE email = '' OR '1'='1'

The condition '1'='1' is always true, so the query returns every row. With other payloads an attacker can append UNION SELECT to read other tables or add statements that change data.

Types of SQL injection

  • In-band SQL injection: results come back in the same channel. Error-based variants use database error messages to reveal structure; UNION-based variants merge the output of another query into the response.
  • Blind SQL injection: the application shows no data or errors. Attackers ask true or false questions and observe differences in the response (boolean-based) or in response time, for example with SLEEP() or pg_sleep() (time-based).
  • Out-of-band SQL injection: data is exfiltrated through a different channel, such as DNS or HTTP requests triggered by the database. It depends on database features and network access.
  • Second-order SQL injection: the payload is stored first and executed later, when another part of the application uses the stored value in a query.

Impact

Depending on the query and the database permissions, SQL injection can lead to authentication bypass, theft of personal or payment data, manipulated records, deleted tables or, in some configurations, command execution on the database host. In the OWASP Top 10 2021, injection is category A03. Many publicly disclosed data breaches started with a single injectable parameter, and individual cases often receive a CVE when they affect widely used software.

Preventing SQL injection

  • Parameterized queries: pass input as parameters instead of building SQL text. The database then treats it strictly as data.
  • Careful use of ORMs: ORMs parameterize by default, but raw query functions and string-built filters reintroduce the risk.
  • Allowlists for identifiers: table names, column names and sort orders cannot be parameterized, so validate them against a fixed list.
  • Least privilege for database accounts: the application user should not own the schema or have rights it does not need, which limits the damage of a successful injection.
  • Code review and testing: static analysis finds string-built queries before release; dynamic testing confirms what is exploitable.
  • Additional layers: a web application firewall or RASP can block known patterns, but they complement secure code rather than replace it.
# safe: parameterized query
cursor.execute("SELECT * FROM users WHERE email = %s", (email,))

Detecting SQL injection attempts

Most applications on the internet see automated injection attempts every day. Signals include request parameters with SQL keywords, quotes, comment sequences or encoded payloads in access and WAF logs, bursts of HTTP 500 errors on one endpoint, database syntax errors in application logs, and unusually slow queries caused by time-based payloads. In MITRE ATT&CK, exploitation of web applications falls under Exploit Public-Facing Application (T1190). A related attack class for AI applications is prompt injection, where instructions instead of SQL are smuggled into input.

The observability perspective

SQL injection crosses the boundary between the web layer and the database. A distributed trace shows the incoming request, the parameters that reached the service and the database span with its statement and duration. Combined with logs and error rates, this makes it possible to tell whether an attempt merely produced errors, triggered unusual queries or actually returned data, and which endpoint and code path were involved.

How Obseria helps

Obseria detects and makes SQL injection visible; it does not block requests and is not a WAF. Static code analysis (SAST) finds insecure query patterns in your repositories before release, and runtime code analysis (IAST) uses your service logs and requests to surface weaknesses such as SQL error signatures. App and API threat detection recognizes SQL injection attempts in access and WAF logs and shows affected endpoints, detection only. With distributed tracing and database monitoring, you can follow an attempt from the request to the query it caused, and block in your own WAF, proxy or code.

Frequently asked questions

Parameterized queries or prepared statements for every query that contains input. They separate code from data, so input cannot change the structure of the statement. Least privilege database accounts limit the damage if a flaw remains.

A WAF can block many known payloads, but attackers often find encodings or variants that bypass rules. It is a useful additional layer, not a replacement for parameterized queries.

In blind SQL injection the application does not show query results or errors. Attackers infer data by asking true or false questions and observing differences in the response or in the response time.

ORMs parameterize queries by default, which prevents most injection. Raw SQL functions, string-built filters and dynamic column or table names can still be vulnerable and need the same care.

Put these concepts into practice

Try Obseria free for 14 days, with OpenTelemetry and no proprietary agents.