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

Free lesson

Microsegmentation with host firewall rules

After this lesson: I can write firewall rules so each service accepts only the callers it needs.

65 min lesson9 commandsWireGuard lab: build a VPN tunnel between two sites in your 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

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

By the end of this lesson

Note

I can write firewall rules so each service accepts only the callers it needs.

Key words

WordWhat it meansExample
Microsegmentationwalls around each service, not only around the network
Flowone caller allowed to one service on one port
nftablesthe Linux firewall that filters packets
Chainan ordered list of firewall ruleschain input
Default droprefusing whatever no rule allows
Countera running count of packets a rule matched

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

Start 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

FromToPortWhy
the campus, 10.30.0.0/24proxy8000people reach the front door
proxy, 10.30.0.5grades8080the proxy passes requests on
grades, 10.30.0.10db5432the app reads the grades

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

Try it: accept or drop

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

Make sure the campus is running, with posture

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

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

Scan from the printer before any wall

Command
zt printer python3 scan.py

Shown 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

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

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

Write the rules for db

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

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

Write the rules for the proxy

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

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

Load the three and read one back

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

Shown 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

Command
zt printer python3 scan.py; zt printer python3 get.py http://10.30.0.10:8080/admin/export X-User:carla

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

The path the app needs still works

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

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

The 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

Command
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]*')"; done

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

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

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.