soc-home-lab

09 — pcap-forensics

Problem: SOC analysts are handed packet captures and expected to find the attack in them, but most labs start with “open Wireshark and look around” — which teaches clicking, not reading packets. This lab starts one level lower: parse the pcap yourself, then hunt in what you parsed.

What it is: A from-scratch packet parser and hunt script (stdlib Python only, no tshark, no scapy, no dpkt), two synthetic pcaps, and a pytest suite that pins the exact finding set.

The two captures

Both are synthetic, generated by generate_pcap.py (deterministic — re-running it reproduces both files byte-for-byte). All addresses are documentation ranges (RFC 5737); all domains use the reserved .example TLD. IP/TCP/UDP checksums are zeroed: real stacks fill them in, and this parser does not verify them.

File Packets What’s in it
samples/malicious.pcap 29 Four planted behaviours + ordinary background traffic (normal DNS, a normal HTTP fetch)
samples/benign.pcap 9 The same protocols doing ordinary things — nothing should fire

The four detectors (hunt_pcap.py, 233 lines)

Detector Behaviour Why it fires here
SYN_SCAN One source sends SYN-only packets to ≥ 10 distinct destination ports 203.0.113.60 scans 12 ports on 192.0.2.10; a normal client opens one or two
DNS_EXFIL A DNS query label ≥ 40 chars, or a full name ≥ 63 chars Three queries with 40-char labels to exfil-test.example
SUSPICIOUS_HTTP HTTP from a scripting user agent (curl, wget, python, Go-http-client) or for a sensitive path (/admin/, /etc/passwd, cmd=) curl/8.0.1 fetching /admin/config.zip
BEACON ≥ 5 small (1–100 byte) PSH packets on one flow at near-regular intervals 6 packets from 192.0.2.50 to 203.0.113.60:4444, exactly 30 s apart

Run it

python3 generate_pcap.py                 # regenerate both samples
python3 hunt_pcap.py samples/malicious.pcap
python3 hunt_pcap.py samples/benign.pcap  # expect: no findings
python3 -m pytest tests/ -v

Expected on the malicious pcap: 6 findings (1 SYN_SCAN, 3 DNS_EXFIL, 1 SUSPICIOUS_HTTP, 1 BEACON), TP=6, FP=0 on the synthetic data. Open the flagged packet numbers in Wireshark and confirm each one by hand — the finding names the packet number for exactly that reason.

What this parser does not do

Exercises

  1. Run the hunt, then open malicious.pcap in Wireshark and find each flagged packet by number. Write one sentence per finding explaining why it is suspicious in context (not just “the script said so”).
  2. Add a fifth detector of your own: a DNS query for a .example domain that returns no answer is normal here — pick a behaviour that is actually in the capture (for example, the same source opening SYNs to one port repeatedly) and decide whether it deserves to fire. If it does not, write down why.
  3. Break the parser on purpose: truncate malicious.pcap mid-packet and confirm the script stops cleanly instead of crashing (the test suite already checks this — read the test first, then do it by hand).

Skills: Packet parsing, protocol decoding, detection engineering, evidence handling (packet numbers as citations).