Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Hunting beacons that hide their rhythm

After this lesson: I can score connections by how regular they are to find a beacon with jitter.

65 min lesson11 commandsDetection engineering lab: Suricata rules, Sigma rules and Zeek-style logs on one private Ubuntu VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it means
Beacona program calling home on a schedule
Jitterrandom change added to a beacon's wait
Medianthe middle value when sorted
Coefficient of variationthe standard deviation divided by the mean
Known servicea 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The 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

Command
cd ~/detect

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make sure the hunt folder exists

Command
mkdir -p hunt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the Tuesday capture maker

Command
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.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make an hour of Tuesday traffic

Command
python3 hunt/tuesday.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Turn the hour into logs in hunt/tue

Command
mkdir -p hunt/tue && cd hunt/tue && zeek-lite -r ../tuesday.pcap && cd ~/detect

Shown 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

Command
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.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the beacon scorer, part 2

Command
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.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Score every pair in the hour

Command
python3 hunt/beacons.py hunt/tue/conn.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
zeek-cut id.resp_h user_agent < hunt/tue/http.log | sort | uniq -c | sort -rn | head -n 3

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Turn the office capture into logs

Command
zeek-lite -r pcaps/office.pcap

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Score the office capture

Command
python3 hunt/beacons.py conn.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: range against spread

Code: python
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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.