#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:
- 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.
- 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 inboundNEWattempts (like port23Telnet) are blocked. - Application Layer / Proxy Firewall (OSI Layer 7): Inspects HTTP headers, URLs, DNS queries, and SMTP messages before traffic reaches internal servers.
- Next-Generation Firewall (NGFW): Adds Deep Packet Inspection (DPI), Intrusion Prevention System (IPS), TLS/HTTPS inspection, application control, and AI/ML anomaly detection.
- 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
- 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.
$ 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.
- Internet (Untrusted)External users and scanners
- Outer FirewallAllows only TCP 80/443 to DMZ
- DMZ (Semi-trusted)Public web, email, and DNS servers
- Internal FirewallRestricts DMZ-to-LAN database traffic
- 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
ALLOWabove a narrowDENY(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
ufwandnftables. - A DMZ isolates public servers from the internal LAN, while a WAF blocks Layer 7 web attacks such as SQL injection and path traversal.