# NoSQL injection

## What is it?&#x20;

NoSQL injection is where an attacker can manipulate the queries made to a NoSQL database through user input.&#x20;

**A simple example:**&#x20;

* A vulnerable web application has the endpoint /search?user={username}&#x20;
* When a request is made, the application queries a NoSQL database (e.g., MongoDB) like this: `db.users.find({username: {$eq: username}})`&#x20;
* If an attacker inserts a payload into {username} such as {"$ne": ""}, it may modify the query to retrieve all users.&#x20;
* The vulnerable application sends this query to the database, potentially leaking all usernames.&#x20;

It's important to note that payloads may vary depending on the database, query, and application. NoSQL injection can lead to:&#x20;

* Sensitive data exposure&#x20;
* Data manipulation&#x20;
* Denial of service&#x20;

**Other learning resources:**

**Writeups:**

*Have a good writeup & want to share it here? Drop me a message on LinkedIn.*&#x20;

## Checklist:&#x20;

* [ ] What is the technology stack you're attacking?&#x20;
* [ ] What NoSQL DB is being used (MongoDB, CouchDB, etc.)?&#x20;
* [ ] Verify injection points:&#x20;
  * [ ] URL parameters&#x20;
  * [ ] Form fields&#x20;
  * [ ] HTTP headers (e.g., cookies, etc.)&#x20;
  * [ ] Out-of-band (data retrieved from a third party)&#x20;
* [ ] Test with different operators: $eq, $ne, $gt, $gte, $lt, $lte, etc.&#x20;
* [ ] Can you trigger different responses?&#x20;
* [ ] Test for login bypass: {"$ne": ""}&#x20;
* [ ] Test for blind NoSQLi&#x20;
* [ ] Test for errors&#x20;
* [ ] Test for conditional responses&#x20;
* [ ] Test for conditional errors&#x20;
* [ ] Test for time delays&#x20;
* [ ] Test for out-of-band interactions&#x20;
* [ ] Is there a blocklist?&#x20;
  * [ ] Can you bypass the blocklist?

## Exploitation

```
# basic login bypass
{"username": "anyname", "password": {"$ne": ""}}
```

```
# retrieve data
{"$where": "this.someField == 'someValue'"}
```

```
# blind
{"someField": {"$regex": "^someValue"}}
```

## References & Resources

{% embed url="<https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/07-Input_Validation_Testing/05.6-Testing_for_NoSQL_Injection>" %}
OWASP WSTG - Testing for NoSQL
{% endembed %}

{% embed url="<https://portswigger.net/web-security/nosql-injection>" %}
PortSwigger NoSQL Injection
{% endembed %}
