SkillPilotAI

Firewall: stateless vs stateful, ACL and DMZ

A firewall matches packets against rules and allows or drops them. Knowing the difference between stateless, stateful and application firewalls, how ACL order works, and how zones and a DMZ are designed is core network security and a very common interview topic.

95.1 Why is it used?

  • A box with no rules forwards every packet: an attacker's SYN to port 22 of the office PC goes straight through.
  • Without a firewall every service in the office is open to the whole world.
  • A firewall checks each packet against an ACL (Access Control List) and allows only what is needed.

95.2 How it works: stateless vs stateful vs application

TypeWhat it looks atExample
Packet filter (stateless)Each packet alone: IP, port, protocolRouter ACL
Stateful firewallHeader + connection state (state table / conntrack)iptables with conntrack, almost every modern firewall
Application firewall (L7)URL, headers, bodyWAF, next-generation firewall (NGFW)
  • Stateless ACL example: rule 1 allows dport 80 LAN -> internet, rule 2 allows sport 80 back in for replies, everything else dropped.
  • Weakness: rule 2 only checks the source port, so an attacker sending from source port 80 to port 22 is allowed.
  • A stateful firewall creates an entry when the PC starts a connection; replies match it as ESTABLISHED.
  • An attacker's NEW packet matches no entry and no allow rule, so default deny drops it.
Stateful firewall vs the source port 80 trick
  1. 1.Office PC sends SYN :80 to the website: conntrack creates an entry

  2. 2.Website's SYN-ACK matches the entry: ESTABLISHED, allowed

  3. 3.Attacker 203.0.113.50 sends SYN from sport 80 to PC port 22

  4. 4.No conntrack entry, state is NEW, no rule allows it

  5. 5.Default deny: DROP at the firewall

95.3 ACL order and default deny

  • Rules are read top to bottom.
  • First match wins: the remaining rules are not checked.
  • Specific rules (exceptions) on top, general rules below.
  • Default deny last: anything not clearly allowed is dropped, so a forgotten rule never leaves a port open.

95.4 Zones and DMZ design

  • The firewall splits the network into zones with different trust: WAN (internet, zero trust), DMZ (public servers), LAN (trusted PCs, database).
  • Internet -> DMZ: only port 80 of the web server 172.16.115.80.
  • DMZ -> LAN: only web server -> database 192.168.115.20 port 5432.
  • LAN -> DMZ: allowed (admin traffic); WAN -> LAN: dropped.
DMZ: a hacked web server still cannot reach the LAN
  1. 1.Internet user -> web server :80 in the DMZ: allowed

  2. 2.Internet -> database :5432 in the LAN: WAN to LAN, DROP

  3. 3.Web server -> database :5432: the one DMZ to LAN rule, allowed

  4. 4.Hacked web server -> office PC :22: DMZ to LAN, DROP

  5. 5.Office PC -> web server :22 (admin): LAN to DMZ, allowed by the network firewall

The DMZ is a buffer zone between the internet and the LAN

95.5 Host firewall vs network firewall

PointNetwork firewallHost firewall
WhereAt the network edge, between zonesInside each machine
ExamplesLinux router with iptables, hardware firewallWindows Defender Firewall, iptables/ufw, macOS application firewall
ProtectsTraffic between zonesThat one host, even from its own zone
In the labAllows LAN -> DMZWeb server still drops port 22

Using both is called defense in depth.

95.6 Practical: stateless ACL and the source port trick

Lab: one Docker container with namespaces for LAN, DMZ and internet, and a Linux firewall (no NAT) in the middle.

Terminal
iptables -L FORWARD -n --line-numberscurl -s -m 3 --http0.9 http://203.0.113.80/nc -zv -w 2 -p 80 192.168.115.10 22nc -zv -w 2 192.168.115.10 22
Output
fw$ iptables -L FORWARD -n --line-numbersChain FORWARD (policy DROP)num  target  prot  opt  source     destination1    ACCEPT  6     --   0.0.0.0/0  0.0.0.0/0  tcp dpt:802    ACCEPT  6     --   0.0.0.0/0  0.0.0.0/0  tcp spt:80pc$ curl -s -m 3 --http0.9 http://203.0.113.80/hello from internet siteinet$ nc -zv -w 2 -p 80 192.168.115.10 22Connection to 192.168.115.10 22 port [tcp/ssh] succeeded!inet$ nc -zv -w 2 192.168.115.10 22nc: connect to 192.168.115.10 port 22 (tcp) timed out: Operation in progress
  • With -p 80 (source port 80) the attacker reached SSH on the PC: rule 2 matched.
  • Without a source port the same connection timed out (policy DROP).

95.7 Practical: first match wins

