A heartbeat with a stutter
The lab's implant calls home every 60 seconds, give or take one. That steady rhythm gave it away in module 105. Whoever writes the next one knows that too.
So real implants add jitter: they sleep 90 seconds, then 70, then 110. In this lesson you hunt one that does, in a made-up hour of the office, in your own lab copy.
By the end of this lesson
Note
I can score connections by how regular they are to find a beacon with jitter.
Key words
| Word | What it means |
|---|---|
| Beacon | a program calling home on a schedule |
| Jitter | random change added to a beacon's wait |
| Median | the middle value when sorted |
| Coefficient of variation | the standard deviation divided by the mean |
| Known service | a regular job you have checked and written down |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThe one rule
Important
Hunt only in your own lab copy. Never contact an address you think is an attacker's server, and never test a real network without written permission. Unauthorised access is an offence under the Computer Misuse Act 1990. The implant and its server are made up and harmless.
Jitter breaks a fixed tolerance
Module 105 called a pair a beacon when its gaps differed by a few seconds at most. Jitter defeats that: with 30 per cent jitter on 90 seconds, the gaps range from about 63 to 117.
The Runs make an hour of the office on Tuesday from the lab's own generator, with one jittery beacon hidden in it. Then they turn it into logs in a folder of its own.
Move into the lab's folder
cd ~/detectShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make sure the hunt folder exists
mkdir -p huntShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the Tuesday capture maker
printf '%s\n' 'import random, sys' 'sys.path.insert(0, "/usr/share/detectlab")' 'from labgen import PCS, Pcap, T0, http, office, ports' 'r = random.Random(20261110)' 'p, nxt = Pcap(), ports(41000)' 'start = T0 + 8 * 86400' 'office(p, r, start, 60, PCS, nxt)' 'UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"' 't = start + 20' 'while t < start + 3600:' ' http(p, t, "10.20.1.12", nxt(), "203.0.113.150", "static.cdn-cache.example", "GET", "/pixel.gif", UA)' ' t = t + 90 * r.uniform(0.7, 1.3)' 'for i in range(12):' ' http(p, start + 120 + 300 * i, "10.20.1.30", nxt(), "192.0.2.30", "backup.riverside.example", "POST", "/push", "riverside-backup/2.1", "part=1")' 'p.save("hunt/tuesday.pcap")' 'print(len(p), "packets in", "hunt/tuesday.pcap")' > hunt/tuesday.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make an hour of Tuesday traffic
python3 hunt/tuesday.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Turn the hour into logs in hunt/tue
mkdir -p hunt/tue && cd hunt/tue && zeek-lite -r ../tuesday.pcap && cd ~/detectShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score how even the gaps are
Instead of a fixed tolerance, measure the spread. The coefficient of variation is the standard deviation of the gaps divided by their mean. A clock scores 0; people browsing score near 1.
The script scores each pair with at least 10 connections as 1 minus that, so the most regular pairs come first. It also prints the median gap, the shortest and the longest.
Write the beacon scorer, part 1
printf '%s\n' 'import statistics, sys' 'def log(name):' ' fields, rows = [], []' ' for line in open(name):' ' parts = line.rstrip(chr(10)).split(chr(9))' ' if parts[0] == "#fields":' ' fields = parts[1:]' ' elif not line.startswith("#"):' ' rows.append(dict(zip(fields, parts)))' ' return rows' 'times = {}' 'for row in log(sys.argv[1]):' ' times.setdefault((row["id.orig_h"], row["id.resp_h"]), []).append(float(row["ts"]))' 'print("source destination n median min max cv score")' 'found = []' 'for pair, ts in times.items():' ' if len(ts) < 10:' ' continue' ' ts.sort()' ' gaps = [b - a for a, b in zip(ts, ts[1:])]' > hunt/beacons.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the beacon scorer, part 2
printf '%s\n' ' cv = statistics.pstdev(gaps) / statistics.mean(gaps)' ' found.append((round(max(0, 1 - cv), 2), pair, len(ts), gaps, cv))' 'for score, pair, n, gaps, cv in sorted(found, reverse=True):' ' print(pair[0], pair[1], n, round(statistics.median(gaps)), round(min(gaps)), round(max(gaps)), round(cv, 2), score)' >> hunt/beacons.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score every pair in the hour
python3 hunt/beacons.py hunt/tue/conn.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think the most regular connection is the attacker. Here the top score, a perfect 1.0, is the nightly backup posting every 300 seconds. Backups, clocks and update checks are regular by design. A score ranks what to check; context decides.
Context turns a score into a lead
Check the pairs at the top. The backup is a known service: its owner and change record are written down. The second pair is one PC fetching a pixel with a browser's User-Agent, so stacking User-Agents would never have found it.
Then score the lab's office capture too. Its implant scores 0.99. Tools such as RITA score beacons this way, with sizes and durations too.
Count requests by destination and User-Agent
zeek-cut id.resp_h user_agent < hunt/tue/http.log | sort | uniq -c | sort -rn | head -n 3Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Turn the office capture into logs
zeek-lite -r pcaps/office.pcapShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score the office capture
python3 hunt/beacons.py conn.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsTry it: range against spread
import statistics
gaps = [85, 104, 71, 96, 66, 113, 90, 78]
cv = statistics.pstdev(gaps) / statistics.mean(gaps)
print("range", max(gaps) - min(gaps), "seconds")
print("cv", round(cv, 2), "score", round(1 - cv, 2))
# Change every gap to 90, then press Run.
Make a free account to save your work and track your progress.
Assignment: score pairs for beaconing
gaps(times) returns the gaps between a list of connection times, in seconds, after sorting the times.
score(times, least) returns None when there are fewer than least times. Otherwise it returns 1 minus the coefficient of variation of the gaps (population standard deviation divided by mean), never below 0, rounded to 2 places.
rank(pairs, least, known) takes a dictionary from a pair name to its times and a list of known pair names. It returns (score, name) for every pair with a score that is not known, highest score first, then by name.
def gaps(times):
return []
def score(times, least):
return None
def rank(pairs, least, known):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go