Chapter 3 of the Linux Networking Cookbook, "Building a Linux Firewall", lays out a number of iptables scripts for different uses, such as a server firewall, an Internet gateway, sharing an Internet connection, and host firewalls. It includes start-stop scripts so you can easily bring your firewall up and down, and have it start automatically at boot.
The other chapters in the book include iptables rules for specific services and situations, but there is one useful function that didn't make it into the book, and that is some simple iptables rules for blocking brute-force login attacks, such as the automated SSH attacks that infest the Internet. These will continue to wander and annoy long after the sociopaths who launched them are dead and forgotten. All you need are two rules per service, like this:
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m
recent --set --name SSH
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m
recent --update --seconds 60 --hitcount 8 --rttl --name SSH -j DROP
This limits all incoming connections to port 22 to 8 per minute. To prevent
locking yourself out, you can create a whitelist:
iptables -N ssh-whitelist
iptables -A ssh-whitelist -s [your-ip-address] -m recent --remove --name
SSH -j ACCEPT
Then modify your limiting rules like this:
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m
recent --set --name SSH
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -j
ssh-whitelist
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m
recent --update --seconds 60 --hitcount 8 --rttl --name SSH -j DROP
You can easily adapt this for any service. The --set --name option creates an arbitrary name, so you can call it anything you want. Just make sure it matches the --name in your DROP rule.
Recipe 7.15 tells how to use the excellent DenyHosts utility, which is another good way to do the same job.