Output
fw$ iptables -L FORWARD -n --line-numbersnum  target  prot  opt  source        destination1    ACCEPT  6     --   0.0.0.0/0     0.0.0.0/0  tcp dpt:802    ACCEPT  6     --   0.0.0.0/0     0.0.0.0/0  tcp spt:803    DROP    0     --   203.0.113.50  0.0.0.0/0inet$ nc -zv -w 2 -p 80 192.168.115.10 22Connection to 192.168.115.10 22 port [tcp/ssh] succeeded!fw$ iptables -L FORWARD -n --line-numbersnum  target  prot  opt  source        destination1    DROP    0     --   203.0.113.50  0.0.0.0/02    ACCEPT  6     --   0.0.0.0/0     0.0.0.0/0  tcp dpt:803    ACCEPT  6     --   0.0.0.0/0     0.0.0.0/0  tcp spt:80inet$ nc -zv -w 2 -p 80 192.168.115.10 22nc: connect to 192.168.115.10 port 22 (tcp) timed out: Operation in progressfw$ iptables -L FORWARD -n -v --line-numbersChain FORWARD (policy DROP 2 packets, 120 bytes)num  pkts  bytes  target  prot  opt  in    out   source        destination1    2     120    DROP    0     --   wan+  *     203.0.113.50  0.0.0.0/02    10    633    ACCEPT  6     --   lan+  wan+  0.0.0.0/0     0.0.0.0/0  tcp dpt:803    13    701    ACCEPT  6     --   wan+  lan+  0.0.0.0/0     0.0.0.0/0  tcp spt:80
  • A DROP for 203.0.113.50 appended at the end (-A) did nothing: rule 2 matched first.
  • The same rule inserted at line 1 (-I) blocked the attack; -v counters show 2 packets dropped.

95.8 Practical: stateful rules with zones and DMZ

Output
fw$ iptables -S FORWARD-P FORWARD DROP-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT-A FORWARD -i lan+ -o wan+ -j ACCEPT-A FORWARD -i lan+ -o dmz -j ACCEPT-A FORWARD -d 172.16.115.80/32 -i wan+ -o dmz -p tcp -m tcp --dport 80 -j ACCEPT-A FORWARD -s 172.16.115.80/32 -d 192.168.115.20/32 -i dmz -o lan+ -p tcp -m tcp --dport 5432 -j ACCEPTinet$ nc -zv -w 2 -p 80 192.168.115.10 22nc: connect to 192.168.115.10 port 22 (tcp) timed out: Operation in progressinet$ curl -s -m 3 --http0.9 http://172.16.115.80/DMZ web pageinet$ nc -zv -w 2 192.168.115.20 5432nc: connect to 192.168.115.20 port 5432 (tcp) timed out: Operation in progressweb$ nc -zv -w 2 192.168.115.20 5432Connection to 192.168.115.20 5432 port [tcp/postgresql] succeeded!web$ nc -zv -w 2 192.168.115.10 22nc: connect to 192.168.115.10 port 22 (tcp) timed out: Operation in progress
  • First rule: conntrack RELATED,ESTABLISHED, so replies need no wide sport rule; the source port trick now times out.
  • Zone rules then allow only LAN -> WAN/DMZ, WAN -> web :80, DMZ web -> DB :5432.
  • conntrack -L -p tcp on the firewall shows the state table (TIME_WAIT, CLOSE entries).

95.9 Practical: host firewall and Windows rules

Output
web$ iptables -L INPUT -n --line-numbersChain INPUT (policy DROP)num  target  prot  opt  source     destination1    ACCEPT  0     --   0.0.0.0/0  0.0.0.0/02    ACCEPT  0     --   0.0.0.0/0  0.0.0.0/0  ctstate RELATED,ESTABLISHED3    ACCEPT  6     --   0.0.0.0/0  0.0.0.0/0  tcp dpt:80pc$ nc -zv -w 2 172.16.115.80 22nc: connect to 172.16.115.80 port 22 (tcp) timed out: Operation in progresspc$ curl -s -m 3 --http0.9 http://172.16.115.80/DMZ web page
PowerShell
Get-NetFirewallRule -Name CoreNet-DHCP-In,    FPS-ICMP4-ERQ-In |  Format-List Name, DisplayName, Enabled,    Direction, Action, ProfileGet-NetFirewallRule -Name CoreNet-DHCP-In |  Get-NetFirewallPortFilter |  Format-List Protocol, LocalPort, RemotePort
Output
Name        : CoreNet-DHCP-InDisplayName : Core Networking - Dynamic Host Configuration Protocol (DHCP-In)Enabled     : TrueDirection   : InboundAction      : AllowProfile     : Any Name        : FPS-ICMP4-ERQ-InDisplayName : File and Printer Sharing (Echo Request - ICMPv4-In)Enabled     : FalseDirection   : InboundAction      : AllowProfile     : Private, Public Protocol   : UDPLocalPort  : 68RemotePort : 67
  • The web server's INPUT chain (policy DROP) allows only loopback, ESTABLISHED and port 80, so port 22 times out even from the LAN.
  • Get-NetFirewallRule only reads: the DHCP-In rule is enabled, the ping (Echo Request) rule is disabled; DHCP uses UDP local 68, remote 67.

95.10 Common mistakes

  • Appending a block rule (-A) below a broad allow: it never matches; insert it on top (-I).
  • Stateless reply rules like 'allow sport 80': attackers can fake the source port.
  • Thinking a firewall makes everything safe: SQL injection arrives inside allowed HTTPS on port 443; you need a WAF and secure code.
  • Putting public servers in the LAN instead of a DMZ: one hacked server exposes everything.

95.11 Interview tips

  • Stateless: each packet alone, fast, but needs wide reply rules that can be misused.
  • Stateful: keeps a connection table and only allows packets of allowed connections (RELATED,ESTABLISHED).
  • Bonus: first match wins, and always end with default deny.
  • DMZ design: if the web server is hacked, the attacker reaches only DB port 5432; the rest of the LAN stays safe.
  • Host + network firewall = defense in depth; L7 attacks need a WAF.