How To : netfilter block IPs address from a blacklist - PeerBlock under GNU/Linux

This How To show how to use netfilter firewall in order to block ips thanks to a blacklist file, as PeerBlock does under Windows.

Intro

Configuration

Commands

Download a black list

We can download list from www.iblocklist.com. For example download the PrimaryThreats here. We need to unzip and format the file to make it work with iptables.

root@host:~# unzip file.zip
root@host:~# cut -d ":" -f2 PrimaryThreats.txt | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" > PrimaryThreats_V2.txt
root@host:~# sed -i 's/\x0D$/ /' PrimaryThreats_V2.txt
root@host:~# for i in `cat PrimaryThreats_V2.txt` ; do echo "$i"; iptables -I INPUT -m iprange --src-range "$i" -j DROP; done
root@host:~# iptables -I INPUT -p tcp -m tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT
root@host:~# iptables -I INPUT -p tcp -m multiport --sports 80,443,53 -m state --state ESTABLISHED,RELATED -j ACCEPT
root@host:~# iptables -I INPUT -i lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT
root@host:~# iptables -L -n -v | tr -s " " | grep -v "^ 0"
Licence Creative Commons
This website http://shebangthedolphins.net is licensed to the public under a licence Creative Commons Attribution licence.
Contact :