Packet Filtering

Prabhaker Mateti

Abstract: Packet filtering is a technique for improving network security that removes packets whose fields match certain patterns. Even though it is called "filtering", packet alteration before accepting is available. This lecture describes how packet filtering is done and presents the setup of a filter based on Linux.

PMpacketFiltering.ppt

Table of Contents

  1. Educational Objectives
  2. Packet Filtering
  3. Packet Filtering Concepts
  4. A Packet Filtering Example
  5. Lab Experiment
  6. Acknowledgements
  7. References

Educational Objectives

  1. Learn filtering of IP packets as a prelude to setting up firewalls.
  2. Understand what is meant by stateless and stateful packet filtering.
  3. Understand what support is necessary from the OS kernel.
  4. Appreciate the limitations of security achievable through filtering.
  5. Learn the details of Linux IPtables.

Packet Filtering

An IP packet is a sequence of bytes containing

  1. The source IP address and port
  2. The destination IP address and port
  3. Information about the protocol by which the packet is to be handled
  4. Error checking information
  5. The data, acknowledgment, request or command from the originating system
  6. Usually, some sort of information about the type and status of the data being sent

Protocols such as Telnet and SMTP lend themselves to packet filtering. Filtering examines certain details of the packets at the bottom most layer of the network software of an OS. In the case of incoming packets, filtering occurs soon after they are received by the NIC, and in the case of outgoing packets, it occurs just as they are about to be sent out via the NIC. The filtering is also applicable to packets with addresses belonging to the loop back interface (127.0.0.0).

A screening router a packet filtering router. This is the simplest of the firewalls. A screening router, unlike an ordinary router, determines not only whether or not it can route a packet towards its destination, but also whether or not it should. "Should" or "should not" are determined by the site's security policy.

[Screening router diagram]
Using a screening router to do packet filtering

1. Packet Filtering Concepts

Even though the word "filtering" seems to suggest no changes are made to the packet being examined, there are several well-known manipulations of packets.

1.1 Network Address Translation - NAT

"Basic Network Address Translation or Basic NAT is a method by which IP addresses are mapped from one group to another, transparent to end users. Network Address Port Translation, or NAPT is a method by which many network addresses and their TCP/UDP (Transmission Control Protocol/User Datagram Protocol) ports are translated into a single network address and its TCP/UDP ports. Together, these two operations, referred to as traditional NAT, provide a mechanism to connect a realm with private addresses to an external realm with globally unique registered addresses." [RFC 3022]

Address translation substitutes, in an IP packet, one IP address with another consistently. Suppose we have 20 machines, M10 to M19, with private addresses in the range of 192.168.17.10 to 192.168.17.29, but only two validly assigned IP addresses, say A == 130.108.18.111 and B == 130.108.19.222.

In static NAT, any two of the hosts, say MA, and MB statically chosen from the range of M10 to M19 can be given the privilege of communicating with hosts on the Internet by substituting with the A and B addresses when communicating with the Internet. No address translation occurs when MA and MB are communicating with others in the 192.168.*.* network. The NAT server records this "translation" in a table so that when replies to these packets are received, the NAT server knows where to redirect them. We should locate the NAT service on a secure machine where we can disable the NAT translation quickly if needed. Note that assigning the host machines M18 and M19 the IP addresses A and B would have made it impossible for hosts M10 to M17, with their private addresses, connect to A and B.

In dynamic NAT, the NAT server assigns the two addresses A and B that it controls to whichever of the hosts M10 through M19 is about to communicate with the Internet. DNAT is more complex than SNAT in that the NAT server must now decide when a host, say M6, is done communicating with the Internet so that the regular IP address, say B, assigned to it currently can be considered available for other Mx hosts.

Under what may be termed as strict NAT, with two addresses available, as in the above example, only two internal hosts can be communicating with the Internet at any time.

1.2 Masquerading

Masquerading (MASQ) substitutes a single IP address, say B, for an entire internal network. Masquerading is a special form of Source NAT. The MASQ server records the original source address of a packet and the destination address, and then replaces the source IP address as B and forwards it out to the Internet. When the remote host replies, the MASQ server is able to recognize this masqueraded packet and do the reverse translation.

1.3 IP port forwarding

Port forwarding refers to the systematic substitution of one port number for another in a packet. Obviously, this is relevant for TCP and UDP packets. Often, the port forwarding is coupled with address translation also. Port forwarding and transparent proxying are special forms of Destination NAT.

1.4 Static and Dynamic Filters

Packet filtering can be static by letting desired methods of connecting between the internal and external networks left open at all times. The advantages of static packet filtering are:

Because it does very little work outside of routing traffic, the overhead is extremely low, so near or at hardware speed traffic is likely. The disadvantages of static packet filtering are that it:

