Who asked the computer who it is?
whoami ran on a PC. On its own, that is a helpdesk habit. Started by a hidden PowerShell, which Word started, which Outlook opened from an email, it tells a different story.
One event is a word. The chain of who started whom is a sentence. You read those chains in the lab's made-up office, in your own lab copy.
By the end of this lesson
Note
I can build a process tree to see which program started which.
Key words
| Word | What it means | Example |
|---|---|---|
| Process | a program while it runs | |
| Parent process | the process that started another | |
| Process tree | processes drawn under the ones that started them | |
| Process ID | a number for a running process, reused later | 4120 |
| Process GUID | an id an agent gives each process, never reused |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThe one rule
Important
Read process records only from your own lab copy. A real PC's process history shows what a person did, so reading it needs written permission. Unauthorised access to data is an offence under the Computer Misuse Act 1990. The office and its events are made up.
Stack parent and child pairs first
Every process creation event names a process and its parent process. Stacking the pairs, as in lesson 3, shows which starts are common and which are rare.
services.exe starting svchost.exe is routine. Word starting PowerShell is not. The script keeps only each program's name, not its folder.
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 pair stacker
printf '%s\n' 'import sqlite3' 'db = sqlite3.connect("logs/events.db")' 'count = {}' 'for parent, image in db.execute("SELECT ParentImage, Image FROM logs"):' ' pair = parent.split(chr(92))[-1] + " > " + image.split(chr(92))[-1]' ' count[pair] = count.get(pair, 0) + 1' 'for pair in sorted(count, key=lambda p: (count[p], p)):' ' print(count[pair], pair)' > hunt/pairs.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Stack the parent and child pairs
python3 hunt/pairs.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsIds join the events into a tree
The lab's table names a parent by its program, so two Words cannot be told apart. An endpoint agent such as Sysmon records ids as well: a process ID for the process and its parent, and a process GUID for each.
The Runs write that fuller record for PC-15 and PC-12, then draw PC-15 as a process tree, linked by GUID.
Write the fuller process record, part 1
printf '%s\n' 'import sqlite3' 'B = chr(92)' 'C, W, O = "C:" + B + "Windows" + B, "C:" + B + "Windows" + B + "System32" + B, "C:" + B + "Office16" + B' 'ROWS = [' ' ["09:02:11", "PC-15", "{15-01}", 3010, "{15-00}", 640, C + "explorer.exe", "explorer.exe"],' ' ["09:05:40", "PC-15", "{15-02}", 5120, "{15-01}", 3010, O + "OUTLOOK.EXE", "OUTLOOK.EXE"],' ' ["14:58:02", "PC-15", "{15-03}", 6644, "{15-02}", 5120, O + "WINWORD.EXE", "WINWORD.EXE invoice-0412.docx"],' ' ["15:00:00", "PC-15", "{15-04}", 4120, "{15-03}", 6644, W + "powershell.exe", "powershell.exe -nop -w hidden -enc SQBFAFgA"],' ' ["15:10:00", "PC-15", "{15-05}", 7012, "{15-04}", 4120, W + "whoami.exe", "whoami /all"],' ' ["15:10:30", "PC-15", "{15-06}", 7020, "{15-04}", 4120, W + "net.exe", "net user /domain"],' ' ["15:11:05", "PC-15", "{15-07}", 7100, "{15-04}", 4120, W + "tasklist.exe", "tasklist /v"],' > hunt/procs.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the fuller process record, part 2
printf '%s\n' ' ["15:40:00", "PC-15", "{15-08}", 4120, "{15-01}", 3010, W + "notepad.exe", "notepad.exe todo.txt"],' ' ["09:15:20", "PC-12", "{12-01}", 2900, "{12-00}", 610, C + "explorer.exe", "explorer.exe"],' ' ["15:29:50", "PC-12", "{12-02}", 5300, "{12-01}", 2900, O + "EXCEL.EXE", "EXCEL.EXE prices.xlsx"],' ' ["15:30:00", "PC-12", "{12-03}", 6100, "{12-02}", 5300, W + "powershell.exe", "powershell.exe -File refresh-prices.ps1"],' ']' 'db = sqlite3.connect("hunt/procs.db")' 'db.execute("DROP TABLE IF EXISTS procs")' 'db.execute("CREATE TABLE procs (UtcTime, Computer, ProcessGuid, ProcessId, ParentProcessGuid, ParentProcessId, Image, CommandLine)")' 'for row in ROWS:' ' db.execute("INSERT INTO procs VALUES (?, ?, ?, ?, ?, ?, ?, ?)", ["2026-11-02 " + row[0]] + row[1:])' 'db.commit()' 'print(len(ROWS), "processes in", "hunt/procs.db")' >> hunt/procs.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the process record
python3 hunt/procs.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the tree drawer
printf '%s\n' 'import sqlite3, sys' 'host = sys.argv[1]' 'by = sys.argv[2] if len(sys.argv) > 2 else "guid"' 'rows = sqlite3.connect("hunt/procs.db").execute(' ' "SELECT ProcessGuid, ProcessId, ParentProcessGuid, ParentProcessId, CommandLine, UtcTime FROM procs WHERE Computer = ? ORDER BY UtcTime", [host]).fetchall()' 'me = {}' 'for row in rows:' ' me[row[0] if by == "guid" else row[1]] = row' 'kids = {}' 'for row in rows:' ' parent = row[2] if by == "guid" else row[3]' ' kids.setdefault(parent if parent in me else None, []).append(row)' 'def show(row, depth):' ' print(" " * depth + row[5][11:] + " " + row[4])' ' for kid in kids.get(row[0] if by == "guid" else row[1], []):' ' if not kid == row:' ' show(kid, depth + 1)' 'for root in kids.get(None, []):' ' show(root, 0)' > hunt/tree.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Draw PC-15's tree
python3 hunt/tree.py PC-15Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA process ID is reused
Windows hands a process ID back once its process ends. On PC-15, Notepad got 4120 at 15:40, PowerShell's old number.
Link by ID alone and PowerShell's three children also hang under Notepad, which started half an hour after them. Link by GUID, or by ID and start time together.
Draw PC-15's tree by process ID
python3 hunt/tree.py PC-15 pidShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a process ID names one process for ever. It names one only while that process runs, and Windows reuses it soon after. Join events by a GUID, or by the ID and a time window, or the tree tells a false story.
Draw PC-12's tree
python3 hunt/tree.py PC-12Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: name each process's parent
procs = [
["{1}", None, "explorer.exe"],
["{2}", "{1}", "WINWORD.EXE"],
["{3}", "{2}", "powershell.exe"],
["{4}", "{3}", "whoami.exe"],
]
name = {guid: image for guid, parent, image in procs}
for guid, parent, image in procs:
print(name.get(parent, "-"), ">", image)
# Add ["{5}", "{3}", "net.exe"] to the list, then press Run.
Make a free account to save your work and track your progress.
Assignment: build a process tree
A process is a dictionary with "guid", "parent" (a GUID, or None) and "image", in the order they started.
children(procs) returns a dictionary from each GUID that started something to the list of its children's GUIDs, in order.
ancestry(procs, guid) returns the images from the oldest known ancestor down to that process, as a list. A parent that is not in the list ends the chain.
suspicious(procs, parents, children_images) returns the GUIDs, in order, of processes whose image is in children_images and whose parent's image is in parents.
def children(procs):
return {}
def ancestry(procs, guid):
return []
def suspicious(procs, parents, children_images):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go