Blocking IPs with iptables firewall
May 27th, 2009A couple of days ago I made a post extolling the virtues of using the route command to block IP addresses. While this works, there is an even more powerful firewall actually built right into linux. It’s called iptables.
Iptables is an extremely powerful firewall built into linux, and can be a bit daunting to use at first. There are lots of GUIs available for iptables, but I told myself I would figure out most of linux through the CLI (command line interface) since at some point my x-windows may fail to load for whatever reason and I may be forced into runlevel 3 to fix it. (runlevel 3 is pure CLI, no gui whatsoever. If you ever used a DOS based computer, it’s like that, but way more powerful. If you want to try it out, open up a terminal and type init 3, to get back to x-windows environment, type init 5)
At any rate, you can add ips to iptables and tell it what to do with them. If you type iptables -L (might need to be su) in terminal, then you will see your current firewall setup.
To add an IP address to be blocked, you will need to issue this command(again might need to be su):
iptables -I INPUT -s [address to be blocked] -j DROP
Here is a breakdown of the command. Iptables is broken down into 3 chains. INPUT, FORWARD, and OUTPUT. INPUT is the connections your PC receives, FORWARD is connections or packets that it forwards to another host or IP (if your pc was functioning as a firewall or router for your network) and OUTPUT is connections or packets leaving your PC to the outside world. In my above example, the -I INPUT flag is telling it to insert the following rule in the INPUT chain. the -s flag is to tell it the source IP address or host you are making the rule for. the -j flag stands for jump, and tells iptables what to do if the packet received matches the rule. In this case we want to DROP the packet, or stop it from being received.
So for instance, if I wanted to block IP 25.25.25.25 from connecting to my pc, then I would enter this command as su in the terminal:
iptables -I INPUT -s 25.25.25.25 -j DROP
That would enter a rule in my firewall to take any packet coming into my PC from 25.25.25.25 and drop refuse it.
I have also written a simple shell script that will do this for me so I don’t have to enter the whole command every time, I just named the scriopt ipblock and I can type ipblock 25.25.25.25 and it will automatically enter the rule in iptables.
Here is the script in case anyone might find it useful. I use the sudo command in my script, so if you don’t have sudo on your system for whatever reason, you can run it as su -c, but you will need root access to manipulate the iptables.
ipblock.sh
#!/bin/bash #all this script does is add an offending IP address to IPTABLES for #blocking. sudo iptables -I INPUT -s $1 -j DROP echo IP ADDRESS of $1 blocked.
Now just save that as ipblock, and execute a
chmod +x ipblock
to make it executable. Then you can execute it with ./ipblock [ip address], et voila, ip address blocked. You can verify by executing iptables -L from the terminal, which will list the rules in your iptables firewall.
EDIT: Almost forgot, you’ll need to save these changes or they go away on your next reboot, which is a huge pain. The best way to do this on Fedora Core 10 is with a service call to the iptables service. As root:
[Adam@Deepblue ~]$ service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]