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
| Word | What it means |
|---|---|
| Analytic | a query that runs on a schedule and reports matches |
| Known list | checked regular activity an analytic skips |
| Data request | asking for logs a hunt found missing |
| Hunting loop | hypothesis, hunt, finding, detection, new hypothesis |
| Hunt metric | a number that shows what hunting achieved |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThe 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
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 Sigma rule
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.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the rule on the events
det sigma hunt/encoded.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAn 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
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 the Tuesday hour
python3 hunt/tuesday.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Turn it 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.
Write the known list
printf '%s\n' '10.20.1.30 192.0.2.30 nightly backup, owner IT, change CHG-2211' > hunt/known.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the analytic, 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' '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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the analytic, part 2
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the analytic on the Tuesday hour
python3 hunt/analytic.py hunt/tue/conn.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
Turn the known-good capture into logs
mkdir -p hunt/good && cd hunt/good && zeek-lite -r ../../pcaps/known-good.pcap && cd ~/detectShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the analytic on known-good
python3 hunt/analytic.py hunt/good/conn.logShown 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?
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.
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