githubEdit

MongoDB

1) Run nmap

Brute-force attack

nmap -sV --script mongodb-brute -n -p 27017 <IP>

Enumeration

nmap -sV --script "mongo* and default" -p 27017 <IP>

2) Metasploit MongoDB Login Scanner

use auxiliary/scanner/mongodb/mongodb_login
set RHOSTS <target_ip>
set RPORT 27017
run

3) Manual Interaction via PyMongo

from pymongo import MongoClient
client = MongoClient('host', 27017, username='username',
password='password')
client.server_info() # Retrieve basic server info
admin = client.admin #If we have admin creds available
admin_info = admin.command("serverStatus")

# List databases
cursor = client.list_databases()
for db in cursor:
print(db)
print(client[db["name"]].list_collection_names())

4) MongoDB commands

5) MongoDB Login methods

Basic MongoDB login without specifying a database:

Specify both host and port:

Specify a particular database:

Login with a username and password:

If no credentials are required, you’ll gain access to the instance. However, if authentication is enabled, you’ll need valid credentials

6) Manual Inspection

Configuration file: mongo.db

Bitnami MongoDB setup

Check if authentication is not required (noauth enabled). If true, then MongoDB is running without authentication.

Connect to MongoDB without authentication

7) Default Admin Users

Attackers who gain access to a user-level MongoDB account can escalate their privileges by logging in as the admin user if authentication isn’t properly configured.

Identify the Admin Database

Switch to admin database and check if a user exists

Login with default or weak credentials

8) Misconfigured Role-Based Access Control (RBAC)

MongoDB uses Role-Based Access Control (RBAC) to define what actions users can perform. Sometimes, roles are misconfigured, allowing users with limited roles to gain access to privileged operations.

Enumerate User Roles

Misconfigured privileges:

Sensitive database: admin

Example: Create a new admin account

9) File System Access via MongoDB

MongoDB allows you to store files and binary data using the GridFS system. If the attacker gains dbOwner or dbAdmin privileges, they can exploit MongoDB to read or write files directly to the underlying system.

Privileges:

Write a file to the system

Read files from the system

10) Insecure Bindings

By default, MongoDB listens on all available network interfaces, which can expose the database to the public internet. If attackers can gain access to an exposed MongoDB API, they may escalate privileges through misconfigured network settings.

Identify the Binding IP (Check if MongoDB is open to the public internet)

If yes, connect remotely as admin

11) Misconfigured backup systems

MongoDB databases are often backed up regularly. If these backups are exposed to unauthorized users or misconfigured, an attacker can gain access to sensitive data or credentials stored in these backups.

Access backup directories

Extract Admin credentials from backup

12) Execute commands

Requirements: An application that uses the database runs as the owner we want to escalate

Steps:

1)

2) Copies the bash binary and gives ownership and SGID set with the context of the user that the database is running

Last updated