Finding vulnerabilities

SQL injection

When input reaches a query unsanitized, you can change what the query means.

intro tutorial

SQL injection (SQLi) happens when untrusted input is concatenated into a SQL query, letting you alter the query’s structure rather than just its data.

Why it happens

Vulnerable query
SELECT * FROM users WHERE username = '$user' AND password = '$pass';

If $user is admin' --, it becomes:

Injected query
SELECT * FROM users WHERE username = 'admin' -- ' AND password = '';

The -- comments out the password check and you’re logged in as admin.

Detect it

A single quote that changes the response is the first signal. A lower-noise method is boolean-based testing — compare an always-true vs always-false condition:

True vs false
id=10 AND 1=1 -- normal
id=10 AND 1=2 -- differs → likely injectable

Extract with UNION

When results are reflected, find the column count, then pull data:

Columns, then extraction
' ORDER BY 3 -- -
' UNION SELECT username, password, NULL FROM users -- -

The fix: parameterized queries

Safe — parameterized
cur.execute(
"SELECT * FROM users WHERE username = %s AND password = %s",
(user, password),
)