Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Firewalls and Segmentation

Stateful Inspection and Connection Tracking

See how a stateful firewall tracks TCP and UDP connections, the TCP state machine, timeouts and table exhaustion, by building a working connection tracker in Python.

A plain packet filter judges each packet alone. That has an awkward consequence: to let a client browse the web you would have to allow replies from any server on any high port back in, which also lets an attacker send packets to those same ports. Stateful inspection solves this by remembering. When a client inside opens a connection, the firewall writes an entry in a connection table; packets that belong to that connection are allowed, and everything unsolicited is dropped by default.

Almost every firewall you will meet, from home routers to cloud security groups, works this way. In this lesson you will see how the table is built from the TCP handshake, how UDP and ICMP get "pseudo-connections", why the table has limits (and why attackers target it), and you will build a small connection tracker that behaves like the real thing.

The idea in one picture

sequenceDiagram
    participant C as Inside client
    participant F as Stateful firewall
    participant S as Outside server
    C->>F: SYN (new, outbound allowed by policy)
    Note over F: add entry: C:51514 <-> S:443, state SYN_SENT
    F->>S: SYN
    S->>F: SYN+ACK
    Note over F: matches entry, state ESTABLISHED
    F->>C: SYN+ACK
    C->>F: ACK and data
    S-->>F: unsolicited packet to C:51515
    Note over F: no matching entry: DROP

The firewall learns from outbound packets. A reply matches an entry; a stranger does not.

What a state table entry contains

An entry identifies a connection by the 5-tuple: protocol, source address, source port, destination address, destination port. It also stores the connection's state, counters and a timeout. A reply packet has the tuple reversed, so the firewall looks up both directions as one entry.

For TCP the firewall follows the connection through its life:

State Meaning
SYN_SENT A SYN was seen from the initiator; nothing back yet
SYN_RECV The server's SYN+ACK was seen
ESTABLISHED The handshake finished; data flows both ways
FIN_WAIT, CLOSE_WAIT, TIME_WAIT One or both sides sent FIN; closing
CLOSED / removed Both FINs or an RST seen; entry deleted after a short wait

A good tracker also checks sequence numbers: a packet whose sequence number is far outside the window does not belong to the connection, even if the addresses match. That stops "blind" injection of forged packets into an existing connection.

UDP and ICMP: no connections, but still state

UDP has no handshake, so the firewall creates an entry when it sees the first outbound packet and keeps it for a timeout (for DNS, typically a few seconds; for longer flows, tens of seconds). A reply that arrives from the expected address and port within that time matches the entry. ICMP echo replies are matched against the earlier echo requests. Related errors (such as "destination unreachable" for a tracked connection) are classed as related and allowed.

Some protocols open extra connections that are announced inside the data stream, such as active FTP. Firewalls handle these with application helpers that read the control channel and open a short-lived pinhole for the data connection. Helpers are powerful but add attack surface; modern protocols avoid them.

Building a connection tracker

The program below implements a miniature stateful firewall. Outbound connections from the inside are allowed, a state table records them, replies match the table, and unsolicited inbound packets are dropped. Time is simulated by a counter so the timeout behaviour is visible. Read the code as an explanation of the idea, not as a production design.

Python A miniature stateful firewall

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two stranger packets are dropped, even though one targets a host that is allowed to browse, because nothing in the table matches them. The final late DNS answer is also dropped: the UDP entry expired. This is why scanning an internal address from the internet through a stateful firewall finds nothing.

The table is a finite resource

State costs memory, and every firewall has a maximum number of entries. Attackers know it. A SYN flood creates thousands of half-open entries; a flood of random UDP creates many short-lived ones; both can fill the table so legitimate connections are refused. Defences include:

  • SYN cookies and SYN proxying: the firewall answers the handshake itself without storing state until the client proves it is real.
  • Per-source limits: cap new connections per second and total entries per address.
  • Shorter timeouts for half-open states and generous timeouts only for established ones.
  • Table sizing and monitoring: alert when usage passes, say, 70%.

The next program models table exhaustion with and without a per-source limit so you can see why the limit matters.

Python Table exhaustion and a per-source limit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without a limit the attacker fills the table and honest users are refused; with a per-source cap, the attacker consumes only a small slice. Real firewalls combine this with SYN cookies and upstream filtering for floods that come from many sources.

Stateful is not the same as safe

A stateful firewall decides on the connection, not on the content. A client allowed to reach port 443 can still download malware over it. State tables also assume that inside is trusted: a compromised internal host can initiate connections out to a command server, and the firewall will faithfully allow the replies. That is why egress rules, proxies and monitoring exist, and why the next lessons add segmentation and egress control.

Asymmetric routing breaks stateful firewalls. If the outbound packets leave through firewall A and the replies come back through firewall B, B has no entry and drops the reply. Clustered firewalls synchronise their tables for this reason.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a stateful firewall do when a packet arrives from the internet that matches no table entry and no allow rule?

    Unsolicited inbound packets have no matching connection, so default deny drops them.
  2. 2. How does a stateful firewall treat UDP, which has no handshake?

    The first outbound UDP packet creates an entry with a timeout, and replies matching that entry within the time are allowed.
  3. 3. Why do SYN floods threaten stateful firewalls?

    Each half-open connection occupies a table entry, so a flood can exhaust it and block legitimate users.
  4. 4. A stateful firewall allows a client to open an outbound connection to a malware server on port 443. Why?

    Stateful inspection tracks connections but does not judge intent. Egress filtering, proxies and monitoring address that.

Summary

  • A stateful firewall records connections in a table keyed by the 5-tuple and allows matching replies while dropping unsolicited packets.
  • TCP states follow the handshake and teardown; UDP and ICMP use timeouts and request/reply matching.
  • The table is finite: SYN floods and similar attacks target it, so use SYN cookies, per-source limits and monitoring.
  • Application helpers open extra pinholes for protocols like active FTP but add attack surface.
  • State tracking does not inspect content or intent; combine with egress control, proxies and detection.

Next: Network Segmentation, VLANs and the DMZ