Introduction
Network security is a primary consideration in any decision to host a
website as the threats are becoming more widespread and persistent
every day. One means of providing additional protection is to invest in a
firewall. Though prices are always falling, in some cases you may be
able to create a comparable unit using the Linux iptables package on an
existing server for little or no additional expenditure.
This chapter shows how to convert a Linux server into:
- A firewall while simultaneously being your home website's mail, web and DNS server.
- A router that will use NAT and port forwarding to both protect your home network and have another web server on your home network while sharing the public IP address of your firewall.
Packet Processing In iptables
All packets inspected by iptables pass through a sequence of built-in tables (queues) for processing. Each of these queues is dedicated to a particular type of packet activity and is controlled by an associated packet transformation/filtering chain.
There are three tables in total. The first is the mangle table which is responsible for the alteration of quality of service bits in the TCP header. This is hardly used in a home or SOHO environment.The second table is the filter queue which is responsible for packet filtering. It has three built-in chains in which you can place your firewall policy rules. These are the:
- Forward chain: Filters packets to servers protected by the firewall.
- Input chain: Filters packets destined for the firewall.
- Output chain: Filters packets originating from the firewall.
- Pre-routing chain: NATs packets when the destination address of the packet needs to be changed.
- Post-routing chain: NATs packets when the source address of the packet needs to be changed
Processing For Packets Routed By The Firewall
Queue Type
|
Queue Function
|
Packet Transformation Chain in Queue
|
Chain Function
|
Filter
|
Packet filtering
|
FORWARD
|
Filters packets to servers
accessible by another NIC on the firewall.
|
INPUT
|
Filters packets destined to the
firewall.
|
||
OUTPUT
|
Filters packets originating from
the firewall
|
||
Nat
|
Network Address Translation
|
PREROUTING
|
Address translation occurs before
routing. Facilitates the transformation of the destination IP address to be
compatible with the firewall's routing table. Used with NAT of the
destination IP address, also known as destination NAT or DNAT.
|
POSTROUTING
|
Address translation occurs after
routing. This implies that there was no need to modify the destination IP
address of the packet as in pre-routing. Used with NAT of the source IP
address using either one-to-one or many-to-one NAT. This is known as source
NAT, or SNAT.
|
||
OUTPUT
|
Network address translation for
packets generated by the firewall. (Rarely used in SOHO environments)
|
||
Mangle
|
TCP header modification
|
PREROUTING
POSTROUTING
OUTPUT
INPUT
FORWARD
|
Modification of the TCP packet
quality of service bits before routing occurs. (Rarely used in SOHO
environments)
|
You need to specify the table and the chain for each firewall rule you create. There is an exception: Most rules are related to filtering, so iptables assumes that any chain that's defined without an associated table will be a part of the filter table. The filter table is therefore the default.
To help make this clearer, take a look at the way packets are handled by iptables. In Figure 14.1 a TCP packet from the Internet arrives at the firewall's interface on Network A to create a data connection.
The packet is first examined by your rules in the mangle table's PREROUTING chain, if any. It is then inspected by the rules in the nat table's PREROUTING chain to see whether the packet requires DNAT. It is then routed.
If the packet is destined for a protected network, then it is filtered by the rules in the FORWARD chain of the filter table and, if necessary, the packet undergoes SNAT in the POSTROUTING chain before arriving at Network B. When the destination server decides to reply, the packet undergoes the same sequence of steps. Both the FORWARD and POSTROUTING chains may be configured to implement quality of service (QoS) features in their mangle tables, but this is not usually done in SOHO environments.
If the packet is destined for the firewall itself, then it passes through the mangle table of the INPUT chain, if configured, before being filtered by the rules in the INPUT chain of the filter table before. If it successfully passes these tests then it is processed by the intended application on the firewall.
At some point, the firewall needs to reply. This reply is routed and inspected by the rules in the OUTPUT chain of the mangle table, if any. Next, the rules in the OUTPUT chain of the nat table determine whether DNAT is required and the rules in the OUTPUT chain of the filter table are then inspected to help restrict unauthorized packets. Finally, before the packet is sent back to the Internet, SNAT and QoS mangling is done by the POSTROUTING chain
Targets And Jumps
Each firewall rule inspects each IP packet and then tries to identify it as the target of some sort of operation. Once a target is identified, the packet needs to jump over to it for further processing. Table 14.2 lists the built-in targets that iptables uses.
Descriptions Of The Most Commonly Used Targets
target | Desciption | Most Common Options |
---|---|---|
ACCEPT
|
|
N/A
|
DROP
|
|
N/A
|
LOG
|
|
--log-prefix "string"Tells iptables to prefix all log messages with a user defined string. Frequently used to tell why the logged packet was dropped |
REJECT
|
|
--reject-with qualifierThe qualifier tells what type of reject message is returned. Qualifiers include: icmp-port-unreachable (default) icmp-net-unreachable icmp-host-unreachable icmp-proto-unreachable icmp-net-prohibited icmp-host-prohibited tcp-reset echo-reply |
DNAT
|
|
--to-destination ipaddressTells iptables what the destination IP address should be |
SNAT
|
|
--to-source [- ][:Specifies the source IP address and ports to be used by SNAT. |
MASQUERADE
|
|
[--to-portsSpecifies the range of source ports to which the original source port can be mapped. |
Important Iptables Command Switch Operations
Each line of an iptables script not only has a jump, but they also have a number of command line options that are used to append rules to chains that match your defined packet characteristics, such the source IP address and TCP port. There are also options that can be used to just clear a chain so you can start all over again. Tables 14.2 through 14.6 list the most common options.General Iptables Match Criteria
iptables command Switch | Desciption |
---|---|
-t <-table->-table->
|
If you don't specify a table, then the filter table is assumed. As discussed before, the possible built-in tables include: filter, nat, mangle
|
-j
|
Jump to the specified target chain when the packet matches the current rule. |
-A
|
Append rule to end of a chain |
-F
|
Flush. Deletes all the rules in the selected table |
-p
|
Match protocol. Types include, icmp, tcp, udp, and all |
-s
|
Match source IP address |
-d
|
Match destination IP address |
-i
|
Match "input" interface on which the packet enters. |
-o
|
Match "output" interface on which the packet exits |
In this command switches example
iptables -A INPUT -s 0/0 -i eth0 -d 192.168.1.1 -p TCP -j ACCEPT
iptables is being configured to allow the firewall to accept TCP packets coming in on interface eth0 from any IP address destined for the firewall's IP address of 192.168.1.1. The 0/0 representation of an IP address means any.
Common TCP and UDP Match Criteria
Switch | Desciption |
---|---|
-p tcp --sport
|
TCP source port. Can be a single value or a range in the format: start-port-number:end-port-number |
-p tcp --dport
|
TCP destination port. Can be a single value or a range in the format: starting-port:ending-port |
-p tcp --syn
|
Used to identify a new TCP connection request. ! --syn means, not a new connection request |
-p udp --sport
|
UDP source port. Can be a single value or a range in the format: starting-port:ending-port |
-p udp --dport
|
UDP destination port. Can be a single value or a range in the format: starting-port:ending-port |
In this example:
iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP \ --sport 1024:65535 --dport 80 -j ACCEPTiptables is being configured to allow the firewall to accept TCP packets for routing when they enter on interface eth0 from any IP address and are destined for an IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination port is port 80 (www/http).
Common ICMP (Ping) Match Criteria
Matches used with ---icmp-type | Desciption |
---|---|
--icmp-type
|
The most commonly used types are echo-reply and echo-request |
In this example:
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPTiptables is being configured to allow the firewall to send ICMP echo-requests (pings) and in turn, accept the expected ICMP echo-replies.
Consider another example
iptables -A INPUT -p icmp --icmp-type echo-request \ -m limit --limit 1/s -i eth0 -j ACCEPT
The limit feature in iptables specifies the maximum average number of matches to allow per second. You can specify time intervals in the format /second, /minute, /hour, or /day, or you can use abbreviations so that 3/second is the same as 3/s.
In this example, ICMP echo requests are restricted to no more than one per second. When tuned correctly, this feature allows you to filter unusually high volumes of traffic that characterize denial of service (DOS) attacks and Internet worms.
iptables -A INPUT -p tcp --syn -m limit --limit 5/s -i eth0 -j ACCEPT
You can expand on the limit feature of iptables to reduce your vulnerability to certain types of denial of service attack. Here a defense for SYN flood attacks was created by limiting the acceptance of TCP segments with the SYN bit set to no more than five per second.
Common Extended Match Criteria
Switch | Desciption |
---|---|
-m multiport --sports
|
A variety of TCP/UDP source ports separated by commas. Unlike when -m isn't used, they do not have to be within a range.
|
-m multiport --dports
|
A variety of TCP/UDP destination ports separated by commas. Unlike when -m isn't used, they do not have to be within a range.
|
-m multiport --ports
|
A variety of TCP/UDP ports separated by commas. Source and destination ports are assumed to be the same and they do not have to be within a range. |
-m --state
|
The most frequently tested states are:
ESTABLISHED: The packet is part of a connection that has seen packets in both directions NEW: The packet is the start of a new connection RELATED: The packet is starting a new secondary connection. This is a common feature of such protocols such as an FTP data transfer, or an ICMP error. INVALID: The packet couldn't be identified. Could be due to insufficient system resources, or ICMP errors that don't match an existing data flow. |
iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP \ --sport 1024:65535 -m multiport --dports 80,443 -j ACCEPT iptables -A FORWARD -d 0/0 -o eth0 -s 192.168.1.58 -i eth1 -p TCP \ -m state --state ESTABLISHED -j ACCEPT
Here iptables is being configured to allow the firewall to accept TCP packets to be routed when they enter on interface eth0 from any IP address destined for IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination ports are port 80 (www/http) and 443 (https). The return packets from 192.168.1.58 are allowed to be accepted too. Instead of stating the source and destination ports, you can simply allow packets related to established connections using the -m state and --state ESTABLISHED options.
Using User Defined Chains
As you may remember, you can configure iptables to have user-defined chains. This feature is frequently used to help streamline the processing of packets. For example, instead of using a single, built-in chain for all protocols, you can use the chain to determine the protocol type for the packet and then hand off the actual final processing to a user-defined, protocol-specific chain in the filter table. In other words, you can replace a long chain with a stubby main chain pointing to multiple stubby chains, thereby shortening the total length of all chains the packet has to pass through. For exampleiptables -A INPUT -i eth0 -d 206.229.110.2 -j fast-input-queue iptables -A OUTPUT -o eth0 -s 206.229.110.2 -j fast-output-queue iptables -A fast-input-queue -p icmp -j icmp-queue-in iptables -A fast-output-queue -p icmp -j icmp-queue-out iptables -A icmp-queue-out -p icmp --icmp-type echo-request \ -m state --state NEW -j ACCEPT iptables -A icmp-queue-in -p icmp --icmp-type echo-reply -j ACCEPTHere six queues help assist in improving processing speed. Table 14.7 summarizes the function of each.
Custom Queues Example Listing
Chain | Desciption |
---|---|
INPUT
|
The regular built-in INPUT chain in iptables |
OUTPUT
|
The regular built-in OUTPUT chain in iptables
|
fast-input-queue
|
Input chain dedicated to identifying specific protocols and shunting the packets to protocol specific chains. |
fast-output-queue
|
Output chain dedicated to identifying specific protocols and shunting the packets to protocol specific chains. |
icmp-queue-out
|
Output queue dedicated to ICMP |
icmp-queue-in
|
Input queue dedicated to ICMP |