> 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/dom-xss-document-write-with-location-search.md).

# location.search 中的 DOM document.write XSS

### 使用源 location.search 的 document.write 中的 DOM XSS

该实验在搜索查询跟踪功能中包含一个 XSS 侧的 DOM 漏洞。该页面使用 `document.write` 将来自……的数据写入 `location.search` 到 DOM 中，这可以通过 URL 进行控制。

**漏洞描述**/ `document.write` 会以……调用 `location.search` 未经净化的字符串。由于输入直接来自 URL，攻击者可以通过操纵……向页面注入 HTML/JS `location.search`.

<figure><img src="/files/8b3bbfc86ddd3944bed83e1fc44992b49f7b8511" alt=""><figcaption></figcaption></figure>

**解决目标** 执行一次 DOM 侧的 XSS 攻击，以调用 `alert()` 函数，当页面加载时。

**注释/提示**/ 代码会生成类似一个 `<img>` 标签，其中包含一个 `searchTerms` 带引号的参数，例如：

```html
<img src="/resources/images/tracker.gif?searchTerms=Jordan">
```

<figure><img src="/files/182625497b1513e718a1f29c64f5e97cafe7afbc" alt=""><figcaption></figcaption></figure>

如果你闭合该 `<img>` 标签和引号，你就可以插入任意 HTML/JS 内容。

**已测试载荷示例（已提供）**/ 闭合标签并注入 HTML：

```html
"><h1>hack</h1>
```

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

闭合标签并注入脚本以触发 `alert`:

```javascript
"><script>alert("xss")</script>
```

<figure><img src="/files/1ca55d92d2aab74fa1515f8bef9bf8a682d0fee2" 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 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/dom-xss-document-write-with-location-search.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.
