---
title: "Network Security: Firewalls and Segmentation"
domain: cybersecurity
level: intermediate
duration: "55 min"
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."
---

# Network Security: Firewalls and Segmentation

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

![A table comparing four firewall types. The stateless packet filter inspects layer 3 IP and layer 4 ports, decides on addresses, protocol, port and TCP flags, does not track connections, and appears in router ACLs and cloud network ACLs. The stateful firewall inspects the same layers and also decides on connection state, tracks connections, and appears in cloud security groups and nftables. 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 only, decides on URL, method, headers and body to catch SQL injection and XSS, and sits in front of a web app as a reverse proxy or CDN. A footer explains host firewalls versus network firewalls.](../../../assets/diagrams/netsec-firewall-types.svg)

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

![Three zones around one firewall. The untrusted Internet zone holds a visitor or attacker at 203.0.113.10. The firewall has one interface per zone and denies traffic between zones by default. The semi-trusted DMZ holds a web server at 10.40.2.10 on tcp 80. The trusted internal zone holds a database at 10.40.3.10 on tcp 5432 and staff laptops in 10.40.3.0/24. Allowed flows are ticked: Internet to the web server on 80 and 443, and web server to database on 5432. A dashed red line with a cross shows Internet to internal traffic being dropped. A legend defines north-south and east-west traffic.](../../../assets/diagrams/netsec-dmz-zones.svg)

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

![A five-step process read top to bottom through the rule list, where the first match decides. Step 1 invalid: broken or out-of-state packets are dropped. Step 2 return traffic: replies to an allowed connection are accepted. Step 3 allow rule: a listed flow of source, destination and port is accepted. Step 4 log: a rate-limited record of what is about to drop, then evaluation continues. Step 5 default deny: anything that matched nothing is dropped by the policy. A note warns that a rule after the final drop is never reached.](../../../assets/diagrams/netsec-default-deny.svg)

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

![Left, the perimeter model: traffic from the Internet passes one edge firewall, the only checkpoint, into a trusted flat internal network with an 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 in front of each resource. The enforcement point asks a policy decision point, which weighs identity, device and context against policy and decides. Only if allowed does the request reach the resource. Three principles: never trust location, verify explicitly, least privilege per request.](../../../assets/diagrams/netsec-perimeter-vs-zero-trust.svg)

*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
