Skip to main content
Firewalls, Stateful Filtering & Rule Order
Lesson 4 of 7

Firewalls, Stateful Filtering & Rule Order

Compare stateless, stateful, proxy, and next-generation firewalls, evaluate top-down ACL rule order, inspect Linux ufw and nftables policies, and design DMZ and WAF layers.

Beginner 24 min Networking
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Compare packet filtering, stateful inspection, application proxy, and next-generation firewalls across OSI layers
  • Evaluate firewall Access Control Lists (ACLs) using first-match rule order and a default-deny allow-list policy
  • Inspect and verify Linux host firewall policies using ufw and nftables
  • Explain how a DMZ and Web Application Firewall (WAF) isolate public services and defend against Layer 7 attacks

Recommended first: TCP, UDP, Ports & ICMP Diagnostics

#Why this matters

Every open TCP or UDP port expands a host's attack surface. A firewall inspects traffic against defined rules and decides whether to allow or drop each packet or session. Whether hardening a Linux host with ufw or nftables or protecting a campus perimeter, accurate firewall policies are a primary defense.

However, a single misordered rule can expose SSH to the internet, and a Layer 3-4 filter cannot inspect malicious HTTP payloads.

#Firewall generations and inspection depth

Four generations of firewalls inspect progressively deeper into the network stack:

  1. Packet Filtering Firewall (OSI Layers 3-4): Checks each packet independently by IP, protocol, and port. Fast and lightweight, but stateless, vulnerable to IP spoofing, and blind to Layer 7 content.
  2. Stateful Inspection Firewall (OSI Layers 3-4): Tracks active sessions in a state table (such as 10.10.10.5:49152 -> 192.0.2.10:443 [ESTABLISHED]). Permitted return traffic passes automatically while unsolicited inbound NEW attempts (like port 23 Telnet) are blocked.
  3. Application Layer / Proxy Firewall (OSI Layer 7): Inspects HTTP headers, URLs, DNS queries, and SMTP messages before traffic reaches internal servers.
  4. Next-Generation Firewall (NGFW): Adds Deep Packet Inspection (DPI), Intrusion Prevention System (IPS), TLS/HTTPS inspection, application control, and AI/ML anomaly detection.
Stateless Packet Filter vs Stateful Inspection Firewall
Stateless Packet Filter
  • Inspects each packet independently (L3-L4)
  • Very fast, minimal memory overhead
  • Needs explicit rules for request and reply
  • Vulnerable to spoofed ACK or unsolicited packets
Stateful Inspection Firewall
  • Tracks active sessions in a state table
  • Moderate memory for connection tracking
  • Permits return traffic for ESTABLISHED flows
  • Blocks unsolicited inbound NEW connections

#Rule order, ACL logic, and Linux host firewalls

Firewall Access Control Lists (ACLs) run top-down using first-match logic: the first matching rule executes (ALLOW or DENY) and evaluation stops. Strong policies use an allow-list approach ending with a Default Deny catch-all (Any -> Any Any Any DENY).

Rule Source Destination Proto Port Action Purpose
1 Any 192.0.2.10 TCP 80, 443 ALLOW Public web in DMZ
2 10.10.10.5 192.0.2.10 TCP 22 ALLOW Admin SSH
3 Any Any TCP 23 DENY Block Telnet
100 Any Any Any Any DENY Default deny all

Modern Linux kernels use nftables (replacing legacy iptables), with UFW offering a simple frontend.

Checking Linux host firewall rules with ufw and nftables
$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    10.10.10.5
[ 2] 443/tcp                    ALLOW IN    Anywhere
[ 3] 23/tcp                     DENY IN     Anywhere
$ sudo nft list ruleset
table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
    iifname "lo" accept
    ip saddr 10.10.10.5 tcp dport 22 accept
    tcp dport { 80, 443 } accept
  }
}

Here policy drop; enforces default deny while ct state established,related accept enables stateful filtering through conntrack.

#Network zones: DMZ and Web Application Firewalls

A perimeter architecture places public web, mail, and DNS servers in a semi-trusted DMZ (Demilitarized Zone) between an outer firewall and an internal firewall. If a DMZ host is compromised, the internal firewall still shields databases and workstations in the internal LAN.

  1. Internet (Untrusted)External users and scanners
  2. Outer FirewallAllows only TCP 80/443 to DMZ
  3. DMZ (Semi-trusted)Public web, email, and DNS servers
  4. Internal FirewallRestricts DMZ-to-LAN database traffic
  5. Internal LANDatabases, intranet, and employee hosts

Because ports 80 and 443 must stay open, Layer 3-4 filters cannot stop web attacks. A Web Application Firewall (WAF) (ModSecurity, Cloudflare WAF, AWS WAF) inspects Layer 7 HTTP/HTTPS traffic to block OWASP Top 10 payloads including SQL injection ('; DROP TABLE--), cross-site scripting (XSS), path traversal (../../etc/passwd), and Layer 7 DDoS. A WAF adds defense in depth but does not replace secure coding.

#Defending / Common mistakes

  • Rule shadowing: A broad ALLOW above a narrow DENY (or vice versa) breaks policy silently; place specific rules first.
  • Missing egress filtering: Unrestricted outbound rules make malware callbacks and exfiltration easier.
  • Relying only on L3-L4 or blind LAN trust: Packet filters miss SQL injection on port 443, and Zero Trust networks segment internal LAN traffic too.
  • Skipping drop logs: Log denied management ports (SSH, Telnet) to detect reconnaissance early.

#Summary

  • Packet filters inspect L3-L4 headers independently, stateful firewalls track sessions, application proxies inspect L7 content, and NGFWs add DPI, IPS, TLS inspection, and anomaly detection.
  • ACL rules evaluate top-down (first match wins) and end with a Default Deny allow-list policy.
  • Linux hosts enforce stateful default-drop rules with ufw and nftables.
  • A DMZ isolates public servers from the internal LAN, while a WAF blocks Layer 7 web attacks such as SQL injection and path traversal.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge