> 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/websockets/websocket-handshake-manipulation.md).

# WebSocket 握手操控

### 操纵 WebSocket 握手以利用漏洞

这个在线商店的实时聊天功能基于 **WebSockets**. / 系统中存在一个 XSS 过滤器：它很严格，但存在可利用的缺陷。

聊天输入框允许你发送消息，这些消息会在客服一侧实时显示。

<figure><img src="/files/546e0a7eb4d1a7e4006e4155f80f7b0ce31caecf" alt=""><figcaption></figcaption></figure>

#### 初始 XSS 过滤器测试

将以下载荷发送到聊天中时：

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

服务器返回以下消息：

* **检测到攻击：事件处理器**
* WebSocket 连接立即 **关闭**.

<figure><img src="/files/14d8614df8e042cb25bb3b23de9b4ea401b4ad02" alt=""><figcaption></figcaption></figure>

只需重新加载页面后，会出现以下消息：

* **该地址已被列入黑名单**

因此，该 IP 地址会被临时封禁。

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

#### 绕过黑名单

在建立 WebSocket 握手时添加以下 HTTP 头：

```http
X-Forwarded-For: 0.0.0.0
```

<figure><img src="/files/926866eba32e3c0c367b24776fa53457d4070fb9" alt=""><figcaption></figcaption></figure>

服务器再次接受了连接。/ 为了避免每次都手动添加，此头被配置为 **在代理中永久**.

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

#### XSS 过滤绕过

连接恢复后，将载荷稍作修改以绕过过滤器：

```javascript
<img src=0 OnErrOR=alert`0`>
```

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

这种断行和语法上的变化使其能够绕过检测。

<figure><img src="/files/f31f8a97a78b5e333b51b46f70e8486ce8c41744" 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/websockets/websocket-handshake-manipulation.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.
