SQL Injection

Video + 11 min read · includes a lab

Login form
admin.chaicorp.example
Parameterised queries
Database
SQL injection
' OR '1'='1
SQL Injection
0:00 / 4:09 · SQL Injection
Chapters and transcript

SQL Injection What if one quote mark could let someone log in with no password? That is SQL injection, one of the most famous web vulnerabilities. In this lesson you will see how it works on our practice site, and how developers stop it for good.

Where SQL fits Most websites store their data in a database, and talk to it using SQL. When you log in, the browser sends your username and password. The website builds a SQL command, and sends it to the database. If a user is found, you are logged in.

The login query Here is the query a vulnerable website might build. The website pastes what you typed straight into the command. Look: the username ali and the password sit right inside the SQL. And each one is wrapped in quote marks. That pasting is the whole problem.

Login bypass Now the laptop. This is the staff login on our practice site, admin dot chaicorp dot example. As the username, I type admin. As the password, I type a quote, then OR, then one equals one, in quotes. I click Log in. The site lets me in, with no real password.

What a quote does Why did that work? The quote closed the text early, so the password is now empty. The rest of the input became part of the SQL command: OR one equals one. SQL checks AND before OR. And one equals one is always true, so every row matches, and the attacker is in.

What a quote does Another classic trick uses a comment. In many databases, two dashes start a comment. As the username, I type admin, a quote, then two dashes. The password can be anything. Everything after the dashes is ignored, including the password check.

The impact Bypassing a login is only the start. The attacker sends input that changes the command. The website passes it to the database. Depending on the database, SQL injection can let an attacker read every table, change or delete data, and sometimes run commands on the server itself.

Signs of SQLi How do testers spot it? Adding a single quote causes an error or a strange page. Error messages mention SQL, MySQL or syntax. Results change when you add a condition that is true, compared with one that is false. Tools such as sqlmap automate this testing, but only use them with permission.

Quick check Quick check. You are the developer of that login page. Which change really stops SQL injection? Think about why the attack worked, then choose.

The fix The real fix is parameterised queries, also called prepared statements. The vulnerable code pastes input into the command, so input can become code. The safe code sends the query and the data separately. The database always treats the input as data, never as part of the command.

The fix Let me try the same trick against a fixed version of the login. I type admin, then the same quote, OR, one equals one, and click Log in. This time the login fails. The database looked for a password that really is that strange text, and found none.

Extra layers Add extra layers too. Use an ORM or query builder that parameterises for you. Give the database account only the permissions it needs. Show generic error messages to users, and log the details privately. But remember: blocking words like OR, or removing quotes, is not a fix. Attackers have many ways around filters.

Putting it together Let us put it together. SQL injection happens when input is pasted into a SQL command. A quote can close the text early, and the rest becomes code. Attackers can bypass logins, read tables and change data. The real fix is parameterised queries, with extra layers on top. Now read the lesson below, and try the lab.

Most websites store their data in a database and talk to it using SQL. If a website builds SQL commands by pasting in user input, attackers can change those commands. This is SQL injection (SQLi), one of the most famous web vulnerabilities.

A normal login query

When you log in, a vulnerable website might build this query:

SELECT * FROM users WHERE username = 'ali' AND password = 'secret123'

If a user is found, you are logged in. The username and password come straight from the form.

What a quote does

Now imagine someone types admin as the username and this as the password:

' OR '1'='1

The query becomes:

SELECT * FROM users WHERE username = 'admin' AND password = '' OR '1'='1'

The quote closed the text early, and the rest of the input became part of the SQL command. SQL checks AND before OR, so the database reads it as: (username is admin and password is empty) or (1 equals 1). Since '1'='1' is always true, every row matches and the attacker is logged in without a password.

Another classic trick uses a comment to cut off the rest of the query:

admin' --
SELECT * FROM users WHERE username = 'admin' -- ' AND password = '...'

Everything after -- is ignored, including the password check.

What attackers can do with SQLi

Bypassing a login is only the start. Depending on the database, SQL injection can let an attacker:

  • Read every table: customers, passwords, card data
  • Change or delete data
  • Sometimes run commands on the server itself

Signs a tester looks for

  • Adding a single quote ' causes an error or a strange page
  • Error messages that mention SQL, MySQL or syntax
  • Results that change when you add ' AND '1'='1 versus ' AND '1'='2

Tools such as sqlmap automate this testing, but only use them with permission.

How to defend

Parameterised queries (also called prepared statements) are the real fix. The query and the data are sent separately, so input is always treated as data:

// Vulnerable
db.query("SELECT * FROM users WHERE username = '" + username + "'");

// Safe
db.query("SELECT * FROM users WHERE username = ?", [username]);

Extra layers:

  • Use an ORM or query builder that parameterises for you
  • Give the database account only the permissions it needs
  • Show generic error messages to users, and log the details privately

Blocking words like OR or removing quotes is not a fix. Attackers have many ways around filters.

In the lab, you will see the query change live as you type, and log in without a password.

HANDS-ON LAB

Login Bypass

This login builds its database query from your input. Log in without a password.

Start the lab →

Check your understanding

Answer every question correctly to complete this lesson. If you miss one, try the quiz again.

1. What causes SQL injection?

2. What does -- do in many SQL databases?

3. What is the best defence against SQL injection?

Locked: answer every quiz question above correctly to complete this lesson.

Spotted a mistake or something unclear? Report a mistake