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

Free lesson

Turning a hunt into a lasting detection

After this lesson: I can turn a hunt's finding into an automated check and test it before it runs.

65 min lesson13 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

Found once, by hand

This week's hunts found a hidden PowerShell and a beacon with jitter. Both were found by a person, by hand. If the same tricks come back next month, will anyone be looking?

A hunt pays off twice when what it found becomes something that runs by itself. You make two such checks from your own hunts, in your own lab copy.

By the end of this lesson

Note

I can turn a hunt's finding into an automated check and test it before it runs.

Key words

WordWhat it means
Analytica query that runs on a schedule and reports matches
Known listchecked regular activity an analytic skips
Data requestasking for logs a hunt found missing
Hunting loophypothesis, hunt, finding, detection, new hypothesis
Hunt metrica number that shows what hunting achieved

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

Test new checks only on your own lab copy and its captures. A real deployment needs the owner's written permission, and a bad check can flood a team with alerts. The Computer Misuse Act 1990 covers any unauthorised access. Everything here is made up.

Each finding becomes the right kind of check

A finding that one event shows precisely becomes a detection rule, such as Sigma. A finding that needs sums over many events, like a beacon score, becomes a scheduled analytic. A hunt that could not see becomes a data request, like PC-15's thin logging.

Lesson 1 found encoded PowerShell from Word. Stacking found one from Explorer too, so the rule drops the parent.

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 Sigma rule

Command
printf '%s\n' 'title: Encoded PowerShell from any parent' 'id: 3c1e9b52-6a0d-4f7e-8b21-5d4c7a9e0f13' 'status: experimental' 'description: From hunt H1 of 10 November 2026, widened to any parent' 'logsource:' '  category: process_creation' '  product: windows' 'detection:' '  selection:' '    Image|endswith: powershell.exe' '    CommandLine|contains: " -enc "' '  condition: selection' 'level: medium' 'tags:' '  - attack.execution' '  - attack.t1059.001' > hunt/encoded.yml

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

Run the rule on the events

Command
det sigma hunt/encoded.yml

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

An analytic needs a known list

The beacon hunt becomes an analytic: score every pair, report those above 0.7. Left alone it would report the backup every night. So it reads a known list, each line naming the pair, its owner and its change record.

Test it first: it should fire on the Tuesday hour and stay silent on the lab's known-good capture.

Write lesson 4's 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 the Tuesday hour

Command
python3 hunt/tuesday.py

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

Turn it 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.

Write the known list

Command
printf '%s\n' '10.20.1.30 192.0.2.30 nightly backup, owner IT, change CHG-2211' > hunt/known.txt

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

Write the analytic, 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' 'known = [" ".join(line.split()[:2]) for line in open("hunt/known.txt") if line.strip()]' 'times = {}' 'for row in log(sys.argv[1]):' '    times.setdefault(row["id.orig_h"] + " " + row["id.resp_h"], []).append(float(row["ts"]))' 'alerts = 0' 'for pair in sorted(times):' '    ts = sorted(times[pair])' '    if len(ts) < 10:' '        continue' '    gaps = [b - a for a, b in zip(ts, ts[1:])]' > hunt/analytic.py

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

Write the analytic, part 2

Command
printf '%s\n' '    score = 1 - statistics.pstdev(gaps) / statistics.mean(gaps)' '    if score < 0.7:' '        continue' '    if pair in known:' '        print("known", pair, round(score, 2))' '        continue' '    alerts = alerts + 1' '    print("ALERT beacon", pair, len(ts), "connections, score", round(score, 2))' 'print(alerts, "alerts")' >> hunt/analytic.py

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

Run the analytic on the Tuesday hour

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

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

Turn the known-good capture into logs

Command
mkdir -p hunt/good && cd hunt/good && zeek-lite -r ../../pcaps/known-good.pcap && cd ~/detect

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

Run the analytic on known-good

Command
python3 hunt/analytic.py hunt/good/conn.log

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

A common mistake

Warning

Many people think a hunt is finished once it becomes a rule. The rule covers the variant you found, and attackers change. Keep the hypothesis on the list and hunt it again with new data: that is the hunting loop.

Count what hunting achieved

A team shows its hunting works with hunt metrics: hunts run, rules and analytics made, gaps closed, and dwell time when something is found. Counting alerts alone rewards noise.

Every inconclusive hunt returns once its data request is met, and every confirmed one returns to look for the next variant.

Try it: what does a finding become?

Code: python
precise = True
needs_maths = False
visible = True

if not visible:
    print("ask for the data first")
elif needs_maths:
    print("a scheduled analytic")
elif precise:
    print("a detection rule")
else:
    print("keep it as a hunt")
# Set needs_maths = True for the beacon, then press Run.

Make a free account to save your work and track your progress.

Assignment: route findings and count hunts

route(finding) takes a dictionary with three booleans, "visible", "needs_maths" and "precise". It returns "data request" if the data could not see it, else "analytic" if it needs sums over many events, else "rule" if one event shows it precisely, else "hunt again".

A hunt is a dictionary with "name", "outcome" ("confirmed", "refuted" or "inconclusive") and "made", the number of rules and analytics it produced.

metrics(hunts) returns a dictionary with the keys "hunts", "confirmed", "refuted", "inconclusive" and "made", each a count or total.

rehunt(hunts) returns the names of the confirmed and inconclusive hunts, sorted.

Code: python
def route(finding):
    return "rule"

def metrics(hunts):
    return {}

def rehunt(hunts):
    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.