- Install
ipset
:
apt-get install ipset
- Create new ipset:
ipset create tor iphash
- Read Tor Exit Node List and add to ipset:
curl -sSL "https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=$(curl icanhazip.com)" | sed '/^#/d' | while read IP; do
ipset -q -A tor $IP
done
Note: This should run as daily cronjob.
- Block ipset with
iptables
:
iptables -A INPUT -m set --match-set tor src -j DROP
@MarcosT96, according to https://www.dan.me.uk/tornodes the URL does not take an "ip" parameter. The only parameter is ?exit, which will list exit nodes only (which is likely what most people want anyway).
@mtheophy, surely we are interested in IPv6 as well :) Many servers have IPv6 nowadays. I grab the list to a temporary file and then
grep
the IP addresses I'm interested in. For IPv4 I usegrep -E "^([0-9]+\.){3}[0-9]+$"
and for IPv6 I usegrep -E "^[23]...:"
(IPv6 regex matching is a complex topic, but this suffices for me for now) and then use it to populate the ipv6-tor-nodes set.