Labs / Network Security / Firewalls

Lab 1 of 3 · Firewalls

Firewall rules: keep the street out and let the staff in

Write a firewall's rules by hand: drop vs reject, and why their order decides everything.

Start this lab 5 steps

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.

Three chains, and a packet meets one
Three chains, and a packet meets one
The same refusal, heard two ways
The same refusal, heard two ways
The first line that fits decides
The first line that fits decides
What no line names meets the policy
What no line names meets the policy
The answer is a packet too
The answer is a packet too

Steps

  1. 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: street for the outside world, foyer for the staff, backstage for 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 patron and ask for the till. Then add a rule on stageDoor with the action DROP, which throws a matching packet away, and ask again while the lab records what reaches ticketServer.

    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/
  2. 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, curl on Linux waits about two minutes, never learning whether a firewall, a dead machine or a cut cable is to blame.

    REJECT is 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 DROP facing strangers and REJECT facing your own people; some engineers reject everywhere.

    nmap lets you hear what a stranger hears. It names each port by what came back: open when a program answered, closed when a reset came back, and filtered when 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 REJECT and 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
  3. 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, on foyer.

    Ines wants one exception. -s names a sender, 10.40.1.0/24 covers the whole foyer, and -j ACCEPT lets a matching packet through. Together, -s 10.40.1.0/24 -p tcp --dport 8080 -j ACCEPT reads "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 FORWARD prints it, and with -v each rule shows two counters: how many packets (pkts) and how many bytes it 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
  4. 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-10000 asks nmap to try every port up to 10000. (For iptables, -p means 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 on stageDoor it has been ACCEPT from the start, which is why everything got through before you typed a thing.

    iptables -P FORWARD DROP changes 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/
  5. A rule for the public page

    The theatre's page needs a rule of its own. -p tcp --dport 80 -j ACCEPT reads "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 patron picked.

    --dport names the port a packet is going to, and --sport names the port it comes from.

    Add the rule, then ask for the page from patron while the lab records at ticketServer. 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 cross stageDoor the 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

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