Mousey's Guide to netfilter and iptablesThe system described here is a low-level method of packet filtering that enables a UNIX/Linux box to function as an advanced perimeter firewall with capabilities that are comparable to, if not better than, expensive commercial equipment. Basic TheoryThe basic description of iptables is a ‘kernel firewall’ that uses netfilter kernel modules to implement packet filtering policies. In other words, the netfilter modules provide the kernel packet filtering services, and iptables provides the user space method of defining the rules. The rules are defined through the /sbin/iptables. The chances are the firewall configuration desktop application will be a front-end for /sbin/iptables. Firewall rules are grouped into 'chains'. Each table should have the following chains by default:
Each chain is going to define how netfilter handles packets as they pass through the networking components of the kernel. More TablesWhat might cause a little confusion is thgat other table exist alongside iptables. There’s another table for Network Address Translation (nat), a third table for modifying packets as they passes through netfilter (mangle), and a fourth table that allows packets through without interference or alteration (raw).So now an iptables setup encompasses the following tables:
Also on a UNIX/Linux system, we have (or should have) ip6tables. This is very important, because IPv6 and IPv4 headers are both different and have definite field lengths, which is how a firewall knows what it’s reading. Unless we set up IPv6 packet filtering as well, we could end up with a firewall that ignores all incoming IPv6 traffic. Kernel ModulesWith Ubuntu and Linux Mint (and probably most mainstream distros) the netfilter modules are compiled into the kernel. Compiling the kernel yourself should give you the option of having netfilter as a loadable module. Administrating iptables and Packet FilteringAs I’ve already mentioned, filtering policies can be administrated using /sbin/iptables, but this is the cumbersome and rather complicated method until we develop a BASH script for our own needs. What I didn’t know, until looking into this, is that the Uncomplicated Firewall and its GUI (gufw) are also front-ends for iptables, and we can configure the firewall using those instead.
Here are some example commands: So far so good. The simpler rules share pretty much the same format of other command line stuff. In the above rule, the packets are directed to the INPUT chain, they are sifted for TCP packets going through port 80 or 443, and they are ‘jumped’ to the ACCEPT chain.
Another example, from The Geek Stuff tutorial, allows all incoming SSH, HTTP and HTTPS traffic over TCP by chaining three policies within a single iptables entry:
Here we’ll notice there’s the state option, because iptables/netfilter can provide stateful filtering, that’s to say it can look at a packet in the context of whatever stream, and not as an isolated bit of data. In the above entry, I’m guessing that netfilter is being configured to accept only packets that are either establishing a connection or are part of an established connection, and therefore ignore anything that might be associated with a Denial of Service attack or stealth scan. We also have two network interfaces: input and output (both eth0). In the case of a perimeter firewall, both will be needed as the host will bridge the local network and the outside world. Persistence
Some people have reported problems in trying to make the firewall rules persistent. That’s because the rules are lost if they aren’t explicitly saved before the system is rebooted. The core problem here seems to be that iptables is intended for production systems that aren’t rebooted as often as PCs, and therefore the tables must be saved to file and manually restored unless the system is configured otherwise. As always, this is done in the command line:
I believe the Red Hat distro also comes with a script/exectutable that automates this further:
This does the same as running iptables-save. The /etc/sysconfig/iptables file is then saved as /etc/sysconfig/iptables.save. The system should then run iptables-restore when the system reboots. Useful Links
FEDORA DOCUMENTATION. 2013. Fedora Documentation: Saving IPTables Rules. [WWW]. http://docs.fedoraproject.org/en-US/fedora/17/html/Security_Guide/sect-Security_Guide-IPTables-Saving_IPTables_Rules.html. (17th August 2013). |