Writeup · 31 Nights of Root

Beacon

forensicseasysolvedFlag: r00t{REDACTED}

Overview

A suspected botnet infection. beacon.pcap holds a captured traffic segment from the affected host. Something in there is checking in with someone it shouldn’t be.

sequenceDiagram
    participant B as Bot 10[.]60[.]4[.]112
    participant C as C2 91[.]242[.]87[.]19:4444
    B->>C: CHECKIN:host-7734:...000:idle
    B->>C: CHECKIN:host-7734:...060:idle
    B->>C: CHECKIN:host-7734:...120:idle
    B->>C: CHECKIN:host-7734:...180:active
    C->>B: TASK:ack:r00t{REDACTED}

Prerequisites / Setup

  • Python 3 (struct, pickle) — no tshark, scapy, or dpkt was available in the sandbox and pip had no index access, so a ~20-line libpcap parser built on struct was used
  • beacon.pcap in the working directory

Recon

Step 1 — Parse the capture

Goal: Read the classic libpcap capture (Ethernet, microsecond timestamps) into per-packet records since no packet-analysis tools were available.

import struct, sys, pickle

data = open(sys.argv[1], "rb").read()
off, pkts = 24, []                      # skip 24-byte global header
while off + 16 <= len(data):
    sec, usec, caplen, _ = struct.unpack("<IIII", data[off:off+16]); off += 16
    pkts.append((sec + usec / 1e6, data[off:off+caplen])); off += caplen

ip = lambda b: ".".join(map(str, b))
rows = []
for t, d in pkts:
    if struct.unpack("!H", d[12:14])[0] != 0x0800:        # not IPv4 (ARP etc.)
        continue
    ihl, proto = (d[14] & 15) * 4, d[23]
    src, dst, l4 = ip(d[26:30]), ip(d[30:34]), d[14+ihl:]
    sp, dp = struct.unpack("!HH", l4[:4])
    if proto == 6:                                         # TCP
        hl = (l4[12] >> 4) * 4
        rows.append((t, "tcp", src, dst, sp, dp, l4[hl:], l4[13]))
    elif proto == 17:                                      # UDP
        rows.append((t, "udp", src, dst, sp, dp, l4[8:], 0))
pickle.dump(rows, open(sys.argv[2], "wb"))

Step 2 — Triagem by protocol

Goal: Discard the noise and find the anomalous flow.

Traffic Packets Verdict
TCP/443 ~1,560 TLS-style noise, many hosts, 3–4 packets each
UDP/53 650 Plain DNS queries
UDP/123 60 NTP
ARP 240 Background
TCP/4444 16 Anomaly

DNS is a red herring: only 14 unique names were queried, all type A, all well-known SaaS/CDN hosts (update[.]googleapis[.]com, outlook[.]office365[.]com, fonts[.]googleapis[.]com, clients2[.]google[.]com, teams[.]microsoft[.]com, graph[.]microsoft[.]com, drive[.]google[.]com, s3[.]amazonaws[.]com, cdn[.]jsdelivr[.]net, time[.]windows[.]com, docs[.]google[.]com, api[.]slack[.]com, github[.]com, zoom[.]us). No long or high-entropy labels, no TXT/NULL queries — no tunnelling.

Step 3 — Isolate the suspect flow

Goal: Confirm port 4444 is the only non-443 TCP session carrying payload.

Port 4444 is the Metasploit/netcat default and a common C2 port. Only one conversation uses it:

10.60.4.112:39281 -> 91.242.87.19:4444   SYN
91.242.87.19:4444 -> 10.60.4.112:39281   SYN/ACK
10.60.4.112:39281 -> 91.242.87.19:4444   PSH/ACK  33 B   (t = 75.3 s)
10.60.4.112:39281 -> 91.242.87.19:4444   PSH/ACK  33 B   (t = 77.1 s)
10.60.4.112:39281 -> 91.242.87.19:4444   PSH/ACK  33 B   (t = 79.4 s)
10.60.4.112:39281 -> 91.242.87.19:4444   PSH/ACK  35 B   (t = 81.6 s)
91.242.87.19:4444 -> 10.60.4.112:39281   PSH/ACK  44 B   (t = 83.1 s)   <- server reply
                                          FIN/ACK both ways

Four small client-to-server messages followed by one longer server reply — the shape of a check-in/tasking exchange. The payloads are clearly not plaintext:

74ea11a08c4b79983c8cb4761a9563d0f338069563d3f732079264d3fd6b53ce31
74ea11a08c4b79983c8cb4761a9563d0f338069563d3f732079262d3fd6b53ce31
74ea11a08c4b79983c8cb4761a9563d0f338069563d3f732079366d3fd6b53ce31
74ea11a08c4b79983c8cb4761a9563d0f338069563d3f732079 36cd3fd6354d63d95a2

Exploitation

Step 4 — Single-byte XOR fails

Goal: Rule out the trivial case.

Brute-forcing all 256 single-byte keys and scoring for printable ASCII gives nothing usable (best candidate 0x40 is still garbage).

Step 5 — Find the key length

