Cryptography / SQL injection

SQL injection

How text typed into a form can be read by a database as code, using the textbook login example, and the two defences that stop it: parameterized queries and least privilege.

Beginner7 steps~7 min
Typed input — whatever the visitor puts in the form
Query text — SQL written by the programmer
Database — runs whatever query text it receives
// THE SHORT VERSION

In 30 seconds

SQL injection happens when a program builds a database query by gluing user input into the query text. A quote mark in the input can end the data early, and whatever follows is run as code.

That can skip a password check, read other people’s records or delete tables.

The fix is a parameterized query: the code and the values reach the database separately, so input is never read as SQL. A database account with only the permissions the app needs limits whatever damage remains.

SQL injection, step by step

STEPS01 / 07

The example compares a stored password directly to keep the query short; real systems store only password hashes. Comment and quoting rules differ a little between database products.

STEP 01 / 07 A query glued from text
THE MATH
q = "… WHERE name = '" + name + "' AND pass = '" + pass + "'"
  1. Step 1: A query glued from text. The login page takes what you type and glues it into a line of SQL, the language databases are queried in. With an ordinary name and password this works: the database finds one matching row and Alice is logged in. The weakness is that your text and the programmer’s code now sit in one string.
  2. Step 2: A quote in the input. Now a visitor types a name that contains a quote mark. In SQL a quote ends a piece of text, so the database stops reading the name there and treats what follows as instructions. Two dashes begin a comment. Input that was meant as data has become part of the code.
  3. Step 3: The password check is gone. Everything after the dashes is ignored, including the password check. The query that actually runs asks only for the row named alice. The database returns it and the app logs the visitor in as Alice without a password. This is the textbook login bypass.
  4. Step 4: Reading and deleting data. The same flaw in any form reaches further than logins. Typed SQL can bolt on a second query that returns rows from another table, such as every user’s record. Where several statements are allowed at once, it can delete a table outright. One unguarded text box puts the whole database at risk.
  5. Step 5: Send code and data apart. The fix is to stop mixing the two. With a parameterized query, also called a prepared statement, the code is sent first with question marks as placeholders, and the database works out what the query means. The values travel separately and are only ever treated as data.
  6. Step 6: Now it is only an odd name. The same input is now harmless. The database looks for a user whose name is literally alice' -- with the quote and dashes included. Nobody has that name, so it returns no rows and the login is refused. The value never passes through the SQL parser, so nothing in it can act as code.
  7. Step 7: Least privilege limits harm. As a second line of defence, the app connects with a database account that can do only what the app needs: read and add orders, not drop tables or read payroll. If one query is ever written the unsafe way, the damage stops at that account’s permissions.
VALUES SO FAR
inputalicenormal
methodglue stringscause
input····attack
check····bypass
UNION····theft
DROP····loss
?····fix
answer····safe
account····limit
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?