Firewalls and proxies: rule order, stateful filtering, and what each layer can see
How packet filters, stateful firewalls, forward proxies, reverse proxies, and web application firewalls decide what passes, and how to read a rule set for the gaps.
Diagram
A request through three layers of filtering
Step 1 of 4
Host firewall
nftables or Windows Defender Firewall decides which local ports accept connections.
sudo nft list ruleset | head -20
chain input { type filter hook input priority 0; policy drop;
ct state established,related accept
tcp dport 22 ip saddr 10.0.5.0/24 accept }accept, with a few deny rules
Exposed the moment it starts
Hard: you must know every bad port
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Rule order decides
- What fails
- Most firewalls stop at the first match, so a broad allow above a narrow deny wins.
- Who is affected
- Every service behind that rule.
- What you would notice
- An “any any accept” rule near the top of a list.
- What you change
- Order specific rules first, end with an explicit deny, and review hit counts.
Egress matters as much as ingress
- What fails
- Malware and data theft go outbound. Many networks filter only inbound.
- Who is affected
- Data on any host that can reach the internet freely.
- What you would notice
- Servers with an unrestricted outbound policy.
- What you change
- Allow outbound only to update, logging, and partner destinations a server needs.
A proxy sees what it terminates
- What fails
- A reverse proxy that terminates TLS can inspect HTTP. A network firewall in front of TLS cannot.
- Who is affected
- Teams that assume the firewall blocks web attacks.
- What you would notice
- Expecting a layer 4 rule to stop an injection in a POST body.
- What you change
- Fix the application, and use the WAF for virtual patching while the fix ships.
Stateless, stateful, and application-aware
A stateless filter judges each packet alone, so return traffic needs its own rule. A stateful firewall tracks connections and lets replies through automatically. An application-aware firewall or proxy understands protocols such as HTTP and DNS and can filter on hostnames, methods, or paths.
Reviewing a rule set
Export the rules, then for each allow rule write the owner, the service, and whether it is still used. Hit counters show rules nothing matches. Look for overlapping rules, any-to-any entries, management ports open to large ranges, and outbound rules that allow everything.
$ sudo iptables -L INPUT -v -n --line-numbers num pkts bytes target prot opt in out source destination 1 9.1M 12G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 2 1204 71K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 <- SSH from anywhere 3 0 0 ACCEPT tcp -- * * 10.9.0.0/16 0.0.0.0/0 tcp dpt:8080 <- unused for months?
Forward and reverse proxies
A forward proxy sits in front of clients and controls where they may go; it is how many organizations log and filter outbound web traffic. A reverse proxy sits in front of servers, terminates TLS, and forwards to backends. Header handling at reverse proxies is where host header and request smuggling problems start.
Check yourself
Recognition and the fix. Nothing here is a procedure.
Rule 1 allows TCP 22 from any address and rule 5 denies TCP 22 from the internet. What happens to SSH from the internet?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.
This page stands on its own. The lab and the glossary card are the next reading.