Skip to content

Instantly share code, notes, and snippets.

@thomasv314
Created March 21, 2013 16:17
Show Gist options
  • Save thomasv314/5214326 to your computer and use it in GitHub Desktop.
Save thomasv314/5214326 to your computer and use it in GitHub Desktop.
script that adapts mikhailian's implementation of blocking tor nodes... http://mikhailian.livejournal.com/48051.html
#! /bin/bash
# script to block tor nodes
# original credit for this goes to mikhailian: http://mikhailian.livejournal.com/48051.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ "${1}" == "" ]; then
echo This script should be run with: ./block-tor ip-address-here
exit 0
else
echo Blocking all tor exit nodes for: $1
fi
echo Created ipset: tor
# create a new set for individual IP addresses
ipset -N tor iphash
echo Downloading tor block list for: $IP
# get a list of Tor exit nodes that can access $YOUR_IP, skip the comments and read line by line
wget -q https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=$1 -O -|sed '/^#/d' |while read IP
do
# add each IP address to the new set, silencing the warnings for IPs that have already been added
echo Blocking: $IP
ipset -q -A tor $IP
done
echo Adding tor ipset to IPTABLES
# filter our new set in iptables
iptables -A INPUT -m set --match-set tor src -j DROP
echo TOR connections should now be blocked. This script will have to be run again on server restart.
@thomasv314
Copy link
Author

This script requires ipset be installed on the server.

On ubuntu: sudo apt-get install ipset

http://ipset.netfilter.org/

Usage:

curl -o block-tor      https://gist.github.com/tommyvyo/5214326/raw/a9975005c766d49c7a80e26d76b477cf7ab25ed4/block-tor.sh

chmod u+x block-tor

sudo ./block-tor your-ip-address

@thomasv314
Copy link
Author

Original credit for this goes to @mikhailian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment