What it means
A firewall stands on a boundary between networks and decides which traffic may cross. A simple packet filter judges each packet alone by its addresses, ports and protocol. A stateful firewall also remembers the connections it has allowed, in a state table. A reply to an inside request can then return without a rule of its own. An unrequested packet that only looks like a reply is dropped.
Rules are read in order, and on many firewalls the first match decides. The last rule should deny everything that no earlier rule allowed. A firewall sees addresses and ports. Unless it also ends the encrypted session, it cannot read what an allowed connection carries.
See the idea / Networks
A reply gets in by matching a state row, not a rule.
The update server opens a connection and its reply comes back in. Then an outside host tries a port that no rule allows.
Station North bench. A stateful firewall sits between the update server, 192.0.2.10, and the internet. It keeps a state table with one row for each connection it has allowed. The server fetches updates from a mirror at 203.0.113.80. The firewall reads four rules from the top, and the first match decides. Rule 1 lets the server open connections to port 443. Rule 2 lets outside hosts open connections to the server's ports 80 and 443. Rule 3 blocks one known bad host. Rule 4 denies everything else. A SYN opens a TCP connection, a SYN-ACK accepts it, and an ACK confirms.
01 / 04
Outbound SYN
The update server sends a SYN to the mirror at 203.0.113.80, port 443. No state row exists, so the firewall reads its rules. Rule 1 allows it.
More about this step
The firewall reads five values from each packet: both addresses, both ports and the protocol. The SYN leaves from port 50514 on the server. The firewall looks in the state table first and finds nothing, because this is a new connection. It then reads the rules from the top. Rule 1 matches an outbound connection from the server to port 443, so no later rule is read.
Step through at your own pace.
Keep in mindThis is one bench with four rules and first-match order. Some firewalls order rules differently. The firewall reads addresses, ports and protocol. It does not read what is inside an encrypted connection, and it cannot tell whether an allowed connection is harmless. Address translation, row timeouts and reject responses are not shown.
Read the visual, step by step
Server calls out, reply returns
- Outbound SYN. The update server sends a SYN to the mirror at 203.0.113.80, port 443. No state row exists, so the firewall reads its rules. Rule 1 allows it. The firewall reads five values from each packet: both addresses, both ports and the protocol. The SYN leaves from port 50514 on the server. The firewall looks in the state table first and finds nothing, because this is a new connection. It then reads the rules from the top. Rule 1 matches an outbound connection from the server to port 443, so no later rule is read.
- State row. Because it allowed a new connection, the firewall writes a row: both addresses, both ports, the protocol, and the state Initiated. The row is 192.0.2.10 port 50514, 203.0.113.80 port 443, TCP, Initiated. It describes this one conversation. It is not a rule and it opens nothing else.
- Reply. The mirror's SYN-ACK arrives for port 50514. No rule allows that port inbound. The packet matches the row, so it passes. The firewall checks the state table before the rules. The SYN-ACK has the same addresses and ports as the row, reversed, so it is return traffic. The rule list is not read for this packet. Return traffic that matches a row needs no rule of its own.
- Established. The server's ACK goes out and completes the handshake. The row changes to Established. The ACK also matches the row. From now on packets in either direction of this conversation pass by matching it. The row ends when the connection closes or after a long time with no traffic.
Unrequested SYN to port 22
- Inbound SYN. An outside host at 198.51.100.20 sends a SYN to the server's port 22. Nothing inside asked for it. The packet comes from 198.51.100.20 port 51201 to 192.0.2.10 port 22. Port 22 is commonly used for remote administration. The firewall has not decided anything yet.
- State table. The firewall checks its state table first. No row matches this conversation. A row would exist only if the server had opened a connection to this host and port, or a rule had already allowed one. Neither happened.
- Rules. It reads the rules from the top. Rule 1 covers only outbound calls, rule 2 only ports 80 and 443, and rule 3 only another host. Rule 4 matches. Rule 4 is the default deny. It matches any packet that no rule above it allowed. An allow rule opens only the ports it names, so allowing 443 did nothing for 22.
- Drop. The firewall drops the packet and sends nothing back. The server never sees it. The firewall log records the drop: 198.51.100.20:51201 to 192.0.2.10:22, TCP, rule 4. With drop, the sender waits and retries. A firewall set to reject would send a reset so the sender learns at once.
This is one bench with four rules and first-match order. Some firewalls order rules differently. The firewall reads addresses, ports and protocol. It does not read what is inside an encrypted connection, and it cannot tell whether an allowed connection is harmless. Address translation, row timeouts and reject responses are not shown.
Flow references: NIST SP 800-41 Rev. 1: stateful inspection and deny by default · RFC 9293: TCP connection establishment
AN ILLUSTRATIVE SCENARIO
A small accounting firm
An accounting firm has one firewall between its office and the internet. Years of quick fixes have left 60 rules, including one that allows remote desktop from anywhere to a server that was retired. A new partner asks what each rule is for. Nobody knows for a third of them. The firm records an owner and a reason for every rule and removes the ones with neither. It moves the default deny to the end, where it belongs.
Put it to work
- Write down what must cross each boundary: which source, which destination, which port, and why. Allow that and nothing else.
- End every rule list with a rule that denies and logs all remaining traffic. Record an owner and a reason for each allow rule.
- Review the rules on a schedule. Remove any rule whose system, owner or reason no longer exists.
How to check your work
From outside, test that a port you allow answers and a port you do not allow is dropped or refused. Then read the firewall log and confirm the blocked attempt appears with the rule that stopped it.
Connect the ideas
- Network segmentation
Splitting a network into zones by purpose and trust, with a check on each boundary. A compromise in one zone then cannot freely reach the others.
- Proxy
A server that makes connections on behalf of others. A forward proxy stands in for inside clients going out, and a reverse proxy stands in front of servers taking calls from outside.
- WAF
A control that inspects web application traffic and applies rules to detect or block unwanted requests, often including known attack patterns.
- Defense in depth
Layering independent controls so that one failure does not become a complete compromise.