Same room, same afternoon
In the old way, a red team tested in March and sent a report in May. By then the defenders had changed half their rules and nobody could repeat the tests.
A purple team sits together instead. One person runs a test, another checks the alerts, and the rule is fixed before tea. You run that loop on your own lab copy.
By the end of this lesson
Note
I can run a test, see whether a detection fired, and tune the rule until it does.
Key words
| Word | What it means |
|---|---|
| Purple team loop | run a test, observe, tune, retest |
| Detection | a rule that turns telemetry into an alert |
| False alarm | an alert on normal work |
| Tuning | changing a rule so it fits what is normal |
| Retest | running a test again after a change |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThe one rule
Important
Run tests only in your own lab copy. On a real network, the owner's written permission comes first, and the defenders are told when you start and stop. Unauthorised access is an offence under the Computer Misuse Act 1990. These tests are harmless.
Run, observe, tune, retest
Each test ends in one of three places: none, where the sensor saw nothing; telemetry, where it saw but nothing alerted; or alert. G1, an admin's nightly job, should stay quiet.
The scorer turns every Sigma rule in a folder into SQL with the real sigma-cli and runs it on the tests' events. It needs emu/run.py from lesson 2.
Move into the lab's folder and make emu
cd ~/detect && mkdir -p emuShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the scorer, part 1
printf '%s\n' 'import glob, sqlite3, subprocess, sys' 'from run import TESTS' 'db = sqlite3.connect("emu/events.db")' 'fired = {}' 'for name in sorted(glob.glob(sys.argv[1] + "/*.yml")):' ' out = subprocess.run(["sigma", "convert", "-t", "sqlite", name], capture_output=True, text=True).stdout' ' query = [line for line in out.split(chr(10)) if line.startswith("SELECT")][0]' ' for (test,) in db.execute("SELECT TestId FROM (" + query + ")"):' ' fired.setdefault(test, []).append(name.split("/")[-1][:-4])' 'events = dict(db.execute("SELECT TestId, COUNT(*) FROM logs GROUP BY TestId"))' 'out = open("emu/results.csv", "w")' 'out.write("test,technique,events,alerts,outcome" + chr(10))' 'print("test technique events alerts outcome rules")' 'for test, (technique, name, steps, cleanup) in TESTS.items():' ' n, hits = events.get(test, 0), fired.get(test, [])' ' outcome = "alert" if hits else "telemetry" if n else "none"' ' if technique == "-":' > emu/score.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the scorer, part 2
printf '%s\n' ' outcome = "false-alarm" if hits else "quiet"' ' out.write(",".join([test, technique, str(n), str(len(hits)), outcome]) + chr(10))' ' print(test.ljust(4), technique.ljust(10), str(n).ljust(6), str(len(hits)).ljust(6), outcome.ljust(11), " ".join(sorted(set(hits))) or "-")' >> emu/score.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the tests and the admin job afresh
rm -f emu/events.db && python3 emu/run.py all G1 > /dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score them against the lab's own Sigma rules
python3 emu/score.py sigmaShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsWrite rules for this platform
The lab's rules were written for the office's Windows PCs, so this Linux server had no detection at all. Coverage on one platform says nothing about another.
Write two Linux rules: one for discovery programs and one for crontab. Then score the same events again.
Make a folder for the Linux rules
mkdir -p emu/rulesShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the discovery rule
printf '%s\n' 'title: Discovery commands on a Linux server' 'id: 6d1c2e10-4b7a-4f3e-9a52-1c0b7e3d8a01' 'logsource:' ' category: process_creation' ' product: linux' 'detection:' ' selection:' ' Image|endswith:' ' - /whoami' ' - /id' ' - /uname' ' - /hostname' ' condition: selection' 'level: low' 'tags:' ' - attack.discovery' ' - attack.t1033' ' - attack.t1082' > emu/rules/discovery.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the cron rule
printf '%s\n' 'title: A crontab installed from a file' 'id: 0f4a8b62-2d19-4c77-8e3b-5a6d9c1f2b03' 'logsource:' ' category: process_creation' ' product: linux' 'detection:' ' selection:' ' Image|endswith: /crontab' ' condition: selection' 'level: medium' 'tags:' ' - attack.persistence' ' - attack.t1053.003' > emu/rules/cron.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score the tests against your rules
python3 emu/score.py emu/rulesShown 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 more alerts mean better detection. An alert on every admin's nightly job trains analysts to ignore that rule. A rule is ready only when it fires on the test and stays quiet on known-good work, so test both every time.
Tune, add, then retest from fresh
Tuning: listing a crontab changes nothing, so the cron rule now ignores a command line ending in -l. A4 left a hidden folder made with mkdir, so a new rule looks for that.
Then retest: run the tests again from an empty record, so the result comes from fresh events.
Tune the cron rule
printf '%s\n' 'title: A crontab installed from a file' 'id: 0f4a8b62-2d19-4c77-8e3b-5a6d9c1f2b03' 'logsource:' ' category: process_creation' ' product: linux' 'detection:' ' selection:' ' Image|endswith: /crontab' ' listing:' ' CommandLine|endswith: " -l"' ' condition: selection and not listing' 'level: medium' 'tags:' ' - attack.persistence' ' - attack.t1053.003' > emu/rules/cron.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the staging rule
printf '%s\n' 'title: A hidden folder made for staging' 'id: 9b2e5c47-81d3-4a0f-b6e2-3f7a1d9c0e44' 'logsource:' ' category: process_creation' ' product: linux' 'detection:' ' selection:' ' Image|endswith: /mkdir' ' CommandLine|contains: /.' ' condition: selection' 'level: medium' 'tags:' ' - attack.collection' ' - attack.t1074.001' > emu/rules/staging.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Retest from fresh and score again
rm -f emu/events.db && python3 emu/run.py all G1 > /dev/null && python3 emu/score.py emu/rulesShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsRemove A3's cron job again
python3 emu/run.py cleanShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: track the loop's results
outcome(events, alerts) returns "none" when there are no events, "alert" when there is at least one alert, and "telemetry" otherwise.
false_alarms(results) takes a dictionary from a test id to (technique, alerts). Known-good tests have the technique "-". Return the sorted ids of known-good tests with at least one alert.
closed(before, after) takes two dictionaries from a test id to its outcome. Return the sorted ids whose outcome was not "alert" before and is "alert" after.
def outcome(events, alerts):
return "none"
def false_alarms(results):
return []
def closed(before, after):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go