> 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-with-csp-bypass.md).

# 带 CSP 及绕过技术的 XSS

带 CSP 及绕过技术的 XSS 笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 受 CSP 保护的反射型 XSS，带有 CSP 绕过

#### 实验描述

由内容安全策略（CSP）保护的跨站反射式脚本。目标是执行 `alert()` 通过绕过 CSP。预期的解决方案仅适用于 Chrome。

#### 目标

找到一个巧妙的 XSS 向量，绕过 CSP 并调用 `alert()` 函数。

观察

* 通过注入一个 `<script>` 标签到参数中，该标签会出现在 HTML 中，但 JavaScript 不会运行：/ 注入示例： `'</h1><script>alert(0)</script>` —— 该标签可见，但被 CSP 阻止。

```javascript
' </h1><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-4df666884d538a9410518879d47be470afd0bb8a%2Fimage%20(3060).png?alt=media" alt=""><figcaption></figcaption></figure>

CSP 只允许来自同一域的脚本（`'self'`).

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

* CSP 报告表明需要一个 token（ `令牌` 该参数似乎与 CSP 的行为有关）。

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

* 通过添加 `&token=test` 到 URL 中，应用程序会解析该参数。

```
&token=test
```

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

可以直接通过 `令牌` 参数注入 CSP 指令，例如：

```
&token=;script-src-elem 'unsafe-inline'
```

该指令会被考虑到

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

在强制添加 `script-src-elem 'unsafe-inline'`之后，以下注入可使 JavaScript 执行：

```javascript
<script>alert(0)</script>&token=;script-src-elem 'unsafe-inline'
```

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


---

# 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/xss-with-csp-bypass.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.
