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

Free lesson

Planning attacks with attack trees

After this lesson: I can build an attack tree and find the cheapest attack in it.

60 min lesson6 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it means
Attack treea diagram of every way to reach one attack goal
Goalwhat the attacker wants, at the top of the tree
Leafone step the attacker can take on its own
OR nodereached when any one child is done
AND nodereached only when every child is done

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

Starter

Have a go

Starter

4 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

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

Try it: cost an OR and an AND

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

Command
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

Command
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

Command
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

Command
docker exec jupyter python3 steps/tree.py

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

Add the ownership check

Command
docker exec jupyter python3 steps/tree.py check

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

Add multi-factor sign-in too

Command
docker exec jupyter python3 steps/tree.py check mfa

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

What tree.py printed

DefencesCheapest costCheapest attack
None5Change the order number in the address
Ownership check10Guess a weak password
Ownership check and multi-factor210Guess 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 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

Assignment: 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.

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

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.