NoSQL Injection Lab: Log In as Admin Without a Password

✨ AI Summary
MongoLogin takes your username and password as JSON and drops them straight into a database query. It expects two strings. What happens when you send something that is not a string? In NoSQL injection you do not break out of quotes, you change the shape of the query itself — and the login waves you through.
Lab at a glance: Intermediate · 90 XP · about 15 minutes · runs in your browser, no signup or setup. Try it yourself first; the full solution is behind a spoiler below.

What this lab teaches

NoSQL injection abuses the fact that document databases like MongoDB take structured queries, not flat strings. When an app builds a query directly from untrusted JSON, an attacker can slip in query operators instead of values.

The lesson: the danger is not special characters, it is letting the user control the structure of the query. SQL injection breaks out of a string; NoSQL injection changes a value into an object the database treats as a command.

How MongoLogin checks your password

You submit {"username":"admin","password":"..."} and the backend hands that object straight to MongoDB as the query filter. Because your input becomes part of the filter, whatever you put in the password field is interpreted by the database, not compared as a plain string.

The attack approach

  • Notice the login accepts a JSON body, not a normal form — the password value is passed into the query untouched.
  • Instead of a password string, send an object that uses a MongoDB comparison operator.
  • Pick an operator that matches any stored value, so the password check is always true.
  • Submit with username admin and watch the query match the admin record regardless of the real password.

Full walkthrough (spoiler)

Tried a few shapes and still locked out? Open the steps. Working it out yourself first is worth more XP.

Show the step-by-step solution
  1. Switch the password from a string to an object in the JSON body.
  2. Use a not-equal style operator against an impossible value so the comparison is always true for every document.
  3. Keep the username as admin so the filter still targets the admin account.
  4. Send it. The query now reads 'username is admin AND password is anything', so the admin record matches and you are logged in. The lab detects the admin session and marks you solved.

Why it works

The backend trusts the JSON body to be two strings and never validates the type. MongoDB sees an object where it expected a value, interprets the key as an operator, and happily evaluates it. No quote-breaking, no payload encoding — the bug is purely structural: user input decided how the query behaves.

Core principleTreat every value from the client as data, never as part of the query structure. Validate types before they reach the database.

Where this happens in the real world

NoSQL injection shows up wherever a request body is passed into a query builder without type checks — common in Node.js and Express apps that forward req.body straight into a MongoDB find(). It is a recurring bug-bounty finding in JSON APIs, and OWASP tracks it under Injection, which sits at A03 in the OWASP Top 10 (2021) alongside SQL injection.

How to defend against it

  • Validate and cast types. The password must be a string — reject anything that is not, before it reaches the query.
  • Use a schema layer (such as a validated model) so request bodies cannot introduce arbitrary operators.
  • Never pass raw request bodies into a query. Build the filter from named, type-checked fields.
  • Compare password hashes explicitly rather than letting the database match the value.
Mr Elite tip: On any JSON API you are authorised to test, try sending an object where the app expects a string. If the behaviour changes, the input is reaching a query engine that trusts its structure.

Try it, then go further

Reading about it is the easy part. Open the lab, try your own approach before peeking at the spoiler, and see how many attempts it takes.

❓ Frequently asked questions

What is NoSQL injection?
An attack that abuses apps which build NoSQL database queries from untrusted input, letting an attacker inject query operators to change how the query behaves — for example to bypass a login.
How is it different from SQL injection?
SQL injection breaks out of a quoted string in a text query. NoSQL injection usually sends a structured object (JSON) where a value was expected, changing the query's logic without any quote-breaking.
Does using MongoDB make me vulnerable?
No. The database is not the problem — passing unvalidated user input straight into a query is. Type-check every value and NoSQL injection goes away.
How do I test for it safely?
Only on apps you are authorised to test. Send an object with a comparison operator where a string is expected and watch whether the response or auth behaviour changes.
⚡
Join free to earn XP for reading this article Track your progress, build streaks and compete on the leaderboard.
Join Free
Lokesh N. Singh aka Mr Elite
Lokesh N. Singh aka Mr Elite
Founder, Securityelites · AI Red Team Educator
Founder of Securityelites and creator of the SE-ARTCP credential. Working penetration tester focused on AI red team, prompt injection research, and LLM security education.
About Lokesh ->

Leave a Comment

Your email address will not be published. Required fields are marked *