Every service has a front door
In lesson 2 the printer went round the proxy and asked the grades app directly. Locks on the front door do nothing while the side doors stand open.
Today each service gets its own walls.
The one rule
Warning
Everything in this module happens on the lab machine that is your own. Probing someone else's firewall needs the owner's written permission. In the UK, without it, it can break the Computer Misuse Act 1990.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can write firewall rules so each service accepts only the callers it needs.
Key words
| Word | What it means | Example |
|---|---|---|
| Microsegmentation | walls around each service, not only around the network | |
| Flow | one caller allowed to one service on one port | |
| nftables | the Linux firewall that filters packets | |
| Chain | an ordered list of firewall rules | chain input |
| Default drop | refusing whatever no rule allows | |
| Counter | a running count of packets a rule matched |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsStart from the flows the app needs
Write down who must call whom, on which port. Anything not on the list is refused.
Network segmentation puts walls between zones. Microsegmentation puts a wall around each service, so two machines on one switch still cannot talk unless a flow says so.
The flows on the campus
| From | To | Port | Why |
|---|---|---|---|
| the campus, 10.30.0.0/24 | proxy | 8000 | people reach the front door |
| proxy, 10.30.0.5 | grades | 8080 | the proxy passes requests on |
| grades, 10.30.0.10 | db | 5432 | the app reads the grades |
The cells students fill in are left blank in this preview.
Try it: accept or drop
flows = [("laptop", "proxy", 8000), ("proxy", "grades", 8080), ("grades", "db", 5432)]
tries = [("laptop", "proxy", 8000), ("printer", "grades", 8080), ("printer", "db", 5432),
("proxy", "grades", 8080)]
for src, dst, port in tries:
ok = (src, dst, port) in flows
print(src, "->", dst, port, "accept" if ok else "drop")
# Add the flow ("printer", "grades", 8080) to flows. Which line changes, and why is that bad?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsMake sure the campus is running, with posture
cd ~/zt && zt up; zt start db db python3 db.py && zt start grades grades python3 app.py && zt start proxy proxy python3 proxy.py --posture && python3 idp.py issue amyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan from the printer before any wall
zt printer python3 scan.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
One chain on each service
Each file below makes one table with an input chain. Its policy drops by default. It accepts replies to its own connections, its own loopback and the one flow from the table.
The last rule counts what it drops. The file first deletes its old table, so loading it twice gives the same rules.
Write the rules for grades
mkdir -p ~/zt && printf '%s\n' 'table inet zt' 'delete table inet zt' 'table inet zt {' ' chain input {' ' type filter hook input priority 0' ' policy drop' ' iif lo accept' ' ct state established,related accept' ' ip saddr 10.30.0.5 tcp dport 8080 accept' ' counter drop comment "refused"' ' }' '}' > ~/zt/grades.nftShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the rules for db
mkdir -p ~/zt && printf '%s\n' 'table inet zt' 'delete table inet zt' 'table inet zt {' ' chain input {' ' type filter hook input priority 0' ' policy drop' ' iif lo accept' ' ct state established,related accept' ' ip saddr 10.30.0.10 tcp dport 5432 accept' ' counter drop comment "refused"' ' }' '}' > ~/zt/db.nftShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the rules for the proxy
mkdir -p ~/zt && printf '%s\n' 'table inet zt' 'delete table inet zt' 'table inet zt {' ' chain input {' ' type filter hook input priority 0' ' policy drop' ' iif lo accept' ' ct state established,related accept' ' ip saddr 10.30.0.0/24 tcp dport 8000 accept' ' counter drop comment "refused"' ' }' '}' > ~/zt/proxy.nftShown here, not run. Press Start lab to type it into your own lab’s terminal.
Load the three and read one back
cd ~/zt && for n in grades db proxy; do sudo ip netns exec zt-$n nft -f $n.nft; done && sudo ip netns exec zt-grades nft list rulesetShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a rule that loaded is a rule that works. A rule can load and still allow too much, or block a path the app needs. Prove each one with traffic: a call that must fail and a call that must still work.
Scan again and try the side door
zt printer python3 scan.py; zt printer python3 get.py http://10.30.0.10:8080/admin/export X-User:carlaShown here, not run. Press Start lab to type it into your own lab’s terminal.
The path the app needs still works
cd ~/zt && sudo ip netns exec zt-laptop python3 devagent.py report laptop && zt laptop python3 get.py http://10.30.0.5:8000/grades token=amy.tok posture=laptop.postureShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsThe fix, and how it is seen
The fix is these rules, kept as files in version control and loaded the same way on every start. Review the flow list when an app changes.
A dropped packet is a caller trying a path it should not. Watch each chain's counter: a rising count on db means something other than grades is knocking.
Read the drop counters
for n in grades db proxy; do echo "$n: $(sudo ip netns exec zt-$n nft list chain inet zt input | grep -o 'packets [0-9]*')"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: rules from a flow list
Turn a flow list into firewall rules.
allowed(flows, src, dst, port) is True only when one flow matches all three. rules_for(host, flows) returns the sorted nftables accept lines, ip saddr <from> tcp dport <port> accept, for every flow ending at host. unused(flows, seen) returns the flows never seen in traffic, in their order.
def allowed(flows, src, dst, port):
# flows: [{"from": "10.30.0.5", "to": "10.30.0.10", "port": 8080}, ...]
# True only when one flow matches all three.
return True
def rules_for(host, flows):
# The nftables accept lines for host, sorted, one a flow that ends at host:
# "ip saddr 10.30.0.5 tcp dport 8080 accept"
return []
def unused(flows, seen):
# seen: the (from, to, port) tuples that really happened in a week of traffic.
# Return the flows never seen, in their order: candidates to remove.
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go