The attacker takes the easy road
Someone wants to see what another student orders for lunch. They will not break strong encryption to do it. They will try the cheapest way that works.
If you lock the front door, they try the window. An attack tree lists every road to one goal, so you can see which road is cheapest.
By the end of this lesson
Note
I can build an attack tree and find the cheapest attack in it.
Key words
| Word | What it means |
|---|---|
| Attack tree | a diagram of every way to reach one attack goal |
| Goal | what the attacker wants, at the top of the tree |
| Leaf | one step the attacker can take on its own |
| OR node | reached when any one child is done |
| AND node | reached only when every child is done |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsPlan on paper, test only your own
Warning
An attack tree is a plan, and this lesson attacks nothing. Testing any step on a real system needs the owner's written permission. In the UK, the Computer Misuse Act 1990 makes access without permission a crime. Your own lab copy is the only place to try things.
OR gives choices, AND adds steps
The goal sits at the top. Below it, an OR node means any one child is enough. An AND node means the attacker needs every child.
Each leaf gets a cost: effort, money or skill. An OR costs as much as its cheapest child. An AND costs the sum of its children. The cheapest road to the goal is the one to defend first.
An attack tree for the canteen app
flowchart TB
G["OR: Read the orders of another student"] --> S["OR: Sign in as them"]
G --> N["Change the order number in the address: 5"]
G --> B["AND: Read a database backup"]
S --> P["Phish their password: 30"]
S --> W["Guess a weak password: 10"]
B --> F["Find the backup file: 40"]
B --> E["Break its encryption: 500"]Check
Have a go
Check
3 questionsTry it: cost an OR and an AND
# An AND needs every child. An OR needs only one.
phish = 30
guess = 10
find_backup = 40
break_encryption = 500
sign_in = min(phish, guess) # OR
backup = find_backup + break_encryption # AND
print("sign in costs", sign_in)
print("backup costs", backup)
print("cheapest goal", min(sign_in, backup))
# Multi-factor sign-in adds 200 to phish and guess. Change them and run again.
Make a free account to save your work and track your progress.
A defence moves the attacker
The lab script holds the tree as nested lists and works out every cost. Then it tries two defences. An ownership check blocks the order number trick. Multi-factor sign-in adds 200 to both password leaves.
Watch the cheapest attack after each defence. It never disappears; it moves to the next road.
Write the attack tree script, part 1
docker exec jupyter sh -c "mkdir -p steps && printf '%s\n' 'import sys' 'tree = [\"or\", \"Read the orders of another student\", [' ' [\"or\", \"Sign in as them\", [' ' [\"leaf\", \"Phish their password\", 30],' ' [\"leaf\", \"Guess a weak password\", 10],' ' ]],' ' [\"leaf\", \"Change the order number in the address\", 5],' ' [\"and\", \"Read a database backup\", [' ' [\"leaf\", \"Find the backup file\", 40],' ' [\"leaf\", \"Break its encryption\", 500],' ' ]],' ']]' 'def cost(node):' ' if node[0] == \"leaf\":' ' if \"check\" in sys.argv and \"order number\" in node[1]:' ' return None' ' if \"mfa\" in sys.argv and \"password\" in node[1]:' ' return node[2] + 200' ' return node[2]' ' costs = []' ' for child in node[2]:' ' costs.append(cost(child))' ' if node[0] == \"and\":' ' if None in costs:' ' return None' ' return sum(costs)' > steps/tree.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the attack tree script, part 2
docker exec jupyter sh -c "printf '%s\n' ' open_costs = [c for c in costs if c is not None]' ' if len(open_costs) == 0:' ' return None' ' return min(open_costs)' 'def path(node):' ' if node[0] == \"leaf\":' ' return [node[1]]' ' if node[0] == \"and\":' ' out = []' ' for child in node[2]:' ' out = out + path(child)' ' return out' ' best = None' ' for child in node[2]:' ' c = cost(child)' ' if c is not None and (best is None or c < cost(best)):' ' best = child' ' return path(best)' 'def show(node, depth):' ' c = cost(node)' ' label = \"blocked\" if c is None else \"cost \" + str(c)' ' gate = \"\" if node[0] == \"leaf\" else node[0].upper() + \" \"' ' print(\" \" * depth + gate + node[1] + \" \" + label)' ' if not node[0] == \"leaf\":' ' for child in node[2]:' ' show(child, depth + 1)' 'show(tree, 0)' >> steps/tree.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the attack tree script, part 3
docker exec jupyter sh -c "printf '%s\n' 'print(\"cheapest attack:\", \" + \".join(path(tree)))' >> steps/tree.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Find the cheapest attack
docker exec jupyter python3 steps/tree.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Add the ownership check
docker exec jupyter python3 steps/tree.py checkShown here, not run. Press Start lab to type it into your own lab’s terminal.
Add multi-factor sign-in too
docker exec jupyter python3 steps/tree.py check mfaShown here, not run. Press Start lab to type it into your own lab’s terminal.
What tree.py printed
| Defences | Cheapest cost | Cheapest attack |
|---|---|---|
| None | 5 | Change the order number in the address |
| Ownership check | 10 | Guess a weak password |
| Ownership check and multi-factor | 210 | Guess a weak password |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think the most frightening leaf is the one to fix first. Breaking the encryption sounds worst, but it costs 500. Fix the cheapest road first, because that is the one an attacker will take.
Fix it, then watch for it
The fixes are an ownership check on every order the app returns and multi-factor sign-in. Detection matters too. Log every order request refused by the check, and alert on many failed sign-ins for one account. The tree tells you which logs are worth reading.
Check
Have a go
Check
3 questionsAssignment: cost an attack tree
Write the three functions that read an attack tree. Each one calls itself on the children.
A node is ["leaf", name, cost], ["or", name, children] or ["and", name, children]. cheapest(node) returns the cheapest cost: a leaf its cost, an OR its cheapest child, an AND the sum. leaves(node) returns every leaf name, left to right. possible(node, can) says whether an attacker who can do the leaves in can reaches the node.
# A node is ["leaf", name, cost], ["or", name, children] or ["and", name, children].
def cheapest(node):
# A leaf costs its cost, an OR its cheapest child, an AND the sum of its children.
return 0
def leaves(node):
# The names of every leaf under the node, left to right.
return []
def possible(node, can):
# can is a list of leaf names the attacker can do. Can they reach this node?
return False
Make a free account to save your work and track your progress.
Exit quiz
Have a go