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
| Word | What it means | Example |
|---|---|---|
| Atomic test | a small test of one technique | |
| Procedure | exactly how a technique is carried out | |
| Telemetry | the records a sensor keeps | |
| Cleanup | undoing what a test changed | |
| Input argument | a value a test lets you change | #{file} |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThe 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
| Field | Value |
|---|---|
| Technique | T1053.003, Scheduled Task/Job: Cron |
| Platform | Linux |
| Commands | write emu/work/emu.cron, then crontab emu/work/emu.cron |
| What the job does | Every 10 minutes, echo emu-test into /tmp/emu-cron.txt |
| Cleanup | crontab -r |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsThe 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
cd ~/detect && mkdir -p emuShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the tests and their runner, part 1
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the tests and their runner, part 2
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the tests and their runner, part 3
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run test A1
python3 emu/run.py A1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Run tests A1 to A5
python3 emu/run.py allShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the telemetry the tests left
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 questionsA 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
crontab -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run every cleanup
python3 emu/run.py cleanShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the cron job has gone
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
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.
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