"Whip me, beat me, make me use ipchains" Linux Netfilter-Howto, Second Place Motto
This is a short presentation on filtering IP packets for Linux system security. Many of the details are glossed over due to time considerations. All the real details are available in the source code for those seeking the ultimate knowledge. Some variations of the details can be found in the HOWTOs or in the files found under /usr/doc. But make sure YOU have some understanding of what is going on before implementing ANYTHING in this presentation because misapplication of this knowledge is like playing Russian roulette with a shotgun. Often the best security for the novice is to TURN the system off when not in use.1. What is a packet and why does it need to be filtered?
The Internet works with the TCP/IP protocol which places information in datagrams that most people call packets.
[ sender | recipient | X | data] <- pseudo
datagram
(X explained later)
(not to scale)
The sender address is in 32 bit number as is the recipient address. So you see addresses like 192.38.24.12. So if you send a packet rom 192.38.24.12 to 10.11.12.13, how does 192.38.24.12 know what to do with the datagram?
2. Your ethernet card's Robert DeNiro impression!
Ethernet cards listen to signals and puts the bits into a datagram. Then the computer has to determine if You talking to me?'. If the datagram is not meant for this computer it does the forgetta-boudit' and goes on it's merry way. But if the datagram is meant for this computer, it has more work to do!
3. Ports and Sockets, a limited liability company
Burried deep in the datagram is a port number (the X stuff). Think as the IP address as the street address of a large apartment building and the port as the apartment number. So the system know knows it has datagram destined for a certain apartment, er, port on the system. Look in /etc/services to match port number for services.
Port numbers are fairly standardized i.e. 25 for SMTP, & 80 for http. New services like Samba's SWAT usually make you add their number. Any number above 1024 is non-privledged and any below 1024 are to be suspected.
4. Quick summerizining example
User with pid 666 on 192.67.134.28 is using Mozilla over port 1550 to get the home page of greatgeek.com. Via DNS, 192.67.134.28 get the IP address of www.greatgeek.com and sends a message to port 80 at that address that says:
The http demon on www.greatgeek.com gets the receiving IP & port numbers from the request and sends the requested information.
That was very sanitized, streamlined, and misses many wonderful things like packet sequence numbers, CSMA/CD mechanics, and ICMP packets.
5. So (AGAIN) why filter packets?
Not all packets come with good intentions or ask for things you want to give out freely. And each packet takes processing power to process. Many denial of service type attacks make your system so busy handling bogus requests that it can do nothing else.
The sooner you dump the unwanted packets the better for overall processing. IP filter examines the header of the packet and decides the fate of the packet.
TCP wrappers are a comparatively high level way of denying access. System administrator sets up a /etc/hosts.allow and a /etc/hosts.deny to say who can and can not use programs like in.telnetd and in.ftpd.
Good things about wrappers: Do limit access
Bad things about wrappers: Too much packet processing before they
work and only protect programs started by inetd.
A BSD-ism to determine which packets are allowed.
Good things about IPFW: works okay.
Bad things about IPFW mainly BSD.
A very blatent copy of IPFW.
Good things about IPFWADM works, popular, and does good
, and
has masquerading.
Bad things about IPFWADM not really low enough, fragment
reassembly lacking, 32 bit counters.
Very good kernel support and a little easier to setup than ipfwadm.
Good things about ipchains: really precise tool
Bad things about ipchains Flushing rules leave you vulnerable,
easy to misconfigure, code not modular (can't plug in MAC address
filtering, etc.), and slow on rejects.
Netfilter is a packet mangling framework that provide ways of passing different packet types to different parts of the kernel. Also has cool NAT and RNAT features.
The reason that Netfilter will replace Ipchains is that it is better designed. With Ipchains, each packet runs through the entire gauntlet of rules. Netfilter expedites packet handling with a more straight forward design which puts less load on the kernel.
Good things about netfilter: Modularity, faster rejects.
Bad things about netfilter: unproven.
Netfilter has a series of queues that can hand packets off much faster to the daemons, is modular, and is part of the 2.3.15 and later kernels.
11. Netfiler will have IPCHAINS compatibility
Netfilter will be able to use your ipchains rules, so you will not lose everything you develop ipchains.
12. Where Do I start with IP Chains
Basically IPCHAINS does four things. It can just drop or DENY a packet, It can REJECT which is a DENIED packet and an ICMP message sent to the sender saying service is denied, FORWARD which pushed the packet down another wire, and REDIRECT which send the packet to a port different that the one requested.
Your kernel must have these defined:
2.0 kernels : CONFIG_EXPERIMENTAL, CONFIG_FIREWALL,
CONFIG_IP_FIREWALL, and CONFIG_IP_FIREWALL_CHAINS
2.2 kernels : CONFIG_FIREWALL and CONFIG_IP_FIREWALL
A primitive rc.firewall to be called from your rc.local
#!/bin/sh # allow ip packet to be forwarded between NICS echo "1" > /proc/sys/net/ipv4/ip_forward # Kill off syncookies attack echo "1" > /proc/sys/net/ipv4/tcp_syncookies # Do not allow any packets to be forwarded ... /sbin/ipchains -P forward DENY # ... but well let the masquerading systems /sbin/ipchains -A forward -s 10.0.0.0/8 -j MASQ # Allow ftp to masquerade /sbin/modprobe ip_masq_ftp
14. Where to get an easier start with IPCHAINS
Two good places to go are:
www.linux-firewall-tools.com: Javascript to generate custom rule set.
ipchains.nerdherd.org: three good scripts -> Standalone, masquerade, and routeable.
Mason and gfcc are also good tools after trying the above. Cruse the freshmeat.net site for more tools!
Packets go though a 'chain of rules' to finally get accepted. It is not efficient but NETFILTER will take care of that in the 2.4 Kernels
start | flow of packet-> | end | ||||||
---|---|---|---|---|---|---|---|---|
filter | Checksum | Sanity | Input | Demasq | Routing | Forward | Output | Accept |
IF BAD | DENY | DENY | DENY/REJECT | goto Output | Local Process | DENY/REJECT | DENY/REJECT |