
Flash CTF - Socket Malfunction
Socket Malfunction Writeup
Overview
We get a packet capture of a door controller's WebSocket session. The recording starts after the connection was already up, so there is no handshake to tell any tool what the traffic is. One side of the conversation reads as plain JSON and the other is noise. Our job is to read the noisy side and recover the credential the operator sent.
Solution
We are given a pcap capture file:
$ file control-socket.pcap
control-socket.pcap: pcap capture file, microsecond ts (little-endian) - version 2.4 (Ethernet, capture length 65535)Before anything else, let's check whether the credential is simply sitting there.
$ strings control-socket.pcap | grep -i skillbit
$Nothing, so it is not stored as text. Opening the file shows one short TCP session and nothing else.
$ tshark -r control-socket.pcap
1 0.000000 192.168.1.42 → 192.168.1.9 TCP 54 51788 → 47821 [SYN] Seq=0 Win=8192 Len=0
2 0.006874 192.168.1.9 → 192.168.1.42 TCP 54 47821 → 51788 [SYN, ACK] Seq=0 Ack=1 Win=8192 Len=0
3 0.012268 192.168.1.42 → 192.168.1.9 TCP 54 51788 → 47821 [ACK] Seq=1 Ack=1 Win=8192 Len=0
4 0.047698 192.168.1.9 → 192.168.1.42 TCP 104 47821 → 51788 [PSH, ACK] Seq=1 Ack=1 Win=8192 Len=50
5 0.086782 192.168.1.42 → 192.168.1.9 TCP 94 51788 → 47821 [PSH, ACK] Seq=1 Ack=51 Win=8192 Len=40Every packet is plain TCP. We were told this is WebSocket traffic, but the capture began after the upgrade and the port is not one any dissector claims, so nothing decodes it for us. Following the stream shows why that matters.
server: {"ev":"hello","fw":"2.4.1","dev":"bay-door-ctl"}
client: ..s..k.......I...........I.......I....Q.
server: {"ev":"state","door":"closed","lock":"engaged"}
client: ..41Y.O.:uP.c:UD-p..{sQH{".b2qX].q@J4,GZhvSnhkk_ilk.7{FH)l..7e.L
server: {"ev":"auth","result":"ok","scope":"operator"}
server: {"ev":"state","door":"open","lock":"released"}
That's the whole flow. The controller identifies itself, reports that the door is closed, accepts an authentication message we never get to see, and then reports that the door is open. Whatever we're looking for was sent in the client message right before "result":"ok", but that's the one part we can't read.
Only the client side is unreadable, which is a strange way for encryption to behave. If the session were encrypted, the server's frames would be noise too. Let's look at the raw bytes of that client turn.
81 ba 34 31 59 18 4f 13 3a 75 50 13 63 3a 55 44 2d 70 16 1d 7b 73 ...
Both client turns start 81, and so do the readable server turns. That is a WebSocket frame header: 0x81 is the FIN bit plus opcode 1, meaning a complete text message. The next byte differs between the two sides though. The server's is a small number, the length. The client's is 0xba, which has its top bit set.
That top bit is the mask flag. RFC 6455 requires every client-to-server frame to be masked, and the four bytes straight after the length are the masking key. So 0xba is the mask bit plus a payload length of 58, the key is the next four bytes, and the payload follows.
81 FIN + text
ba masked, payload length 58
34 31 59 18 masking key
4f 13 3a 75 ... payload
The masking is a plain XOR with that key repeating every four bytes. The key is not a secret and it is not negotiated, it travels in the open right next to the data it covers. It exists so a broken proxy cannot be tricked into caching a forged response, not to hide anything.
So we XOR the payload with the key.
$ python3 -c "
from scapy.all import rdpcap, Raw
f = [bytes(p[Raw].load) for p in rdpcap('control-socket.pcap') if Raw in p and p.sport == 51788][1]
k = f[2:6]
print(bytes(b ^ k[i % 4] for i, b in enumerate(f[6:])).decode())"
{"cmd":"auth","key":"SkillBit{m4sk1ng_1s_n0t_3ncrypt10n}"}The same thing works in CyberChef: paste the payload bytes, add an XOR operation, and set the key to 34315918 in hex.
Cyberchef Recipe: From Hexdump + XOR