SQL Injection
Key Takeaways
- SQL injection happens when user input is concatenated into a database query and changes what the query does.
- The main types are in-band, blind and out-of-band, defined by how the attacker gets results back.
- A successful SQL injection attack can expose, alter or delete data and sometimes run commands on the database host.
- Parameterized queries are the core of SQL injection prevention, backed by least-privilege database accounts.
What Is SQL Injection?
SQL injection (SQLi) is a vulnerability in which an application builds a SQL statement from untrusted input, letting an attacker inject SQL syntax that changes the query’s logic. MITRE tracks it as CWE-89, and OWASP groups it under Injection, A05 in the OWASP Top 10:2025.
A login form that runs SELECT * FROM users WHERE name = ' + input + ' shows the problem. Entering ' OR '1'='1 makes the condition always true, returning every user.
The Main Types of SQL Injection and How Each Works
In-band SQL injection
The attacker sends the payload and reads results in the same channel. Error-based injection forces database errors that leak table names or values. UNION-based injection appends a UNION SELECT to pull data from other tables into the page.
Blind SQL injection
The application returns no query output or errors, so the attacker infers data one condition at a time. Boolean-based payloads ask true-or-false questions and compare page differences. Time-based payloads such as SLEEP(5) or WAITFOR DELAY reveal answers through response delays.
Out-of-band SQL injection
The database is made to send data to an attacker-controlled server, typically through DNS or HTTP requests triggered by functions like SQL Server’s xp_dirtree or Oracle’s UTL_HTTP. It is used when responses are too unstable for blind techniques.
Second-order injection is a variant of any of these: the payload is stored first and executed later, when another query reuses it.
The Database Access SQL Injection Opens Up
The attacker inherits the permissions of the database account the application uses. Depending on those rights, they can:
- Read. Dump customer records, password hashes, payment data and session tokens.
- Modify. Change prices, balances, roles or passwords, including promoting their own account to admin.
- Delete. Drop tables or wipe records, causing outages and data loss.
- Bypass authentication. Log in as any user without a password.
- Execute. Run OS commands through features like SQL Server’s
xp_cmdshellor MySQL file writes, turning SQLi into remote code execution.
Where SQL Injection Vulnerabilities Hide and How to Find Them
SQLi hides wherever input meets a query: search boxes, filters and sort parameters, cookies, HTTP headers, JSON and GraphQL API bodies, and ORM calls that fall back to raw SQL. ORDER BY and table-name parameters are frequent misses, since they cannot be parameterized.
Manual testing starts with a single quote or a logic test such as AND 1=1 versus AND 1=2, watching for errors or content changes. Time-based payloads confirm blind cases. sqlmap automates detection and extraction once a candidate is found, while DAST scanners and Burp Suite cover breadth.
Scanners catch obvious cases but miss second-order and logic-dependent ones, a gap worth weighing when comparing the best AI penetration testing tools. If an annual test keeps returning the same SQLi findings, it is one of the signs your annual pentest report is not keeping pace with your code.
The Controls That Stop SQL Injection at the Code Level
- Parameterized queries. Use prepared statements with bound parameters in every query, so input is always treated as data.
- Safe ORM use. Rely on the ORM’s query builder and avoid raw query methods with string concatenation.
- Allowlists for identifiers. Map sort columns and table names to fixed values instead of passing user input.
- Least privilege. Give the app’s database account only the rights it needs, with no admin or file-system privileges.
- Error handling. Return generic errors to users and log details server-side.
Novee’s AI pentesting agents test injection paths across APIs and multi-step workflows, three independent agents validate each finding, and Agentic Remediation retests to confirm fixes hold.
FAQ
SQL injection manipulates SQL syntax in relational databases like MySQL or PostgreSQL. NoSQL injection targets document and key-value stores such as MongoDB, often by injecting query operators like $ne or $gt through JSON input. The root cause is the same, untrusted input shaping a query, but payloads and defenses differ.
The most widely deployed relational databases are the most targeted: MySQL and MariaDB, Microsoft SQL Server, PostgreSQL, Oracle and SQLite. No relational engine is immune, because the flaw lies in how the application builds queries. Payloads vary by engine, which is why attackers fingerprint the database first.
SQL injection falls under the Injection category of the OWASP Top 10, which ranks A05 in the 2025 edition. Injection held the #1 spot in 2010, 2013 and 2017 and dropped to A03 in 2021. The decline reflects wider use of safer frameworks, not the disappearance of the flaw.
A web application firewall can block many common payloads and buy time as a virtual patch. It cannot guarantee protection, because attackers bypass signatures with encoding, comments and alternate syntax. The only durable fix is parameterizing the vulnerable queries in code, with the WAF as an extra layer.
Yes, if the stored procedure builds dynamic SQL by concatenating its input and running it with EXEC or sp_executesql. Stored procedures that use parameters only in static statements are safe. Calling any procedure through a concatenated string from the application can also reintroduce injection.