Where would you break in?
Your college canteen launches an app. Students order lunch, pay by card and collect it at noon. The kitchen sees each order on a screen.
Before anyone tests the app, someone has to ask where it could be attacked. You cannot answer that until you can see the whole system on one page.
By the end of this lesson
Note
I can draw a system as a data flow diagram with its trust boundaries.
Key words
| Word | What it means |
|---|---|
| Threat model | a written record of what could go wrong and what you will do |
| Data flow diagram | a picture of how data moves through a system |
| External entity | a person or system outside your control |
| Process | code that takes data in and does something with it |
| Data store | a place where data rests, such as a database |
| Trust boundary | a line where the level of trust changes |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsFour questions make a threat model
A threat model answers four questions. What are we working on? What can go wrong? What are we going to do about it? Did we do a good job?
This lesson answers the first one. Lessons 2 and 3 find what can go wrong. Lessons 4 and 5 decide what to do. Lesson 6 keeps the answers up to date.
The five parts of a data flow diagram
| Part | Drawn as | In the canteen app |
|---|---|---|
| External entity | A box | Student, Card payments, Admin |
| Process | A circle or rounded box | Web app, Kitchen screen |
| Data store | Two parallel lines | Orders database |
| Data flow | An arrow with a label | save order, payment result |
| Trust boundary | A dashed line | Between the internet and the college |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsThe canteen app as a data flow diagram
flowchart LR
S["Student"] -->|sign in and order| W(("Web app"))
W -->|order status| S
W -->|save order| D[("Orders database")]
D -->|orders to cook| K(("Kitchen screen"))
W -->|payment request| C["Card payments"]
C -->|payment result| W
A["Admin"] -->|edit menu| DTrust boundaries show where attacks start
A trust boundary is where data moves between places you trust differently. The internet and the college network are two zones. So are the card company and the staff office.
Any flow that crosses a boundary needs a question: who sent this, and can I believe it? Most threats you find later sit on those crossings.
Try it: which flows cross a boundary?
# Each element: name, kind, zone. Each flow: label, from, to.
zone = {"Student": "internet", "Web app": "college", "Orders database": "college"}
flows = [
["sign in", "Student", "Web app"],
["save order", "Web app", "Orders database"],
]
for f in flows:
if zone[f[1]] != zone[f[2]]:
print(f[0], "crosses a trust boundary")
else:
print(f[0], "stays inside", zone[f[1]])
# Add a flow ["payment", "Web app", "Card payments"] and a zone for Card payments.
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsThe model is data you can check
In the lab the canteen app is written as two lists. Each element has a name, a kind and a zone. Each flow has a label, a start and an end.
The script counts each kind, marks every crossing and checks one rule. Data must pass through a process. A flow from a person straight into a data store means some code is missing from the drawing.
Write the diagram script, part 1
docker exec jupyter sh -c "mkdir -p steps && printf '%s\n' 'import sys' 'elements = [' ' [\"Student\", \"external entity\", \"internet\"],' ' [\"Web app\", \"process\", \"college\"],' ' [\"Orders database\", \"data store\", \"college\"],' ' [\"Kitchen screen\", \"process\", \"college\"],' ' [\"Card payments\", \"external entity\", \"payments\"],' ' [\"Admin\", \"external entity\", \"staff\"],' ']' 'flows = [' ' [\"sign in and order\", \"Student\", \"Web app\"],' ' [\"order status\", \"Web app\", \"Student\"],' ' [\"save order\", \"Web app\", \"Orders database\"],' ' [\"orders to cook\", \"Orders database\", \"Kitchen screen\"],' ' [\"payment request\", \"Web app\", \"Card payments\"],' ' [\"payment result\", \"Card payments\", \"Web app\"],' ' [\"edit menu\", \"Admin\", \"Orders database\"],' ']' 'if \"fixed\" in sys.argv:' ' elements.append([\"Admin page\", \"process\", \"college\"])' > steps/dfd.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the diagram script, part 2
docker exec jupyter sh -c "printf '%s\n' ' flows[6] = [\"edit menu\", \"Admin\", \"Admin page\"]' ' flows.append([\"menu changes\", \"Admin page\", \"Orders database\"])' 'kind = {}' 'zone = {}' 'for e in elements:' ' kind[e[0]] = e[1]' ' zone[e[0]] = e[2]' 'for k in [\"external entity\", \"process\", \"data store\"]:' ' n = 0' ' for e in elements:' ' if e[1] == k:' ' n = n + 1' ' print(k, n)' 'print(\"data flows\", len(flows))' 'crossing = 0' 'for f in flows:' ' note = \"\"' ' if not zone[f[1]] == zone[f[2]]:' ' note = \" CROSSES \" + zone[f[1]] + \" | \" + zone[f[2]]' ' crossing = crossing + 1' ' print(f[0] + \":\", f[1], \"to\", f[2] + note)' 'print(\"crossing a trust boundary:\", crossing)' 'bad = 0' 'for f in flows:' ' if not kind[f[1]] == \"process\" and not kind[f[2]] == \"process\":' ' print(\"CHECK:\", f[0], \"joins\", f[1], \"to\", f[2], \"with no process between\")' >> steps/dfd.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the diagram script, part 3
docker exec jupyter sh -c "printf '%s\n' ' bad = bad + 1' 'if bad == 0:' ' print(\"every flow passes through a process\")' >> steps/dfd.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the canteen diagram
docker exec jupyter python3 steps/dfd.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
What dfd.py printed
| Line | Meaning |
|---|---|
| external entity 3, process 2, data store 1 | Six elements |
| data flows 7 | Seven arrows |
| crossing a trust boundary: 5 | Five flows to question first |
| CHECK: edit menu joins Admin to Orders database | A flow with no process between |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a data flow diagram should show every class and function. It shows where data goes and where trust changes. Draw a process for each part that handles data, and stop when the boundaries are clear.
Add the missing Admin page
docker exec jupyter python3 steps/dfd.py fixedShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: check a diagram
Write the checks the lab script made, for any diagram.
An element is [name, kind, zone] and a flow is [label, from, to]. zone_of(elements, name) returns the zone of the named element. crossing(elements, flows) returns the labels of flows whose ends are in different zones, in order. no_process(elements, flows) returns the labels of flows where neither end is a process.
def zone_of(elements, name):
# elements is a list of [name, kind, zone]. Return the zone of the named one.
return ""
def crossing(elements, flows):
# flows is a list of [label, from, to]. Return the labels whose two ends are in different zones.
return []
def no_process(elements, flows):
# Return the labels of flows where neither end is a "process".
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go