SQL Injection

User input reaches a database query without being separated from the query's own syntax, so it can change what the query does.

SeverityCritical
CVSS 3.19.8 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H Open in the calculator
CVSS 4.09.3 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N Open in the calculator
CWECWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
OWASP Top 10A05:2025 Injection
MITRE ATT&CKT1190 Exploit Public-Facing Application
CategoryInjection

Description

The application builds SQL queries by combining fixed query text with values supplied by the user, without separating the two. A value containing SQL syntax, such as a quotation mark followed by further clauses, is therefore read by the database as part of the query rather than as data.

By changing the query in this way, an attacker can read any data the application's database account can reach, including other users' records, password hashes and secrets kept in the database. Depending on that account's privileges and the database platform, they may also be able to change or delete data, bypass authentication, read or write files on the database server, or run operating system commands on it, which would give them a foothold in the hosting environment.

SQL injection can be confirmed directly, where the database's own error messages or the results of an altered query are returned in the response, or indirectly, where the application behaves differently when a condition injected into the query is true or false, or responds more slowly when a delay is injected. The indirect forms, often called blind SQL injection, are as exploitable as the direct one, though extracting data takes more requests. Automated tools such as sqlmap can identify and exploit every form of the weakness with little effort, so the risk does not depend on an attacker's skill.

Remediation

Keep data apart from query syntax everywhere the application talks to its database:

  • Use parameterised queries (prepared statements) for every query that includes a value from outside the application, including values from cookies, headers and other systems. The query text is fixed, and each value is bound to a placeholder, so it can never change the query's structure.
  • Where an ORM or query builder is used, use its parameter binding rather than its raw query methods, and review any place where query text is built by concatenation.
  • Where a value cannot be a parameter, such as a column name or sort direction, choose it from a fixed allowlist in the code rather than passing the user's value through.
  • Run the application's database account with the least privilege it needs, without administrative rights, file access or the ability to run system commands.

As an interim measure, a web application firewall rule can block known injection payloads on the affected parameters, but it can be bypassed and is not a fix. Confirm the fix by repeating the original test against every affected parameter and confirming that the injected syntax is treated as a literal value.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding