Setting Up An iptables Firewall: Part 2

In Part 1 of this series, we set up a very basic firewall that essentially just restricts what ports we can connect to without doing much else to defend our server. In this part, we’re going to build from there and add additional restrictions to block a substantial portion of potentially malicious traffic that simply has no business being on the internet at all.

The first thing we’re going to address are martians. No, not the little green men or Matt Damon; in this context, a martian is an IP packet with a source address reserved for special use. Like with our opened ports from the previous part, we’re going to create a separate chain and add our rules to that:

Nothing should be too surprising here, and I’ve included comments listing the IANA‘s reason for reserving these networks in each rule. We have introduced the --source parameter, which as you can see can take a network range in CIDR notation (it can also take a plain IP address, e.g. 123.45.67.89).

Before we go any further, however, there’s a big caveat I have to put out here: This chain assumes that your machine is connected directly to the internet. If instead you are on a LAN or otherwise expect traffic to be coming from one of the “private-use networks”, you should remove the rule corresponding to that network from this list — a packet is not a martian if it comes from an address that makes sense to be sending to your machine!

Now, of course, we need to add a rule to the INPUT chain so that our firewall will actually use it. However, we can’t simply use the -A argument we’ve become so familiar with: That would Append the rule to the end of the INPUT chain, which means that we would not prevent these martians from connecting to our opened services! Clearly that’s not what we want, so how do we put the rule into the order we want it in?

The answer is that we use the -I argument to Insert the rule into our chain. First we need to know what the current order is, and for that we will use the -L argument to List the rules, as well as the --line-num argument to show their line numbers:

Now we can see that our services chain is invoked from rule #5; we want our new martians chain to take that place, however, so we tell -I that we want to insert our new rule so that it’s now #5, pushing the existing one down further in the list:

-I INPUT 5-m comment --comment"Filter martians"-j martians

-I INPUT 5 -m comment --comment "Filter martians" -j martians

If you now repeat the above listing command, you’ll see our new rule is now #5 in the list, while the rule that invokes our services chain has been moved to become #6.

Now that that’s taken care of, let’s also block some common attacks that script kiddies and hackers will often use to try and exploit servers on the internet. We’ll once again put these rules into a separate chain:

Since all of these rules are based on flags in the TCP packet’s header, we restrict matches to the TCP protocol (-p tcp) and then load the tcp module (-m tcp). We’ve also added comments to each of these to provide a clue as to what they do, but we’re just going to gloss over them for now — each of these rules describes a packet with header flags that should never be seen in a legitimate connection attempt, and which are used solely in various attacks and reconnaissance methods.

Remember back in Part 1 when I mentioned that the RELATED,ESTABLISHED rule would be crucial to the rest of our design? Here we see that in action: Our attacks chain here is built under the assumption that we are only worried about packets that aren’t part of any existing connection. Without that rule in place, we would have to reconsider how we detect potential attacks in this chain.

One more thing I will point out is the second rule (third line, “SYN flag checking”), and specifically the appearance of the ! character in the argument list. This is the “not” operator, and it inverts the meaning of the following condition from “packets which match” to instead mean “packets which do not match”. In practice there’s not often a call for this operator, but when it’s needed it’s incredibly valuable — without it implementing such a case would either require incredibly careful ordering of rules in our chain (resulting in something very fragile and more difficult to maintain), or create an entirely separate chain to replace this one simple rule.

Once again, however, we can’t simply add this one to the end of our INPUT chain, or else it won’t protect our services! There’s no hard and fast rule about whether this one should come before or after the martians rule we just inserted above, however in general you want to drop “bad” packets as early as possible; from watching the counters on my own server, I see more being dropped from this chain than I see martians, so I’m going to insert it above the martians chain:

(You can — and, until you’re really comfortable with iptables, absolutely should — always review the listing with sudo iptables -L INPUT --line-num before inserting a new rule like this, however here I know that we haven’t done anything else to the INPUT chain since inserting the martians chain as rule #5, and therefore I know that I can insert this one at rule #5, which will push the martians chain to rule #6, and the services chain will become rule #7; check the output listing again to see!)

That’s it for Part 2! We’ve enhanced the security of our system now by proactively blocking traffic that has no legitimate business coming to us. We’ll continue to build on this as we add in additional protections. To save our new changes, if you’ve already installed the iptables-persistent package (from Part 1) you can run this command (at least on Debian-based systems):

This will prompt the package to once again ask you if you want to save the current rules; tell it “yes”, and once again the file /etc/iptables/rules.v4 will be updated with your current configuration. Your file should now look (in part) like this: