TExcelTAD
Home › Network Security: Discovery, Segmentation, Firewalls, VPN & IDS › Stateless versus Stateful Filtering

Stateless versus Stateful Filtering

A server makes an outbound HTTPS request to download updates. The reply comes back from 192.0.2.80:443 to some random high port on the server, say 50514. Should the firewall let it in? A stateless filter judges each packet alone and can't know that 50514 is a reply: either it opens all high ports (and with them every service on a high port) or it drops the replies and breaks the download. A stateful filter remembers that the server started a connection to 192.0.2.80:443 and admits exactly the packets that belong to it. This difference is the foundation of every firewall design in this section.

Watching the difference

The lab runs the same eight packets ("replies and odd packets": replies to the server's own connections, an ICMP error, a stray ACK, two scans and an admin login) through two rule sets on the lab server. Set 0 ("web + ssh from admin net") has only port rules, like a stateless filter. Set 2 ("stateful") starts with connection-tracking rules.

chain input { type filter hook input priority 0; policy drop; } on 10.10.0.101tcp dport { 80, 443 } accept2 pkts2ip saddr 10.10.0.0/24 tcp dport 22 accept1 pkt3ip protocol icmp accept1 pkt—policy drop (no rule matched)4 pktspackets (source → protocol/destination port, conntrack state)#1203.0.113.7 → tcp/443 establishedHTTPS session✓ accept (rule 1)#2192.0.2.80 → tcp/50514 establishedreply to our download✕ drop (policy)#3192.0.2.53 → udp/40123 establishedreply to our DNS query✕ drop (policy)#4192.0.2.1 → icmp relatedICMP error for us✓ accept (rule 3)#5198.51.100.9 → tcp/443 invalidstray ACK✓ accept (rule 1)#6198.51.100.9 → tcp/23 newtelnet scan✕ drop (policy)#7198.51.100.9 → tcp/445 newSMB scan✕ drop (policy)#810.10.0.21 → tcp/22 newadmin SSH✓ accept (rule 2)
Packet Set 0 (port rules) Set 2 (stateful)
#1 HTTPS session packet to 443, established accept (rule 1, port 443) accept (rule 1, established)
#2 reply to our download, to 50514 drop (policy): the download breaks accept (established)
#3 reply to our DNS query, UDP 40123 drop (policy) accept (established)
#4 ICMP error about our connection accept (rule 3, any ICMP) accept (related)
#5 stray ACK to 443, no connection accept (rule 1: port 443 matches) drop (rule 2, invalid)
#6, #7 Telnet and SMB scans drop (policy) drop (policy)
#8 admin SSH from 10.10.0.21 accept (rule 2) accept (rule 4)

Set 0 gets it wrong in both directions: it drops legitimate replies (4 accepted, 4 dropped) and accepts a packet that belongs to no connection, because it only looks at the port. Set 2 accepts the four packets that belong to real conversations and the allowed new admin login, and drops the three that don't (5 accepted, 3 dropped). Switch the rule set in the lab to see both runs.

What "state" means

Linux's connection tracker (conntrack, used by nftables and iptables) keeps an entry for every flow it has seen, keyed by protocol, addresses and ports, and classifies each packet:

State Meaning
new the first packet of a flow not yet in the table (for TCP normally a SYN)
established a packet of a flow that has seen traffic in both directions (or the reply direction of one we started)
related a new flow that belongs to an existing one: ICMP errors about it, FTP data connections with a helper
invalid a packet that fits no known flow and can't start one (a stray ACK, wrong sequence window, malformed)
untracked explicitly exempted from tracking (notrack)

UDP and ICMP have no connections, so the tracker uses pseudo-state: a UDP datagram from A to B creates an entry, and B's reply within the timeout counts as established.

A connection tracker in miniature

The program is a toy version of the tracker: it records flows the server starts, classifies each incoming packet, and applies the stateful rule set. Real conntrack also follows TCP sequence numbers, timeouts and the full state machine (The TCP Handshake and State Machine).

python · try it yourself
SERVER = "10.10.0.10"
table = set()      # (proto, local_port, remote_ip, remote_port) of flows we know

def outbound(proto, lport, rip, rport):
    table.add((proto, lport, rip, rport))
    return f"out {proto} {SERVER}:{lport} -> {rip}:{rport}  (tracked)"

def classify(proto, src, sport, dport, flags=""):
    key = (proto, dport, src, sport)
    if key in table:
        return "established"
    if proto == "icmp":
        return "related" if flags == "error-for-tracked" and table else "new"   # real conntrack reads the flow quoted inside the ICMP error
    if proto == "tcp" and flags != "S":
        return "invalid"            # not a SYN and no flow: can't start a connection
    return "new"

def verdict(state, proto, src, dport):
    if state in ("established", "related"): return "accept (ct state)"
    if state == "invalid": return "drop (invalid)"
    if proto == "tcp" and dport == 443: return "accept (new HTTPS)"
    if proto == "tcp" and dport == 22 and src.startswith("10.10.0."): return "accept (admin SSH)"
    return "drop (policy)"

print(outbound("tcp", 50514, "192.0.2.80", 443))
print(outbound("udp", 40123, "192.0.2.53", 53))
incoming = [("tcp", "192.0.2.80", 443, 50514, "A"), ("udp", "192.0.2.53", 53, 40123, ""),
            ("icmp", "192.0.2.1", 0, 0, "error-for-tracked"), ("tcp", "198.51.100.9", 51000, 443, "A"),
            ("tcp", "203.0.113.7", 40000, 443, "S"), ("tcp", "198.51.100.9", 51001, 23, "S"),
            ("tcp", "10.10.0.21", 52000, 22, "S"), ("udp", "198.51.100.9", 53, 40124, "")]
for proto, src, sport, dport, flags in incoming:
    st = classify(proto, src, sport, dport, flags)
    print(f"in  {proto:4} {src:>12}:{sport:<5} -> :{dport:<5} {flags or '-':3} {st:12} {verdict(st, proto, src, dport)}")
out tcp 10.10.0.10:50514 -> 192.0.2.80:443  (tracked)
out udp 10.10.0.10:40123 -> 192.0.2.53:53  (tracked)
in  tcp    192.0.2.80:443   -> :50514 A   established  accept (ct state)
in  udp    192.0.2.53:53    -> :40123 -   established  accept (ct state)
in  icmp    192.0.2.1:0     -> :0     error-for-tracked related      accept (ct state)
in  tcp  198.51.100.9:51000 -> :443   A   invalid      drop (invalid)
in  tcp   203.0.113.7:40000 -> :443   S   new          accept (new HTTPS)
in  tcp  198.51.100.9:51001 -> :23    S   new          drop (policy)
in  tcp    10.10.0.21:52000 -> :22    S   new          accept (admin SSH)
in  udp  198.51.100.9:53    -> :40124 -   new          drop (policy)

The last line is worth a second look: a UDP packet from 198.51.100.9 pretending to be a DNS reply (source port 53) to a port next to our real query. A stateless rule like "allow UDP from port 53" would accept it; the tracker sees no matching flow and the policy drops it. Source-port rules are one of the oldest firewall mistakes for exactly this reason.

Where each kind belongs

Stateless (ACLs, notrack rules) Stateful (nftables/iptables with conntrack, commercial firewalls)
Decision per packet from headers only headers + flow table
Return traffic must be allowed by explicit rules (established keyword on TCP flags, port ranges) automatic for tracked flows
Cost tiny, line rate in hardware memory per flow; the table can be filled (DoS)
Good for core-switch ACLs, anti-spoofing at the edge, dropping obvious junk before tracking, very high-volume services trust boundaries, hosts, anything with outbound connections

Because the conntrack table is finite, a SYN flood or a fast scan can fill it (SYN Floods and SYN Cookies). Watch nf_conntrack_count against nf_conntrack_max, and for very busy public services consider stateless handling (notrack) of the flood-prone traffic with SYN cookies on the host.

How to verify statefulness in a review

  • The first rules of every chain should be ct state established,related accept and ct state invalid drop (Reading and Writing nftables Rulesets).
  • No rules that allow traffic by source port (udp sport 53 accept, tcp sport 80 accept).
  • No "allow high ports from anywhere" rules: they are a stateless workaround and they expose every high-port service.
  • Conntrack helpers (FTP, SIP) are enabled only explicitly and only where needed: automatic helpers have been abused to open ports.

Pitfalls

  • Asymmetric routing: if replies return through a different firewall, that firewall sees only half the flow, classifies it as invalid and drops it.
  • Accepting established before checking for invalid: put invalid drop right after, or merge them carefully.
  • Conntrack table exhaustion under load: every new connection is then dropped, including legitimate ones.

Practice

1. In the lab with set 0, why is the stray ACK (#5) accepted?

Rule 1 (tcp dport { 80, 443 } accept) only checks protocol and port. The packet is TCP to 443, so it matches, even though no connection exists. Set 2 classifies it as invalid and drops it.

2. A stateless edge ACL contains permit tcp any any established. What does that match, and what can an attacker do with it?

TCP segments with ACK or RST set. An attacker can send ACK packets to any port (an ACK scan maps which ports a stateless filter passes), and some stacks respond in ways that leak information. It also admits anything a stateful firewall would call invalid.

3. In the toy tracker, add outbound("tcp", 50600, "203.0.113.99", 22) before the incoming list and an incoming ("tcp", "203.0.113.99", 22, 50600, "A"). What happens and what would a real admin ask?

It is classified established and accepted. The admin should ask why the web server opened an outbound SSH connection to an Internet host: servers rarely need it, and it is a classic tunnelling or exfiltration path (Egress Filtering).

Test yourself

Why does a stateless filter struggle with replies to outbound connections?

  1. Replies are encrypted
  2. Replies arrive on random high ports that a per-packet rule can't tie to the original connection
  3. Replies use UDP
  4. Replies have no ports

In the stateful lab run, how many of the eight packets were accepted?

  1. 3
  2. 4
  3. 5
  4. 8

What conntrack state does a TCP ACK with no matching flow get?

  1. new
  2. established
  3. related
  4. invalid

Why is udp sport 53 accept dangerous?

  1. DNS is slow
  2. Anyone can send packets from source port 53 to any UDP port
  3. It blocks DNS
  4. It only works with IPv6