Dynamic packet filters open and close "doors" in the firewall based on header information in the data packet. Once a series of packets has passed through the "door" to it’s destination, the firewall closes the door.

1.5 Limitations of Packet Filtering

Limitations of packet filters include:

2. A Packet Filtering Example

Assume that our network consists of an internal network 192.168.17.0/24 (which we call "PRVT"), exposed servers in a separate network 130.108.17.0/24 (called "DMZ" for Demilitarized Zone), and a connection 130.108.17.1 to the Internet (called "EXTRN"). In the following, the interfaces eth0 are used for "outside" connections, and eth1 is used for "internal" connections, and 0-for-o 1-for-i are helpful mnemonic reminders.

eth1 and eth2 are located on the Packet Filter Box (PFB).

External Network (EXTRN)
       |
       |eth0
       |130.108.127.1
---------------
|             |                 Server Network (DMZ)
|     PFB     |------------------------------------------------
|             |130.108.17.250   |             |              |
|             |eth2             |             |              |
---------------             --------       -------        -------
       |eth1                | MAIL |       | DNS |        | WWW |
       |192.168.17.1        --------       -------        -------
       |             eth0 130.108.17.228  130.108.17.229  130.108.17.230
       |             eth1 192.168.17.228  192.168.17.229  192.168.17.230
       |
       |
Internal Network (PRVT)

On the DMZ, we have three separate machines named MAIL, DNS, and WWW, each with two NICs. We have the following security goals.

  1. PFB should be able to ping and traceroute any network. This is useful for diagnosis. Access to DNS should be provided to make ping and DNS more useful.
  2. MAIL server: Accept SMTP from internal and external. Accept POP-3 from internal only.
  3. DNS server: Send DNS to external. Accept DNS from internal, external and packet filter box
  4. WWW server: Accept HTTP from internal and external. Rsync access from internal.
  5. Internal Network to External: Allow http, ftp, ping, traceroute, ssh to external.
  6. Allow Internal Network SMTP to Mail server. Allow Internal Network POP-3 to Mail server This is how internal clients read their mail.
  7. Allow Internal Network to be able to look up external names using DNS server.
  8. Allow Internal Network rsync to WWW server.
  9. Allow Internal Network to surf the WWW server.
  10. Allow internal Network ping to PFB so that they can test if the firewall box is up.
  11. No other traffic is permitted.

We will set the PFB as a Linux box in a sequence of three parts. Part 1 is contained in this article. Part 2 is described in the Firewalls article. Part 3 is described in the An Example Firewall Setup.

3. Packet Filtering in Linux

Linux kernels 2.4.x and later have built-in support for sophisticated filtering of packets. This support, called the The Linux Netfilter Architecture, is generally compiled as "loadable kernel modules" (LKM). The LKMs are inserted into the kernel address space using the insmod (or modprobe) utility. In what follows we are assuming that the Linux is properly configured for packet filtering and appropriate modules have been loaded. At a minimum, lsmod should list the ip_tables module. E.g., in the packet filtering script, we may have:

/sbin/insmod ip_tables
  /sbin/insmod ip_tables
  /sbin/insmod iptable_nat
  /sbin/insmod ip_nat_ftp
  /sbin/insmod ipt_LOG
  /sbin/insmod ipt_mac
  /sbin/insmod ipt_state
  /sbin/insmod ipt_MASQUERADE 
  /sbin/insmod ip_conntrack
  /sbin/insmod ip_conntrack_ftp

The full pathname for ip_tables (and other modules) is of the form /lib/modules/2.4.21-0.2mdk/ kernel/net/ipv4/netfilter/ip_tables.o.gz where 2.4.21-0.2mdk is the kernel version string, and the extension .gz indicates that the module is stored as a compressed file.

There are internal data structures, called "firewall chains". Each of these chains is an ordered sequence of rules. Each rule examines the contents of a packet (typically only the headers) and decides on an action such as DROP the packet, or pass it on to the next rule, etc. These chains are interpreted (in the sense of compilers and interpreters) by a piece of code loosely called a hook. If a rule does not match the packet, the next rule in the chain is applied. Finally, when there are no more rules to apply, the kernel obeys the chain policy to decide what to do.

There is a command line program named iptables that inserts, deletes, reorders the rules in the chains.

3.1 Packet Traversal

