Online Book Reader

Home Category

Linux Firewalls - Michael Rash [16]

By Root 459 0
name on the firewall instead of by IP address.

To simplify the task of building the iptables policy, assume there is a single internal network with a non-routable network address of 192.168.10.0[6] and a Class C subnet mask 255.255.255.0 (or /24 in CIDR notation).

The internal network interface on the firewall (see Figure 1-2) is eth1 with IP address 192.168.10.1, and all internal hosts have this address as their default gateway. This allows internal systems to route all packets destined for systems that are not within the 192.168.10.0/24 subnet out through the firewall. The external interface on the firewall is eth0, and so as to remain network agnostic, we designate an external IP address of 71.157.X.X to this interface.

Figure 1-2. Default network diagram

There are two malicious systems represented: one on the internal network (192.168.10.200, hostname int_scanner) and the other on the external network (144.202.X.X, hostname ext_scanner). The network diagram in Figure 1-2 is included for reference here, and we will refer to it in later chapters as well. All traffic examples in the book reference the network diagram in Figure 1-2 unless otherwise noted, and you will see the hostnames in this diagram used at the shell prompts where commands are executed so that it is clear which system is generating or receiving traffic.

iptables.sh Script Preamble

To begin the iptables.sh script, it is useful to define three variables, IPTABLES and MODPROBE (for the paths to the iptables and modprobe binaries) and INT_NET (for the internal subnet address and mask), that will be used throughout the script (see ❶ below). At ❷ any existing iptables rules are removed from the running kernel, and the filtering policy is set to DROP on the INPUT, OUTPUT, and FORWARD chains. Also, the connection-tracking modules are loaded with the modprobe command.

[iptablesfw]# cat iptables.sh

#!/bin/sh

❶ IPTABLES=/sbin/iptables

MODPROBE=/sbin/modprobe

INT_NET=192.168.10.0/24

### flush existing rules and set chain policy setting to DROP

echo "[+] Flushing existing iptables rules..."

❷ $IPTABLES -F

$IPTABLES -F -t nat

$IPTABLES -X

$IPTABLES -P INPUT DROP

$IPTABLES -P OUTPUT DROP

$IPTABLES -P FORWARD DROP

### load connection-tracking modules

$MODPROBE ip_conntrack

$MODPROBE iptable_nat

$MODPROBE ip_conntrack_ftp

$MODPROBE ip_nat_ftp

The INPUT Chain

The INPUT chain is the iptables construct that governs whether packets that are destined for the local system (that is, after the result of a routing calculation made by the kernel designates that the packet is destined for a local IP address) may talk to a local socket. If the first rule in the INPUT chain instructs iptables to drop all packets (or if the policy setting of the INPUT chain is set to DROP), then all efforts to communicate directly with the system over any IP communications (such as TCP, UDP, or ICMP) will fail. The Address Resolution Protocol (ARP) is also an important class of traffic that is ubiquitous on Ethernet networks. However, because ARP works at the data link layer instead of the network layer, iptables cannot filter such traffic, since it only filters IP traffic and overlying protocols.

Hence, ARP requests and replies are sent and received regardless of the iptables policy. (It is possible to filter ARP traffic with arptables, but a discussion of this topic is beyond the scope of this book, since we generally concentrate on the network layer and above.)

Note

iptables can filter IP packets based on data link layer MAC addresses, but only if the kernel is compiled with the MAC address extension enabled. In the 2.4 kernel series, the MAC address extension must be manually enabled, but the 2.6 kernel series enables it by default.

Continuing with the development of the iptables shell script, after the preamble, we use the following commands to set up the INPUT chain.

###### INPUT chain ######

echo "[+] Setting up INPUT chain..."

### state tracking rules

❸ $IPTABLES -A INPUT -m state --state INVALID -j LOG --log-prefix "DROP INVALID "

--log-ip-options

Return Main Page Previous Page Next Page

®Online Book Reader