Stateful Packet Filtering Firewall With Iptables
Stateful Packet Filtering Firewall With Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 1/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 2/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 3/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
The following table shows the order of processing for each of the four
possible packet flows. For example consider what happens to an
incoming packet. First the mangle then the nat table rules in the
PREROUTING chain are used, then the mangle table rules in the
INPUT chain, and finally the filter table rules in the INPUT
chain.
https://wpollock.com/AUnixSec/IptablesOverview.htm 4/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
Use the “-t table” option to specify which table to use when
adding/removing a rule. If this option is omitted then the filter
table is used by default.
Rules and match criteria
A rule is zero or more matching criteria and one (optional) target. A
rule is added to a specific table and chain. A rule with no criteria
matches all packets.
A rule with no target does nothing; processing will continue with the
next rule on the chain. Such a rule may be useful for accounting
purposes. Iptables keeps a byte and a packet count for each rule, that
is updated whenever a packet matches the rule’s criteria. So, to count
all incoming packets on interface eth0:
iptables -A INPUT -i eth0
By default iptables can use criteria that match IPv4 layers 3 and 4
headers:
-d [!] destination address[/mask] -s [!] source
address[/mask]
-i [!] input interface -o [!] output
interface
-p [!] protocol [!] -f
Here’s an example: iptables -t filter -A INPUT -i lo -
j ACCEPT
A “!” means “not”. For most values either numbers or names can be
given: port numbers or service names, IP addresses or DNS name,
protocol number or name, and even interfaces can be given names.
Most criteria have other names: ‑s = ‑‑source = ‑‑src. When
omitted the default is to match on all (i.e., no “-p” is the same as “-
p all”). Note it is a bad idea to use a DNS name that would cause a
remote DNS query!
The “-f” matches fragments (all but the first frame of a fragmented
IP packet).
For interfaces you can follow the name with a “+” to indicate all
interfaces that start with that name (e.g., “ppp+”, “eth+”).
To use additional criteria a match extension must be used. Each match
extension provides a set of extra criteria you can add to the rule. You
https://wpollock.com/AUnixSec/IptablesOverview.htm 5/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
specify which match extensions you want to use using the “-m
extension_name”, followed by the criteria that extension enables.
A few match extensions are loaded automatically if you use the
corresponding protocol in the rule: tcp, udp, and icmp. The first
two allow “--dport num” and “--sport num” to specify source
and destination port numbers (and ranges). The icmp match
extension allows “--icmp-type type”. (And others too.)
To see which criteria an extension provides, use:
iptables -m extension_name --help
Some match extensions will cause the kernel to load additional
loadable kernel modules.
The iptables man page lists all the match extensions available. One
special match extension is “arp”, which enables layer 2 match
criteria. A very useful match extension is “state”, which can be
used to see if a packet is part of an existing session (or flow):
iptables -A INPUT -m state --state ESTABLISHED -
j ACCEPT
Targets
The target of a rule determines what to do with a packet that matched
the criteria. You specify the target with the “-j target” option.
For example:
iptables -A INPUT -i eth0 -p tcp --dport 23 -
j DROP
says to DROP (discard and ignore) any packets that match the criteria:
incoming, from network interface eth0, to TCP port 23 (telnet).
There are only four built-in targets, however additional target
extensions can be used. The standard distribution includes many
target extensions. Some targets are only valid in chains in a particular
table. (For example the NAT targets are only valid in the nat table.)
Target extensions (especially those that modify the packet) allow
additional commands. For example:
iptables -t mangle -A PREROUTING -p tcp --
dport 80 \
-j MARK --set-mark 3
https://wpollock.com/AUnixSec/IptablesOverview.htm 6/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 7/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
Using iptables
To add or remove rules from chains you specify the table (default is
filter) and the chain. For example, to append a new rule to the
end of the OUTPUT chain of the nat table:
iptables -t nat -A OUTPUT criteria -j
action
You can insert rules in any position in the chain by using “-I” instead
of “-A”.
To delete a rule use: iptables -D chain {rule|rule-
num}
To delete all rules use: iptables ‑F [chain]
Create a new chain with: iptables [-t table] -N name.
Delete all user-defined chains: iptables -X [chain]
List [all] rules [no DNS]: iptables [-v] [-n] -L.
Reset per-rule packet and byte counters: iptables -Z
[chain]
You can set the default policy for a built-in chain with:
iptable -P chain {ACCEPT|DROP}
User-defined chains don’t have default policies but you can just put a
reject/drop everything rule at the end for the same effect.
The best way to work with complex sets of rules is to edit the file
where your OS stores the rules (in the “iptables-save” format),
then simply restart iptables. On Red Hat systems this is
/etc/sysconfig/iptables. If there is no such file (on other
systems) then save the rules as a shell script that you can run at boot
time.
When building a firewall you can use the iptables command to modify
the currently loaded (in RAM) rules. These take effect immediately.
Once you’ve added and tweaked the rules to the point that they work,
you can save the current rules to a file with iptables-save. The
result can be used as input to iptables-restore. You can see
this in the file /etc/sysconfig/iptables, which is where RH
stores the firewall rules.
https://wpollock.com/AUnixSec/IptablesOverview.htm 8/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
Filtering Basics [Adapted from “Linux firewalls” 3rd Ed., by Suehring and
Ziegler,
(C)2006 by Peason Ed.]
Start by deleting all user defined chains, and clearing all rules from
the five built in chains. Next add a default policy of DROP to the
INPUT, OUTPUT, and FORWARD chains. (The other two chains
don’t need to drop packets, so you can leave the default-default policy
of ACCEPT for them.)
If you stop here you are safe! But it will pay to add some holes in this
firewall. At this point is is a good idea to check that all kernel level
network protections have been enabled.
A very common situation to to allow the same packets into this host as
you will allow your host to forward, when acting as a router. If not
routing you can just add filter table rules to the INPUT and OUTPUT
chains. If routing, you will usually want the same rules for the INPUT
and FORWARD chains. The way to deal with this is to create a new
user-defined chain, say “MYRULZ”, and all the common rules to this
chain. Then you only have this for INPUT and FORWARD:
-A INPUT -j MYRULZ
-A FORWARD -j MYRULZ
It is usually safe and useful to accept packets from your loopback
interface. These can only have been sent from programs on your host,
to other programs on your host. So add this (if using routing, add to
MYRULZ instead):
-A INPUT -i lo -j ACCEPT
Next we want to allow some outside packets to get through. To start
with we want to allow return packets from our outgoing connections.
This generally means to allow incoming packes that are part of some
established conversation. (TCP or UDP). However you may also
want to allow related packets, if using FTP (FTP establishes an
outgoing connection, then the remote server initiates another using a
different port. This is related to but not part of the established
conversation.)
-A INPUT -m state --state ESTABLISHED,RELATED -
j ACCEPT
Some packets are obviously fake and should never be allowed, no
matter what holes you decide to add. You should drop any incoming
https://wpollock.com/AUnixSec/IptablesOverview.htm 9/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
packets from a NIC with the following source addresses (See RFC-
3330 list of special IPv4 addresses):
You host’s IP address assigned to that NIC.
Any address from one of your internal LANs (that is attached to a
different NIC)
Class A, B, C private addresses: 10.0.0.0 - 10.255.255.255, 172.16.0.0
- 172.31.255.255, 192.168.0.0 - 192.168.255.255.
Class D (multicast) addresses (these are legal destinations only, never
sources): 224.0.0.0 - 239.255.255.255
Class E (reserved) addresses: 240.0.0.0 - 247.255.255.255
Higher addresses are not used legally: 248.0.0.0 - 255.255.255.255
Looback addresses: 127.0.0.0 - 127.255.255.255
Illegal source broadcast addresses. While some broadcase source
addresses are legal on a single LAN (e.g., 0.0.0.0 for a DHCP
requests) you will never see any broadcast address from the Internet,
or as a regular non-broadcast packet. So in many cases you can filter
out any source of 0.0.0.0 - 0.255.255.255, and also 255.255.255.255.
Link-local addresses are reserved for zeroconf addressing and are
private on a given lan, if used at all. So these addresses are not vald
sources from other networks: 169.254.0.0 - 169.254.255.255
192.0.2.0/24 block is reserved for TEST-NET, to be used in examples
and documentation.
192.88.99.0/24 - This block is allocated for use as 6to4 relay anycast
addresses, according to [RFC3068]. So if you don’t use IPv6 then
you shouldn’t see these.
198.18.0.0/15 - This block has been allocated for use in benchmark
tests of network interconnect devices. Its use is documented in
[RFC2544].
Any unallocation addresses. You can check IANA and the various
RARs (e.g., ARIN) to see which blocks are currently unassigned. No
legal packet should have such a source address. But be careful! New
allocations are made all the time so you would have to carefully
monitor those and frequently update your firewall. (I don’t know of
any site that provides you will alerts on this, but it would be a good
idea!)
https://wpollock.com/AUnixSec/IptablesOverview.htm 10/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
Any known problem networks/hosts. You can simply drop all packes
that come from problem sites, provided those sites aren’t also used for
legitimate purposes. A business must balance the extra security
against lost of customers. (An alterniative is to rate limit packets from
those sites.)
Limiting Sources and Services
In many cases you can limit some types of traffic to only be legal
from a small number of source IPs.. For example SSH (port 22)
access to a production server should only be allowed from a few
places: inside the company LAN, and perhaps a few remote sites that
have administrative access.
You can also drop traffic based on source port. Legitimate packets for
services such as mail or web will have a source port in the
unregistered (dynamic or private) range of 49152 through 65535.
While there may be exceptions you will need to allow (e.g., SMB) in
general source ports in the range 0 to 49151 (and especially the well-
known ports of 0 to 1023.
At this point is is just a matter of adding rules for the holes you with
to allow. For example to allow access to a web service (port 80):
-A INPUT -m state --state NEW -p tcp --dport 80 -
j ACCEPT
Don’t forget to allow related ports, such as DNS, IPP, MTA, POP,
HTTPS, etc.
In the end you may wish to add a rule to drop the packets. The
purpose is to be able to see the accounting statistics on packets
dropped. To troubleshoot your system you may also add LOG rules.
(Show log from yborstudent.)
Egress Filtering
Many sites assume no local evil users or security problems and simply
allow any outgoing packet (set the policy on the OUTPUT chain to
ACCEPT and add no rules).
A better approach it to limit outgoing packets to ones with a correct
source IP address (and source port), and that are part of an established
session. A production server will rarely if ever initiate out-going
connections, it should just respond to incoming ones. (There are
exceptions, e.g. a mail server using a real-time black list or an FTP
https://wpollock.com/AUnixSec/IptablesOverview.htm 11/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 12/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 13/14
11/8/23, 12:11 AM Stateful Packet Filtering Firewall with Iptables
https://wpollock.com/AUnixSec/IptablesOverview.htm 14/14