MongoDB 27017

1. Nmap Scans for MongoDB

nmap --script mongodb-info -p 27017 <target-ip>

For database enumeration:

nmap --script mongodb-databases -p 27017 <target-ip>

2. Brute Forcing Credentials

  • Single username, password list:

hydra -l username -P passwords.txt <target-ip> mongodb
  • Username list, single password:

hydra -L usernames.txt -p password <target-ip> mongodb

3. Connecting to MongoDB

  • Local Connection:

mongo
mongo --port 27017
  • Remote Connection:

mongo --host <target-ip> --port 27017

For authenticated access:

mongo "mongodb://username:password@<target-ip>:27017/?authSource=admin"

4. Basic Commands

  • List all databases:

  • Switch database or create if it doesn’t exist:

  • List collections in the current database:

  • List all users in the current database:

  • View database users:

  • Run a JavaScript file:


5. Data Retrieval Using Queries

  • Find all documents in a collection:

  • Find one document matching a condition:

  • Operators:

    • $eq: Equal

    • $ne: Not equal

    • $gt: Greater than


6. Exploitation Techniques

  1. Unsecured Instances:

    • Unauthenticated MongoDB instances allow direct access. Simply connect using:

    • Dump all databases:

    • Export data using mongodump:

  2. Weak Credentials:

    • If credentials are identified (e.g., via brute force), authenticate and escalate data access.

  3. No Authorization Restrictions:

    • Misconfigured MongoDB can allow creation or modification of collections:

  4. Script Injection via $where:

    • Execute JavaScript within queries:


7. Automation with Scripts

You can automate enumeration and exploitation using custom scripts or tools:

  • Python automation script:

Last updated