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

Free lesson

Running the purple team loop

After this lesson: I can run a test, see whether a detection fired, and tune the rule until it does.

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

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

WordWhat it means
Purple team looprun a test, observe, tune, retest
Detectiona rule that turns telemetry into an alert
False alarman alert on normal work
Tuningchanging a rule so it fits what is normal
Retestrunning a test again after a change

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

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

Command
cd ~/detect && mkdir -p emu

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

Write the scorer, part 1

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

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

Write the scorer, part 2

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

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

Run the tests and the admin job afresh

Command
rm -f emu/events.db && python3 emu/run.py all G1 > /dev/null

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

Score them against the lab's own Sigma rules

Command
python3 emu/score.py sigma

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

Write 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

Command
mkdir -p emu/rules

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

Write the discovery rule

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

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

Write the cron rule

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

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

Score the tests against your rules

Command
python3 emu/score.py emu/rules

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

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

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

Write the staging rule

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

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

Retest from fresh and score again

Command
rm -f emu/events.db && python3 emu/run.py all G1 > /dev/null && python3 emu/score.py emu/rules

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

Remove A3's cron job again

Command
python3 emu/run.py clean

Shown 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.

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

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.