Lab 12: NoSQL Injection

Objective

Exploit MongoDB-style operator injection against a live API from Kali Linux:

  1. $ne (not-equal) bypass — bypass login by sending {"password": {"$ne": "x"}} — any user whose password is not "x" matches

  2. $regex enumeration — use regex operators to enumerate usernames character by character

  3. $exists and $in operators — extract users by field existence and value membership

  4. Search endpoint operator injection — dump the entire user collection via the search endpoint


Background

NoSQL databases like MongoDB use JSON-based queries that support operator objects ($ne, $gt, $regex). When user input is merged directly into query objects, an attacker can inject operators to manipulate query logic — the NoSQL equivalent of SQL injection.

Real-world examples:

  • 2021 — Multiple Node.js APIs — MongoDB apps using User.findOne({username: req.body.username, password: req.body.password}) are vulnerable when Express.js parses ?username[$ne]=x into {username: {$ne: 'x'}}.

  • 2019 npm mongoose-express — popular middleware didn't sanitise operator objects; millions of packages affected.

  • Ruby on Rails + MongoDBparams hash allows nested objects; {"password": {"$gt": ""}} matches all documents where password is greater than empty string (i.e., any non-empty password).

  • GraphQL + MongoDB — GraphQL variables passed directly to MongoDB find(); attacker injects operators in the variable object.

OWASP: A03:2021 Injection


Architecture

Time

40 minutes


Lab Instructions

Step 1: Setup


Step 2: Launch Kali


Step 3: $ne Operator Injection — Login Bypass

📸 Verified Output:


Step 4: Enumerate Users via $regex


Step 5: Product Filter Injection


Steps 6–8: Full Enumeration + Remediation + Cleanup


Further Reading

Last updated