Cryptography / Firewalls

Firewalls

How a firewall decides which network traffic gets through: rules on address, port and protocol, default deny, remembering connections, reading web requests, and what it cannot see.

Beginner7 steps~7 min
Outside host — any machine on the internet
Firewall — checks every packet that crosses
Packet — a piece of traffic with a label
Inside machine — the server or laptop being protected
// THE SHORT VERSION

In 30 seconds

A firewall stands between networks and decides, packet by packet, what may pass. Its rules name addresses, ports and protocols, and the safe design allows a few things and denies everything else.

A stateful firewall also remembers which connections were started from inside, so replies get in and unrequested packets do not. A web application firewall goes further and reads the requests themselves.

It is one layer, not the whole defence: it cannot judge harm inside traffic it allows, or anything that never passes through it.

Firewalls, step by step

STEPS01 / 07

Simplified: real rule sets also match direction, network interface and TCP flags, and a WAF has to decrypt HTTPS before it can read a request. The addresses are from ranges reserved for documentation.

STEP 01 / 07 A gate every packet crosses
THE MATH
packet label: source, destination, port, protocol
  1. Step 1: A gate every packet crosses. A firewall sits where two networks meet, so all traffic between them has to pass through it. Data travels in packets, and each packet carries a label: the address it came from, the address it is going to, a port number that picks out a service on that machine, and a protocol such as TCP.
  2. Step 2: Rules: address, port, protocol. The firewall holds a list of rules and checks each packet against them from the top; the first rule that matches decides. This packet is TCP for the web server’s port 443, so rule 1 lets it through. Rule 2 lets only the office network reach port 22, which is used for remote administration.
  3. Step 3: Default deny. A packet for port 3306, where a database listens, matches neither allow rule. It falls through to the last rule, which refuses everything. This is default deny: rather than listing what is forbidden, you list the few things that are permitted and block the rest, including whatever nobody thought of.
  4. Step 4: Stateless: no memory. A laptop inside asked a web server for a page, and the reply comes back to a temporary port, 51514. A stateless filter judges every packet alone and remembers nothing, so it needs a broad rule admitting anything from port 443 to high ports. A stranger’s packet dressed up as a reply gets in as well.
  5. Step 5: Stateful: remember requests. A stateful firewall keeps a table of the connections it has seen start from inside. When the laptop’s request left, it noted both addresses and both ports. The real reply matches that entry and passes. The stranger’s packet matches nothing and is dropped. Nearly every firewall in use today works this way.
  6. Step 6: Reading the request: a WAF. To a packet filter, every request to a website looks alike: TCP, port 443, allowed. A web application firewall (WAF) stands in front of the web server and reads the requests themselves, blocking ones that match known attack patterns. It is a safety net; the application still has to be written safely.
  7. Step 7: What a firewall cannot stop. A firewall only judges traffic that passes through it, and only by what it can see. It cannot spot harm inside traffic it allows, such as a convincing phishing email or a login with a stolen password. It cannot stop a device that is already infected inside the network, or anything that takes a path around it.
VALUES SO FAR
source203.0.113.9address
dest192.0.2.10address
rule 1····allow
rule 2····allow
rule 3····default
packet····stateless
table····stateful
WAF····layer 7
limit····caveat

Spotted a mistake on this page?