Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

Security Tools

  • Print
  • + Share This
From the author of

iptables

The iptables tool is the next generation of TCP/IP filtering and monitoring for the Linux environment. The tool is an interface to the netfilter module of the Linux kernel. Netfilter provides Network Address Translation (NAT) and a security mechanism for your network.

iptables was written by Rusty Russell, who is also the author of the ipchains tool. The work was sponsored by Watchguard, and the community is supported by Penguin Computing, the Samba Team and SGI, and Jim Pick. The Samba Team also maintains a mailing list (see lists.samba.org for more details).

An Overview of iptables and Netfilter

Netfilter is a kernel module for the 2.4 kernel series, and it is responsible for packet filtering. It looks at the headers of each packet moving through the gateway and makes a decision about what to do with them. Packets might be accepted (passed along to the routing process or the receiving protocol stack), dropped (not forwarded), or they might have some more complicated action taken. People choose to filter their traffic for many reasons. The most common reasons include segregating network traffic, securing an internal network, providing NAT for internal hosts, and controlling internal users' access to external services.

Netfilter starts with three sets of rules: INPUT, OUTPUT, and FORWARD. These rule sets are called chains (short for firewall chains). Traffic moves through these chains as shown in Figure 8.

Figure 8 IP traffic moving through the firewall chains.

Each chain represents a list of rules that are consulted sequentially for each packet. As traffic moves through a rule chain, it is examined to determine what to do with it. If it is accepted, it passes to the next point in the diagram. If it is dropped, the packet stops there. If a rule doesn't specify what to do with a packet, the packet is examined by the next rule.

Setting Up iptables

The first step in setting up iptables is to build the appropriate kernel modules. These can either be built as separate modules or included in the kernel. To build them into the kernel, answer 'Y' to the question CONFIG_NETFILTER during kernel configuration on any kernel of 2.3.15 or better. You might find that there are additional kernel modules you want to build and install (such as ip_conntrack_ftp).

With the necessary kernel modules available, you can build and install the userspace tools. You can download iptables from netfilter.kernelnotes.org or either of the other sites mentioned previously. After downloaded, iptables follows the normal configure, make, make install pattern.

You can use the set of rules shown in Listing 4 to test1 your newly installed iptables and netfilter.

Listing 4 A Test Script for iptables

# create a new table
iptables -N GATE

# set up rules for it
iptables -A GATE -m state --state ESTABLISHED, RELATED -j ACCEPT
iptables -A GATE -i eth0 -s 192.168.1.20 -j LOG --log-prefix \
 "connection from test bad machine, dropping:"
iptables -A GATE -s 192.168.1.20 -j DROP
iptables -A GATE -s 192.168.1.21 -j LOG --log-prefix \
 "connection from test good machine, accepting:"
iptables -A GATE -J LOG --log-prefix "Accepting packet:"
iptables -A GATE -j DROP

# apply it to the INPUT and FORWARD chains.
iptables -A INPUT -j GATE

After you've installed these rules, you can test them by trying to connect from 192.168.1.20 and 192.168.1.21. The first connection should fail (with a log entry), and the second should succeed (also with a log entry). If this happens, everything is fine and you can flush the rules:

iptables -F GATE
iptables -X GATE
iptables -F INPUT

Now that the tools have been built, installed, and verified you can configure them for use at your site.

iptables at Work

Before you start writing iptables rules, it is important to sit down and think about three things: What policy are you trying to implement, how can you keep the rules and chains manageable, and how can you keep the rules as efficient as possible without making them unmanageable. If the rules you write don't implement the policy you're trying to enforce, they might as well not be there. If you (and your coworkers) can't read and maintain the rules, someone is going to break them. If they don't work efficiently, they will throttle your network.

As important as efficiency is, we tend to overlook a major component of it—human time. If your rules are hard to read and understand, they will take a great deal of human time to maintain. In addition, as they get hairy, your chances of making a mistake increase dramatically. I don't like working late into the weekend trying to figure out why a needlessly complex set of rules isn't working the way I want it to, and I'm sure you don't either.

Remember that each rule a packet must traverse takes time. When you have lots of packets going through your packet filter, the little chunks of time add up. Keeping this in mind, you should make sure that (as much as possible) the rules that will be used most often should be the first rules in the chain. In addition, the more bytes you check in the packet headers the more work you're going to do. This means that again, you want to ensure that each rule checks as few bytes as possible—without compromising your security.

In order to actually work with your chains, you'll need to use the switches in Table 3.

Table 3 iptables Chain Operations

Switch

Function

-N

Creates a new rule chain.

-L

Lists the rules in a chain.

-P

Changes the policy for a built-in chain.

-Z

Zeroes the byte and packet counters in a chain.

-F

Flushes all the rules from a chain.

-X

Deletes an empty chain (except the built-in chains).

In addition to operations on chains, you also need to perform operations on individual rules within a chain. Table 4 shows some of the major operations on rules within a chain and the switches needed to invoke them. (Notice that these rules actually operate on the chain itself, modifying the content of the chain on a rule-by-rule basis.)

Table 4 iptables Rule Operations

Switch

Function

-A

Adds this rule to the end of a chain.

-I

Inserts this rule at the specified point in the chain.

-R

Replaces a rule at the specified point in the chain.

-D

Deletes a rule; can be specified by number or by matching content.

Using the rule and chains options from Table 4, you can start to modify the built-in chains or add your own. The additional information you'll need is in the following sections. Let's start by dissecting one of the rules from our GATE chain used in the preceding tests.

iptables -A GATE -s 192.168.1.20 -j DROP

The -A GATE statement means that this is a new rule being added to the end of the GATE chain. The -s 192.168.1.20 statement applies this rule to any packet with a source address of 192.168.1.20. The -j DROP tells netfilter to drop this packet without further processing. Each rule that you build will follow this basic pattern.

Filtering by Address

In our example rule, we defined a source address with the -s option. This option can also be used to define a range of addresses using a network mask:

-s 192.168.1.0/24

or a hostname:

-s crashtestdummy

In addition to source addresses, we can also filter by destination address with the -d switch. (--source, --src, --destination, and --dst are all legal forms of this switch as well.)

Filtering by Interface

Instead of filtering by address, you can also define filters by the interface. -i and --in-interface define inbound interfaces, while -o and --out-interface define outbound interfaces.

The INBOUND chain only matches inbound interfaces and the OUTBOUND chain only matches outbound interfaces. The FORWARD chain can match either. If you define a nonexistent interface, it will not match any packets until (or unless) that interface is brought up. There is also a special interface matching wildcard—the + indicates any interface matching the string preceding it:

-o eth+

That matches any packet destined to be sent out any Ethernet interface.

Filtering by Protocol

Protocols can be matched as well by using the -p switch. You can identify protocols either by the (IP) protocol number or (for the TCP, UDP, and ICMP) by its name. To specify all ICMP traffic, you could use

-p ICMP

Inverting a Selection

Sometimes it is easier to say "anything that isn't foo" than it is to specify all the individual things you want to talk about. iptables allows for this with an inversion prefix. To invert a selection, you use !. Selecting anything that doesn't come from the 192.168.1.0/24 network would look like this:

-s ! 192.168.1.0/24

Creating Your Own Selection Criteria

iptables is designed to be extensible, so you can define your own criteria. Definitions can be based upon existing TCP, UDP, ICMP, State, or other extensions.

TCP extensions include examination of TCP Flags and TCP Source and Destination Ports. To look for the SYN and ACK flags being set without any other flags being turned on, you could do this:

-p tcp --tcp-flags ALL SYN, ACK

To look for connections to X servers, you could do this:

-p tcp --destination-port 6000-6010

A --source-port extension is also provided. Both of these extensions allow four kinds of port listings: a single port, a range of ports (as above), all ports at or above a listed port (1024-), or all ports at or below a listed port (-1024).

UDP extensions are similar, but include only the --destination-port and --source-port options from the TCP extensions.

ICMP extensions provide searching of the ICMP Type and Code with the --icmp-type switch. ICMP Type name, the numeric value of the Type, or the Type and Code separated by a "/" are all allowed. To search for an ICMP Redirect for Host, you could do this:

-p icmp --icmp-type 5/1

Inspection of a connection's state can determine whether a packet is requesting a new connection, related to an existing connection (such as an ICMP error message), part of an existing connection, or indeterminate. To search for packets that are requesting new connections, you could do this:

-m state --state NEW

Defining and Using Chains

In our testing example, we created a new chain called GATE and applied it to both the INPUT and FORWARD chains. The flow of traffic through the INPUT chain looks something like Figure 9 after we've added these rules.

Figure 9 Flow of traffic through a netfilter chain.

We created the chain and set it into motion with the following steps:

  1. Create the chain.

  2. Add rules to it.

  3. Apply the chain.

Creating the chain was done with the following command:

iptables -N GATE

The name is not required to be all uppercase, but cannot be the name of an existing rule chain.

Rules were added sequentially in our example, but we could have inserted them in any order using the -I switch instead. We could have also deleted or replaced existing rules had we so desired.

After the rules were written, we applied the rule with the following command:

iptables -A INPUT -j GATE

This command adds a rule to the INPUT chain that causes traffic to jump to the GATE chain for further processing. If you don't do this, no traffic will ever be checked by your rule chain!

Special Handling

At times you will want to log traffic that moves through your packet filter. This logging can take up a great deal of disk space, so it is normally done only on a reduced basis. Three switches are of special importance when logging. We used one of these options in the logging rules in our example.

iptables -A GATE -J LOG --log-prefix "Accepting packet:"

After declaring that packets matching this rule were to be logged, we gave the --log-prefix switch to prepend some text onto our log entries. The other switch used when logging is --log-level, which takes a syslog-style logging level as an argument, and directs the logging to that logging level. Because logging can take up so much disk space, it is most often used with the -m limit option as well. This command will limit the number of matches to a default of three matches per hour, with a burst of 5 matches.

When a packet is dropped, it is dropped silently. If you want to send an ICMP Port Unreachable message, you can specify that the packet be rejected with this:

-J REJECT

If you want to use multiple chains in conjunction with each other, you don't want to end a chain by dropping all remaining packets. Instead, you can pass control of the packet back to the parent chain with this:

-J RETURN

If this rule is hit and the chain is built-in, the default policy is executed. If the rule is hit in a user-defined chain, the packet begins to traverse the parent chain again at the point it jumped out to the current chain.

  • + Share This
  • 🔖 Save To Your Account