Packet filtering is something I've always hard a hard time getting my head around. Not the basics; that's easy
enough. It's just the incredible level of detail, the difficulty of keeping it all in your head at once.
And then, of course, there are all the different flavors: ipfw, ipfilters, ipchains, and now iptables. It gets more
than a little confusing, and I've never taken the time for more than a cursory look at any of them.
Well, time to change that. I needed to learn more about iptables because the SME Server firewall/mail server I sell now uses this. So..
The basic idea of any packet filtering is to look at a network packet and decide what to do with it: accept it as is and let it go on its way, stop it dead, or change it in some way (which usually
involves sending it somewhere other than where it was originally headed).
Chains and Tables
Iptables starts with three built in chains. You can add more chains, (generally for convenience). Let's understand what it comes with first.
It is important to first understand what packets these chains see.
If a packet comes from this machine (is generated by an application running on this machine), it will go to the OUTPUT chain only.
A packet coming TO this machine traverses the INPUT chain only.
A packet going somewhere else uses FORWARD only.
THAT'S NOT HOW IPCHAINS WORKS. A packet going somewhere else never sees INPUT with iptables. Similarly, a forwarded packet
never sees the OUTPUT chain with iptables. In some ways this makes iptables easier to understand, but if you have the ipchains flow
stuck in your head, it makes it confusing.
Another major difference is that iptables is stateful; that is, it keeps track of each connection. You can look at connections by
examining /proc/net/ip_connact. Here's a little bit from a machine:
tcp 6 426345 ESTABLISHED src=127.0.0.1 dst=127.0.0.1 sport=1031 dport=1030 src=127.0.0.1 dst=127.0.0.1 sport=1030 dport=1031 [ASSURED] use=1
tcp 6 426345 ESTABLISHED src=127.0.0.1 dst=127.0.0.1 sport=1025 dport=1024 src=127.0.0.1 dst=127.0.0.1 sport=1024 dport=1025 [ASSURED] use=1
tcp 6 426345 ESTABLISHED src=127.0.0.1 dst=127.0.0.1 sport=1033 dport=1032 src=127.0.0.1 dst=127.0.0.1 sport=1032 dport=1033 [ASSURED] use=1
tcp 6 431999 ESTABLISHED src=10.1.36.3 dst=10.1.36.248 sport=33019 dport=22 src=10.1.36.248 dst=10.1.36.3 sport=22 dport=33019 [ASSURED] use=1
tcp 6 431944 ESTABLISHED src=10.1.36.3 dst=10.1.36.248 sport=33022 dport=21 src=10.1.36.248 dst=10.1.36.3 sport=21 dport=33022 [ASSURED] use=1
tcp 6 426345 ESTABLISHED src=127.0.0.1 dst=127.0.0.1 sport=1027 dport=1026 src=127.0.0.1 dst=127.0.0.1 sport=1026 dport=1027 [ASSURED] use=1
tcp 6 426345 ESTABLISHED src=127.0.0.1 dst=127.0.0.1 sport=1029 dport=1028 src=127.0.0.1 dst=127.0.0.1 sport=1028 dport=1029 [ASSURED] use=1
Note that you can see an ssh and an ftp connection there.
You need the ip_connact module to have iptables understand the relationship between the control and data sides of an ftp
connection. If that makes no sense right now, you might want to read the ftp section in /Security/dslsecure.html.
This module is also used by the nat translation module.
There are other differences. The http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-10.html lists most of them (there's a lot of other good iptables help at http://www.netfilter.org/documentation/ too).
But back to chains: why would you want to add your own chains - it looks like the standard three pretty well cover everything? True,
but you'd usually do that so that you can apply the rules you make for the new chain to other chains. A quick example:
You create a chain called "mychain" and add a bunch of rules to it. You want both the INPUT and the FORWARD chains to use those rules.
iptables -N mychain
iptables -A mychain -m state --state ESTABLISHED,RELATED -j ACCEPT
# .. more rules ..
iptables -A mychain -j DROP
iptables -A INPUT -j mychain
iptables -A FORWARD -j mychain
That saves the effort of writing out the same rules for both INPUT and FORWARD chains. It's also, unfortunately, why professionally
written iptables firewalls are so hard to comprehend: you have to follow them back through chain after chain to figure out what's
really going on. One chain will list several other chains as targets for itsvarious rules, and those in turn may list others - it can be hard to follow.
Now for some more confusion. You can have more than one network card on the machine. That's the whole idea of a firewall: one
interface to the internet, one or more to the internal lan. A packet coming in on the external interface may be a FORWARD to it, and
an INPUT to the lan side. Therefore, you may have to write more than one rule to control the packet.
Tables make this even more confusing. This is straight from the manual page:
There are current three independent tables (which tables
are present at any time depends on the kernel configura
tion options and which modules are present).
This option specifies the packet matching table
which the command should operate on. If the kernel
is configured with automatic module loading, an
attempt will be made to load the appropriate module
for that table if it is not already there.
The tables are as follows:
filter This is the default table. It contains the built-
in chains INPUT (for packets coming into the box
itself), FORWARD (for packets being routed through
the box), and OUTPUT (for locally-generated pack
nat This table is consulted when a packet that creates
a new connection is encountered. It consists of
three built-ins: PREROUTING (for altering packets
as soon as they come in), OUTPUT (for altering
locally-generated packets before routing), and
POSTROUTING (for altering packets as they are about
to go out).
mangle This table is used for specialized packet alter
ation. It has two built-in chains: PREROUTING (for
altering incoming packets before routing) and OUT
PUT (for altering locally-generated packets before
Does you head hurt yet? Mine sure did. It gets worse: while those three are probably all you have at most, you could
have more. You can find out by "cat /proc/net/ip_tables_names".
This also introduces another complication: if you want to list the rules for the chains, you also need to specify the table.
If you just do "iptables -L -n" (don't forget the -n to avoid wasting time asking DNS to resolve your internal addresses), you
only get the filter table. To get them all, do something like:
for i in `cat /proc/net/ip_tables_names`
echo "Table $i:"
iptables -L -n -t $i
Got that all digested? Good, because now we have to learn about extensions. Look in /lib/iptables or /usr/lib/iptables.
You should find a bunch of libraries, here are just a few:
Each of these are things you can use in iptables rules. We used the "state" module in the user defined chains example above. That's great, but how do you use these things? Well,
some of them are documented in the "man iptables" page, but they are also self documenting. Try these:
iptables -p tcp --help
iptables -m state --help
iptables -j LOG --help
How do you know whether to use -p, -j or -m? Honestly, it can be a little confusing, but if one doesn't work, try another- you'll find it by trial and error if no other way. When you
are reading someone else's rules, you may need at least this to understand what their rule is trying to do. You may also find http://www.netfilter.org/documentation/netfilter-extensions-HOWTO.html helpful.
Writing iptables rules
There is no way that I'm even going to attempt to write firewall rules. I will, when necessary, add to or modify someone else's rules to do something needful that they
didn't include. The level of knowledge necessary for that is substantially less than that required for actual authoring. Even that can be daunting, however: these things
can be very complicated.
There are iptables firewall generators available on the net. Use Google and search for "iptables firewall". Some of these are pretty
well documented, so you can learn quite a bit more about iptables by studying them.
Copyright and Reprint Info
Our Daily Email of Breaking eBusiness News
About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com
WebProNews RSS Feed