> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/pentesting-web/xss-cross-site-scripting/dom-xss.md).

# DOM XSS

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？ 想要看到您的**公司在HackTricks中做广告**吗？ 或者想要访问**PEASS的最新版本或下载HackTricks的PDF**吗？ 请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFT收藏品](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享您的黑客技巧。**

</details>

## DOM漏洞

当来自攻击者控制的**源**（如`location.search`、`document.referrer`或`document.cookie`）的数据不安全地传输到**接收端**时，就会发生DOM漏洞。接收端是可以执行或呈现有害内容的函数或对象（例如`eval()`、`document.body.innerHTML`）。

* **源**是攻击者可以操纵的输入，包括URL、Cookie和Web消息。
* **接收端**是潜在危险的终点，恶意数据可能导致不良影响，如脚本执行。

当数据从源流向接收端时，如果没有适当的验证或清理，就会产生风险，从而可能导致XSS等攻击。

{% hint style="info" %}
**您可以在** [**https://github.com/wisec/domxsswiki/wiki**](https://github.com/wisec/domxsswiki/wiki) **中找到更多更新的源和接收端列表。**
{% endhint %}

**常见源：**

```javascript
document.URL
document.documentURI
document.URLUnencoded
document.baseURI
location
document.cookie
document.referrer
window.name
history.pushState
history.replaceState
localStorage
sessionStorage
IndexedDB (mozIndexedDB, webkitIndexedDB, msIndexedDB)
Database
```

**常见漏洞：**

| [**开放重定向**](#open-redirect)                            | [**Javascript注入**](#javascript-injection)             | [**DOM数据操纵**](#dom-data-manipulation)          | **jQuery**                                      |
| ------------------------------------------------------ | ----------------------------------------------------- | ---------------------------------------------- | ----------------------------------------------- |
| `location`                                             | `eval()`                                              | `scriptElement.src`                            | `add()`                                         |
| `location.host`                                        | `Function() 构造函数`                                     | `scriptElement.text`                           | `after()`                                       |
| `location.hostname`                                    | `setTimeout()`                                        | `scriptElement.textContent`                    | `append()`                                      |
| `location.href`                                        | `setInterval()`                                       | `scriptElement.innerText`                      | `animate()`                                     |
| `location.pathname`                                    | `setImmediate()`                                      | `someDOMElement.setAttribute()`                | `insertAfter()`                                 |
| `location.search`                                      | `execCommand()`                                       | `someDOMElement.search`                        | `insertBefore()`                                |
| `location.protocol`                                    | `execScript()`                                        | `someDOMElement.text`                          | `before()`                                      |
| `location.assign()`                                    | `msSetImmediate()`                                    | `someDOMElement.textContent`                   | `html()`                                        |
| `location.replace()`                                   | `range.createContextualFragment()`                    | `someDOMElement.innerText`                     | `prepend()`                                     |
| `open()`                                               | `crypto.generateCRMFRequest()`                        | `someDOMElement.outerText`                     | `replaceAll()`                                  |
| `domElem.srcdoc`                                       | **\`\`**[**本地文件路径操纵**](#local-file-path-manipulation) | `someDOMElement.value`                         | `replaceWith()`                                 |
| `XMLHttpRequest.open()`                                | `FileReader.readAsArrayBuffer()`                      | `someDOMElement.name`                          | `wrap()`                                        |
| `XMLHttpRequest.send()`                                | `FileReader.readAsBinaryString()`                     | `someDOMElement.target`                        | `wrapInner()`                                   |
| `jQuery.ajax()`                                        | `FileReader.readAsDataURL()`                          | `someDOMElement.method`                        | `wrapAll()`                                     |
| `$.ajax()`                                             | `FileReader.readAsText()`                             | `someDOMElement.type`                          | `has()`                                         |
| **\`\`**[**Ajax请求操纵**](#ajax-request-manipulation)     | `FileReader.readAsFile()`                             | `someDOMElement.backgroundImage`               | `constructor()`                                 |
| `XMLHttpRequest.setRequestHeader()`                    | `FileReader.root.getFile()`                           | `someDOMElement.cssText`                       | `init()`                                        |
| `XMLHttpRequest.open()`                                | `FileReader.root.getFile()`                           | `someDOMElement.codebase`                      | `index()`                                       |
| `XMLHttpRequest.send()`                                | [**链接操纵**](#link-manipulation)                        | `someDOMElement.innerHTML`                     | `jQuery.parseHTML()`                            |
| `jQuery.globalEval()`                                  | `someDOMElement.href`                                 | `someDOMElement.outerHTML`                     | `$.parseHTML()`                                 |
| `$.globalEval()`                                       | `someDOMElement.src`                                  | `someDOMElement.insertAdjacentHTML`            | [**客户端JSON注入**](#client-side-sql-injection)     |
| **\`\`**[**HTML5存储操纵**](#html-5-storage-manipulation)  | `someDOMElement.action`                               | `someDOMElement.onevent`                       | `JSON.parse()`                                  |
| `sessionStorage.setItem()`                             | [**XPath注入**](#xpath-injection)                       | `document.write()`                             | `jQuery.parseJSON()`                            |
| `localStorage.setItem()`                               | `document.evaluate()`                                 | `document.writeln()`                           | `$.parseJSON()`                                 |
| **``**[**`拒绝服务`**](dom-xss.md#denial-of-service)**``** | `someDOMElement.evaluate()`                           | `document.title`                               | **\`\`**[**Cookie操纵**](#cookie-manipulation)    |
| `requestFileSystem()`                                  | **\`\`**[**文档域操纵**](#document-domain-manipulation)    | `document.implementation.createHTMLDocument()` | `document.cookie`                               |
| `RegExp()`                                             | `document.domain`                                     | `history.pushState()`                          | [**WebSocket-URL中毒**](#websocket-url-poisoning) |
| [**客户端SQL注入**](#client-side-sql-injection)             | [**Web消息操纵**](#web-message-manipulation)              | `history.replaceState()`                       | `WebSocket`                                     |
| `executeSql()`                                         | `postMessage()`                                       | \`\`                                           | \`\`                                            |

**`innerHTML`** 漏洞在任何现代浏览器上不接受 `script` 元素，也不会触发 `svg onload` 事件。这意味着您需要使用替代元素如 `img` 或 `iframe`。

这种类型的XSS可能是**最难发现**的，因为您需要查看JS代码内部，查看是否**使用**了您可以控制的任何对象的**值**，并在这种情况下，查看是否有**任何滥用**它以执行任意JS的方法。

## 查找工具

* <https://github.com/mozilla/eslint-plugin-no-unsanitized>

## 示例

### 开放重定向

来源：<https://portswigger.net/web-security/dom-based/open-redirection>

**DOM中的开放重定向漏洞** 发生在脚本将攻击者可以控制的数据写入能够跨域进行导航的漏洞点时。

重要的是要理解，如果您控制重定向发生的URL起始部分，那么执行任意代码，如 **`javascript:alert(1)`**，是可能的。

漏洞点：

```javascript
location
location.host
location.hostname
location.href
location.pathname
location.search
location.protocol
location.assign()
location.replace()
open()
domElem.srcdoc
XMLHttpRequest.open()
XMLHttpRequest.send()
jQuery.ajax()
$.ajax()
```

### Cookie manipulation

From: <https://portswigger.net/web-security/dom-based/cookie-manipulation>

DOM-based cookie-manipulation漏洞发生在脚本将攻击者可控制的数据合并到cookie的值中时。如果cookie在网站内部使用，此漏洞可能导致网页的意外行为。此外，如果cookie用于跟踪用户会话，则可以利用它来执行会话固定攻击。与此漏洞相关的主要漏洞点是：

Sinks:

```javascript
document.cookie
```

### JavaScript注入

From: <https://portswigger.net/web-security/dom-based/javascript-injection>

DOM-based JavaScript注入漏洞是在脚本运行数据时创建的，这些数据可以被攻击者控制为JavaScript代码。

Sinks:

```javascript
eval()
Function() constructor
setTimeout()
setInterval()
setImmediate()
execCommand()
execScript()
msSetImmediate()
range.createContextualFragment()
crypto.generateCRMFRequest()
```

### 文档域操纵

来源：<https://portswigger.net/web-security/dom-based/document-domain-manipulation>

**文档域操纵漏洞**发生在脚本使用攻击者可控制的数据设置`document.domain`属性时。

`document.domain`属性在浏览器中对**同源策略**的**执行**起着**关键作用**。当来自不同源的两个页面将它们的`document.domain`设置为**相同值**时，它们可以无限制地进行交互。尽管浏览器对可分配给`document.domain`的值施加一定的**限制**，防止将完全不相关的值分配给实际页面源，但也存在例外情况。通常，浏览器允许使用**子域**或**父域**。

漏洞点：

```javascript
document.domain
```

### WebSocket-URL poisoning

From: <https://portswigger.net/web-security/dom-based/websocket-url-poisoning>

**WebSocket-URL毒化**发生在脚本使用**可控数据作为WebSocket连接的目标URL**时。

Sinks:

`WebSocket`构造函数可能导致WebSocket-URL毒化漏洞。

### 链接操纵

From: <https://portswigger.net/web-security/dom-based/link-manipulation>

**基于DOM的链接操纵漏洞**发生在脚本将**攻击者可控数据写入当前页面内的导航目标**，如可点击链接或表单的提交URL时。

Sinks:

```javascript
someDOMElement.href
someDOMElement.src
someDOMElement.action
```

### Ajax请求操纵

From: <https://portswigger.net/web-security/dom-based/ajax-request-header-manipulation>

**Ajax请求操纵漏洞**会在脚本将**可由攻击者控制的数据写入使用`XmlHttpRequest`对象发出的Ajax请求**时出现。

Sinks:

```javascript
XMLHttpRequest.setRequestHeader()
XMLHttpRequest.open()
XMLHttpRequest.send()
jQuery.globalEval()
$.globalEval()
```

### 本地文件路径操纵

来源：<https://portswigger.net/web-security/dom-based/local-file-path-manipulation>

**本地文件路径操纵漏洞**会在脚本将**可由攻击者控制的数据传递给文件处理 API**作为 `filename` 参数时出现。攻击者可以利用这个漏洞构造一个 URL，如果被另一个用户访问，可能导致**用户的浏览器打开或写入任意本地文件**。

漏洞点：

```javascript
FileReader.readAsArrayBuffer()
FileReader.readAsBinaryString()
FileReader.readAsDataURL()
FileReader.readAsText()
FileReader.readAsFile()
FileReader.root.getFile()
FileReader.root.getFile()
```

### 客户端SQL注入

From: <https://portswigger.net/web-security/dom-based/client-side-sql-injection>

**客户端SQL注入漏洞**发生在脚本以不安全的方式将**可由攻击者控制的数据**合并到客户端SQL查询中时。

Sinks:

```javascript
executeSql()
```

### HTML5存储操纵

来源：<https://portswigger.net/web-security/dom-based/html5-storage-manipulation>

**HTML5存储操纵漏洞**是指脚本将**可由攻击者控制的数据存储在Web浏览器的HTML5存储**（`localStorage`或`sessionStorage`）中时出现的漏洞。虽然这个操作本身并不是一个安全漏洞，但如果应用程序随后**不安全地读取并处理存储的数据**，就会变得有问题。这可能允许攻击者利用存储机制进行其他基于DOM的攻击，如跨站脚本和JavaScript注入。

漏洞点：

```javascript
sessionStorage.setItem()
localStorage.setItem()
```

### XPath注入

From: <https://portswigger.net/web-security/dom-based/client-side-xpath-injection>

**DOM-based XPath注入漏洞**发生在脚本将**可由攻击者控制的数据合并到XPath查询**中时。

Sinks:

```javascript
document.evaluate()
someDOMElement.evaluate()
```

### 客户端JSON注入

From: <https://portswigger.net/web-security/dom-based/client-side-json-injection>

**DOM-based JSON注入漏洞**发生在脚本将**可由攻击者控制的数据合并到一个被解析为JSON数据结构的字符串中，然后由应用程序处理**时。

Sinks:

```javascript
JSON.parse()
jQuery.parseJSON()
$.parseJSON()
```

### Web消息操纵

From: <https://portswigger.net/web-security/dom-based/web-message-manipulation>

**Web消息漏洞**会在脚本将**可由攻击者控制的数据作为Web消息发送到浏览器中的另一个文档**时出现。一个**易受攻击的Web消息操纵示例**可在[PortSwigger的Web安全学院](https://portswigger.net/web-security/dom-based/controlling-the-web-message-source)找到。

漏洞点：

使用`postMessage()`方法发送Web消息可能会导致漏洞，如果用于接收消息的事件侦听器以不安全的方式处理传入数据。

### DOM数据操纵

From: <https://portswigger.net/web-security/dom-based/dom-data-manipulation>

**DOM数据操纵漏洞**会在脚本将**可由攻击者控制的数据写入DOM中的字段**，并在可见UI或客户端逻辑中使用时出现。攻击者可以利用此漏洞构造一个URL，如果另一个用户访问该URL，就可以改变客户端UI的外观或行为。

漏洞点:

```javascript
scriptElement.src
scriptElement.text
scriptElement.textContent
scriptElement.innerText
someDOMElement.setAttribute()
someDOMElement.search
someDOMElement.text
someDOMElement.textContent
someDOMElement.innerText
someDOMElement.outerText
someDOMElement.value
someDOMElement.name
someDOMElement.target
someDOMElement.method
someDOMElement.type
someDOMElement.backgroundImage
someDOMElement.cssText
someDOMElement.codebase
document.title
document.implementation.createHTMLDocument()
history.pushState()
history.replaceState()
```

### 拒绝服务

来源：<https://portswigger.net/web-security/dom-based/denial-of-service>

**基于DOM的拒绝服务漏洞**发生在脚本将**可由攻击者控制的数据不安全地传递给有问题的平台API**时。这包括当调用时可能导致用户计算机消耗**过多的CPU或磁盘空间**的API。这类漏洞可能会产生重大副作用，例如浏览器通过拒绝尝试将数据存储在`localStorage`中或终止繁忙脚本来限制网站功能。

漏洞点：

```javascript
requestFileSystem()
RegExp()
```

## DOM欺骗

{% content-ref url="/pages/aX5LndYiCzrlYCN4dlJ7" %}
[Dom Clobbering](/pentesting-web/xss-cross-site-scripting/dom-clobbering.md)
{% endcontent-ref %}

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？想要在HackTricks中看到您的**公司广告**？或者想要访问**PEASS的最新版本或下载HackTricks的PDF**？请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[NFT收藏品**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享您的黑客技巧**。

</details>


---

# 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://hacktricks.xsx.tw/pentesting-web/xss-cross-site-scripting/dom-xss.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.
