26/11/2008

Network Firewalls including NAT, routing, arp, DMZs and a dash of layer 7 (Mike Rose, Chemistry/DAMTP)

A talk given as part of the TechLink Workshops and Seminars program.

This talk is somewhat generic with the implementation done on Linux with iptables. I am not an iptables expert. Instead I use an OSS GUI to generate the rules/script, hence this talk should be accessible to non-iptables experts.

The talk will cover: NAT'ing, routing, firewall design, host/protocol rules, deployment, DMZs, cost and resource practicalities, deployment, application level filtering/inspection and a brief comparison with a commercial firewall.

Contents

What is a FW?

" A firewall is a dedicated appliance, or software running on another computer, which inspects network traffic passing through it, and denies or permits passage based on a set of rules. "

This and more definitions are available from the Firewall entry on wikipedia.

Why use FWs?

Routing

Getting packets from one place to another

Which is more important, complete security or a functional network?

If the firewall rules "go wrong" I prefer a functional network for an academic environment.

The Tools I've used

Basic Network FW Design Suggestions

Test/Development System Setup

I have found it very helpful to setup a development/test system that is similar to the live system, including:

The System Setup Used for the Following Examples

There's no particularly reason for the above choices except that I tend to use Debian on servers and openSuSe for laptops.

Simple Port-based firewall

Port-based firewall with DMZ

NATing FW

External Access to a server behind the firewall

Notes: if you can find another way to do make things work, then please do so. For example putting the server to which external access is required outside the firewall (or in a DMZ) and letting your internal network clients/servers connect to it is probably better.
You can use proxy arp to actually give the server the externally accessible IP address if you want to. Some notes for how to do this with Linux:
# to get a public IP, behind the firewall, to forward through the firewall:

# on the firewall:
IP=131.111.11X.XXX # IP of firewall
HOST_IP=131.111.11X.XXX # IP of computer behind firewall for proxy_arp
# NOTE: both firewall and public IP computer behind the firewall need IPs in the same
# network
# eth0 = external interface
route add -host ${HOST_IP} eth0 
# enable proxyarp on both interfaces of the firewall:
echo 1 > /proc/sys/net/ipv4/conf/eth0/proxy_arp
echo 1 > /proc/sys/net/ipv4/conf/eth1/proxy_arp

# NOTE: $IP needs to be in the same network as the firewall IP
# AND
# The Proxy ARP is actually only used to get packets from external to internal network.
# To get packets back the other way, the normal IP routing functionality is employed.
# Therefore the firewall would need an IP address in each network to route packets
# back to hosts...

Fluffy (Your friendly local neighbourhood network welcome message)

This is a favourite of mine. If an unkown host is connected to our physical network we like to be nice and direct any web-browsing at a helpful page suggesting how network connectivity can be achieved.

In Cambridge University

Consider:

Experiences with Layer 7 - cation Layer Filtering

An application layer firewall may inspect traffic at the application layer and, for example, block viruses, certain websites and attempts to exploit known logical flaws in client software. I believe this is typically done by matching patterns of undesirable traffic to network traffic. Therefore the effectiveness and accurray will be dependent upon how good the patterns are.
My experience with the application layer filtering (Smart Defense) in Checkpoint firewall (R60) is this:

One answer to these problems was to upgrade from R60 to R62. I asked how this would help given that the patterns would be the same. The answer was:
" As of R62 - the behaviour of smart defence inspection has been greatly improved / re-written. Instead of the traffic following a linear path through the firewall when in monitor only mode - the traffic passes through the kernel without inspection but is also copied through the smart defence module to generate the relevant log entry so there is no way for monitor-only to possibly block traffic. "
I have been, informally, told that any Checkpoint release with a 0 at the end should be avoided.

Comparison with a Commerical Firewall

This is a comparison of iptables on Linux with fwbuilder against Checkpoint Firewall. The comparison is purely my own opinion and based upon my personal experience working with Checkpoint (R60) and then replacing a Checkpoint firewall with a Linux, iptables and fwbuilder. .
CategoryCheckpointLinux iptables and fwbulder
Software Licence CostLots. E.g. 18K for initial licence0. free software
Software Maintenance CostLots. E.g. 1.5K per annum for maintenance0. free software
Software Support CostLots. E.g. 1.2K per annum for support from a 3rd party0. free software
Staff time on software issuesLots. Submit a suport request to your support provider. If the support provider can answer then it is quick. If the support request has to go to Checkpoint the wait is long then you might need to run extensive debugging and send them fairly large filesSome. There is a huge amount of information out there on the www and in books. Colleagues can also be very helpful.
Training CostsLots. E.g. 2K per person for the 5 days of training you really do need Medium. Staff time spent using and learning iptables, fwbuilder and Linux. A test setup is a good idea
Hardware CostsMore. Windows computer to run the GUI. Smart centre on a computer. Firewall on another computerLess. A computer to run the firewall
Site to site VPNdoes this wellI do not know and have not investigated
VPN for client computersLots of licence costs and no support for Linux. Allow access to your VPN server. Use openVPN, Windows VPN, etc.
Automatic fail-overImpressive. You need more licences. Cluster 2 firewall computers together and you get very good failover. You can also have both FWs active (in the cluster). On the training course we saw no traffic loss when disconnecting one of the clustered FW computers. The heartbeat application could be used, although it will not synchronise the state tables between two firewalls. pf on openBSD will, I am told, do the full monty with state table synchronisation between clustered servers as one firewall. For a GUI and many other feature there is pfsense on freeBSD which has been ported to openBSD
Notes: I also experienced problems with Checkpoint R60 blocking Windows Domain (Active Directory) joins from Windows 2003 Server and Windows Vista. The solution was to apply the hot fixes to R60. Unfortunately applying the hotfixes resulted in machines with multiple IP addresses being blocked. Months passed and then the solution was to turn off the "out of state" (fw_allow_out_of_state_post_syn) checking.