A packet traverses the netfilter subsystem of Linux as shown in the diagram below. Having passed a few simple sanity checks (e.g., not truncated, IP checksum OK, not a promiscuous receive), packets enter the PRE_ROUTING chain. These packets go through the processing by conntrack, mangle, NAT modules that have hooked themeselves here as shown below each chain. The resulting packets move on to the routing code, which decides whether the packet is destined for another host, or a local process.

 --->{PREROUTING}------>ROUTING----->{FORWARD}---------->{POSTROUTING}----->
     Mangle               |           Filter     ^        NAT(Src)
     Conntrack            |                      |        Conntrack
     NAT (Dst)            |                   ROUTING
     (QDisc)              v                      |
                       {INPUT}                {OUTPUT} 
                          | Mangle, Filter       ^ Mangle, Filter
                          | Conntrack            | Conntrack
                          v                      | NAT(Dst)

If a packet destination is this host, it is sent to the INPUT hook, which may send the result to the local process (if any). If the packet's destination is another host instead, it is sent to the FORWARD hook. If the kernel does not have forwarding enabled, or it doesn't know how to forward the packet, the packet is dropped. If forwarding is enabled, the packet goes to the FORWARD chain. If it is ACCEPTed, it will move on to POST_ROUTING. The OUTPUT hook is called for packets that are created locally on this host. Routing occurs after this hook is called. Just before leaving the network interfaces of this machine, the packets go through the POST_ROUTING hook.

Kernel modules can register to process the packets at any of these hooks. The functions in the module read/write selected portions of the contents of a packet and can finally do one of five things:

  1. ACCEPT: pass on the packet to the next, i.e., continue traversal.
  2. DROP: drop the packet as though it never appeared; the rest of the traversal does not happen.
  3. STOLEN: consume the packet; similar to drop.
  4. QUEUE: queue the packet (usually so that a user supplied program can process it further).
  5. REPEAT: call this hook again.

3.2 IP Tables

A packet selection subsystem called "IP Tables" is built over the netfilter framework. The packet selection method is of three kinds.

Packet filtering

The filter table hooks at INPUT, FORWARD and/or OUTPUT points. A filter does not make changes to a packets internals.

Network Address Translation

The `nat' table is fed packets from the PREROUTING (for non-local packets) and POSTROUTING. These are for destination and source alterations respectively. This table is different from the `filter' table, in that only the first packet of a new connection will traverse the table. The result of this traversal is then applied to all future packets in the same connection.

Packet Mangling

