Showing raw Markdown: the renderer could not load.
## Why this matters
A small company runs everything on one flat network: staff laptops, the public web server, the file server and the customer database. One employee opens a booby-trapped attachment. The malware on that laptop doesn't need to break in anywhere: it can already reach the database on its port, because nothing between the two machines says no. Two days later the customer data is gone. The attacker never defeated a single security control, because there were no controls between the machines.
A **firewall** is the control that says no. It sits between networks (or on a single machine), compares each connection with a **policy**, and allows or blocks it. Used well, firewalls turn one big blast radius into many small ones: a compromised web server can reach its database on one port and nothing else. Used badly (a long list of exceptions nobody dares to remove), they give a false sense of safety.
This lesson explains the kinds of firewalls and what each one can see, how to split a network into zones with a demilitarized zone (DMZ), how to write a rule set that blocks everything except what you need, what intrusion detection adds, and how zero trust changes the picture. The lab then makes you build and test it.
> [!IMPORTANT]
> This module is defensive. You scan and test only systems you own or are explicitly authorized to test. The lab runs in isolated containers on your own machine.
## Learning objectives
- Compare stateless packet filters, stateful firewalls, next-generation firewalls and web application firewalls (WAF).
- Design network zones with a demilitarized zone (DMZ) and write a default-deny rule set that allows only the required flows.
- Explain what intrusion detection and prevention systems (IDS/IPS) do and how they complement firewalls.
- Explain the principles of zero trust architecture and how they differ from perimeter-based security.
- Verify a firewall policy by testing allowed and blocked flows and reading the firewall's logs.
## Key concepts
### Firewall types: what each one can see
Think of a building's reception. A very simple guard checks only the address on each envelope. A better guard also remembers who left the building, so a reply from that person is let in. An expert guard opens the envelope and reads it. And a specialist guard at the website's door reads only the web requests. These are the four firewall types, from least to most inspection.
A **stateless packet filter** judges every packet alone, using only the header fields: source and destination Internet Protocol (IP) address, protocol, source and destination port and, for the Transmission Control Protocol (TCP), the flags. It keeps no memory. To allow a web server's answers to leave, you must write a rule for the reply packets too, usually "TCP from port 443 to any high port", which is broader than necessary. It is fast and simple. Router **access control lists (ACLs)** and cloud network ACLs work this way.
A **stateful firewall** adds a **connection table** (connection tracking). When a connection is allowed, the firewall records it: addresses, ports and TCP state. Packets that belong to a recorded connection (**established** traffic) and related ones (for example an Internet Control Message Protocol (ICMP) error about that connection) are accepted automatically, and only a new connection must match a rule. That gives you the useful asymmetry: "clients inside may open connections to the Internet, and the Internet may not open connections inside". Cloud security groups and Linux nftables with `ct state` are stateful. Most firewalls today are at least stateful. The state itself is defined for TCP in [RFC 9293](https://www.rfc-editor.org/rfc/rfc9293).
A **next-generation firewall (NGFW)** is a stateful firewall that also looks at layer 7. It identifies the application regardless of the port (a video call on TCP 443 isn't the same as a file-sharing tool on TCP 443), ties rules to users and groups instead of just IP addresses, can inspect decrypted Transport Layer Security (TLS) traffic where policy and law allow it, and includes an intrusion prevention engine and threat feeds. The price is cost, complexity and performance. "NGFW" is a marketing label rather than a standard, so check what a product really does.
A **web application firewall (WAF)** protects one kind of service: web applications. It reads Hypertext Transfer Protocol (HTTP) requests (URL, method, headers, cookies and body) and blocks patterns of attacks such as Structured Query Language (SQL) injection and cross-site scripting (XSS), often with rule sets like the OWASP Core Rule Set. It usually runs as a reverse proxy or as a cloud service (often a content delivery network, CDN) in front of the site. It doesn't replace a network firewall: it sees nothing about Secure Shell (SSH) or database traffic, and a network firewall can't tell a normal HTTP request from a malicious one.
Where the firewall runs matters too. A **network firewall** sits between networks and filters everything that crosses it. A **host firewall** runs on each machine (nftables, Windows Defender Firewall, a cloud security group attached to one instance) and still protects when an attacker is already inside the network. You want both.
<figure class="diagram" style="--dg-w: 1180px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1180 560" width="1180" height="560" role="img" aria-labelledby="netsec-firewall-types-title netsec-firewall-types-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="netsec-firewall-types-title">Four firewall types compared by what they inspect</title>
<desc id="netsec-firewall-types-desc">A table with four rows. The stateless packet filter inspects layer 3 (IP) and layer 4 (ports) of each packet on its own, decides on addresses, protocol, port and TCP flags, does not track connections, and is typical in router ACLs and cloud network ACLs. The stateful firewall inspects the same layers, also decides on connection state (new or established), tracks connections, and is typical in cloud security groups and nftables with connection tracking. The next-generation firewall inspects layers 3, 4 and 7, decides on application, user, content and threat signatures, tracks connections, and sits at the network perimeter. The web application firewall inspects layer 7 HTTP requests only, decides on URL, method, headers and body to catch SQL injection and cross-site scripting patterns, and sits in front of a web application as a reverse proxy or CDN. A footer notes that a host firewall runs on each machine and a network firewall sits between networks.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1179" height="559" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<text x="28" y="38" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Firewall type</text>
<text x="288" y="38" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">What it inspects</text>
<text x="600" y="38" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Decides on</text>
<text x="850" y="38" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Tracks connections?</text>
<text x="990" y="38" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Typical place</text>
<path class="dg-line" d="M20 52H1160" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="20" y="66" width="1140" height="86" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(32 78) scale(1.1667)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></g></g>
<text x="70" y="98" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Stateless packet filter</text>
<text x="70" y="118" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Each packet alone</text>
<g class="dg-l3"><rect class="dg-solid" x="288" y="96" width="82" height="26" rx="13" fill="#b45309"/><text x="329" y="113.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L3 IP</text></g>
<g class="dg-l4"><rect class="dg-solid" x="376" y="96" width="82" height="26" rx="13" fill="#c2410c"/><text x="417" y="113.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L4 ports</text></g>
<rect class="dg-tint" x="464" y="96" width="100" height="26" rx="13" fill="#f0f0ec" stroke="#cfcfc8" stroke-width="1.2"/><text x="514" y="113.5" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle">L7 content</text>
<text x="600" y="102" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">Source and destination IP,</text>
<text x="600" y="120" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">protocol, port, TCP flags</text>
<text x="850" y="114" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="700">No</text>
<text x="990" y="102" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">Router ACLs,</text>
<text x="990" y="120" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">cloud network ACLs</text>
<rect class="dg-card" x="20" y="164" width="1140" height="86" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(32 176) scale(1.1667)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<text x="70" y="196" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Stateful firewall</text>
<text x="70" y="216" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Packets and sessions</text>
<g class="dg-l3"><rect class="dg-solid" x="288" y="194" width="82" height="26" rx="13" fill="#b45309"/><text x="329" y="211.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L3 IP</text></g>
<g class="dg-l4"><rect class="dg-solid" x="376" y="194" width="82" height="26" rx="13" fill="#c2410c"/><text x="417" y="211.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L4 ports</text></g>
<rect class="dg-tint" x="464" y="194" width="100" height="26" rx="13" fill="#f0f0ec" stroke="#cfcfc8" stroke-width="1.2"/><text x="514" y="211.5" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle">L7 content</text>
<text x="600" y="200" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">The same, plus connection</text>
<text x="600" y="218" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">state: new or established</text>
<g class="dg-ok"><text x="850" y="212" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Yes</text></g>
<text x="990" y="200" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">Cloud security groups,</text>
<text x="990" y="218" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">nftables with ct state</text>
<rect class="dg-card" x="20" y="262" width="1140" height="86" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(32 274) scale(1.1667)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></g></g>
<text x="70" y="294" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Next-generation firewall</text>
<text x="70" y="314" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Applications and users</text>
<g class="dg-l3"><rect class="dg-solid" x="288" y="292" width="82" height="26" rx="13" fill="#b45309"/><text x="329" y="309.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L3 IP</text></g>
<g class="dg-l4"><rect class="dg-solid" x="376" y="292" width="82" height="26" rx="13" fill="#c2410c"/><text x="417" y="309.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L4 ports</text></g>
<g class="dg-l7"><rect class="dg-solid" x="464" y="292" width="100" height="26" rx="13" fill="#6d28d9"/><text x="514" y="309.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L7 content</text></g>
<text x="600" y="298" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">Application, user, content,</text>
<text x="600" y="316" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">known threat signatures</text>
<g class="dg-ok"><text x="850" y="310" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Yes</text></g>
<text x="990" y="298" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">Network perimeter,</text>
<text x="990" y="316" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">data centre edge</text>
<rect class="dg-card" x="20" y="360" width="1140" height="86" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(32 372) scale(1.1667)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g></g>
<text x="70" y="392" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Web application firewall</text>
<text x="70" y="412" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">HTTP requests only</text>
<rect class="dg-tint" x="288" y="390" width="82" height="26" rx="13" fill="#f0f0ec" stroke="#cfcfc8" stroke-width="1.2"/><text x="329" y="407.5" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle">L3 IP</text>
<rect class="dg-tint" x="376" y="390" width="82" height="26" rx="13" fill="#f0f0ec" stroke="#cfcfc8" stroke-width="1.2"/><text x="417" y="407.5" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle">L4 ports</text>
<g class="dg-l7"><rect class="dg-solid" x="464" y="390" width="100" height="26" rx="13" fill="#6d28d9"/><text x="514" y="407.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">L7 content</text></g>
<text x="600" y="396" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">URL, method, headers, body:</text>
<text x="600" y="414" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500">SQL injection, XSS patterns</text>
<g class="dg-ok"><text x="850" y="408" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Per HTTP request</text></g>
<text x="990" y="396" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">In front of a web app:</text>
<text x="990" y="414" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">reverse proxy or CDN</text>
<g class="dg-l2"><g class="dg-icon" transform="translate(32 480) scale(1.0000)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="66" y="498" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Host firewall: runs on each machine, so it still protects when the network does not</text>
<g class="dg-l1"><g class="dg-icon" transform="translate(32 516) scale(1.0000)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="66" y="534" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Network firewall: sits between networks and filters everything that crosses</text>
</svg></figure>
*Figure 1. Each step to the right of the table inspects more of the packet: from headers only to the application's own content.*
> [!CHECK]
> A web server is allowed to answer requests from the Internet on TCP 443. Which kind of filter lets the replies back out without a rule for the reply packets?
>
> - [ ] A stateless packet filter, because it automatically allows all return traffic
> - [x] A stateful firewall, because the reply belongs to a connection it has recorded as established
> - [ ] A web application firewall, because it filters only inbound traffic
> - [ ] None: every reply always needs its own rule
>
> A stateful firewall matches replies to its connection table. A stateless filter judges each packet alone, so the reply needs an explicit rule.
> [!CHECK]
> An attacker sends `' OR 1=1 --` in a login form of your site on TCP 443. Which control is designed to recognise this?
>
> - [ ] A stateful network firewall that allows TCP 443 from the Internet
> - [ ] A stateless filter on the router
> - [x] A web application firewall in front of the site
> - [ ] A host firewall that blocks TCP 22
>
> The request is perfectly valid TCP to an allowed port. Only a layer 7 control that reads the HTTP request can see the attack pattern.
### Zones, the DMZ and segmentation
A flat network has one zone: everyone is "inside". **Segmentation** splits it into **zones** of similar trust and risk, and puts a firewall at the borders. Common zones are the **Internet** (untrusted), the **internal** network (trusted: staff, databases, internal applications), a **management** network and a **guest** network.
The server that must be reachable from the Internet (the web server, the mail gateway, the public DNS) is the most exposed machine you own. It will be scanned and attacked all day. You don't want it in the same zone as your database. So you put it in a **demilitarized zone (DMZ)**, also called a screened subnet ([NIST SP 800-41](https://csrc.nist.gov/pubs/sp/800/41/r1/final)): a separate network between the Internet and the internal network, with its own firewall rules. The name is borrowed from the neutral strip between two armies.
The design rules are short:
- The Internet may reach **only** the DMZ servers, and only on the service ports (for example TCP 80 and 443).
- The DMZ may reach **only** the specific internal services it needs (for example the database on TCP 5432), and nothing else.
- The Internet never reaches the internal network directly.
- Internal users may reach the Internet and the DMZ, but the DMZ doesn't start connections to the internal users.
If an attacker takes over the web server, they land in the DMZ, where there is nothing valuable, and the firewall still blocks them from every internal host except one port on one database.
Two words describe the directions of traffic. **North-south** traffic crosses the edge of the network: between the Internet and the DMZ, or between users and the Internet. **East-west** traffic moves sideways inside the network: web server to database, server to server. Attackers who are already inside spend most of their time in east-west traffic, which is why a perimeter firewall alone isn't enough. **Microsegmentation** takes the same idea to its limit: instead of a few big zones, each workload (or group of workloads with the same role) gets its own small zone and its own rules, usually enforced on the host or by the cloud (for example a security group that says "accept TCP 5432 only from members of the web security group"). The unit of trust becomes the workload, not the subnet.
<figure class="diagram" style="--dg-w: 1180px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1180 590" width="1180" height="590" role="img" aria-labelledby="netsec-dmz-zones-title netsec-dmz-zones-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="netsec-dmz-zones-title">A DMZ between the Internet and the internal network</title>
<desc id="netsec-dmz-zones-desc">Three zones around one firewall. On the left, the untrusted Internet zone holds a visitor or attacker at 203.0.113.10. In the middle, the firewall has one interface per zone and denies traffic between zones by default. At the top right, the semi-trusted DMZ holds a web server at 10.40.2.10 on tcp port 80. At the bottom right, the trusted internal zone holds a database at 10.40.3.10 on tcp port 5432 and the staff laptops in 10.40.3.0/24. Two allowed flows are marked with a tick: Internet to the web server on tcp 80 and 443, and the web server to the database on tcp 5432 only. A dashed red line marked with a cross shows Internet to internal traffic, which is dropped because no rule allows it. A legend explains that north-south traffic crosses the edge between the Internet and the DMZ and east-west traffic moves between the DMZ and the internal zone.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1179" height="589" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><rect class="dg-tint" x="20" y="40" width="260" height="280" rx="16" fill="#f0f6f9" stroke="#9ac3d9" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 53) scale(0.7500)" fill="none" stroke="#0369a1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g>
<text x="62" y="67" font-size="13" class="dg-ink" fill="#0369a1" font-weight="700">Internet · untrusted</text></g>
<g class="dg-l4"><rect class="dg-tint" x="740" y="20" width="420" height="230" rx="16" fill="#fbf4f0" stroke="#e7b39e" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(756 33) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g>
<text x="782" y="47" font-size="13" class="dg-ink" fill="#c2410c" font-weight="700">DMZ · semi-trusted</text></g>
<g class="dg-l2"><rect class="dg-tint" x="740" y="280" width="420" height="230" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(756 293) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g>
<text x="782" y="307" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Internal zone · trusted</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M250 160H380" stroke="#0369a1" stroke-width="3.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M620 135H760" stroke="#c2410c" stroke-width="3.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M751 129l8 6-8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M620 240H690V395H760" stroke="#15803d" stroke-width="3.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M751 389l8 6-8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M145 320V470H740" stroke="#b91c1c" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M731 464l8 6-8 6" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-card" x="45" y="100" width="200" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(130 110) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="145" y="160" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Visitor or attacker</text>
<text x="145" y="179" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">203.0.113.10</text>
<rect class="dg-card" x="380" y="90" width="240" height="170" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(396 106) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<g class="dg-l3"><text x="438" y="127" font-size="15" class="dg-ink" fill="#b45309" font-weight="700">Firewall</text></g>
<text x="398" y="158" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">One interface per zone</text>
<text x="398" y="178" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Default deny between zones</text>
<rect class="dg-card" x="770" y="72" width="380" height="128" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(786 88) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<g class="dg-l4"><text x="828" y="109" font-size="14" class="dg-ink" fill="#c2410c" font-weight="700">Web server</text></g>
<text x="788" y="140" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.40.2.10 · tcp 80</text>
<rect class="dg-card" x="760" y="330" width="190" height="106" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(776 346) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<g class="dg-l2"><text x="818" y="367" font-size="14" class="dg-ink" fill="#15803d" font-weight="700">Database</text></g>
<text x="778" y="398" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.40.3.10 · tcp 5432</text>
<rect class="dg-card" x="962" y="330" width="190" height="106" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(978 346) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<g class="dg-l2"><text x="1020" y="367" font-size="14" class="dg-ink" fill="#15803d" font-weight="700">Staff laptops</text></g>
<text x="980" y="398" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.40.3.0/24</text>
<g class="dg-ok"><circle class="dg-solid" cx="646" cy="135" r="11" fill="#15803d"/><text x="646" y="139.5" font-size="13" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">✓</text></g>
<g class="dg-ok"><circle class="dg-solid" cx="690" cy="395" r="11" fill="#15803d"/><text x="690" y="399.5" font-size="13" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">✓</text></g>
<g class="dg-warn"><circle class="dg-solid" cx="440" cy="470" r="11" fill="#b91c1c"/><text x="440" y="474.5" font-size="13" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">✕</text></g>
<g class="dg-l4"><rect class="dg-bg-plain" x="668.447" y="123" width="67.105" height="24" rx="12" fill="#ffffff"/><text x="702" y="139.5" font-size="11.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">80, 443</text></g>
<g class="dg-l2"><rect class="dg-bg-plain" x="666.97" y="306" width="46.06" height="24" rx="12" fill="#ffffff"/><text x="690" y="322.5" font-size="11.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">5432</text></g>
<text x="145" y="500" font-size="12.5" class="dg-ink" fill="#b91c1c" font-weight="700" text-anchor="middle">Internet → internal: dropped, no rule allows it</text>
<g class="dg-l4"><rect class="dg-tint" x="20" y="540" width="348" height="26" rx="13" fill="#f9ece7" stroke="#e4aa92" stroke-width="1.2"/><text x="194" y="557.5" font-size="12" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle">North-south: crosses the edge, Internet ↔ DMZ</text></g>
<g class="dg-l2"><rect class="dg-tint" x="380" y="540" width="305" height="26" rx="13" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/><text x="532.5" y="557.5" font-size="12" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">East-west: moves inside, DMZ ↔ internal</text></g>
</svg></figure>
*Figure 2. Only two flows are allowed: Internet to the web server, and the web server to one database port. The direct path from the Internet to the internal zone has no rule, so it is dropped.*
> [!NOTE]
> The addresses in this lesson and lab are documentation or private ranges: `203.0.113.0/24` is a documentation range ([RFC 5737](https://www.rfc-editor.org/rfc/rfc5737)) that stands for "the Internet", and `10.40.2.0/24` and `10.40.3.0/24` are private addresses ([RFC 1918](https://www.rfc-editor.org/rfc/rfc1918)). In a real DMZ the web server usually has a public address, or a public address translated to the private one.
> [!CHECK]
> After a web server in the DMZ is compromised, what should the firewall still prevent?
>
> - [ ] Visitors from the Internet reaching the web server on TCP 443
> - [x] The web server opening connections to any internal host on any port
> - [ ] The database answering the web server on the connection the web server opened
> - [ ] The web server answering visitors
>
> The DMZ is designed to contain a compromise: only the specific flow the web server needs (for example to the database port) is allowed, everything else from the DMZ to the internal zone is dropped.
### Default deny, rule order and logging
There are two ways to write a firewall policy. **Default allow** says "everything is permitted unless a rule blocks it". You must think of every bad thing in advance, and every new service is open the day it starts. **Default deny** says "everything is blocked unless a rule allows it". You list the flows you need, and anything you forgot is blocked by default. A forgotten rule causes a visible, fixable failure ("the app can't reach the database"). A forgotten block under default allow causes a silent exposure. Use default deny. It is the firewall form of **least privilege**: each flow is allowed for a stated source, destination and port, and no more.
Rule sets are evaluated **in order**, and **the first matching rule wins** (this is true of most firewalls and of nftables chains). So the order matters:
1. Drop what is clearly invalid.
2. Accept **return traffic** of connections already allowed (`established,related`). Put it first because it matches the most packets.
3. Accept the specific **new** flows: source zone, destination host, protocol and port.
4. **Log** what is about to be dropped.
5. **Drop** everything else, by the chain's policy or a final rule.
A rule added after the final drop is never reached. That is a very common mistake: the new rule exists, the traffic is still blocked.
<figure class="diagram" style="--dg-w: 1180px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1180 300" width="1180" height="300" role="img" aria-labelledby="netsec-default-deny-title netsec-default-deny-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="netsec-default-deny-title">How a default-deny rule set evaluates a packet</title>
<desc id="netsec-default-deny-desc">A five-step process, read from top to bottom of the rule list, where the first match decides. Step 1, invalid: a broken packet or one that belongs to no known connection is dropped. Step 2, return traffic: a reply to a connection that was allowed is accepted. Step 3, allow rule: a packet that matches a listed flow of source, destination and port is accepted. Step 4, log: a rate-limited record is written of what is about to be dropped, and evaluation continues. Step 5, default deny: anything that matched nothing is dropped by the policy. A note warns that a rule placed after the final drop is never reached, so order matters.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1179" height="299" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<text x="20" y="30" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700">A packet arrives at the firewall and the rules run from top to bottom: the first match decides</text>
<rect class="dg-card" x="20" y="52" width="200.8" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="42" cy="76" r="12" fill="#0369a1"/><text x="42" y="80.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(180.8 64) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g></g>
<text x="34" y="114" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Invalid?</text>
<text x="34" y="134" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Broken, or not part of</text>
<text x="34" y="151" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">any known connection</text>
<path class="dg-line" d="M226.8 111H248.8" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M240.8 105l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="254.8" y="52" width="200.8" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="276.8" cy="76" r="12" fill="#0369a1"/><text x="276.8" y="80.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(415.6 64) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="268.8" y="114" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Return traffic?</text>
<text x="268.8" y="134" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Reply to a connection</text>
<text x="268.8" y="151" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">that was allowed</text>
<path class="dg-line" d="M461.6 111H483.6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M475.6 105l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="489.6" y="52" width="200.8" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="511.6" cy="76" r="12" fill="#0369a1"/><text x="511.6" y="80.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(650.4 64) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></g></g>
<text x="503.6" y="114" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Allow rule?</text>
<text x="503.6" y="134" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Listed flow: source,</text>
<text x="503.6" y="151" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">destination, port</text>
<path class="dg-line" d="M696.4 111H718.4" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M710.4 105l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="724.4" y="52" width="200.8" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="746.4" cy="76" r="12" fill="#0369a1"/><text x="746.4" y="80.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(885.2 64) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<text x="738.4" y="114" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Log it</text>
<text x="738.4" y="134" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Rate-limited record of</text>
<text x="738.4" y="151" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">what is about to drop</text>
<path class="dg-line" d="M931.2 111H953.2" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M945.2 105l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="959.2" y="52" width="200.8" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="981.2" cy="76" r="12" fill="#0369a1"/><text x="981.2" y="80.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">5</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(1120 64) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<text x="973.2" y="114" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Default deny</text>
<text x="973.2" y="134" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Nothing matched:</text>
<text x="973.2" y="151" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">policy drop</text>
<g class="dg-warn"><rect class="dg-solid" x="30.4" y="184" width="180" height="26" rx="13" fill="#b91c1c"/><text x="120.4" y="201.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">match → drop</text></g>
<g class="dg-ok"><rect class="dg-solid" x="265.2" y="184" width="180" height="26" rx="13" fill="#15803d"/><text x="355.2" y="201.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">match → accept</text></g>
<g class="dg-ok"><rect class="dg-solid" x="500" y="184" width="180" height="26" rx="13" fill="#15803d"/><text x="590" y="201.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">match → accept</text></g>
<g class="dg-l1"><rect class="dg-solid" x="734.8" y="184" width="180" height="26" rx="13" fill="#0369a1"/><text x="824.8" y="201.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">then continue</text></g>
<g class="dg-warn"><rect class="dg-solid" x="969.6" y="184" width="180" height="26" rx="13" fill="#b91c1c"/><text x="1059.6" y="201.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">always → drop</text></g>
<path class="dg-line" d="M20 236H1160" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<g class="dg-warn"><g class="dg-icon" transform="translate(20 252) scale(0.9167)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g></g>
<text x="52" y="269" font-size="13" class="dg-text" fill="#17171c" font-weight="600">A rule placed after the final drop is never reached: order matters</text>
</svg></figure>
*Figure 3. The first matching rule decides. Whatever reaches the last step is dropped, and the log shows you what it was.*
Three more habits make a rule set safe:
- **Filter egress** (outbound) traffic too. Most people only think about what comes in. But malware that has landed on a server needs to call out to its operator ("command and control") or to send data away. If your web server can't open connections to the Internet at all, that call fails. Egress filtering also supports anti-spoofing: a network should drop packets leaving it with a source address that doesn't belong to it ([RFC 2827](https://www.rfc-editor.org/rfc/rfc2827), BCP 38).
- **Drop or reject?** A **drop** discards the packet silently: the sender sees a timeout. A **reject** sends back a TCP reset or an ICMP "unreachable": the sender sees a fast refusal. Drop gives an attacker less information and slows a scan; reject gives users a faster error. For traffic from the Internet use drop. Use reject for internal users so that failures are obvious.
- **Log the drops.** A blocked packet is information: a misconfigured application, a scanner or an attacker's first try. Logging every drop can fill the disk and hide the interesting events, so use a **rate limit** and log only denied new connections. Counters on every rule are cheap and show which rules are used and which are dead.
> [!CHECK]
> A rule set has these rules in order: 1) accept established, 2) drop everything, 3) accept TCP 8443 from the Internet to the web server. Users still can't reach port 8443. Why?
>
> - [ ] Rule 1 blocks new connections
> - [x] Rule 2 drops the packets first, so rule 3 is never reached
> - [ ] A firewall cannot accept more than two flows
> - [ ] Port 8443 is reserved and always blocked
>
> The first matching rule wins. A rule placed after a catch-all drop never gets a chance to match.
### Intrusion detection and prevention
A firewall decides by address, port and (sometimes) application. It can't tell that a connection it allowed on TCP 443 carries an exploit, or that the database server has started to talk to a country it never talked to before. An **intrusion detection system (IDS)** watches traffic or system activity and **raises an alert** when it sees something suspicious. An **intrusion prevention system (IPS)** does the same but sits **in line** with the traffic, so it can also **block** it. The analogy is a burglar alarm (detects, tells you) versus a guard who stops the intruder at the door (prevents). The standard reference is [NIST SP 800-94](https://csrc.nist.gov/pubs/sp/800/94/final).
Two things distinguish systems by where they look:
- A **network IDS (NIDS)** looks at the traffic of a network segment, fed by a mirror (Switched Port Analyzer, SPAN) port or a network tap, so it doesn't sit in the path and can't break it. A **host IDS (HIDS)** runs on a machine and watches its logs, files and processes: it sees what is inside encrypted traffic (after decryption on the host) and what the host does, but only on that host. Examples: Suricata, Snort and Zeek for network detection; OSSEC and Wazuh for hosts.
And two things distinguish them by how they decide:
- **Signature-based** detection compares traffic with a database of known attack patterns. It is precise and explains why it alerted, but it misses new attacks until a signature is written.
- **Anomaly-based** detection learns what normal looks like and alerts on deviations. It can catch new attacks, but it also alerts on harmless changes: a new backup job or a busy sales day.
The big practical problem is **false positives**: alerts for traffic that is harmless. An IDS that raises 1,000 alerts a day, of which 5 are real, trains the team to ignore it. A **false negative** is a real attack that raised no alert. Tuning (disabling rules that don't apply to your network, adding context) is a continuous job. And in an IPS a false positive is worse: it blocks legitimate traffic. For that reason many teams run a new IPS rule in detect-only mode first.
IDS/IPS **complements** the firewall, it doesn't replace it. The firewall shrinks what can reach you, the IDS watches the flows that are allowed. A default-deny firewall also makes the IDS more useful: with fewer allowed flows, an unusual connection stands out.
> [!CHECK]
> What is the difference between an IDS and an IPS?
>
> - [ ] An IDS works only on hosts and an IPS only on networks
> - [ ] An IDS uses signatures and an IPS uses anomaly detection
> - [x] An IDS raises alerts; an IPS sits in line and can also block the traffic
> - [ ] An IDS replaces the firewall; an IPS complements it
>
> Both can use signatures or anomaly detection and can run on a network or a host. What differs is the position: in line (IPS) means it can drop traffic, and also that a false positive blocks real users.
### Zero trust
The firewall model so far is the castle and moat: a strong perimeter, and trust for whoever is inside. It has a weakness: once an attacker is inside (a stolen laptop, a phished password, a malicious insider, a compromised VPN), they are trusted too. Cloud services, remote work and personal devices have also dissolved the perimeter: where is the edge when the application is in a cloud and the user is in a café?
**Zero trust** is the answer to that. It isn't a product but a design approach, defined in [NIST SP 800-207](https://csrc.nist.gov/pubs/sp/800/207/final). Its core ideas:
- **Never trust, always verify: location is not trust.** Being on the corporate network (or on the VPN) doesn't grant access. Every request is treated as if it came from an untrusted network.
- **Verify explicitly.** Each request is authenticated and authorised using everything available: the user's identity, the health of the device (patched? encrypted?), the time, the location, the sensitivity of the resource.
- **Per-request policy decisions.** Access to a resource is granted for one session at a time, with the **least privilege** needed, and is re-evaluated when the context changes.
- **Assume breach.** Design as if an attacker is already inside: segment, encrypt traffic between services, and monitor everything.
NIST describes the parts: a **policy decision point (PDP)**, made of a policy engine (decides) and a policy administrator (sets up the session), and a **policy enforcement point (PEP)** in front of each resource that only opens the path when the PDP says yes. Microsegmentation is one of the building blocks: each resource sits behind its own enforcement point, so being "inside" gives you nothing.
<figure class="diagram" style="--dg-w: 1180px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1180 470" width="1180" height="470" role="img" aria-labelledby="netsec-perimeter-vs-zero-trust-title netsec-perimeter-vs-zero-trust-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="netsec-perimeter-vs-zero-trust-title">Perimeter security compared with zero trust</title>
<desc id="netsec-perimeter-vs-zero-trust-desc">Left, the perimeter model. Traffic from the Internet passes one edge firewall, the only checkpoint, into a trusted flat internal network that holds a infected laptop, an app server and a database. The infected laptop moves freely to the app server and the database because nothing inside is checked. Right, zero trust. A user with a device sends a request to a policy enforcement point that sits in front of each resource. The enforcement point asks a policy decision point, which weighs identity, device and context against policy and answers allow or deny. Only if allowed does the request reach the resource, one request at a time. Three principles are listed: never trust location, verify explicitly, and apply least privilege per request.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1179" height="469" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l4"><rect class="dg-tint" x="20" y="20" width="520" height="430" rx="16" fill="#fbf4f0" stroke="#e7b39e" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 33) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></g>
<text x="62" y="47" font-size="13" class="dg-ink" fill="#c2410c" font-weight="700">Perimeter model · trust by location</text></g>
<g class="dg-l1"><rect class="dg-tint" x="560" y="20" width="600" height="430" rx="16" fill="#f0f6f9" stroke="#9ac3d9" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(576 33) scale(0.7500)" fill="none" stroke="#0369a1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g>
<text x="602" y="47" font-size="13" class="dg-ink" fill="#0369a1" font-weight="700">Zero trust · verify every request</text></g>
<rect class="dg-card" x="45" y="70" width="130" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(95 80) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g></g>
<text x="110" y="130" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Internet</text>
<rect class="dg-card" x="245" y="70" width="170" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(315 80) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<text x="330" y="130" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Edge firewall</text>
<text x="330" y="149" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">the only checkpoint</text>
<g class="dg-l1"><path class="dg-stroke" d="M175 107H245" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M236 101l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><rect class="dg-tint" x="40" y="190" width="480" height="250" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(56 203) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></g>
<text x="82" y="217" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Inside = trusted, flat network</text></g>
<g class="dg-l3"><path class="dg-stroke" d="M330 162V190" stroke="#b45309" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l3"><path class="dg-stroke" d="M324 181l6 8 6-8" stroke="#b45309" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M180 316H235V300H325" stroke="#b91c1c" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M180 340H235V356H325" stroke="#b91c1c" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<rect class="dg-card" x="50" y="270" width="130" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-warn"><g class="dg-icon" transform="translate(100 280) scale(1.2500)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="115" y="330" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Infected laptop</text>
<rect class="dg-card" x="325" y="235" width="150" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(385 245) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<text x="400" y="295" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">App server</text>
<rect class="dg-card" x="325" y="335" width="150" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(385 345) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="400" y="395" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Database</text>
<g class="dg-warn"><path class="dg-stroke" d="M316 294l8 6-8 6" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M316 350l8 6-8 6" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="280" y="428" font-size="12.5" class="dg-ink" fill="#b91c1c" font-weight="700" text-anchor="middle">Free movement: no check inside</text>
<rect class="dg-card" x="565" y="230" width="150" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(625 240) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="640" y="290" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">User + device</text>
<text x="640" y="309" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">who and how healthy</text>
<rect class="dg-card" x="995" y="230" width="150" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(1055 240) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="1070" y="290" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Resource</text>
<text x="1070" y="309" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">one request at a time</text>
<rect class="dg-card" x="765" y="60" width="190" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(781 76) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 6h16"/><path d="M4 12h10"/><path d="M4 18h6"/><circle cx="18" cy="16" r="3"/><path d="m22 20-1.8-1.8"/></g></g>
<g class="dg-l7"><text x="823" y="97" font-size="14" class="dg-ink" fill="#6d28d9" font-weight="700">Decision point</text></g>
<text x="783" y="128" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">identity · device · context · policy</text>
<rect class="dg-card" x="765" y="210" width="190" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(781 226) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<g class="dg-l3"><text x="823" y="247" font-size="14" class="dg-ink" fill="#b45309" font-weight="700">Enforcement point</text></g>
<text x="783" y="278" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">in front of each resource</text>
<g class="dg-l1"><path class="dg-stroke" d="M715 266H765" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M756 260l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M955 266H995" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M986 260l8 6-8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M815 172V210" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M809 201l6 8 6-8" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M905 210V172" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M899 181l6-8 6 8" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><rect class="dg-bg-plain" x="796.34" y="179" width="37.32" height="24" rx="12" fill="#ffffff"/><text x="815" y="195.5" font-size="11.5" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle">ask</text></g>
<g class="dg-l7"><rect class="dg-bg-plain" x="876.68" y="179" width="56.64" height="24" rx="12" fill="#ffffff"/><text x="905" y="195.5" font-size="11.5" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle">decide</text></g>
<g class="dg-l1"><rect class="dg-tint" x="580" y="394" width="168" height="26" rx="13" fill="#e6f0f6" stroke="#8ebcd5" stroke-width="1.2"/><text x="664" y="411.5" font-size="12" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle">Never trust location</text></g>
<g class="dg-l7"><rect class="dg-tint" x="758" y="394" width="146" height="26" rx="13" fill="#f0eafb" stroke="#bd9eee" stroke-width="1.2"/><text x="831" y="411.5" font-size="12" class="dg-ink" fill="#6d28d9" font-weight="700" text-anchor="middle">Verify explicitly</text></g>
<g class="dg-l2"><rect class="dg-tint" x="914" y="394" width="226" height="26" rx="13" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/><text x="1027" y="411.5" font-size="12" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">Least privilege, per request</text></g>
</svg></figure>
*Figure 4. In the perimeter model one check protects everything behind it; in zero trust every request to every resource is checked.*
Zero trust doesn't remove firewalls. They are still the enforcement points that implement segmentation; what changes is that they no longer assume anything on the inside is safe.
A brief word on **VPNs**. A **virtual private network (VPN)** builds an encrypted tunnel across an untrusted network, either **site-to-site** (two offices or a cloud network joined, commonly with Internet Protocol Security (IPsec), [RFC 4301](https://www.rfc-editor.org/rfc/rfc4301)) or **remote access** (a laptop connecting to the company, with IPsec, TLS-based VPNs or WireGuard). A VPN protects the traffic **in transit**. It does **not** make the device trustworthy: a traditional VPN that drops the user onto the flat internal network gives an attacker with a stolen laptop everything. Combine a VPN with segmentation and per-resource policy.
> [!CHECK]
> Which statement describes a zero trust approach?
>
> - [ ] Anyone on the corporate VPN is trusted, because the tunnel is encrypted
> - [ ] The edge firewall checks everyone once; after that, access is free
> - [x] Every request is authenticated and authorised for one resource, whatever network it comes from
> - [ ] Firewalls are removed because identity replaces them
>
> Zero trust means no implicit trust from network location. It uses policy decisions per request, and it still needs enforcement points such as firewalls.
## How it works
Follow one packet through the lab's firewall: a client at `203.0.113.10` opens a connection to the web server at `10.40.2.10` on TCP 80.
1. The client sends a TCP SYN to `10.40.2.10:80`. Its route says "send `10.40.0.0/16` to the firewall at `203.0.113.254`".
2. The packet reaches the firewall's `net0` interface. The firewall is also a router with forwarding turned on, so the packet enters the **forward** hook, the path taken by traffic that is passing through (not addressed to the firewall itself).
3. The rules run in order. The packet isn't invalid. It isn't part of an established connection (the table has no entry yet). It matches rule 3: *new* connection, in from `net0`, out to `dmz0`, to `10.40.2.10`, TCP port 80. The firewall **accepts** it, and the connection tracker records the connection.
4. The web server answers with SYN-ACK. That packet matches rule 2 (`established`) and goes through with no specific rule. The same happens for every later packet in both directions.
5. Now a different packet: the same client opens a connection to `10.40.3.10:5432`. No allow rule matches. The log rule records it (rate-limited) and the last rule counts and **drops** it. The client sees nothing and its connect call times out.
To **verify a policy**, test it from both sides. Write down the **flow matrix** first: for each source and destination, what should happen. Then test every cell, with two kinds of checks:
- **Positive tests** prove that the flows you need work (Internet to web on 80, web to database on 5432).
- **Negative tests** prove that everything else is blocked (Internet to database, web to database on 22, database to web, web to the Internet). This is the half people forget, and it is the half an attacker tests.
Then **scan**. A port scanner such as `nmap` run from the outside shows what a stranger sees: any open port you didn't expect is a rule that is too broad or a service you forgot. Scan **before** and **after** applying the policy and compare. A **closed** port answers with a refusal ("nothing is listening here"), a **filtered** port doesn't answer at all (a firewall dropped the probe), and an **open** port accepts the connection. Finally, **read the evidence**: the rule counters (which rules matched, how many packets) and the drop log (what was blocked, from where to where) confirm that blocked flows were blocked **by your rule** and not for another reason, such as a typo in an address.
> [!WARNING]
> Scan only systems that you own or that you are explicitly authorized to test in writing. Scanning other people's systems can be illegal and can trigger their incident response. Scan from outside **and** from each zone to check the internal rules too, because a perimeter scan can't see the rules between your internal zones.
> [!CHECK]
> After applying a default-deny policy you scan the web server from the Internet. Port 80 shows `open`, and 1,023 other ports show `filtered` with no response. What does it mean?
>
> - [ ] Those ports have no service and the host refused each probe
> - [x] The firewall silently dropped the probes to those ports; only port 80 is allowed
> - [ ] The scan failed and should be repeated
> - [ ] The server crashed
>
> `filtered` means no answer at all, which is the signature of a drop. A `closed` port would answer with a refusal.
## Worked example
A small shop has a public web server and a database. The goal: customers reach the site; the web server reaches the database; nothing else is allowed. The design is the one you will build in the lab.
| Zone | Network | Hosts |
|---|---|---|
| Internet (stand-in) | `203.0.113.0/24` | Client `203.0.113.10` |
| DMZ | `10.40.2.0/24` | Web server `10.40.2.10` |
| Internal | `10.40.3.0/24` | Database `10.40.3.10` |
The flow matrix (what should happen):
| From | To | Port | Decision | Reason |
|---|---|---|---|---|
| Internet | Web server | TCP 80 | Allow | The public service |
| Web server | Database | TCP 5432 | Allow | The application needs its data |
| Internet | Database | any | Deny | Never directly reachable |
| Internet | Web server | other (22, 8080) | Deny | Not a public service |
| Web server | Database | other (22) | Deny | Least privilege |
| Database | anything | any | Deny | No reason to start connections |
| Web server | Internet | any | Deny | Egress filtering |
| Replies | any | any | Allow | Established connections |
And as nftables rules on the firewall (a stateful, default-deny table; the lab explains each line):
```text
table inet lab_fw {
chain forward {
type filter hook forward priority 0; policy drop;
ct state invalid drop
ct state established,related counter accept
iifname "net0" oifname "dmz0" ip daddr 10.40.2.10 tcp dport 80 ct state new counter accept
iifname "dmz0" oifname "int0" ip saddr 10.40.2.10 ip daddr 10.40.3.10 tcp dport 5432 ct state new counter accept
limit rate 5/second burst 10 packets log group 1 prefix "lab-netsec DROP: "
counter drop
}
}
```
Count the rules: two new-connection rules cover the two allowed flows, and one `established` rule covers every reply. Every deny row of the matrix is covered by the policy `drop` and costs no rule. This is the benefit of default deny: the list of rules is as long as the list of things you need, not as long as the list of things that could go wrong.
## Common pitfalls
- **Default allow with a few blocks** — it feels safer because nothing breaks, but you protect only against what you thought of. Start from deny.
- **A rule after the final drop** — the rule exists and never matches. Insert it before the drop, and test it.
- **Only testing that allowed things work** — a policy that allows everything passes every positive test. Always run the negative tests too.
- **No egress filtering** — a compromised server freely calls its operator and sends data out. Restrict outbound traffic from servers to what they need.
- **Allowing "any" for convenience** — `allow any to the DMZ "just for now"` stays for years. Use the exact source, destination and port, and add a comment and an owner.
- **Blocking all ICMP** — path MTU discovery and, for IPv6, neighbour discovery depend on ICMP. Allow the needed types ([RFC 4890](https://www.rfc-editor.org/rfc/rfc4890) for ICMPv6) and block the rest.
- **Trusting the VPN or the inside** — after an attacker is connected, a flat network gives them everything. Segment inside the VPN too.
- **Treating the IDS as fire-and-forget** — untuned, it floods the team with false positives until nobody reads the alerts.
## Security considerations
- **The firewall is a target.** Keep its software patched, limit who can administer it, put the management interface on a management network (never on the Internet), use multi-factor authentication and keep the configuration in version control so every change is reviewed.
- **Rule sets decay.** Rules for decommissioned servers stay forever and widen exposure. Review the rules regularly, use the counters to find rules that never match, and remove them after confirmation.
- **A firewall can't see inside encrypted traffic** unless it decrypts it, which brings its own risks (certificates, privacy, the firewall holding the keys). Use host-based controls and endpoint detection for what the network can't see.
- **Segmentation limits movement, it doesn't stop an attack at the allowed port.** The web server's TCP 80 is still open to the world, so the application must be patched and, where it makes sense, protected by a WAF.
- **Log integrity.** An attacker who gains access erases logs. Send firewall and IDS logs to a separate log server, and alert on a log source that goes silent.
- **Only test what you own.** Scanning and testing are done on your own systems, or with written authorization that defines the scope and time window.
## Summary
- Stateless filters judge each packet alone; stateful firewalls track connections so replies are automatic; NGFWs understand applications and users; WAFs read HTTP requests; host and network firewalls complement each other.
- Split the network into zones by trust. Put Internet-facing servers in a DMZ, allow only the exact flows each zone needs, and never allow the Internet to reach the internal zone directly.
- Use default deny, put the established rule first, allow specific new flows, filter egress, log the drops with a rate limit, and remember that the first matching rule wins.
- An IDS alerts and an IPS blocks in line; signature detection is precise but only knows old attacks, anomaly detection can find new ones but raises false positives. They complement the firewall.
- Zero trust drops the idea that "inside" is safe: every request is authenticated and authorised for one resource, using identity, device and context (NIST SP 800-207).
- Verify a policy with positive and negative tests, an `nmap` scan before and after, the rule counters and the drop log.
## Glossary
| Term | Definition |
|---|---|
| Firewall | A control that allows or blocks network connections according to a policy, between networks or on a single host. |
| Policy | The ordered set of rules and the default action that define which flows are allowed. |
| Stateless packet filter | A firewall that judges every packet alone from its header fields and keeps no memory of connections. |
| Stateful firewall | A firewall that tracks connections, so packets of an allowed connection are accepted without extra rules. |
| Connection table | The firewall's record of active connections, used to recognise established and related packets. |
| Established | Traffic that belongs to a connection the firewall already allowed and recorded. |
| Next-generation firewall (NGFW) | A stateful firewall that also identifies applications and users and often includes intrusion prevention. |
| Web application firewall (WAF) | A firewall for web applications that inspects HTTP requests for attack patterns such as SQL injection and XSS. |
| Network firewall | A firewall placed between networks that filters everything crossing it. |
| Host firewall | A firewall running on one machine that filters that machine's own traffic. |
| Segmentation | Splitting a network into zones of similar trust, with a firewall between them. |
| Zone | A group of systems with the same trust level and security requirements. |
| Demilitarized zone (DMZ) | A separate network between the Internet and the internal network that holds the servers reachable from the Internet. |
| North-south traffic | Traffic that crosses the edge of the network, for example between the Internet and a server. |
| East-west traffic | Traffic that moves sideways inside the network, for example between servers. |
| Microsegmentation | Segmentation down to individual workloads, each with its own rules. |
| Default deny | A policy in which everything is blocked unless a rule explicitly allows it. |
| Least privilege | Granting only the access needed for a task, here a flow with a specific source, destination and port. |
| Egress filtering | Filtering of outbound traffic, to stop compromised hosts calling out or sending data away. |
| Drop | Discarding a packet silently, so the sender sees a timeout. |
| Reject | Discarding a packet and telling the sender with a TCP reset or an ICMP unreachable. |
| Intrusion detection system (IDS) | A system that watches traffic or hosts and raises alerts about suspicious activity. |
| Intrusion prevention system (IPS) | An IDS placed in line with the traffic so it can also block what it detects. |
| Network IDS (NIDS) | An IDS that monitors the traffic of a network segment. |
| Host IDS (HIDS) | An IDS that runs on a machine and monitors its logs, files and processes. |
| Signature-based detection | Detection by comparing activity with known attack patterns. |
| Anomaly-based detection | Detection of deviations from a learned baseline of normal behaviour. |
| False positive | An alert for activity that is actually harmless. |
| Zero trust | A security approach that grants no implicit trust based on network location and verifies every request. |
| Policy decision point (PDP) | The zero trust component that decides whether a request is allowed. |
| Policy enforcement point (PEP) | The component in front of a resource that opens or closes the path as the PDP decides. |
| Virtual private network (VPN) | An encrypted tunnel across an untrusted network. |
## Check your understanding
1. A colleague proposes a rule `allow any to 10.40.2.10` because "the web team keeps asking for new ports". What would you answer, and what do you ask for instead?
2. Draw the zones and the allowed flows for a company with a public website, a mail gateway, an internal file server and staff laptops. Which flows are north-south and which are east-west?
3. Why is egress filtering useful even if the inbound rules are perfect?
4. An IPS blocks an important customer's traffic every Monday morning. What is it, what do you do first and why?
5. Your manager says "we have a VPN, so we are zero trust". What is missing?
## References
- [NIST SP 800-41 Rev. 1](https://csrc.nist.gov/pubs/sp/800/41/r1/final) — Guidelines on Firewalls and Firewall Policy
- [NIST SP 800-94](https://csrc.nist.gov/pubs/sp/800/94/final) — Guide to Intrusion Detection and Prevention Systems (IDPS)
- [NIST SP 800-207](https://csrc.nist.gov/pubs/sp/800/207/final) — Zero Trust Architecture
- [RFC 9293](https://www.rfc-editor.org/rfc/rfc9293) — Transmission Control Protocol (TCP)
- [RFC 2827](https://www.rfc-editor.org/rfc/rfc2827) — Network Ingress Filtering (BCP 38)
- [RFC 4890](https://www.rfc-editor.org/rfc/rfc4890) — Recommendations for Filtering ICMPv6 Messages in Firewalls
- [RFC 4301](https://www.rfc-editor.org/rfc/rfc4301) — Security Architecture for the Internet Protocol (IPsec)
- [RFC 5737](https://www.rfc-editor.org/rfc/rfc5737) and [RFC 1918](https://www.rfc-editor.org/rfc/rfc1918) — Documentation and private address ranges
- [nftables wiki](https://wiki.nftables.org/) — the Linux packet filter used in the lab