Kernel Korner - The Hidden Treasures of iptables
Linux's iptables allows powerful firewalls to be implemented at a minute fraction of the cost of many commercial offerings. Basic iptables firewalls are packet filters, which means they inspect the network communications flowing through them a packet at a time and make choices about how those packets are handled. Simple configurations can be used to drop certain packets and accept others. The choice about which policy to apply to a particular packet commonly is made on the basis of the IP address and port number to which it has been sent and the direction in which it is traveling. iptables also can use state information to make more-informed choices based on the state of the connection to which the packet relates. This is known as connection tracking.
A simple and highly effective firewall configuration blocks inbound TCP/IP connection packets and UDP exchanges initiated from the public Internet while allowing outbound ones over translated addresses. This gives users free access to the outside world while protecting them from unwelcome intrusions. Such configurations are a bit simplistic and may need additional filters to be truly useful, but the basic concept is straightforward.
iptables has a lot more to offer than these simple packet-filtering criteria. Some of the extras are fairly well known and even may make their way into some off-the-shelf Linux distributions, but some lesser-known features are worthy of investigation. These are the hidden treasures I intend to point you toward in this article. It would take a book to describe all the possible features and options associated with them, so all I do here is flag their existence and put you on the path of exploration.
Netfilter has two groups of components, the kernel and user-mode pieces. The user-mode group consists of the iptables and related utilities, libraries, manual pages and scripts. The kernel components are patches to existing kernel sources and a number of extra modules.
Applying patches to a system as large and complex as the Linux kernel can be a daunting task to the uninitiated, and the road is littered with traps and potential blind turns. A bad or incompatible patch readily can produce a kernel that doesn't compile, or worse, doesn't boot. The Netfilter team has sought to resolve these difficulties by providing us with a robot guide, POM, or Patch-o-matic. POM is a collection of patches and a script for applying them to your kernel, and it's a joy even for a relative novice to use.
The kernel patches included with POM are classified into a number of groups according to their history and quality. Some of them are base patches needed in every iptables/Netfilter installation. Others are optional or experimental extras that provide interesting features, some of which I describe in this article. These are the promised hidden treasures, what the POM documentation describes as “Maybe broken, Maybe cool extensions.”
Running POM is simple; download the latest Patch-o-matic tarball from the directory /pub/patch-o-matic on ftp.netfilter.org, restore it on your system and run the following command while logged in as root. Make sure to give the correct kernel source directory name as the value of the KERNEL_DIR parameter:
KERNEL_DIR=/usr/src/linux-2.4 ./runme extra
The string module probably is the most widely used extra from the POM trove. It allows packets to be matched against strings occurring anywhere in their data payload. This module has all sorts of uses but needs to be applied carefully so as not to be overzealous. One possible use is to block the downloading of ELF executables from the Web. We can set up a filter that identifies Web return traffic by looking for TCP/IP packets coming from the Internet-facing interface with a source port of 80. If we know that an ELF file starts with hex character 7f followed by the letters ELF (which it does), we can use the string match to search for this sequence. Non-ASCII characters can be embedded in the string by using the pipe symbol to enclose them, so we use |7F|ELF. Assuming that the Internet-facing network interface is eth0, the command is:
iptables -A FORWARD -i eth0 -p tcp --sport 80 \ -m string --string '|7F|ELF' -j DROP
The syntax for embedding hex characters into the string was introduced in iptables 1.2.8. If you are using an earlier version, you need to resort to trickery. For example:
--string "`dd if=/bin/ls bs=4 count=1 2>/dev/null`"
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Server Hardening
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- The Death of RoboVM
- BitTorrent Inc.'s Sync
- The Humble Hacker?
- The US Government and Open-Source Software
- Open-Source Project Secretly Funded by CIA
- New Container Image Standard Promises More Portable Apps
- ACI Worldwide's UP Retail Payments
- Canonical and BQ's Aquaris M10 Ubuntu Edition Tablet
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide