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

Free lesson

Reading process trees to see what ran

After this lesson: I can build a process tree to see which program started which.

60 min lesson11 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

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

WordWhat it meansExample
Processa program while it runs
Parent processthe process that started another
Process treeprocesses drawn under the ones that started them
Process IDa number for a running process, reused later4120
Process GUIDan 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 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

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

Command
cd ~/detect

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

Make sure the hunt folder exists

Command
mkdir -p hunt

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

Write the pair stacker

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

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

Stack the parent and child pairs

Command
python3 hunt/pairs.py

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

Ids 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

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

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

Write the fuller process record, part 2

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

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

Make the process record

Command
python3 hunt/procs.py

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

Write the tree drawer

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

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

Draw PC-15's tree

Command
python3 hunt/tree.py PC-15

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

Command
python3 hunt/tree.py PC-15 pid

Shown 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

Command
python3 hunt/tree.py PC-12

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

Try it: name each process's parent

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

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

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.