# WebSocket Attacks {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** πŸ’¬ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %} ## What are WebSockets WebSocket 연결은 초기 **HTTP** ν•Έλ“œμ…°μ΄ν¬λ₯Ό 톡해 μ„€μ •λ˜λ©°, **μž₯κΈ° 지속적**으둜 μ„€κ³„λ˜μ–΄ μ–Έμ œλ“ μ§€ μ–‘λ°©ν–₯ λ©”μ‹œμ§•μ„ κ°€λŠ₯ν•˜κ²Œ ν•˜μ—¬ 거래 μ‹œμŠ€ν…œμ΄ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ΄λŠ” WebSocket이 μ‹€μ‹œκ°„ 금육 데이터 슀트림과 같은 **저지연 λ˜λŠ” μ„œλ²„ μ‹œμž‘ 톡신**이 ν•„μš”ν•œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ— 특히 μœ λ¦¬ν•˜κ²Œ λ§Œλ“­λ‹ˆλ‹€. ### Establishment of WebSocket Connections WebSocket 연결을 μ„€μ •ν•˜λŠ” 방법에 λŒ€ν•œ μžμ„Έν•œ μ„€λͺ…은 [**μ—¬κΈ°**](https://infosecwriteups.com/cross-site-websocket-hijacking-cswsh-ce2a6b0747fc)μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. μš”μ•½ν•˜μžλ©΄, WebSocket 연결은 일반적으둜 ν΄λΌμ΄μ–ΈνŠΈ μΈ‘ JavaScriptλ₯Ό 톡해 μ‹œμž‘λ©λ‹ˆλ‹€. ```javascript var ws = new WebSocket("wss://normal-website.com/ws"); ``` `wss` ν”„λ‘œν† μ½œμ€ **TLS**둜 보호된 WebSocket 연결을 λ‚˜νƒ€λ‚΄κ³ , `ws`λŠ” **λ³΄μ•ˆλ˜μ§€ μ•Šμ€** 연결을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. μ—°κ²° μ„€μ • 쀑에 λΈŒλΌμš°μ €μ™€ μ„œλ²„ 간에 HTTPλ₯Ό ν†΅ν•œ ν•Έλ“œμ…°μ΄ν¬κ°€ μˆ˜ν–‰λ©λ‹ˆλ‹€. ν•Έλ“œμ…°μ΄ν¬ 과정은 λΈŒλΌμš°μ €κ°€ μš”μ²­μ„ 보내고 μ„œλ²„κ°€ μ‘λ‹΅ν•˜λŠ” κ²ƒμœΌλ‘œ κ΅¬μ„±λ˜λ©°, λ‹€μŒ μ˜ˆμ‹œμ—μ„œ μ„€λͺ…λ©λ‹ˆλ‹€: λΈŒλΌμš°μ €κ°€ ν•Έλ“œμ…°μ΄ν¬ μš”μ²­μ„ λ³΄λƒ…λ‹ˆλ‹€: ```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 ``` μ„œλ²„μ˜ ν•Έλ“œμ…°μ΄ν¬ 응닡: ```javascript HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: websocket Sec-WebSocket-Accept: 0FFP+2nmNIf/h+4BP36k9uzrYGk= ``` 연결이 μ„€μ •λ˜λ©΄ μ–‘λ°©ν–₯으둜 λ©”μ‹œμ§€ κ΅ν™˜μ„ μœ„ν•΄ μ—΄λ € μžˆμŠ΅λ‹ˆλ‹€. **WebSocket ν•Έλ“œμ…°μ΄ν¬μ˜ μ£Όμš” 포인트:** - `Connection` 및 `Upgrade` ν—€λ”λŠ” WebSocket ν•Έλ“œμ…°μ΄ν¬μ˜ μ‹œμž‘μ„ μ•Œλ¦½λ‹ˆλ‹€. - `Sec-WebSocket-Version` ν—€λ”λŠ” μ›ν•˜λŠ” WebSocket ν”„λ‘œν† μ½œ 버전을 λ‚˜νƒ€λ‚΄λ©°, 일반적으둜 `13`μž…λ‹ˆλ‹€. - Base64둜 μΈμ½”λ”©λœ λ¬΄μž‘μœ„ 값이 `Sec-WebSocket-Key` 헀더에 μ „μ†‘λ˜μ–΄ 각 ν•Έλ“œμ…°μ΄ν¬κ°€ κ³ μœ ν•˜λ„λ‘ 보μž₯ν•˜λ©°, μ΄λŠ” 캐싱 ν”„λ‘μ‹œμ™€ κ΄€λ ¨λœ 문제λ₯Ό λ°©μ§€ν•˜λŠ” 데 도움이 λ©λ‹ˆλ‹€. 이 값은 인증을 μœ„ν•œ 것이 μ•„λ‹ˆλΌ 응닡이 잘λͺ» κ΅¬μ„±λœ μ„œλ²„λ‚˜ μΊμ‹œμ—μ„œ μƒμ„±λ˜μ§€ μ•Šμ•˜μŒμ„ ν™•μΈν•˜κΈ° μœ„ν•œ κ²ƒμž…λ‹ˆλ‹€. - μ„œλ²„μ˜ 응닡에 μžˆλŠ” `Sec-WebSocket-Accept` ν—€λ”λŠ” `Sec-WebSocket-Key`의 ν•΄μ‹œλ‘œ, WebSocket 연결을 μ—΄λ €λŠ” μ„œλ²„μ˜ μ˜λ„λ₯Ό κ²€μ¦ν•©λ‹ˆλ‹€. μ΄λŸ¬ν•œ κΈ°λŠ₯은 ν•Έλ“œμ…°μ΄ν¬ ν”„λ‘œμ„ΈμŠ€κ°€ μ•ˆμ „ν•˜κ³  μ‹ λ’°ν•  수 μžˆλ„λ‘ 보μž₯ν•˜λ©°, 효율적인 μ‹€μ‹œκ°„ 톡신을 μœ„ν•œ 길을 μ—΄μ–΄μ€λ‹ˆλ‹€. ### Linux μ½˜μ†” `websocat`을 μ‚¬μš©ν•˜μ—¬ websocket과의 μ›μ‹œ 연결을 μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ```bash websocat --insecure wss://10.10.10.10:8000 -v ``` μ›Ήμ†ŒμΊ£ μ„œλ²„λ₯Ό μƒμ„±ν•˜λ €λ©΄: ```bash websocat -s 0.0.0.0:8000 #Listen in port 8000 ``` ### MitM websocket connections ν΄λΌμ΄μ–ΈνŠΈκ°€ ν˜„μž¬ 둜컬 λ„€νŠΈμ›Œν¬μ—μ„œ **HTTP websocket**에 μ—°κ²°λ˜μ–΄ μžˆλŠ” 것을 λ°œκ²¬ν•˜λ©΄, ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„ κ°„μ˜ MitM 곡격을 μˆ˜ν–‰ν•˜κΈ° μœ„ν•΄ [ARP Spoofing Attack](../generic-methodologies-and-resources/pentesting-network/#arp-spoofing)을 μ‹œλ„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.\ ν΄λΌμ΄μ–ΈνŠΈκ°€ 연결을 μ‹œλ„ν•  λ•Œ, λ‹€μŒμ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€: ```bash websocat -E --insecure --text ws-listen:0.0.0.0:8000 wss://10.10.10.10:8000 -v ``` ### Websockets enumeration You can use the **tool** [**https://github.com/PalindromeLabs/STEWS**](https://github.com/PalindromeLabs/STEWS) **to discover, fingerprint and search for known** **vulnerabilities** in websockets automatically. ### Websocket Debug tools * **Burp Suite**λŠ” 일반 HTTP 톡신과 맀우 μœ μ‚¬ν•œ λ°©μ‹μœΌλ‘œ MitM websockets 톡신을 μ§€μ›ν•©λ‹ˆλ‹€. * The [**socketsleuth**](https://github.com/snyk/socketsleuth) **Burp Suite extension**λŠ” **history**λ₯Ό κ°€μ Έμ˜€κ³ , **interception rules**λ₯Ό μ„€μ •ν•˜λ©°, **match and replace** κ·œμΉ™μ„ μ‚¬μš©ν•˜κ³ , **Intruder** 및 **AutoRepeater**λ₯Ό μ‚¬μš©ν•˜μ—¬ Burpμ—μ„œ Websocket 톡신을 더 잘 관리할 수 있게 ν•΄μ€λ‹ˆλ‹€. * [**WSSiP**](https://github.com/nccgroup/wssip)**:** "**WebSocket/Socket.io Proxy**"의 μ•½μžλ‘œ, Node.js둜 μž‘μ„±λœ 이 λ„κ΅¬λŠ” ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„ κ°„μ˜ λͺ¨λ“  WebSocket 및 Socket.IO 톡신을 **capture, intercept, send custom** λ©”μ‹œμ§€λ₯Ό 보내고 λ³Ό 수 μžˆλŠ” μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. * [**wsrepl**](https://github.com/doyensec/wsrepl)λŠ” **penetration testing**을 μœ„ν•΄ νŠΉλ³„νžˆ μ„€κ³„λœ **interactive websocket REPL**μž…λ‹ˆλ‹€. μ΄λŠ” **incoming websocket messages**λ₯Ό κ΄€μ°°ν•˜κ³  μƒˆλ‘œμš΄ λ©”μ‹œμ§€λ₯Ό λ³΄λ‚΄λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•˜λ©°, 이 톡신을 **automating**ν•˜κΈ° μœ„ν•œ μ‚¬μš©ν•˜κΈ° μ‰¬μš΄ ν”„λ ˆμž„μ›Œν¬λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. * [**https://websocketking.com/**](https://websocketking.com/)λŠ” **websockets**λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€λ₯Έ μ›Ήκ³Ό ν†΅μ‹ ν•˜λŠ” **web**μž…λ‹ˆλ‹€. * [**https://hoppscotch.io/realtime/websocket**](https://hoppscotch.io/realtime/websocket)λŠ” λ‹€λ₯Έ μœ ν˜•μ˜ 톡신/ν”„λ‘œν† μ½œ μ€‘μ—μ„œ **websockets**λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€λ₯Έ μ›Ήκ³Ό ν†΅μ‹ ν•˜λŠ” **web**을 μ œκ³΅ν•©λ‹ˆλ‹€. ## 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. ## Cross-site WebSocket hijacking (CSWSH) **Cross-site WebSocket hijacking**, λ˜ν•œ **cross-origin WebSocket hijacking**으둜 μ•Œλ €μ§„ 이 곡격은 WebSocket ν•Έλ“œμ…°μ΄ν¬μ— 영ν–₯을 λ―ΈμΉ˜λŠ” νŠΉμ •ν•œ 경우의 **[Cross-Site Request Forgery (CSRF)](csrf-cross-site-request-forgery.md)**둜 μ‹λ³„λ©λ‹ˆλ‹€. 이 취약점은 WebSocket ν•Έλ“œμ…°μ΄ν¬κ°€ **CSRF tokens** λ˜λŠ” μœ μ‚¬ν•œ λ³΄μ•ˆ 쑰치 없이 였직 **HTTP cookies**λ₯Ό 톡해 인증될 λ•Œ λ°œμƒν•©λ‹ˆλ‹€. κ³΅κ²©μžλŠ” **μ·¨μ•½ν•œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜**에 λŒ€ν•œ ꡐ차 μ‚¬μ΄νŠΈ WebSocket 연결을 μ‹œμž‘ν•˜λŠ” **μ•…μ„± μ›Ή νŽ˜μ΄μ§€**λ₯Ό ν˜ΈμŠ€νŒ…ν•˜μ—¬ 이λ₯Ό μ•…μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 결과적으둜 이 연결은 μ• ν”Œλ¦¬μΌ€μ΄μ…˜κ³Όμ˜ ν”Όν•΄μžμ˜ μ„Έμ…˜μ˜ μΌλΆ€λ‘œ κ°„μ£Όλ˜λ©°, μ„Έμ…˜ 처리 λ©”μ»€λ‹ˆμ¦˜μ—μ„œ CSRF λ³΄ν˜Έκ°€ λΆ€μ‘±ν•œ 점을 μ•…μš©ν•©λ‹ˆλ‹€. ### Simple Attack **websocket** 연결을 **establishing**ν•  λ•Œ **cookie**κ°€ **μ„œλ²„**둜 **전솑**λœλ‹€λŠ” 점에 μœ μ˜ν•˜μ‹­μ‹œμ˜€. **μ„œλ²„**λŠ” 이λ₯Ό μ‚¬μš©ν•˜μ—¬ **μ „μ†‘λœ cookie**λ₯Ό 기반으둜 각 **νŠΉμ •** **μ‚¬μš©μž**와 그의 **websocket** **μ„Έμ…˜**을 **μ—°κ΄€**μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒ, 예λ₯Ό λ“€μ–΄ **websocket** **μ„œλ²„**κ°€ μ‚¬μš©μžμ˜ λŒ€ν™” **history**λ₯Ό **전솑**ν•˜λŠ” 경우, "**READY"**λΌλŠ” λ©”μ‹œμ§€κ°€ μ „μ†‘λ˜λ©΄, **simple XSS**λ₯Ό 톡해 연결을 μ„€μ •ν•˜λ©΄ (μ΄λ•Œ **cookie**λŠ” ν”Όν•΄μž μ‚¬μš©μžλ₯Ό μΈμ¦ν•˜κΈ° μœ„ν•΄ **μžλ™μœΌλ‘œ 전솑**λ©λ‹ˆλ‹€) "**READY**"λ₯Ό μ „μ†‘ν•˜μ—¬ **λŒ€ν™”**의 **history**λ₯Ό **κ°€μ Έμ˜¬** 수 μžˆμŠ΅λ‹ˆλ‹€. ```markup ``` ### Cross Origin + Cookie with a different subdomain 이 λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Ό [https://snyk.io/blog/gitpod-remote-code-execution-vulnerability-websockets/](https://snyk.io/blog/gitpod-remote-code-execution-vulnerability-websockets/)μ—μ„œ κ³΅κ²©μžλŠ” **μ›Ή μ†ŒμΌ“ 톡신이 λ°œμƒν•˜λŠ” λ„λ©”μΈμ˜ μ„œλΈŒλ„λ©”μΈμ—μ„œ μž„μ˜μ˜ Javascriptλ₯Ό μ‹€ν–‰**ν•˜λŠ” 데 μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€. **μ„œλΈŒλ„λ©”μΈ**μ΄μ—ˆκΈ° λ•Œλ¬Έμ— **μΏ ν‚€**κ°€ **전솑**λ˜μ—ˆκ³ , **Websocket이 Origin을 μ œλŒ€λ‘œ ν™•μΈν•˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ—**, 이λ₯Ό 톡해 ν†΅μ‹ ν•˜κ³  **토큰을 ν›”μΉ˜λŠ”** 것이 κ°€λŠ₯ν–ˆμŠ΅λ‹ˆλ‹€. ### Stealing data from user 당신이 κ°€μž₯ν•˜κ³  싢은 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ λ³΅μ‚¬ν•˜μ„Έμš” (예: .html 파일) 그리고 μ›Ήμ†ŒμΌ“ 톡신이 λ°œμƒν•˜λŠ” 슀크립트 μ•ˆμ— 이 μ½”λ“œλ₯Ό μΆ”κ°€ν•˜μ„Έμš”: ```javascript //This is the script tag to load the websocket hooker //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