Zones, Trust and Deny-by-Default Design
Turn a flat office network into users, servers, DMZ, guest and management zones, write the plan as a deny-by-default nftables ruleset with logged egress exceptions, and prove it with real allowed and blocked flows in network namespaces.
Contentsand lesson path
On this page
- The problem
- Step 1: Choose the zones and write the flow matrix
- Step 2: Build the zones as network namespaces
- Step 3: Translate the matrix into a deny-by-default ruleset
- Step 4: Find and fix the rule-order bug
- Step 5: Prove that egress is denied by default and exceptions are logged
- Common mistakes
- Check your understanding
- Apply it
- Review prompt
Lessons of Network Defense & Monitoring
- Zones, Trust and Deny-by-Default Design Worked example · 25 min
- Intrusion Detection and Network Security Monitoring Concept · 15 min
- Running Suricata and Zeek on a Lab Network Lab · 30 min
- Hunting Scans, Beacons and Floods in Captures and Flows Lab · 30 min
- Securing the Edge: NAC, Wi-Fi, ZTNA and DDoS Escalation Concept · 25 min
- Network Defense Check Knowledge check · 10 questions
What you will learn
- Design a segmentation plan (zones, DMZ, management network, egress control) for a small environment and express it as deny-by-default firewall rules.
Prerequisites: Networking Basics
01. The problem
A company of 60 people has one flat network: laptops, the public web server, the database and the guest Wi-Fi share one address range. If one laptop runs malware, it can reach the database directly. If the web server is compromised, the attacker is already next to everything else.
You will fix this with segmentation: splitting the network into zones, groups of systems that need the same level of trust, and putting a firewall between them. Then you write the policy as deny-by-default rules: nothing crosses a zone boundary unless a rule allows it. NIST SP 800-41 gives the same advice: traffic in either direction that the policy does not expressly permit should generally be blocked.
The requirements from the business are:
- the internet must reach the public website on HTTPS, and nothing else;
- the website needs the database, and only on the database port;
- staff use an internal app on the servers and browse the web;
- guests get internet access only;
- only administrators, from a separate management network, may open SSH sessions to servers and to the firewall itself;
- servers may download OS updates from one mirror, and every other outbound attempt from a server is blocked and recorded.
You solve it in five steps and test each one with real traffic.
02. Step 1: Choose the zones and write the flow matrix
Group systems by what they expose and what they are worth to an attacker. The DMZ (demilitarised zone) holds the systems the internet must reach, so a compromised web server is contained there. The database sits one zone deeper, in servers, because nothing on the internet should talk to it. Users and guest are separate because staff laptops are managed and guest devices are not. The management network (mgmt) carries administrative access such as SSH, so a stolen user password alone cannot reach a server console.
Next, write a flow matrix: every flow the business needs, as source zone, destination, port and reason. Anything not in the matrix is denied. The matrix is the design; the ruleset is its translation, and the matrix also gives you the test cases.
zone subnet example host
users 10.1.10.0/24 10.1.10.10 staff laptop
servers 10.1.20.0/24 10.1.20.10 internal app (443), database (5432)
dmz 10.1.30.0/24 10.1.30.10 public website (443)
guest 10.1.50.0/24 10.1.50.10 visitor phone
mgmt 10.1.99.0/24 10.1.99.10 admin jump host
inet 203.0.113.0/24 203.0.113.10 update mirror (80) and other sites
198.51.100.0/24 198.51.100.66 a command-and-control (C2) server
allowed flows (everything else is denied and logged)
any -> dmz web 443 public website
dmz -> servers db 5432 website reads the database
users -> servers 443 internal app
users -> inet 80, 443 browsing
guest -> inet 443 guest HTTPS, never internal
mgmt -> any 22 SSH, including the firewall itself
servers -> mirror 80 exception CHG-1042, logged03. Step 2: Build the zones as network namespaces
You need Docker and about 30 MB of disk. The lab was tested with the alpine:3.23 package set, nftables 1.1.5, iproute2 6.17 and BusyBox 1.37. One container plays the firewall. Inside it, unshare -n starts six processes, each in its own network namespace: an isolated copy of the Linux network stack. A veth pair, a virtual cable, joins each namespace to the firewall. The container needs NET_ADMIN and SYS_ADMIN to create these, and ip_forward=1 so it routes between zones. The sysctl applies to the container only, not to your host.
Create a folder zones-lab, save the three scripts below in it, and start the container. The last command takes the container off your real network, so every packet in this lab stays inside it.
#!/bin/sh
# Build six zone namespaces and wire each one to this container, the firewall.
set -eu
mkdir -p /run/lab
at() { zname=$1; shift; nsenter -t "$(cat /run/lab/$zname.pid)" -n "$@"; }
zone() { # zone NAME FIREWALL_IP HOST_IP [MORE_HOST_IPS...]
name=$1 fwip=$2 hostip=$3
shift 3
unshare -n sleep infinity &
echo $! > "/run/lab/$name.pid"
sleep 0.2
ip link add "$name" type veth peer name eth0 netns "$(cat /run/lab/$name.pid)"
ip addr add "$fwip/24" dev "$name"
ip link set "$name" up
at "$name" ip link set lo up
at "$name" ip addr add "$hostip/24" dev eth0
for extra in "$@"; do at "$name" ip addr add "$extra/24" dev eth0; done
at "$name" ip link set eth0 up
at "$name" ip route add default via "$fwip"
}
zone users 10.1.10.1 10.1.10.10
zone servers 10.1.20.1 10.1.20.10
zone dmz 10.1.30.1 10.1.30.10
zone guest 10.1.50.1 10.1.50.10
zone mgmt 10.1.99.1 10.1.99.10
zone inet 203.0.113.1 203.0.113.10 198.51.100.66
ip route add 198.51.100.0/24 dev inet
ip -br -4 addr | grep -v -e '^lo' -e '^eth0'#!/bin/sh
# Start one TCP listener per service. Each answers with its own name.
at() { zname=$1; shift; nsenter -t "$(cat /run/lab/$zname.pid)" -n "$@"; }
svc() { at "$1" nc -lk -p "$2" -e echo "$3" & }
svc users 445 users-smb
svc servers 443 app-https
svc servers 5432 db-postgres
svc servers 22 srv-ssh
svc dmz 443 web-https
svc inet 80 mirror-http
svc inet 443 internet-https
nc -lk -p 22 -e echo fw-ssh &
sleep 0.5
echo "listeners started"#!/bin/sh
# Try each flow and compare the result with the zone plan.
at() { zname=$1; shift; nsenter -t "$(cat /run/lab/$zname.pid)" -n "$@"; }
fails=0
probe() { # probe FROM_ZONE DEST PORT EXPECTED(allow|deny)
if at "$1" nc -z -w 2 "$2" "$3" 2>/dev/null; then got=allow; else got=deny; fi
if [ "$got" = "$4" ]; then r=PASS; else r=FAIL; fails=$((fails + 1)); fi
printf '%s %-7s -> %-13s %-4s expect %-5s got %s\n' "$r" "$1" "$2" "$3" "$4" "$got"
}
probe inet 10.1.30.10 443 allow
probe inet 10.1.20.10 5432 deny
probe dmz 10.1.20.10 5432 allow
probe dmz 10.1.20.10 22 deny
probe dmz 10.1.10.10 445 deny
probe users 10.1.20.10 443 allow
probe users 10.1.20.10 5432 deny
probe users 10.1.20.10 22 deny
probe mgmt 10.1.20.10 22 allow
probe users 203.0.113.10 443 allow
probe guest 203.0.113.10 443 allow
probe guest 10.1.20.10 443 deny
probe servers 203.0.113.10 80 allow
probe servers 203.0.113.10 443 deny
probe servers 198.51.100.66 443 deny
probe mgmt 10.1.99.1 22 allow
probe users 10.1.10.1 22 deny
echo "failures: $fails"Every probe targets a port with a listener, so a deny can only come from the firewall. The list includes flows that must fail: the internet reaching the database, the DMZ reaching a laptop, a server calling the C2 address. These negative tests matter most, because a ruleset that allows too much still passes every positive test.
docker run -d --name zones-fw --cap-add NET_ADMIN --cap-add SYS_ADMIN \
--sysctl net.ipv4.ip_forward=1 -v "$PWD:/lab" alpine:3.23 sleep infinity
docker exec zones-fw apk add -q nftables iproute2 tcpdump
docker exec zones-fw ip link set eth0 down
docker exec zones-fw sh /lab/topology.sh
docker exec -d zones-fw sh /lab/services.sh
sleep 1
docker exec zones-fw sh /lab/test-flows.shusers@if2 UP 10.1.10.1/24
servers@if2 UP 10.1.20.1/24
dmz@if2 UP 10.1.30.1/24
guest@if2 UP 10.1.50.1/24
mgmt@if2 UP 10.1.99.1/24
inet@if2 UP 203.0.113.1/24PASS inet -> 10.1.30.10 443 expect allow got allow
FAIL inet -> 10.1.20.10 5432 expect deny got allow
PASS dmz -> 10.1.20.10 5432 expect allow got allow
FAIL dmz -> 10.1.20.10 22 expect deny got allow
FAIL dmz -> 10.1.10.10 445 expect deny got allow
PASS users -> 10.1.20.10 443 expect allow got allow
FAIL users -> 10.1.20.10 5432 expect deny got allow
FAIL users -> 10.1.20.10 22 expect deny got allow
PASS mgmt -> 10.1.20.10 22 expect allow got allow
PASS users -> 203.0.113.10 443 expect allow got allow
PASS guest -> 203.0.113.10 443 expect allow got allow
FAIL guest -> 10.1.20.10 443 expect deny got allow
PASS servers -> 203.0.113.10 80 expect allow got allow
FAIL servers -> 203.0.113.10 443 expect deny got allow
FAIL servers -> 198.51.100.66 443 expect deny got allow
PASS mgmt -> 10.1.99.1 22 expect allow got allow
FAIL users -> 10.1.10.1 22 expect deny got allow
failures: 9With no rules, the firewall routes everything, so all nine negative tests fail. This is the flat network from the problem statement, and it is your baseline.
04. Step 3: Translate the matrix into a deny-by-default ruleset
Save this as fw.nft. Read it from the top, the way the kernel does. Each chain is attached to a hook: input sees packets addressed to the firewall itself, and forward sees packets it routes between zones. policy drop is the default verdict for any packet that reaches the end of the chain. Rules match top-down, and the first rule with a verdict such as accept or drop ends evaluation for that packet. Zones are matched by interface (iifname incoming, oifname outgoing), which is harder to fake than a source address.
#!/usr/sbin/nft -f
# Deny-by-default policy for the six-zone lab. Rules match top-down.
flush ruleset
define MIRROR = 203.0.113.10
table inet fw {
counter egress_exception { }
counter denied { }
chain input {
type filter hook input priority filter; policy drop;
iif "lo" accept
ct state established,related accept
iifname "mgmt" tcp dport 22 accept comment "admin the firewall"
counter name denied log group 1 prefix "deny-in "
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state established,related accept
ct state invalid drop
iifname "mgmt" tcp dport 22 accept comment "SSH only from mgmt"
oifname "dmz" ip daddr 10.1.30.10 tcp dport 443 accept comment "public web"
iifname "dmz" ip daddr 10.1.20.10 tcp dport 5432 accept comment "web to db"
iifname "users" oifname "servers" tcp dport 443 accept comment "internal app"
iifname "users" oifname "inet" tcp dport { 80, 443 } accept comment "browsing"
iifname "guest" tcp dport 443 accept comment "guest HTTPS"
iifname "guest" oifname != "inet" drop comment "guest never internal"
iifname "servers" oifname "inet" ip daddr $MIRROR tcp dport 80 \
counter name egress_exception log group 2 prefix "egress-exception " \
accept comment "CHG-1042 OS updates"
counter name denied log group 1 prefix "deny-fwd "
}
}Three ideas carry most of the weight. First, the firewall is stateful: it tracks connections (ct means connection tracking), so ct state established,related accept lets replies through without a mirror rule for each allowed flow. Second, the egress exception is narrow (one source zone, one address, one port) and carries a counter, a log statement and a comment that names the change ticket, so you can find it, measure it and remove it later. Third, the last rule counts and logs whatever is about to hit the default drop. It has no verdict of its own, so the chain policy still drops the packet.
Check the syntax with nft -c, which parses the file without changing anything, then load it and run the tests.
docker exec zones-fw sh -c 'nft -c -f /lab/fw.nft && echo "syntax OK"'
docker exec zones-fw nft -f /lab/fw.nft
docker exec zones-fw sh /lab/test-flows.shsyntax OK
PASS inet -> 10.1.30.10 443 expect allow got allow
PASS inet -> 10.1.20.10 5432 expect deny got deny
PASS dmz -> 10.1.20.10 5432 expect allow got allow
PASS dmz -> 10.1.20.10 22 expect deny got deny
PASS dmz -> 10.1.10.10 445 expect deny got deny
PASS users -> 10.1.20.10 443 expect allow got allow
PASS users -> 10.1.20.10 5432 expect deny got deny
PASS users -> 10.1.20.10 22 expect deny got deny
PASS mgmt -> 10.1.20.10 22 expect allow got allow
PASS users -> 203.0.113.10 443 expect allow got allow
PASS guest -> 203.0.113.10 443 expect allow got allow
FAIL guest -> 10.1.20.10 443 expect deny got allow
PASS servers -> 203.0.113.10 80 expect allow got allow
PASS servers -> 203.0.113.10 443 expect deny got deny
PASS servers -> 198.51.100.66 443 expect deny got deny
PASS mgmt -> 10.1.99.1 22 expect allow got allow
PASS users -> 10.1.10.1 22 expect deny got deny
failures: 105. Step 4: Find and fix the rule-order bug
One negative test fails: a guest device reaches the internal app on 10.1.20.10:443. Walk the chain with that packet. It arrives on guest, it is new, and it is TCP to port 443. The first rule that matches is iifname "guest" tcp dport 443 accept, so it is accepted. The drop rule below it, the one that was meant to keep guests out of internal zones, is never reached. With first-match evaluation, a broad allow placed above a specific deny silently overrides it.
The fix is to put the specific deny first, or to scope the allow so that it cannot match internal destinations (oifname "inet"). Swap the two guest lines in fw.nft so they read as below, reload, and rerun the tests.
iifname "guest" oifname != "inet" drop comment "guest never internal"
iifname "guest" tcp dport 443 accept comment "guest HTTPS"docker exec zones-fw nft -f /lab/fw.nft
docker exec zones-fw sh -c 'sh /lab/test-flows.sh | grep -e guest -e failures'PASS guest -> 203.0.113.10 443 expect allow got allow
PASS guest -> 10.1.20.10 443 expect deny got deny
failures: 0All seventeen tests now pass. Because the file starts with flush ruleset, reloading replaces the old rules in one step instead of adding duplicates. Keep test-flows.sh next to the ruleset and run it after every change.
06. Step 5: Prove that egress is denied by default and exceptions are logged
Egress filtering controls traffic leaving a zone. It matters because most malware must call out: to fetch commands from a C2 server or to send stolen data. If a server can open any outbound connection on port 443, both look like normal HTTPS. With deny-by-default egress, the database server can reach its update mirror and nothing else, and an unexpected outbound attempt becomes a log line you can alert on.
The ruleset sends log records to nflog groups instead of the kernel log: group 1 for denied packets and group 2 for the exception. Linux only writes firewall logs from the initial network namespace to the kernel log by default (the nf_log_all_netns sysctl), and a container is not that namespace. In production, a daemon such as ulogd2 reads nflog groups and writes them to files or a SIEM (security information and event management system: a central store that collects, searches and correlates logs). Here tcpdump -i nflog:N shows the logged packets directly.
#!/bin/sh
# Show that the egress exception and the default deny both leave a record.
at() { zname=$1; shift; nsenter -t "$(cat /run/lab/$zname.pid)" -n "$@"; }
nft reset counters > /dev/null
tcpdump -i nflog:2 -nn -t -q -c 1 2>/dev/null | sed 's/^/exception log: /' &
tcpdump -i nflog:1 -nn -t -q -c 1 2>/dev/null | sed 's/^/deny log: /' &
sleep 1
at servers nc -z -w 2 203.0.113.10 80 && echo "servers -> mirror:80 allowed"
at servers nc -z -w 2 198.51.100.66 443 || echo "servers -> c2:443 blocked"
wait
nft list countersdocker exec zones-fw sh /lab/egress-check.sh
docker rm -f zones-fwservers -> mirror:80 allowed
exception log: IP 10.1.20.10.38593 > 203.0.113.10.80: tcp 0
deny log: IP 10.1.20.10.46649 > 198.51.100.66.443: tcp 0
servers -> c2:443 blocked
table inet fw {
counter egress_exception {
packets 1 bytes 60
}
counter denied {
packets 2 bytes 120
}
}The exception matched one packet: the SYN that opened the connection. Later packets of that connection were accepted by the established rule near the top, so the counter measures new connections rather than bytes transferred. The C2 attempt was denied, and the counter shows two packets: the first SYN and the retransmission the client sent one second later. In real logs, a host that keeps retrying a denied connection is still trying, so investigate it.
07. Common mistakes
08. Check your understanding
09. Apply it
This is the first part of the skill project: a three-zone lab with deny-by-default rules, including egress.
- Add a fourth zone pair of your own, for example a backup server that must reach the database only at night on one port. Write the flow-matrix line first, then the rule, then a positive and a negative test.
- Add
iifname "users" ip saddr != 10.1.10.0/24 dropnear the top of the forward chain and write a test that proves a spoofed source from the users zone is dropped. - Replace the two guest rules with a single scoped rule (
iifname "guest" oifname "inet" tcp dport 443 accept) and confirm that all seventeen tests still pass.
Keep this lab: Running Suricata and Zeek on a Lab Network adds monitoring to the same zone layout.
10. Review prompt
Without looking back, answer in your own words:
- Why does the public website live in a DMZ while the database lives one zone deeper?
- What does first-match evaluation mean, and how did it let a guest reach the internal app?
- Why does a stateful firewall not need a rule for reply packets?
- What makes an egress exception reviewable, and why log the default deny as well?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.