Goal: Determine the repeating-key XOR key length.

For a repeating-key XOR with key length K:

c[i] ^ c[i+K]  =  p[i] ^ p[i+K]

The key cancels. If the plaintext is ASCII (bit 7 always clear), every such XOR must be < 0x80. Scanning K = 1..20 and counting how many XOR values fall under 0x80:

for K in range(1, 21):
    d = [m[i] ^ m[i+K] for i in range(len(m) - K)]
    print(K, sum(x < 0x80 for x in d), "/", len(d))
K beacon (35 B) reply (44 B)
3 21 / 32 27 / 41
5 10 / 30 13 / 39
6 29 / 29 38 / 38
12 23 / 23 32 / 32
18 17 / 17 26 / 26

K = 6 is a perfect score in both directions (12 and 18 are just its multiples). The same 6-byte key protects client and server messages — cross-checked by noticing the byte pairs f7 32 and fd 6x 5x appearing at matching positions mod 6 in both a beacon and the reply.

Step 6 — Confirm the key is raw bytes, not ASCII

Goal: Show that a dictionary of ASCII keys won’t work.

Solving each key column independently by requiring printable output leaves columns with high-bit-set key bytes (e.g. column 1 only allows a2/a3/a5). So the key is raw bytes, not a passphrase. Letter-frequency scoring of the remaining ~20,000 combinations did not converge either, because each column only has about 6 to 8 samples.

Step 7 — Crib drag to recover the key

Goal: Recover the key from a known-plaintext guess.

With a known key length, any correct guess at the plaintext reveals the key directly:

key[(o+i) % 6] = c[o+i] ^ crib[i]

A crib of 6+ characters fixes the whole key. Dragging a wordlist of plausible C2 vocabulary (beacon, checkin, hello, status, flag{, sleep, task, ack, …) over every offset in the beacon and the reply. For each placement the derived key bytes had to:

  1. be self-consistent where the crib wraps around the key, and
  2. decrypt every other byte at those key phases, in all messages, to printable ASCII.

Most cribs produced junk, but CHECKIN at offset 0 gave:

key = 37 a2 54 e3 c7 02

and the decrypted text was fully legible in all five payloads — far beyond coincidence. The tell-tale of a correct crib is that it produces readable text outside the crib region.

Step 8 — Decrypt the session

Goal: Decrypt all five payloads and read the flag.

import pickle
rows = pickle.load(open("rows.pkl", "rb"))
key = bytes.fromhex("37a254e3c702")
for r in rows:
    if r[1] == "tcp" and 4444 in (r[4], r[5]) and r[6]:
        pt = bytes(b ^ key[i % 6] for i, b in enumerate(r[6]))
        print(r[2], "->", r[3], pt.decode())
10.60.4.112 -> 91.242.87.19  CHECKIN:host-7734:1770000000:idle
10.60.4.112 -> 91.242.87.19  CHECKIN:host-7734:1770000060:idle
10.60.4.112 -> 91.242.87.19  CHECKIN:host-7734:1770000120:idle
10.60.4.112 -> 91.242.87.19  CHECKIN:host-7734:1770000180:active
91.242.87.19 -> 10.60.4.112  TASK:ack:r00t{REDACTED}
Field Value Meaning
Verb CHECKIN Heartbeat to the C2
Bot ID host-7734 Implant identifier
Timestamp 1770000000, +60, +120, +180 Unix epoch seconds, 60 s interval
State idle x3, then active Bot changes state on the 4th check-in
Reply TASK:ack:<data> C2 acknowledges and issues a task

The unix timestamp 1770000000 is 2026-02-02 02:40:00 UTC. The 35-byte fourth message is longer only because active is two characters longer than idle.

Flag

r00t{REDACTED}

The flag text itself is leetspeak for “xor keys hate known plaintext”, which is exactly the attack used.

Lessons / Takeaways

  • Rank by rarity. In a noisy capture the signal is the flow that is different (rare port, tiny payloads, fixed cadence), not the busiest one.
  • Rule out DNS quickly. Check unique-name count, label length and query types before assuming tunnelling.
  • Repeating-key XOR is breakable with no key. c[i]^c[i+K] leaks plaintext structure, so the key length falls out of a simple counting scan (all XORs < 0x80 for ASCII).
  • Known plaintext beats frequency analysis on short messages. With only ~7 bytes per key column, statistics were ambiguous while a single correct crib solved everything. Pick cribs from the challenge text and protocol vocabulary.
  • Validate a crib outside itself. A real hit makes other bytes readable and, with a shared key, decrypts the other direction too.
  • Reused keys and static headers are the real weakness. Four near-identical ciphertexts with the same keystream are a giveaway of a static key; a per-message nonce would have defeated this.

Mitigations

  • Never protect C2 traffic with a static repeating-key XOR; use TLS or an authenticated encryption scheme.
  • Use a per-message nonce/IV so identical plaintexts never produce identical ciphertexts.
  • Detect: any internal host talking to a single external IP on TCP/4444 with sub-50-byte payloads at a fixed interval.

Techniques

References