# WEB10: Dom-based vulnerabilities, Websockets
## Lab: DOM-based open redirection
```!
This lab demonstrates a DOM-based redirection vulnerability that is triggered by web messaging. To solve this lab, construct an HTML page on the exploit server that exploits this vulnerability and calls the print() function.
```
Khi ta click vào một bài post, tag `a` "Back to Blog"

chứa một đoạn mã JS như sau
```html!
<div class="is-linkback">
<a href='#' onclick='returnUrl = /url=(https?:\/\/.+)/.exec(location); if(returnUrl)location.href = returnUrl[1];else location.href = "/"'>Back to Blog</a>
</div>
```
Vậy nếu trong URL có dạng `?url=https...` thì có thể redirect tới đó

=> Final payload
```
https://0a0900a604299279c00b5989008d0027.web-security-academy.net/post?url=https://exploit-0a4e006d04b292f8c0515868018100b8.exploit-server.net/&postId=1
```
Solve bài lab

## Lab: DOM XSS using web messages
```!
This lab demonstrates a simple web message vulnerability. To solve this lab, use the exploit server to post a message to the target site that causes the print() function to be called.
```
Ngay tại trang home, có một đoạn code JS nhận message và gán data cho element có id `ads`. Tuy nhiên không hề check origin của event này vì vậy ta có thể chèn iframe ở exploit server thực hiện post message là xss payload đến trang lab và call print().

Setup exploit server

Delivery to victim và solve bài lab

## Lab: DOM XSS using web messages and a JavaScript URL
```!
This lab demonstrates a DOM-based redirection vulnerability that is triggered by web messaging. To solve this lab, construct an HTML page on the exploit server that exploits this vulnerability and calls the print() function
```
Có thể thấy DOM-based open redirect ngay tại dòng code sau

Bypass đoạn check `indexOf()` bằng cách đặt chuỗi `http:` sau `//` trong javascript url
Payload:
```html!
<iframe src="https://0aa9005704f9cd69c05195fd001500e4.web-security-academy.net/" onload="this.contentWindow.postMessage('javascript:alert(123);//http:', '*')">
```

Solve bài lab

## Lab: DOM XSS using web messages and JSON.parse
```!
This lab uses web messaging and parses the message as JSON. To solve the lab, construct an HTML page on the exploit server that exploits this vulnerability and calls the print() function.
```
Tại trang home, ta cùng xét qua đoạn mã JS sau:

Đầu tiên sẽ tạo một biến `ACMEplayer` là một object có key là `element`, value là iframe object. Sau đó parse JSON từ postMessage và đưa `d.type` vào switch case, ở đây ta thấy vẫn không hề check origin của event và đồng thời ta có thể control được `.src` của iframe nếu `d.type` là `load-channel` -> javascript URL để call print().
Final payload:
```html!
<iframe src="https://0a53007c031c37c9c0bec74500fc002d.web-security-academy.net/" onload='this.contentWindow.postMessage("{\"type\": \"load-channel\", \"url\": \"javascript:print()\"}", "*")'>
```
Solve

## Lab: DOM-based cookie manipulation
```!
This lab demonstrates DOM-based client-side cookie manipulation. To solve this lab, inject a cookie that will cause XSS on a different page and call the print() function. You will need to use the exploit server to direct the victim to the correct pages.
```
Khi ta view details một product, cookie `lastViewedProduct` sẽ được thêm vào chứa link tới product vừa được xem.

Và khi ấn "Last viewed product"

sẽ được chuyển về lại trang detail của product này.
Ctrl U để check soure

ta thấy thuộc tính `href` của tag `a` được lấy từ cookie, vậy thử áp dụng reflect xss ngay chỗ này thì sao ?

Đoạn này Ấn 2 lần mới trigger được payload bởi vì lần thứ nhất sẽ là lưu vào cookie và lần thứ hai sẽ reflect ra response. Khi khai thác ta sẽ cần làm chúng một cách tự động vì vậy có thể dùng iframe và lợi dụng thuộc tính onload của nó để trigger XSS (ở lần hai)
```!
<iframe src="https://0aa700ea0303beeec14b7bee00f200d8.web-security-academy.net/product?productId=1&'><script>alert()</script>" onload="if(!window.x)this.src='https://0aa700ea0303beeec14b7bee00f200d8.web-security-academy.net/';window.x=1;">
```
`window.x` được dùng để ngăn loop vô hạn.

