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.
Steps
-
A reply rule anyone can use
Anouk runs the network at the Ridge Observatory, and
ridgeGateis the firewall between its three networks.campusholdsnightDeskand theplateArchive.galleryholds the publicskyServer.outsideis everybody else: avisitor, and thestarCatalogue, 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.
nightDeskasksstarCataloguefor a page. The request goes out to port 80, and a rule can name that. The answer comes back from port 80 to whichever portnightDeskpicked 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 80in two places. Read it first, and try the archive fromvisitor.Now look harder at that "must". The source port is a number the sender writes on its own packet, and
curl --local-port 80writes 80. Try the archive that way, while the lab records what reachesplateArchive.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/
-
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:NEWfor a packet that starts a conversation, or belongs to one nobody has answered yet, andESTABLISHEDfor 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 80rules withiptables -D FORWARDand 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
-
Read the firewall's memory
The table isn't hidden.
conntrack -Llists it, one line per conversation.Each line holds the conversation twice. The first half,
src=anddst=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_SENTfor an opening still waiting,ESTABLISHEDonce both sides have spoken,TIME_WAITfor a finished one fading out.[UNREPLIED]marks a line nobody has answered, and[ASSURED]one with traffic both ways.nightDeskhas fetched the catalogue's page, tried203.0.113.99, which doesn't answer, and started pulling a huge sky survey,sky.fits, a trickle at a time. Anouk bansnightDeskfrom the outside world until she knows why, with aDROPrule at line 2.Add it, test the machine, and read the table. Last,
conntrack -Ddeletes lines from the table, and-spicks 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
-
Who started it decides
Every conversation sends packets both ways, so "traffic from
gallerytocampus" 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:
campusmay start anything, towardgalleryandoutside.outsidemay start port 80 ongallery, and nothing else.gallerymay start nothing.That last line is why
galleryexists.skyServertakes 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 campusmatches packets that came in on thecampuscard, and-o outsidepackets leaving onoutside. Write thecampuslines 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
-
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
starCataloguea 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
RELATEDon 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