Firewalls filter network traffic based on rules, controlling what enters and exits a network or host. They operate primarily at Layer 3 (IP) and Layer 4 (TCP/UDP).

Types of Firewalls

TypeDescriptionExample
Packet filterStateless, examines individual packetsBasic ACLs
StatefulTracks connection stateiptables, pf
Application (L7)Inspects application dataWAF, next-gen firewalls
Host-basedRuns on individual hostsiptables, Windows Firewall
Network-basedDedicated appliance/devicepfSense, Cisco ASA

Stateful vs Stateless

  • Stateless: Each packet evaluated independently. Must explicitly allow both directions.
  • Stateful: Tracks connections. Return traffic automatically allowed for established connections.

Linux Firewalls

  • iptables - Traditional Linux firewall, widely documented
  • nftables - Modern replacement for iptables, cleaner syntax
  • UFW - Simplified frontend for iptables (Ubuntu/Debian)
  • firewalld - Zone-based frontend (RHEL/Fedora)

iptables Chains

  • INPUT - Incoming traffic destined for host
  • OUTPUT - Outgoing traffic from host
  • FORWARD - Traffic passing through (routing)

macOS/BSD: pf (Packet Filter)

OpenBSD’s packet filter, also used on macOS and FreeBSD.

Common Firewall Rules Pattern

  1. Default deny incoming
  2. Allow all outgoing
  3. Allow established/related incoming (stateful)
  4. Allow loopback
  5. Allow ICMP (optional)
  6. Allow specific services (SSH, HTTP, etc.)
  7. Log dropped packets

Security Best Practices

  • Default deny - Block everything, allow only what’s needed
  • Principle of least privilege - Minimal necessary access
  • Limit SSH access - Restrict by IP or use fail2ban
  • Rate limiting - Prevent brute force and DDoS
  • Log and monitor - Track blocked traffic
  • Keep rules simple - Complex rules lead to mistakes
  • Regular audits - Review and clean up rules

References