## Lab: Exploiting DOM clobbering to enable XSS
```!
This lab contains a DOM-clobbering vulnerability. The comment functionality allows "safe" HTML. To solve this lab, construct an HTML injection that clobbers a variable and uses XSS to call the alert() function.
```
Ở chức năng comment có hỗ trợ HTML, có một đoạn mã JS dùng để load comments GET từ server về. Tuy nhiên nếu nhìn kĩ ta phát hiện có thể khai thác DOM clobbering tại `window.defaultAvatar`. Trong trường hợp này cụ thể là clobbering với chiều sâu là hai level `window.defaultAvatar.avatar`

Payload sẽ như sau
```htmlembedded!
<a id=defaultAvatar><a id=defaultAvatar name=avatar href="cid:"onerror=alert(1)//">
```
Vì là 2 level nên ta cần hai thẻ a để tạo HTML collection, điều đặc biệt ở thẻ a đó là ta có thể lợi dụng `href` và `name` attribute để khiến return value của `toString()` theo ý muốn

Cuối cùng, dùng `cid:` protocol để kí tự `"` không bị url encode (và nó cũng nằm trong [whitelist](https://github.com/cure53/DOMPurify/blob/41766f5527233fc378b49a4d11b3ad9dc5775882/dist/purify.js#L98) của DOMpurify ), lúc send payload ta encode thành `"` để khi trình duyệt decode sẽ đóng được thuộc tính `src` của img tag.

*You might have noticed that the browser often URL encodes the values when using clobbered attributes like href. It's possible to get round this using different protocols such as file system URLs or others:*
```<a id=x href="abc:<>">
<script>
alert(x)//abc:<>
</script>
```
Send comment với payload lên trước, sau đó add thêm một comment nữa (vì lúc này mới trigger dom clobbering).
Solve

## Lab: Clobbering DOM attributes to bypass HTML filters
```!
This lab uses the HTMLJanitor library, which is vulnerable to DOM clobbering. To solve this lab, construct a vector that bypasses the filter and uses DOM clobbering to inject a vector that calls the print() function. You may need to use the exploit server in order to make your vector auto-execute in the victim's browser.
```
Tại mỗi post, sẽ gọi `loadComments('/post/comment')` để load các comments.

Và ta có thể lợi dụng `comment.author` là giá trị `Name` khi Post comment để khai thác XSS.
Nhưng trước hết cần phải bypass được HTMLJanitor library.
`HTMLJanitor.clean()` gọi tới `HTMLJanitor._sanitize()`

Hàm _sanitize này như sau:

Đầu tiên sẽ lấy ra first child từ DOM, sau đó duyệt qua các attribute và kiểm tra để loại bỏ nếu không là các attribute được phép.
Tuy nhiên ta có thể khai thác DOM clobbering ở đây để bypass

Access vào đường dẫn `/post?postId=1#f`

-> XSS thành công vậy việc còn lại là setup exploit server để gửi đến phía victim.
Exploit server, setup như sau:

Đầu tiên sẽ đợi cho các comments của trang được load hoàn tất, sau đó chỉnh lại `src` thành `src+="#f"` để focus vào form tag.
Solve

## Lab: Manipulating WebSocket messages to exploit vulnerabilities
```!
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.
```
Access vào bài lab, có tính năng Live Chat:

Check qua Websocket history ở burp, ta thấy client gửi chat message lên server và server sẽ phản hồi lại cho các user khác

Thử gửi payload xss, ta thấy nội dung đã bị html encode

Nhưng có thể bypass được bằng cách send qua Repeater và chỉnh nội dung của mess lại:

Cuối cùng send với img tag để xss

Kết quả:

## 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.
```
Thử chỉnh sửa websocket message thành một payload xss cơ bản:

-> Bị detect và khi ta F5 lại trang thì bị báo đã bị chặn IP

Có thể dùng trick quen thuộc đó là add header `X-Forwarded-For`, thêm vào trong request websocket handshake:

Và tiếp tục dò tìm các tag không bị chặn, ở đây mình thấy nếu gửi payload là `<iframe srcdoc="<script>alert(1)</script>"` thì server phản hồi

Vậy dựa theo kiến thức từ lab XSS, ta có thể HTML encode `srcdoc`, kết quả:

Solve

## 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.
```
Vì thuộc tính SameSite của cookie là None nên ta có thể khai thác CSWH một cách dễ dàng, setup exploit server như sau

Lấy được thông tin account của victim:

Login vào account này để solve bài lab

###### tags: `portswigger`