SQL Injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries an application makes to its database.
The Core Issue: It occurs when untrusted user input is directly concatenated into a database query string instead of being handled as data. This allows attackers to manipulate the SQL statement to view, modify, or delete data they shouldn't access.
The vulnerability lies in how the computer interprets instructions versus data.
A database speaks SQL (Structured Query Language). It expects commands like SELECT or DROP to tell it what to do.
Websites ask users for input (username, search terms). This input is meant to be passive data used by the command.
If not sanitized, an attacker can input SQL characters (like ') to "break out" of the data area and write new commands.
See exactly what happens inside the database when you log in. Try to bypass the login without a password.
Try entering ' OR '1'='1 in the username field.
| ID | Username | Password |
|---|---|---|
| 1 | admin | *** |
| 2 | alice | *** |
SELECT * FROM users WHERE username = '' AND password = ''
Waiting for query...
Defense is not about filtering "bad" characters. It's about separating data from commands.
The Gold Standard
Instead of stitching strings together, you use placeholders (like ? or :id). The database driver sends the SQL template first, then the data separately. The database treats the input purely as data, never as executable code.
Defense in Depth
Validation: Ensure input matches expected types (e.g., ensure an age is an integer).
Least Privilege: The database user used by the web app should only have permissions it needs. It should never be root or sa.
The attacker uses the same communication channel to launch the attack and gather results. (e.g., seeing data in the web page itself).
No data is transferred via the web application. The attacker reconstructs the database structure by sending payloads and observing the web app's response/behavior (like time delays).
The attacker cannot use the same channel to launch the attack and gather results. They might make the DB send an HTTP request to a server they control.