Using a blockscript to get rid of problematic netblocks

Posted in: Linux
By dePoPo
Sep 5, 2009 - 11:18:56 AM

This setup will provide you with a editable list of ip's or blocks you want to completely ban from your server.

Step 1; The script

Create a file named blockscript
In this example it is located in /root, so the full path will be /root/blockscript
After creating flag it as executable ( chmod +x blockscript )

# clear old rules to prevent tons of doubles
iptables --flush

# process blocklist
BLOCKLIST=/root/blocklist
IPS=$(grep -Ev "^#" $BLOCKLIST)
for b in $IPS
do
        iptables -A INPUT -s $b -j DROP
done
# list results for the user
iptables -L

Step 2; The list

Create a simple textfile with the name and location specified in the script (/root/blocklist in this example) containing the ip numbers you want to block, or the subnets to block in the following format;

example:

61.113.106.97
67.99.96.10
92.48.127.0/24
83.206.77.0/24
83.233.30.0/24
83.233.165.0/24
85.25.62.0/24
94.102.63.11
141.149.218.108
194.192.81.0/24
222.146.51.125

Step 3; Automate

Place a link in your cron.hourly to refresh the active list from your file

cd/etc/cron.daily
ln -sf /root/blockscript blockscript




Visitor Comments