Labs / Network Security / Firewalls

Lab 3 of 3 · Firewalls

Stateful inspection: a firewall that remembers who started it

Give a firewall a memory, and let in only real replies to your own requests.

Start this lab 5 steps

The idea

Somebody outside read the Ridge Observatory's unpublished plates last night, through a Firewall where every rule looks right. Find the hole, then teach ridgeGate to remember who started each conversation.

A request wearing a reply's port
A request wearing a reply's port
Same port, different story
Same port, different story
The ban only meets new conversations
The ban only meets new conversations
A list written as who may start
A list written as who may start
An error about a conversation
An error about a conversation

Steps

  1. A reply rule anyone can use

    Anouk runs the network at the Ridge Observatory, and ridgeGate is the firewall between its three networks. campus holds nightDesk and the plateArchive. gallery holds the public skyServer.

    outside is everybody else: a visitor, and the starCatalogue, a web server.

    A packet filter, the plainest firewall, reads each packet alone: the addresses, the protocol and the ports. It decides and forgets, which makes it fast and simple, and the trouble starts the moment a conversation has two directions.

    nightDesk asks starCatalogue for a page. The request goes out to port 80, and a rule can name that. The answer comes back from port 80 to whichever port nightDesk picked a moment ago, and a rule can't name a port nobody has picked yet.

    So people wrote the rule they could write: accept whatever comes from port 80, because that must be a web server answering. Anouk's list does, with --sport 80 in two places. Read it first, and try the archive from visitor.

    Now look harder at that "must". The source port is a number the sender writes on its own packet, and curl --local-port 80 writes 80. Try the archive that way, while the lab records what reaches plateArchive.

    ridgeGate:~# iptables -L FORWARD -v -n --line-numbers
    nightDesk:~# curl -sS -m 3 http://203.0.113.20/
    visitor:~# curl -sS -m 3 http://10.41.1.20:9200/
    visitor:~# curl -sS -m 3 --local-port 80 http://10.41.1.20:9200/
  2. Remember the conversation

    A packet filter asks "does this packet look allowed?". A stateful firewall asks "am I expecting this packet?", which takes a memory.

    Check Point named the idea in the early 1990s: stateful inspection. The firewall keeps a table of every conversation passing through, written when the first packet goes by: both addresses, both ports, the protocol.

    Linux calls its table connection tracking, or conntrack. It labels every packet before the chains read it, and a rule reads the label with -m conntrack --ctstate: NEW for a packet that starts a conversation, or belongs to one nobody has answered yet, and ESTABLISHED for a packet of a conversation that has seen traffic both ways.

    One line does the work of every reply rule Anouk wrote, and it goes on line 1. Add it, and read the list. Then delete the two --sport 80 rules with iptables -D FORWARD and a line number, the lower one first, because deleting a line moves up everything under it.

    ridgeGate:~# iptables -v -I FORWARD 1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    ridgeGate:~# iptables -L FORWARD -v -n --line-numbers
    ridgeGate:~# iptables -D FORWARD 5
    ridgeGate:~# iptables -D FORWARD 3
    nightDesk:~# curl -sS -m 3 http://203.0.113.20/
    visitor:~# curl -sS -m 3 --local-port 80 http://10.41.1.20:9200/
    ridgeGate:~# iptables -L FORWARD -v -n --line-numbers
  3. Read the firewall's memory

    The table isn't hidden. conntrack -L lists it, one line per conversation.

    Each line holds the conversation twice. The first half, src= and dst= with their ports, is the first packet as the firewall saw it, so it names who started. The second half is what an answer must look like.

    Near the front, a number gives the seconds left before the line is forgotten, unless another packet renews it. A word gives TCP's progress: SYN_SENT for an opening still waiting, ESTABLISHED once both sides have spoken, TIME_WAIT for a finished one fading out. [UNREPLIED] marks a line nobody has answered, and [ASSURED] one with traffic both ways.

    nightDesk has fetched the catalogue's page, tried 203.0.113.99, which doesn't answer, and started pulling a huge sky survey, sky.fits, a trickle at a time. Anouk bans nightDesk from the outside world until she knows why, with a DROP rule at line 2.

    Add it, test the machine, and read the table. Last, conntrack -D deletes lines from the table, and -s picks them by sender.

    ridgeGate:~# conntrack -L
    ridgeGate:~# iptables -v -I FORWARD 2 -s 10.41.1.10 -j DROP
    nightDesk:~# curl -sS -m 3 http://203.0.113.20/
    ridgeGate:~# conntrack -L
    ridgeGate:~# conntrack -D -s 10.41.1.10
    ridgeGate:~# conntrack -L -s 10.41.1.10
  4. Who started it decides

    Every conversation sends packets both ways, so "traffic from gallery to campus" means little. "Who may start a conversation" means everything, and with the memory on line 1 a firewall can be written in those words.

    Anouk's policy: campus may start anything, toward gallery and outside. outside may start port 80 on gallery, and nothing else. gallery may start nothing.

    That last line is why gallery exists. skyServer takes packets from strangers all day, so it's the machine most likely to be broken into, and whoever breaks in should find nothing to open from there. The layout is a screened subnet, often called a DMZ, after the strip of land between two armies.

    A rule can name cards as well as addresses: -i campus matches packets that came in on the campus card, and -o outside packets leaving on outside. Write the campus lines with both, and with --ctstate NEW, which says out loud that a rule is about starting. Then test each direction, the way an auditor would.

    ridgeGate:~# iptables -v -R FORWARD 2 -i campus -o outside -m conntrack --ctstate NEW -j ACCEPT
    ridgeGate:~# iptables -v -A FORWARD -i campus -o gallery -m conntrack --ctstate NEW -j ACCEPT
    nightDesk:~# curl -sS -m 3 http://10.41.2.10/
    skyServer:~# curl -sS -m 3 http://10.41.1.20:9200/
    visitor:~# curl -sS -m 3 http://10.41.2.10/
    visitor:~# curl -sS -m 3 http://10.41.1.20:9200/
    ridgeGate:~# iptables -L FORWARD -v -n --line-numbers
  5. What RELATED is for

    Two kinds of packet don't fit the story so far.

    The first is UDP, which has no opening and no goodbye. Conntrack writes it down anyway: an outgoing packet makes a line, an answer to the same addresses and ports fits it, and a timer ends it about 30 seconds after the last packet, longer once traffic has gone both ways for a while. Ask the name server on starCatalogue a question, and read the table.

    The second is the error message: nothing listens on port 5300 there, so a question sent to it gets an ICMP "port unreachable" back. That message has its own protocol and no ports, so it isn't a packet of the conversation. It does carry a copy of the packet it complains about, which tells conntrack which line it's about.

    You've typed RELATED on line 1 with no reason given. Ask port 5300, then take the word out, ask again, and put it back.

    nightDesk:~# dig +short +time=2 +tries=1 @203.0.113.20 vega.catalogue.lab
    ridgeGate:~# conntrack -L -p udp
    nightDesk:~# dig +time=2 +tries=1 -p 5300 @203.0.113.20 vega.catalogue.lab
    ridgeGate:~# iptables -v -R FORWARD 1 -m conntrack --ctstate ESTABLISHED -j ACCEPT
    nightDesk:~# dig +time=2 +tries=1 -p 5300 @203.0.113.20 vega.catalogue.lab
    ridgeGate:~# iptables -v -R FORWARD 1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    nightDesk:~# dig +time=2 +tries=1 -p 5300 @203.0.113.20 vega.catalogue.lab

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab