We will be using iptables to implement our firewalls. Note that the OUTPUT filter chain applies only to packets whose source is the firewall system itself. Similarly, the INPUT chain applies only to packets destined to sockets (software implementation of ports) on the firewall system. All packets whose source and destination are not the firewall system go through the FORWARD chain only.
Issues to keep in mind are:
See /usr/doc/linux-3.13/filesystems/proc.txt for more information on /proc/sys/net/ipv4/.#!/bin/sh # Begin rc.iptables # Insert connection-tracking modules modprobe nf_conntrack modprobe xt_LOG # Enable Broadcast Echo Protection echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts # Disable Source Routed Packets echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route echo 0 > /proc/sys/net/ipv4/conf/default/accept_source_route # Enable TCP SYN Cookie Protection echo 1 > /proc/sys/net/ipv4/tcp_syncookies # Disable ICMP Redirect Acceptance echo 0 > /proc/sys/net/ipv4/conf/default/accept_redirects # Do not send Redirect Messages echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects # Drop Spoofed Packets coming in on an interface, where responses # would result in the reply going out a different interface. echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter echo 1 > /proc/sys/net/ipv4/conf/default/rp_filter # Log packets with impossible addresses. echo 1 > /proc/sys/net/ipv4/conf/all/log_martians echo 1 > /proc/sys/net/ipv4/conf/default/log_martians # Be verbose on dynamic ip-addresses (not needed in case of static IP) echo 2 > /proc/sys/net/ipv4/ip_dynaddr # Disable Explicit Congestion Notification # too many routers are still ignorant echo 0 > /proc/sys/net/ipv4/tcp_ecn # Set a known state iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # These lines are here in case rules are already in place and the # script is ever rerun on the fly. I want to remove all rules and # pre-existing user defined chains before I implement new rules. iptables -F iptables -X iptables -Z iptables -t nat -F # Allow local-only connections iptables -A INPUT -i lo -j ACCEPT # Allow pings iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT # Allow ssh from instructor's station in 265 lab iptables -A INPUT -s 192.168.1.150 -p tcp --dport 22 -j ACCEPT # Permit answers on already established connections # and permit new connections related to established ones # (e.g. port mode ftp) iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
If you will be listening for UDP requests (-p udp), you will of course omit the options "-m conntrack --ctstate NEW,ESTABLISHED,RELATED" since UDP is not a connection (state) oriented protocol.iptables -A INPUT -p tcp --dport 80 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j ACCEPT
While this is technically true, in practice both UDP and ICMP "connections" effectively exist as long as there is two-way socket traffic within a certain time frame. This is important for maximum efficiency on media streaming using UDP, and because ICMP can be used for error reporting on other connections, it can be treated as a "related" connection. It is important to realize that our firewall ruleYou can update your firewall rules at any time by running /etc/rc.d/rc.iptables because it flushes the rules before inserting the new ones.iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTcan apply to UDP and ICMP packets as well. Therefore it should be at the end of the filter chain.
The best procedure is to always check that the length of incoming data is appropriate. But these observations lead us to suspect that firewalling by packet length may also be an effective means of thwarting some exploits:
iptables -A INPUT -p tcp --dport 80 -m length --length max-length+1:65535 -j DROPNote that the maximum allowable packet length max-length is the length of the IP protocol data unit. Therefore, if you are using Wireshark to determine the maximum packet length ("frame bytes on wire"), you should subtract 14 bytes for the Ethernet header; that will be the value you should use for max-length.
Be sure that this rule precedes any rule which might accept an http request. Note that any packet received after the initial "3-way handshake" (SYN, SYN/ACK and ACK) is part of an "established" connection. The example was using HTTP; in principle this could be applied to other services (but not all!) as well.
Note that the length firewall rule can sometimes be used as a defense during a distributed denial of service attack (DDoS). Most packets received during a DDoS attack have either uniform length, uniform time to live (TTL), or both. A similar rule dropping on TTL would look like:
iptables -A INPUT -p tcp --dport 80 -m ttl --ttl-eq ttl-of-incoming-packets -j DROP
iptables -A OUTPUT -p udp --sport 53 -m limit --limit 10/second -j DROPAdjusting the limit is tricky, since many web pages refer to dozens of servers. Since by design the largest DNS packet is 315 bytes long, this limit would restrict any given PC to about 25 Kbits/second. The attacks referred to above reached a peak of over 300 Gbits/second, which with throttled clients would require a botnet of 12 million PCs, a number far in excess of the "thousands" used.
The bad news is you have to reinstall your entire system. I can't say that strongly enough. You may be able to find and fix a couple of things that have been touched, but there are probably a whole host of things which may not be found for a long time, if at all. Many times, the programs which come out of the rootkit make it impossible to clean up without reformatting. So the things to do are:
Compare the output with that of cat /proc/net/tcp and cat /proc/net/udp.
©2015, Kenneth R. Koehler. All Rights Reserved. This document may be freely reproduced provided that this copyright notice is included.
Please send comments or suggestions to the author.