Building a Firewall with IPtables

"Firewalls are no replacement for security at all layers of the OSI model." John Sutherland SANS Institute

 Linux Firewall History

Stateless Versus Stateful

Example: TCP is a connection-oriented protocol

Denial of Service Attacks

   DoS Example:    Foiling Dos Attacks

How to Configure IPTables

   Kernel support

The iptables Command Line

How packets traverse the chains:

*************** 1. *****************

When a packet enters the firewall system the kernel first looks at the destination address of the packet and makes a routing decision.

*************** 2. *****************

If forwarding is enabled in the kernel, and the packet is destined for another network card.

*************** 3. *****************

A program running on the local system can also send packets

iptables -A OUTPUT -o eth1 -p tcp --sport \
1024:65535 --dport 113 -s $LOCALIP -d $ANYWHERE \
-j ACCEPT
*** NOTE ***

Simple iptables rules example

   iptables -P INPUT DROP
   iptables -A INPUT -s 0/0 -d $LOCALIP -p tcp \
        --dport 80 -j ACCEPT
   iptables -A INPUT -s 0/0 -d $LOCALIP -p tcp \
        --dport 22 -j ACCEPT
   iptables -A INPUT -s 0/0 -d $LOCALIP -p tcp \
        --dport 25 -j ACCEPT
   iptables -A INPUT -j LOG
   iptables -A INPUT -j DROP

Targets/Jumps

 
ACCEPT If packet is perfectly matched, the chain is stopped and the packet accepted
DROP Packet is blackholed and not processed and further
LOG Great for debugging, allows logging of short messages to syslogd.
REJECT Sam as DROP, but sends back an error message. Valid in INPUT, FORWARD, and OUTPUT chains.
SNAT Source Network Address Translation. Only valid with POSTROUTING. The argument --to-source can take a range of IPs. Makes your firewall look like several hosts. (used in masquerading with nat)
DNAT Destination Network Address Translation. Only valid with PREROUTING in nat table. The argument --to-destination can take a range of IPs.  (used in port forwarding with nat)
MASQUERADE No address required (unlike SNAT), works with DHCP connections. Only valid with POSTROUTING in nat table. Masquerades source address to firewall address whatever it may be.
MARK Only valid in mangle table. For changing information in packet header

NAT (Network Address Translation) and iptables

Iptables with the 2.4 Linux kernel can perform Network Address Translation

Putting it all together

(I haven't listed the mangle table in this diagram)

This is one way to go about locking down a private network

Testing Rules

Listing Rules

Log Files
# grep SRC iptables* | sed -e 's/.*SRC=//' \
   -e 's/ .*//'| sort | uniq -c | \
   sort -nr |head -20

 
   
# ip
 
COUNT    IPADDR
--------------------
   3968 209.129.35.85
    659 68.101.245.6
    649 64.160.54.43
    423 63.227.63.133
    415 68.3.97.223
    399 64.164.72.41
    397 24.165.68.196
    378 24.165.11.5
    372 68.86.76.83
    344 12.233.38.156
    342 68.6.108.118
    294 63.207.12.171
    288 66.235.25.68
    284 65.94.105.137
    216 24.130.227.252
    213 67.68.234.84
    213 24.165.41.65
    210 68.105.74.31
    210 12.219.228.245
    204 65.212.170.46

# port
 
COUNT PORT
--------------------
  17253 6112
   2740 1433
   2340 137
   1738 445
   1380 1434
    566 135
    564 139
    357 3006
    345 21
    231 3439
    167 6970
    131 1921
    127 3128
    122 17300
    115 27374
     90 1336
     87 3463
     86 111
     83 1080
     82 8080