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

Free lesson

Writing small, safe atomic tests

After this lesson: I can run a small atomic test that leaves the evidence of one technique.

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

One technique, one test, one minute

A whole emulation takes a day to run. Most questions are smaller: if somebody added a cron job on this server, would we know?

That question needs one small test that does one thing, leaves its evidence and tidies up. In this lesson you write five such tests and run them on your own lab copy.

By the end of this lesson

Note

I can run a small atomic test that leaves the evidence of one technique.

Key words

WordWhat it meansExample
Atomic testa small test of one technique
Procedureexactly how a technique is carried out
Telemetrythe records a sensor keeps
Cleanupundoing what a test changed
Input argumenta value a test lets you change#{file}

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 these tests only in your own lab copy. Running them on a real machine needs written permission from its owner. Unauthorised access to a computer is an offence under the Computer Misuse Act 1990. Every test here is harmless and tidies up after itself.

An atomic test does one thing

Atomic Red Team, an open library, made this shape popular. Each atomic test names one ATT&CK technique, the commands that act it out and a cleanup that undoes them.

Its commands hold input arguments such as #{file}, so one test fits many machines. Ours are written for the lab in Python, one line a test.

Worked example: test A3, a cron job

FieldValue
TechniqueT1053.003, Scheduled Task/Job: Cron
PlatformLinux
Commandswrite emu/work/emu.cron, then crontab emu/work/emu.cron
What the job doesEvery 10 minutes, echo emu-test into /tmp/emu-cron.txt
Cleanupcrontab -r

The cells students fill in are left blank in this preview.

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

The runner is the lab's sensor

This lab server has no endpoint sensor, so the runner plays one. Each program it starts becomes one row of telemetry in emu/events.db, with the lab's own columns and the test's id.

Work done inside Python starts no program, so it leaves no row. A real sensor behaves the same way.

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 tests and their runner, part 1

Command
printf '%s\n' 'import getpass, shlex, shutil, sqlite3, subprocess, sys, time' 'TESTS = {' '    "A1": ["T1033", "System Owner/User Discovery", ["whoami", "id -un"], []],' '    "A2": ["T1082", "System Information Discovery", ["uname -sr", "cat /etc/hostname"], []],' '    "A3": ["T1053.003", "Cron", ["write emu/work/emu.cron", "crontab emu/work/emu.cron"], ["crontab -r"]],' '    "A4": ["T1074.001", "Local Data Staging", ["mkdir -p emu/work/.stage", "copy /etc/os-release emu/work/.stage/"], []],' '    "A5": ["T1070.004", "File Deletion", ["rm -rf emu/work/.stage"], []],' '    "G1": ["-", "Known good: the nightly admin job", ["uname -r", "crontab -l", "df -h /"], []],' '}' 'CRON = "*/10 * * * * echo emu-test >> /tmp/emu-cron.txt"' 'db = sqlite3.connect("emu/events.db")' 'db.execute("CREATE TABLE IF NOT EXISTS logs (TestId, EventID, UtcTime, Computer, User, ParentImage, Image, CommandLine)")' 'def step(test, line):' '    word, rest = line.split()[0], line.split()[1:]' > emu/run.py

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

Write the tests and their runner, part 2

Command
printf '%s\n' '    if word == "write":' '        open(rest[0], "w").write(CRON + chr(10))' '        return "wrote " + rest[0] + " in-process, no new process"' '    if word == "copy":' '        shutil.copy(rest[0], rest[1])' '        return "copied " + rest[0] + " in-process, no new process"' '    done = subprocess.run(shlex.split(line), capture_output=True, text=True)' '    row = [test, 1, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()), "srv-files", getpass.getuser(), sys.executable, shutil.which(word), line]' '    db.execute("INSERT INTO logs VALUES (?, ?, ?, ?, ?, ?, ?, ?)", row)' '    return line + " -> " + (done.stdout + done.stderr).strip().split(chr(10))[0][:50]' 'def clean():' '    for test, (technique, name, steps, cleanup) in TESTS.items():' '        for line in cleanup:' '            print("cleanup", test, step(test + "c", line))' 'def main(args):' '    subprocess.run(["mkdir", "-p", "emu/work"])' '    for test in args:' >> emu/run.py

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

Write the tests and their runner, part 3

Command
printf '%s\n' '        technique, name, steps, cleanup = TESTS[test]' '        print("==", test, technique, name)' '        for line in steps:' '            print("  ", step(test, line))' '    db.commit()' 'if __name__ == "__main__":' '    if sys.argv[1:] == ["clean"]:' '        clean()' '    else:' '        main([t for a in sys.argv[1:] for t in (list(TESTS)[:5] if a == "all" else [a])])' '    db.commit()' >> emu/run.py

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

Run test A1

Command
python3 emu/run.py A1

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

Run tests A1 to A5

Command
python3 emu/run.py all

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

Read the telemetry the tests left

Command
sqlite3 emu/events.db "SELECT TestId, Image, CommandLine FROM logs"

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 one technique always leaves the same evidence. A4's mkdir was a program and was recorded; its copy ran inside Python and was not. An attacker can choose either procedure. For a technique that matters, test more than one procedure.

Every test cleans up after itself

A3 left a real cron job on your server. It is harmless, but a test that leaves changes behind makes the next result harder to trust.

So check what is there, run the cleanup, and check again. In a real exercise, the rules of engagement say who confirms that.

See the cron job A3 installed

Command
crontab -l

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

Run every cleanup

Command
python3 emu/run.py clean

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

Check the cron job has gone

Command
crontab -l || echo "no jobs left"

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

Try it: fill an input argument

Code: python
command = "crontab #{file}"
inputs = {"file": "emu/work/emu.cron"}

for name, value in inputs.items():
    command = command.replace("#{" + name + "}", value)
print(command)
# Change the file to "emu/work/other.cron", then press Run.

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

Assignment: fill and check atomic tests

A test is a dictionary with "id", "platforms" (a list), "command", "inputs" (a dictionary from a name to its default), "changes" (True when it changes the machine) and "cleanup".

fill(command, inputs, given) replaces every #{name} in command with given[name] if it is there, otherwise with inputs[name]. A name in neither is left as it is.

for_platform(tests, platform) returns the sorted ids of the tests whose platforms include platform.

no_cleanup(tests) returns the sorted ids of the tests that change the machine but have an empty cleanup.

Code: python
def fill(command, inputs, given):
    return command

def for_platform(tests, platform):
    return []

def no_cleanup(tests):
    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.