No SQL Injection

  • There are four main types of NoSQL databases.

  • The way NoSQL databases store data varies significantly across the different categories and implementations.

Type
Description
Examples

Document-Oriented Database

Stores data in documents which contain pairs of fields and values. These documents are typically encoded in formats such as JSON or XML.

MongoDB, Amazon DynamoDB, Google Firebase – Cloud Firestore

Key-Value Database

A data structure that stores data in key:value pairs, also known as a dictionary.

Redis, Amazon DynamoDB, Azure Cosmos DB

Wide-Column Store

Used for storing enormous amounts of data in tables, rows, and columns like a relational database, but with the ability to handle ambiguous data types.

Apache Cassandra, Apache HBase, Azure Cosmos DB

Graph Database

Stores data in nodes and uses edges to define relationships.

Neo4j, Azure Cosmos DB, Virtuoso

Basics

  • Sample vulnerable code

app.post('/api/v1/getUser', (req, res) => {
    client.connect(function(_, con) {
        const cursor = con
            .db("example")
            .collection("users")
            .find({username: req.body['username']});
        cursor.toArray(function(_, result) {
            res.send(result);
        });
    });
});
  • Sample exploit

Authentication Bypass

  • Sample vulnerable code:

  • Exploitation:

In-Band Data Extraction

  • Just inject a payload that will force the backend to return all data in the collection.

Sample Automation Script

Server-Side JavaScript Injection

  • $where clause can be abused using the following payload:

  • Data can be exfiltrated using a similar payloads:

Automation

  • Use the following wordlists with ffuf:

    • seclists/Fuzzing/Databases/NoSQL.txt

    • https://github.com/cr0hn/nosqlinjection_wordlists/blob/master/mongodb_nosqli.txt

  • Use NoSQLMap

Last updated