Simple Practical Backup/Recovery/Resilience

Deployment

Extra Techie stuff

The following are some simple commands for viewing iptables rules, log message handling and memory usage (state tables) on Linux:
aptitude install iproute

Useful iptables commands
# list the rules:
iptables -L
# no DNS lookups (numeric output)
iptables -nvL
# to also see the NAT rules with no DNS lookups (numeric output)
iptables -nvL -t nat
# to see NAT stuff with DNS lookups
iptables -t nat L

# to clear the iptables settings
i${IPTABLES} -P INPUT ACCEPT
${IPTABLES} -P OUTPUT ACCEPT
${IPTABLES} -P FORWARD ACCEPT
${IPTABLES} -F
${IPTABLES} -X
${IPTABLES} -Z

# To stop log messages going to the console:
# /proc/sys/net/ipv4/ip_forward
cat >>/etc/sysctl.conf <<EOF
kernel.printk = 3 4 1 7
EOF
sysctl -p

# Memory usage and state tables
http://www.wallfire.org/misc/netfilter_conntrack_perf.txt
# the hashsize (number of buckets) is set with:
/proc/sys/net/ipv4/netfilter/ip_conntrack_buckets
# interesting stuff for connection tracking in:
/proc/sys/net/ipv4/netfilter

# to set the hashsize dynamically, depending upon kernel version:
# Between 2.6.14 and 2.6.19 (included), use:
echo $HASHSIZE > /sys/module/ip_conntrack/parameters/hashsize
# Since 2.6.20, use:
echo $HASHSIZE > /sys/module/nf_conntrack/parameters/hashsize

# to set CONNTRACK_MAX:
# Since Linux kernel version 2.4.23 (thus Linux 2.6 as well), use:
echo $CONNTRACK_MAX > /proc/sys/net/ipv4/netfilter/ip_conntrack_max


# How large do these values need to be?
The size of kernel memory used by netfilter connection tracking is:
size_of_mem_used_by_conntrack (in bytes) =
        CONNTRACK_MAX * sizeof(struct ip_conntrack) +
        HASHSIZE * sizeof(struct list_head)

on i386, kernel 2.6.5, size_of_mem_used_by_conntrack is around
CONNTRACK_MAX * 300 + HASHSIZE * 8 (bytes).
(pointer size is 4 bytes, therefore 2 * 4 = 8 )

By default, CONNTRACK_MAX = HASHSIZE * 8.  This means that there is an average
of 8 conntrack entries per linked list (in the optimal case, and when
CONNTRACK_MAX is reached), each linked list being a hash table entry
(a bucket).
On systems with enough memory and where performance really matters, you can
consider trying to get an average of one conntrack entry per hash bucket,
which means HASHSIZE = CONNTRACK_MAX.

# Debian etch v4.0 on 64-bit Intel
sizeof(struct ip_conntrack) = 304 bytes
sizeof(struct list_head) = 2 * size of pointer = 2 * 8 = 16 bytes

65536 * 304 + 65536 * 8 = 20447232
20447232 / 1024^2 = 19.5MB

304x + 8x = memory(MB).1024^2

x = memory.1024^2 / 312
# we want approximately 1GB for connections:
x = 1024.124^2 / 312 = 3441480.20512820512820512820
# we want a power of 2 number
2^21 = 2097152
2^22 = 4194304 # chosen as this is for the Department perimeter FW

echo 4194304 > /proc/sys/net/ipv4/netfilter/ip_conntrack_max
echo 4194304 > /sys/module/ip_conntrack/parameters/hashsize

# for an internal NATing firewall we want to use 512MB of memory:
1048576
# in /etc/init.d/firewall-chfwnmr
                # set hashsize for performance
                echo 1048576 > /sys/module/ip_conntrack/parameters/hashsize
                # set connection table size
                echo 1048576> /proc/sys/net/ipv4/netfilter/ip_conntrack_max
                # set arp cache timeout to 3 minutes
                echo 180 > /proc/sys/net/ipv4/route/gc_timeout

# "Neighbour table overflow" - arp tables filling up
First of all: this has nothing to do with netfilter, just with
the routing and cacheing of the routes.

Further comments about/from/relating to this talk

" Although I am familiar with iptables, our Technicians are not, this software allows a graphical representation of what is happening, and a way to edit themselves. Which from an 'all can fix/ understand' point of view is excellent. "

" I cannot see why anyone would want a commercial firewall. "

" pfSense is on freeBSD and its home page is here http://www.pfsense.com/
pf is the openBSD firewall like netfilters for linux. pfSync is the tool that copies firewall states between pf firewalls CARP is the openBSD implementation of something like cisco's VRRP allowing 2 (or more) machines to share a floating IP with one as master and one as backup. All this has been ported to freeBSD
pfSense is a build of freeBSD with the pf firewall, pfsync, CARP and php based configuration (even init scripts) and webGUI with all the bits you need to have a decent firewall (DHCP, DNS etc).
I have been running a test setup of 2 pfSense firewalls in a CARP cluster for a while now. The failover is amazing you can yank the power lead out of the master whilst streaming video and it will failover with only a tiny loss of packets. Usually the loss is so small the buffering in the video copes with the loss and you won't even notice. "

"7 months after replacing a Checkpoint firewall with Linux and iptables there have been zero security incidents. This is on a network with some 2000 devices and a varied selection of hardware and operating systems." - Mike Rose.

Other Information