The packet mangling table (the `mangle' table) is used for changing portions of a packet. It hooks into netfilter at the PREROUTING and OUTPUT points.

4. Packet Filter Box Setup

Packet filtering should be set up on the PFB, of course. It should also be setup on the MAIL, DNS and WWW servers. To be even more careful, it should be setup on each of the machines in the private network. Below, we focus only on the PFB.

The command line program named iptables that inserts, deletes, reorders the rules in the chains has a complex syntax for its arguments. To completely understand it, study the tutorial on IP tables and the man pages. There are also many GUI front ends in the open source archives; see, e.g., ShoreWall. Our goal in this lecture is to explain iptables command only as needed in the setup of PFB. Note also that the order in which the rules appear in a chain matters. This order conflicts with that chosen here for pedagogy. So, in the end look at the final script.

Anti-spoofing The Linux kernel has built-in support for checking that IP addresses are not spoofed. We turn on the built-in anti IP-address spoofing for all interfaces as follows. The '*' expands to the names of each of the network interfaces such as lo, eth0 etc.

for f in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 1 > $f; done

There are several other such initializations. See An Example Firewall Setup.

Set filtering policies. Drop everything until firewall setup is complete.

iptables --flush INPUT
iptables --flush OUTPUT
iptables --flush FORWARD
iptables --policy INPUT DROP
iptables --policy OUTPUT DROP
iptables --policy FORWARD DROP
iptables -t nat --policy PREROUTING DROP
iptables -t nat --policy POSTROUTING DROP

Set Up Interfaces This is usually done in the boot scripts. We must make sure the above steps are done before the interfaces are configured, to prevent packet leakage before the rules are set up.

Masquerading. All packets whose source address is in the range 192.168.17.0/24 are sent out of PFB with source address set to that of PFB by causing them to jump to the MASQUERADE chain.

iptables -t nat -A POSTROUTING -s 192.168.17.0/24 -j MASQUERADE
Source address of packets arriving on eth0 must not be a private address.
iptables -A FORWARD -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A FORWARD -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A FORWARD -i eth0 -s 127.0.0.0/8 -j DROP
iptables -A FORWARD -i eth0 -s 255.255.255.255/32 -j DROP

At the inside NIC (eth1), allow only our address as source. We may have traitors among us!

iptables -A FORWARD -i eth1 -s ! 192.168.17.0/24 -j DROP
iptables -A FORWARD -i eth2 -s ! 130.108.127.0/24 -j DROP

Packets with strange TCP flags should be dropped. The --tcp-flags takes two arguments, each can be a comma separated list of TCP flag names or ALL (referring all of them) and NONE. From the first list, a mask of bits is constructed. The second list lists those that should be a 1 for a match. The target is a chain named logdrop that logs the packet and then DROPs.

# XMAS/NULL packets
iptables -A INPUT   -p tcp --tcp-flags ALL ALL  -j logdrop
iptables -A INPUT   -p tcp --tcp-flags ALL NONE -j logdrop
iptables -A FORWARD -p tcp --tcp-flags ALL ALL  -j logdrop
iptables -A FORWARD -p tcp --tcp-flags ALL NONE -j logdrop

Packet Filtering for Through Packets We split the FORWARD chain into various user chains depending on source/dest interfaces; this breaks the problem down into manageable chunks.

iptables --new extrn-dmz
iptables --new extrn-prvt
iptables --new prvt-extrn
iptables --new dmz-extrn

ACCEPTing standard error ICMPs is a common thing to do, so we create a chain for it.

iptables --new icmpchain

Set Up Jumps From FORWARD Chain Unfortunately, we only know (in the FORWARD chain) the outgoing interface. Thus, to figure out what interface the packet came in on, we use the source address (the anti-spoofing prevents address faking). Note that we log anything which doesn't match any of these (obviously, this should never happen).

iptables -A FORWARD -d ! 192.168.17.0/24 -i eth1 -j prvt-extrn
iptables -A FORWARD -s 130.108.17.0/24 -i eth2 -j dmz-extrn
iptables -A FORWARD -d 130.108.17.0/24 -i eth0 -j extrn-dmz
iptables -A FORWARD -j DROP -l

Define the icmpchain Chain Packets which are one of the error ICMPs get ACCEPTed, otherwise, control will pass back to the calling chain. There are many more icmp types that should be carefully considered. See An Example Firewall Setup.

iptables -A icmpchain -p icmp --icmp-type destination-unreachable -j ACCEPT
iptables -A icmpchain -p icmp --icmp-type time-exceeded -j ACCEPT
iptables -A icmpchain -p icmp --icmp-type parameter-problem -j ACCEPT

Lab Experiment

All work should be carried out in Operating Systems and Internet Security (OSIS) Lab, 429 Russ. Use any of the PCs numbered 19 to 30. No other WSU facilities are allowed.

Objective: Setup a simple packet filter. Become familiar with iptables

  1. Setup a network as in NetUtils lab using four Linux PCs. (Use BackTrack or Knoppix.)
  2. Enable ssh and http services on all four PCs at standard ports, and yet another ssh service on P0 at port number 9999. Verify that these services are useable by all PCs.
  3. Set up filtering on P2, using iptables scripts, without using any GUI based tools, so that
    1. P1 can no longer ssh to P3, but P0 can continue to ssh to P3
    2. P3 can no longer ssh to P0 or P2, but P3 can continue to ssh to P1
    3. P3 cannot http to P0, or P1, or P2 but P0, P1, P2 can http to P3.
    4. All other services should remain useable as they were prior to the filter setup.
  4. Include in the Lab Report the scripts of the filtering rules.
  5. Bonus Points: Repeat the above using a GUI firewall configuration tool, such as WebMin/ShoreWall (see References).

Link to Grading Sheet

Acknowledgements

This work was supported in part by NSF DUE-9951380.

References

  1. Oskar Andreasson, "Iptables Tutorial", 2006, http://www.frozentux.net/ documents/ iptables-tutorial/ This is about 270 pages. Recommended Reading.
  2. Simson Garfinkel, Gene Spafford, "Practical Unix and Internet Security," O'Reilly & Associates; Chapter on Firewalls. Required Reading.
  3. Prabhaker Mateti, Firewalls. A lecture from a course on Internet Security. www.cs.wright.edu/ ~pmateti/ InternetSecurity Required Reading.
  4. Prabhaker Mateti, An Example Firewall Setup, A lecture from a course on Internet Security. www.cs.wright.edu/ ~pmateti/ InternetSecurity/ Lectures/ FireWallSetup/ Recommended Reading.
  5. Thomas M. Eastep, "ShoreWall: IP Tables Made Easy," http://www.shorewall.net/, 2012. This is a front end to IP tables. Recommended visit/download. ( apt-get install shorewall shorewall-doc)
  6. http://www.webmin.com/. "Webmin is a web-based interface for system administration for Unix. Using any modern web browser, you can setup user accounts, Apache, DNS, file sharing and much more. Webmin removes the need to manually edit Unix configuration files ...." Recommended: Download the Debian package and install as in dpkg -i webmin*.deb; apt-get -f install. Then add relevant modules: firewall.wbm.gz, shorewall.wbm.gz.

Copyright ©2012 • pmateti@wright.eduOther Internet Security Lectures