# 27017,27018 - Pentesting MongoDB

<details>

<summary><strong>从零开始学习 AWS 黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS 红队专家）</strong></a><strong>！</strong></summary>

支持 HackTricks 的其他方式：

* 如果您想在 HackTricks 中看到您的 **公司广告** 或 **下载 HackTricks 的 PDF**，请查看 [**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取 [**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索 [**PEASS 家族**](https://opensea.io/collection/the-peass-family)，我们的独家 [**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注** 我们的 **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向 [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来分享您的黑客技巧。

</details>

<figure><img src="/files/KRzboz7CsxUiMkcB5sXP" alt=""><figcaption></figcaption></figure>

加入 [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) 服务器，与经验丰富的黑客和赏金猎人交流！

**黑客见解**\
参与深入探讨黑客活动的刺激和挑战的内容

**实时黑客新闻**\
通过实时新闻和见解及时了解快节奏的黑客世界

**最新公告**\
随时了解最新的赏金任务发布和重要平台更新

**加入我们的** [**Discord**](https://discord.com/invite/N3FrSbmwdy)，立即与顶尖黑客合作！

## 基本信息

**MongoDB** 是一个使用 **面向文档的数据库模型** 处理各种形式数据的 **开源** 数据库管理系统。它提供了灵活性和可扩展性，用于管理应用程序中的非结构化或半结构化数据，如大数据分析和内容管理。 **默认端口:** 27017, 27018

```
PORT      STATE SERVICE VERSION
27017/tcp open  mongodb MongoDB 2.6.9 2.6.9
```

## 枚举

### 手动

```python
from pymongo import MongoClient
client = MongoClient(host, port, username=username, password=password)
client.server_info() #Basic info
#If you have admin access you can obtain more info
admin = client.admin
admin_info = admin.command("serverStatus")
cursor = client.list_databases()
for db in cursor:
print(db)
print(client[db["name"]].list_collection_names())
#If admin access, you could dump the database also
```

**一些MongoDB命令：**

```bash
show dbs
use <db>
show collections
db.<collection>.find()  #Dump the collection
db.<collection>.count() #Number of records of the collection
db.current.find({"username":"admin"})  #Find in current db the username admin
```

### 自动化

```bash
nmap -sV --script "mongo* and default" -p 27017 <IP> #By default all the nmap mongo enumerate scripts are used
```

### Shodan

* 所有mongodb：`"mongodb server information"`
* 搜索完全开放的mongodb服务器：`"mongodb server information" -"partially enabled"`
* 仅部分启用认证：`"mongodb server information" "partially enabled"`

## 登录

默认情况下，Mongo 不需要密码。\
**Admin** 是一个常见的 Mongo 数据库。

```bash
mongo <HOST>
mongo <HOST>:<PORT>
mongo <HOST>:<PORT>/<DB>
mongo <database> -u <username> -p '<password>'
```

nmap脚本：***mongodb-brute*** 将检查是否需要凭据。

```bash
nmap -n -sV --script mongodb-brute -p 27017 <ip>
```

### [**暴力破解**](/generic-methodologies-and-resources/brute-force.md#mongo)

查看 */opt/bitnami/mongodb/mongodb.conf* 文件以确定是否需要凭据：

```bash
grep "noauth.*true" /opt/bitnami/mongodb/mongodb.conf | grep -v "^#" #Not needed
grep "auth.*true" /opt/bitnami/mongodb/mongodb.conf | grep -v "^#\|noauth" #Not needed
```

## Mongo Objectid 预测

示例[在这里](https://techkranti.com/idor-through-mongodb-object-ids-prediction/)。

Mongo Object IDs 是**12字节的十六进制**字符串：

![http://techidiocy.com/\_id-objectid-in-mongodb/](/files/zzl2Ph1opmWU8eCPZVEN)

例如，这里是如何解析应用程序返回的实际 Object ID：5f2459ac9fa6dc2500314019

1. 5f2459ac: 1596217772 转换为十进制 = 2020年7月31日星期五 17:49:32
2. 9fa6dc: 机器标识符
3. 2500: 进程 ID
4. 314019: 递增计数器

在上述元素中，机器标识符将在数据库运行在相同的物理/虚拟机器上时保持不变。如果 MongoDB 进程重新启动，进程 ID 将会改变。时间戳将每秒更新一次。通过简单递增计数器和时间戳值来猜测 Object IDs 的唯一挑战在于，Mongo DB 生成 Object IDs 并在系统级别分配 Object IDs。

该工具<https://github.com/andresriancho/mongo-objectid-predict>，给定一个起始 Object ID（您可以创建一个帐户并获取一个起始 ID），它会返回大约 1000 个可能被分配给下一个对象的 Object IDs，因此您只需要对它们进行暴力破解。

## 帖子

如果您是 root 用户，您可以**修改** **mongodb.conf** 文件，以便不需要凭据（*noauth = true*）并**无凭证登录**。

***

<figure><img src="/files/KRzboz7CsxUiMkcB5sXP" alt=""><figcaption></figcaption></figure>

加入[**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) 服务器，与经验丰富的黑客和赏金猎人交流！

**黑客见解**\
参与深入探讨黑客行为的刺激和挑战的内容

**实时黑客新闻**\
通过实时新闻和见解及时了解快节奏的黑客世界

**最新公告**\
通过最新的赏金计划发布和重要平台更新保持信息更新

**加入我们的** [**Discord**](https://discord.com/invite/N3FrSbmwdy) 并开始与顶尖黑客合作！


---

# 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://hacktricks.xsx.tw/network-services-pentesting/27017-27018-mongodb.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.
