Burp Web Academy WebSockets
0x01. PortSwigger Web Security Academy
PortSwigger Web Security Academy 是 Burp Suite 官方推出的免费 Web 安全学习靶场,在学习 Web 安全知识的同时,还可以练习 Burp Suite 的实战技能。
本篇文章讲解 Web Security Academy 之中的 WebSockets 章节。
0x02. WebSockets
2.1 Lab: Manipulating WebSocket messages to exploit vulnerabilities
his online shop has a live chat feature implemented using WebSockets.
Chat messages that you submit are viewed by a support agent in real time.
To solve the lab, use a WebSocket message to trigger an
alert()
popup in the support agent’s browser.
Burp Suite 支持 WebSocket 消息的捕获和重放,就和 HTTP 消息一样。而分析聊天功能,发现代码位于 /resources/js/chat.js
,其中消息在发出之前会进行转义:
1 | function htmlEncode(str) { |
因此,可以通过多种方式进行绕过:
- 修改
chatForm
的encode
属性 - 重放 WebSocket 消息
注意,消息的传递基于 JSON 实现,部分字符会被 JSON 强制转义,可能导致 Payload 失效,比如:
- 发送
<script>alert(1)</script>
,服务器返回<script>alert(1)<\/script>
- 发送
<img src=1 onerror='alert(1)'>
可以正常触发 XSS
2.2 Lab: Cross-site WebSocket hijacking
This online shop has a live chat feature implemented using WebSockets.
To solve the lab, use the exploit server to host an HTML/JavaScript payload that uses a cross-site WebSocket hijacking attack to exfiltrate the victim’s chat history, then use this gain access to their account.
前面的 XSS 漏洞修复了,服务端会对 JSON 中的特殊字符进行转义,比如 <
转义为 <
,另外 Cookie 的属性为 HttpOnly; SameSite=None
,所以 XSS 本身也无法读取 Cookie。
观察聊天室 WebSocket 请求:
1 | GET /chat |
看起来仅仅使用了 Cookie
,注意 Sec-WebSocket-Key
没有鉴权作用,仅仅用作 WebSocket 请求的一部分。
构造如下 Exploit 代码:
1 | <script> |
代码解读:
- 建立 WebSocket 连接,普通协议使用
ws://
,安全协议使用wss://
- 观察聊天室协议交互方式,客户端需要发送
READY
消息 - 当 WebSocket 收到消息时,会调用
onmessage
回调函数,有两种方式注册(event.data
就是消息内容)addEventListener("message", (event) => {});
onmessage = (event) => {};
fetch
用于触发 HTTP 请求(注意是访问攻击者的域名)mode
用于指明请求是否可以跨域cors
是默认模式,表明遵守CORS
设定same-origin
标识完全禁止跨域请求no-cors
标识忽略CORS
设定,一旦使用该设定,就不允许访问 Response 了
Setting
mode
tono-cors
disables CORS for cross-origin requests. This restricts the headers that may be set, and restricts methods to GET, HEAD, and POST. The response is opaque, meaning that its headers and body are not available to JavaScript. Most of the time a website should not useno-cors
: the main application of it is for certain service worker use cases.
代码的功能是通过 WebSocket 的形式触发 CSRF 漏洞,然后将聊天消息转发给 Burp Collaborator,本质上是 WebSocket 版本的 CSRF 漏洞的利用。
2.3 Lab: Manipulating the WebSocket handshake to exploit vulnerabilities
This online shop has a live chat feature implemented using WebSockets.
It has an aggressive but flawed XSS filter.
To solve the lab, use a WebSocket message to trigger an
alert()
popup in the support agent’s browser.
尝试直接通过 WebSocket 发送 XSS Payload,提示检测到攻击:
1 | {"message":"<img src=1 onerror='alert(1)'>"} |
刷新页面居然提示被拉黑了:
1 | "This address is blacklisted" |
使用 X-Forwarded-For 可以绕过黑名单,但是每次检测到 XSS 就会拉黑,所以需要不断重复绕过操作。而 XSS 绕过,就看技术了,参考了官方答案:
1 | {"message":"<img src=1 OnError=alert`1`>"} |
自己测只能盲测,看不到具体的过滤逻辑。
0x03. 小结
- JSON 强制转义可能影响 XSS Payload 的有效性
- WebSocket 版本的 CSRF 漏洞利用
fetch
API- XSS 绕过
0x04. 参考文档
- https://portswigger.net/web-security/all-labs#websockets
- https://portswigger.net/web-security/websockets/cross-site-websocket-hijacking
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-WebSocket-Key
- https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event
- https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch