Docsity

Lecture Notes for LINUX Firewall, Iptables | CIS 238, Study notes of Computer Science

Toggle sidebar
bg1
LINUX Firewall
iptables
Originally, the most popular firewall/NAT package running on Linux was ipchains. . To
address a number of shortcomings, the Netfilter organization created a new product
called iptables . Considered a faster and more secure alternative to ipchains, iptables has
become the default firewall package installed under RedHat and Fedora Linux with the
following improvements:
Better integration with the Linux kernel with the capability of loading iptables-
specific kernel modules designed for improved speed and reliability.
Stateful packet inspection. This means that the firewall keeps track of each
connection passing through it and in certain cases will view the contents of data
flows in an attempt to anticipate the next action of certain protocols. This is an
important feature in the support of active FTP and DNS, as well as many other
network services.
Filtering packets based on a MAC address and the values of the flags in the TCP
header. This is helpful in preventing attacks using malformed packets and in
restricting access from locally attached servers to other networks in spite of their
IP addresses.
System logging that provides the option of adjusting the level of detail of the
reporting.
Better network address translation.
Support for transparent integration with such Web proxy programs as Squid.
A rate limiting feature that helps iptables block some types of denial of service
(DoS) attacks.
Iptables Package Installation and Startup
The iptables RPM filename usually starts with the software package
name by a version number, as in iptables-1.2.9-1.0.i386.rpm and the
associated Gnome configuration front-end – system-config-firewall.
You can start, stop, and restart iptables after booting by using the commands:
service iptables start
service iptables stop
service iptables restart
To get iptables configured to start at boot, use the chkconfig
command:.
chkconfig iptables on
You can determine whether iptables is running or not via the service iptables status
command. Fedora Core will give a simple status message. For example
service iptables status
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Lecture Notes for LINUX Firewall, Iptables | CIS 238 and more Study notes Computer Science in PDF only on Docsity!

LINUX Firewall

iptables

Originally, the most popular firewall/NAT package running on Linux was ipchains.. To address a number of shortcomings, the Netfilter organization created a new product called iptables. Considered a faster and more secure alternative to ipchains, iptables has become the default firewall package installed under RedHat and Fedora Linux with the following improvements:  Better integration with the Linux kernel with the capability of loading iptables- specific kernel modules designed for improved speed and reliability.  Stateful packet inspection. This means that the firewall keeps track of each connection passing through it and in certain cases will view the contents of data flows in an attempt to anticipate the next action of certain protocols. This is an important feature in the support of active FTP and DNS, as well as many other network services.  Filtering packets based on a MAC address and the values of the flags in the TCP header. This is helpful in preventing attacks using malformed packets and in restricting access from locally attached servers to other networks in spite of their IP addresses.  System logging that provides the option of adjusting the level of detail of the reporting.  Better network address translation.  Support for transparent integration with such Web proxy programs as Squid.  A rate limiting feature that helps iptables block some types of denial of service (DoS) attacks.

Iptables Package Installation and Startup

The iptables RPM filename usually starts with the software package name by a version number, as in iptables-1.2.9-1.0.i386.rpm and the associated Gnome configuration front-end – system-config-firewall. You can start, stop, and restart iptables after booting by using the commands: service iptables start service iptables stop service iptables restart To get iptables configured to start at boot, use the chkconfig command:. chkconfig iptables on You can determine whether iptables is running or not via the service iptables status command. Fedora Core will give a simple status message. For example service iptables status

You can determin current ipttables rule status with the following command: iptables –L The iptables application requires you to load certain kernel modules to activate some of its functions. Whenever any type of NAT is required, the iptable_nat module needs to be loaded. The ip_conntrack_ftp module needs to be added for FTP support and should always be loaded with the ip_conntrack module which tracks TCP connection states. As most scripts probably will keep track of connection states, the ip_conntrack module will be needed in any case. The ip_nat_ftp module also needs to be loaded for FTP servers behind a NAT firewall. /etc/sysconfig/iptables file doesn't support the loading of modules, so you'll have to add these statements to your /etc/rc.local file. For example

File: /etc/rc.local

Module to track the state of connections

modprobe ip_conntrack

Load the iptables active FTP module, requires ip_conntrack

modprobe ip_conntrack_ftp

Load iptables NAT module when required

modprobe iptable_nat

Module required for active an FTP server using NAT

modprobe ip_nat_ftp

Packet Processing In iptables

All packets inspected by iptables pass through a sequence of built-in tables (queues) for processing. Each of these queues is dedicated to a particular type of packet activity and is controlled by an associated packet transformation/filtering chain. There are three tables in total.

  • The first table is the mangle table which is responsible for the alteration of quality of service bits in the TCP header. This is not used in a general firewall environmentt.
  • The second table is the filter queue which is responsible for packet filtering. It has three built-in chains in which you can place your firewall policy rules. These are the:  Forward chain: Filters packets to servers protected by the firewall.  Input chain: Filters packets destined for the firewall.  Output chain: Filters packets originating from the firewall.
  • The third table is the nat queue which is responsible for network address translation. It has two built-in chains; these are:  Pre-routing chain: NATs packets when the destination address of the packet needs to be changed.  Post-routing chain: NATs packets when the source address of the packet needs to be changed Processing For Packets Routed By The Firewall

To look at the way packets are handled by iptables refernce the below figure:.

  • A TCP packet from the Internet arrives at the firewall's interface on Network A to create a data connection.
  • The packet is first examined by your rules in the mangle table's PREROUTING chain, if any.
  • It is then inspected by the rules in the nat table's PREROUTING chain to see whether the packet requires DNAT. It is then routed.
  • If the packet is destined for a protected network, then it is filtered by the rules in the FORWARD chain of the filter table and, if necessary, the packet undergoes SNAT in the POSTROUTING chain before arriving at Network B.
  • When the destination server decides to reply, the packet undergoes the same sequence of steps. Both the FORWARD and POSTROUTING chains may be configured to implement quality of service (QoS) features in their mangle tables.
  • If the packet is destined for the firewall itself, then it passes through the mangle table of the INPUT chain, if configured, before being filtered by the rules in the INPUT chain of the filter table before. If it successfully passes these tests then it is processed by the intended application on the firewall.
  • At some point, the firewall needs to reply. This reply is routed and inspected by the rules in the OUTPUT chain of the mangle table, if any.
  • Next, the rules in the OUTPUT chain of the nat table determine whether DNAT is required and the rules in the OUTPUT chain of the filter table are then inspected to help restrict unauthorized packets.
  • Finally, before the packet is sent back to the Internet, SNAT and QoS mangling is done by the POSTROUTING chain Iptables Packet Flow Diagram

Each firewall rule inspects each IP packet and then tries to identify it as the target of some sort of operation. Once a target is identified, the packet needs to jump over to it for further processing.

address translation rewriting the source IP address of the packet > The source IP address is user defined [:-] Specifies the source IP address and ports to be used by SNAT. MASQUERADE (^) > Used to do Source Network Address Translation. > By default the source IP address is the same as that used by the firewall's interface [--to-ports [-]] Specifies the range of source ports to which the original source port can be mapped. Each line of an iptables script not only has a jump, but they also have a number of command line options that are used to append rules to chains that match your defined packet characteristics, such the source IP address and TCP port. There are also options that can be used to just clear a chain so you can start all over again. Gener al Iptables Match Criteria iptables command Switch Description -t If you don't specify a table, then the filter table is assumed. As discussed before, the possible built- in tables include: filter, nat, mangle -j Jump to the specified target chain when the packet matches the current rule. -A Append rule to end of a chain -F Flush. Deletes all the rules in the selected table

-p Match protocol. Types include, icmp, tcp, udp, and all -s Match source IP address -d Match destination IP address -i Match "input" interface on which the packet enters. -o Match "output" interface on which the packet exits Example: iptables is being configured to allow the firewall to accept TCP packets coming in on interface eth0 from any IP address destined for the firewall's IP address of 192.168.1.1. The 0/0 representation of an IP address means any. iptables -A INPUT -s 0/0 -i eth0 -d 192.168.1.1 -p TCP -j ACCEPT Comm on TCP and UDP Match Criteria Switch Description -p tcp --sport __ TCP source port Can be a single value or a range in the format: start-port-number:end-port-number -p tcp --dport

TCP destination port Can be a single value or a range in the format: starting-port:ending-port -p tcp --syn (^) Used to identify a new TCP connection request ! --syn means, not a new connection request

Example: ICMP echo requests are restricted to no more than one per second. When tuned correctly, this feature allows you to filter unusually high volumes of traffic that characterize denial of service (DOS) attacks and Internet worms. You can expand on the limit feature of iptables to reduce your vulnerability to certain types of denial of service attack. Here a defense for SYN flood attacks was created by limiting the acceptance of TCP segments with the SYN bit set to no more than five per second. iptables -A INPUT -p tcp --syn -m limit --limit 5/s -i eth0 -j ACCEPT Exten ded Match Criteria Switch Description -m multiport --sport

A variety of TCP/UDP source ports separated by commas. Unlike when -m isn't used, they do not have to be within a range. -m multiport --dport

A variety of TCP/UDP destination ports separated by commas. Unlike when -m isn't used, they do not have to be within a range. -m multiport --ports

A variety of TCP/UDP ports separated by commas. Source and destination ports are assumed to be the same and they do not have to be within a range. -m --state __ (^) The most frequently tested states are: ESTABLISHED : The packet is part of a connection that has seen packets in both directions NEW : The packet is the start of a new connection RELATED : The packet is starting a new secondary connection. This is a common feature of such protocols such as an FTP data transfer, or an ICMP error. INVALID : The packet couldn't be identified. Could be due to insufficient system resources, or ICMP errors that don't match an existing data flow.

Expansion on the previous example: Here iptables is being configured to allow the firewall to accept TCP packets to be routed when they enter on interface eth0 from any IP address destined for IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination ports are port 80 (www/http) and 443 (https). The return packets from 192.168.1.58 are allowed to be accepted too. Instead of stating the source and destination ports, you can simply allow packets related to established connections using the -m state and --state ESTABLISHED options. iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP --sport 1024:65535 -m multiport --dport 80,443 -j ACCEPT iptables -A FORWARD -d 0/0 -o eth0 -s 192.168.1.58 -i eth1 -p TCP -m state --state ESTABLISHED -j ACCEPT User Defined Chains As you may remember, you can configure iptables to have user-defined chains. This feature is frequently used to help streamline the processing of packets. For example, instead of using a single, built-in chain for all protocols, you can use the chain to determine the protocol type for the packet and then hand off the actual final processing to a user-defined, protocol-specific chain in the filter table. In other words, you can replace a long chain with a stubby main chain pointing to multiple stubby chains, thereby shortening the total length of all chains the packet has to pass through. For example iptables -A INPUT -i eth0 -d 206.229.110.2 -j fast-input-queue iptables -A OUTPUT -o eth0 -s 206.229.110.2 -j fast-output-queue iptables -A fast-input-queue -p icmp -j icmp-queue-in iptables -A fast-output-queue -p icmp -j icmp-queue-out iptables -A icmp-queue-out -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT iptables -A icmp-queue-in -p icmp --icmp-type echo-reply -j ACCEPT C ustom Queu es Exam ple Chain Description INPUT The regular built-in INPUT chain in iptables

-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type 255 -j ACCEPT -A RH-Firewall-1-INPUT -p esp -j ACCEPT -A RH-Firewall-1-INPUT -p ah -j ACCEPT -A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited COMMIT Fedora comes with a program called lokkit that you can use to generate a very rudimentary firewall rule set. Like the service iptables save command, lokkit saves the firewall rules in a new /etc/sysconfig/iptables file for use on the next reboot. And also a GNOME desktop configurator in the system-config-firewall package. You can use the iptables-save and iptables-restore commands to assist you with the continued management of the server. Unlike the service iptables save command, which actually saves a permanent copy of the firewall's active configuration in the /etc/sysconfig/iptables file, iptables-save displays the active configuration to the screen in /etc/sysconfig/iptables format. If you redirect the iptables-save screen output to a file with the > symbol, then you can edit the output and reload the updated rules when they meet your new criteria with the iptables-restore command. iptables-save > firewall-config After editing the firewall-config file with the commands you need, you can reload it into the active firewall rule set with the iptables-restore command. iptables-restore < firewall-config Finally, you should permanently save the active configuration so that it will be loaded automatically when the system reboots: service iptables save

Troubleshooting iptables

Rules You track packets passing through the iptables list of rules using the LOG target added to the end of any rule. The results wind up in /var/log/messages. If you want to log only unwanted traffic, therefore, you have to add a matching rule with a DROP target immediately after the LOG rule outherwise you'll log both desired and unwanted traffic with no way of discerning between the two, because by default iptables doesn't state why the packet was logged in its log message.

iptables Won't Start The iptables startup script expects to find the /etc/sysconfig/iptables before it starts. If none exists, then symptoms include the firewall status always being stopped and the /etc/init.d/iptables script running without the typical [OK] or [FAILED] messages. If you have just installed iptables and have never applied a policy, then you will face this problem. Unfortunately, running the service iptables save command before restarting won't help either. You have to create this file and start the service. touch /etc/sysconfig/iptables chmod 600 /etc/sysconfig/iptables service iptables start Applying iptables firewall rules: [ OK ]