The idea
This morning a stranger on the street refunded forty seats at the Lantern Theatre, through a till page nobody thought was public. Give stageDoor its first firewall rules, and decide who gets through.
Steps
-
Walk in from the street
Ines looks after the computers at the Lantern Theatre. Its
ticketServer(10.40.2.10) shows tonight's play on Port 80, where browsers look for a website. It also runs the till on port 8080, for the staff in the foyer: seats sold, money taken, a refund page, no password.Every packet for that server crosses
stageDoor, a machine with a network card in each of the theatre's three networks:streetfor the outside world,foyerfor the staff,backstagefor the server. Today it passes on whatever it receives: a Router's job is to deliver.A Firewall adds a second job: stopping packets. The word comes from buildings, where a firewall stops a fire spreading. A network firewall stands on the path, reads each packet, and decides whether it goes on.
It decides from facts written on the outside of the packet: the sender, the receiver, the protocol (TCP here), and a port at each end. A rule is one decision written in those facts.
Linux keeps its rules in lists called chains. A packet passing through meets
FORWARD, one of the three drawn here.Stand on the street as
patronand ask for the till. Then add a rule onstageDoorwith the actionDROP, which throws a matching packet away, and ask again while the lab records what reachesticketServer.patron:~# curl -sS -m 3 http://10.40.2.10:8080/ stageDoor:~# iptables -v -A FORWARD -p tcp --dport 8080 -j DROP patron:~# curl -sS -m 3 http://10.40.2.10:8080/ patron:~# curl -sS -m 3 http://10.40.2.10/
-
Drop or reject
Dropping has a cost, and honest people pay it. TCP reads silence as a lost packet and sends it again, and again. Without
-m 3,curlon Linux waits about two minutes, never learning whether a firewall, a dead machine or a cut cable is to blame.REJECTis the other way to refuse: throw the packet away, then answer the sender with an error. For TCP the clearest answer is a reset, one small packet meaning "this connection isn't happening", the same one a machine sends from a port with no program behind it.Which is better is a real argument. Silence costs nothing during a flood of packets, and tells a stranger less, or so the theory goes. A refusal spares honest people the wait: a program that gets a reset can try elsewhere at once.
A common habit is
DROPfacing strangers andREJECTfacing your own people; some engineers reject everywhere.nmaplets you hear what a stranger hears. It names each port by what came back:openwhen a program answered,closedwhen a reset came back, andfilteredwhen no answer came, or an ICMP error saying the packet was blocked.Scan port 8080 beside port 8000, which has nothing behind it. Then swap the rule for a
REJECTand scan again.patron:~# nmap -n -Pn -p 8000,8080 10.40.2.10 stageDoor:~# iptables -v -R FORWARD 1 -p tcp --dport 8080 -j REJECT --reject-with tcp-reset patron:~# curl -sS -m 3 http://10.40.2.10:8080/ patron:~# nmap -n -Pn -p 8000,8080 10.40.2.10
-
An exception for the staff
The stranger is out. The rule names a port and no sender, though, so the staff are out too. Try the till from
boxOffice, onfoyer.Ines wants one exception.
-snames a sender,10.40.1.0/24covers the whole foyer, and-j ACCEPTlets a matching packet through. Together,-s 10.40.1.0/24 -p tcp --dport 8080 -j ACCEPTreads "the foyer may reach the till".Add it the way you added the first rule, with
-A, then try the till again.When a rule seems to do nothing, stop guessing and read the chain.
iptables -L FORWARDprints it, and with-veach rule shows two counters: how many packets (pkts) and how manybytesit has matched. Those counters are the firewall's own diary, and they settle most arguments about what it's doing.Then add the exception again with
-I FORWARD 1, which inserts a rule at line 1, and read the chain once more.boxOffice:~# curl -sS -m 3 http://10.40.2.10:8080/ stageDoor:~# iptables -v -A FORWARD -s 10.40.1.0/24 -p tcp --dport 8080 -j ACCEPT boxOffice:~# curl -sS -m 3 http://10.40.2.10:8080/ stageDoor:~# iptables -L FORWARD -v -n --line-numbers stageDoor:~# iptables -v -I FORWARD 1 -s 10.40.1.0/24 -p tcp --dport 8080 -j ACCEPT boxOffice:~# curl -sS -m 3 http://10.40.2.10:8080/ stageDoor:~# iptables -L FORWARD -v -n --line-numbers
-
Refuse what you did not name
The till is safe. Is it the only thing on that server? A list of what's forbidden only stops what somebody thought of.
-p 1-10000asksnmapto try every port up to 10000. (Foriptables,-pmeans the protocol instead.)There's another way to build the list, from the opposite end. Refuse everything, then name what's allowed. This is called default deny, and it's the difference between a ban list and a guest list: a ban list has to name every troublemaker in town, and a guest list only names your friends.
Every chain already has a setting that makes this work, its policy. Look at the top of your listings:
Chain FORWARD (policy ACCEPT. The policy is what happens to a packet that gets to the bottom with no rule fitting it, and onstageDoorit has beenACCEPTfrom the start, which is why everything got through before you typed a thing.iptables -P FORWARD DROPchanges it. That command prints nothing, so read the chain afterwards to see it took.Default deny has a real price: you have to know everything you depend on. Find out what that means here.
patron:~# nmap -n -Pn -p 1-10000 10.40.2.10 patron:~# curl -sS -m 3 http://10.40.2.10:9000/ stageDoor:~# iptables -P FORWARD DROP stageDoor:~# iptables -L FORWARD -v -n --line-numbers patron:~# curl -sS -m 3 http://10.40.2.10:9000/ patron:~# curl -sS -m 3 http://10.40.2.10/
-
A rule for the public page
The theatre's page needs a rule of its own.
-p tcp --dport 80 -j ACCEPTreads "packets going to port 80 may pass". Add it with-A: nothing above it mentions port 80, so its place doesn't matter this time.The list now reads like a guest list, and it feels finished. But the firewall reads each packet alone, with no idea what it's part of, and a conversation goes both ways. The request goes to port 80, and the server's answer comes back from port 80, to whatever port
patronpicked.--dportnames the port a packet is going to, and--sportnames the port it comes from.Add the rule, then ask for the page from
patronwhile the lab records atticketServer. The recording shows each packet with>pointing from sender to receiver, and each address ends with its port. Mend what's missing with--sport, then do the same for the staff's till, whose answers crossstageDoorthe same way.stageDoor:~# iptables -v -A FORWARD -p tcp --dport 80 -j ACCEPT patron:~# curl -sS -m 3 http://10.40.2.10/ stageDoor:~# iptables -v -A FORWARD -p tcp --sport 80 -j ACCEPT patron:~# curl -sS -m 3 http://10.40.2.10/ stageDoor:~# iptables -v -A FORWARD -d 10.40.1.0/24 -p tcp --sport 8080 -j ACCEPT boxOffice:~# curl -sS -m 3 http://10.40.2.10:8080/ stageDoor:~# iptables -L FORWARD -v -n --line-numbers
Run these steps on real machines