Block Tor IP Addresses

UPDATE: This is not efficient method. Use new method available at Block Tor IP Addresses with CSF Firewall

Here is a PHP script that you can run as cronjob every 1 hour to block Tor traffic on your server.

This require csf firewall installed on your server.

 /var/log/tor-block.log 2>&1


$torIPSource = "https://www.dan.me.uk/torlist/";
$torIPs = file_get_contents($torIPSource);
$torIPArray = explode("\n", $torIPs);

foreach ($torIPArray as $torIP) {
    $torIP = trim($torIP);
    if (empty($torIP)) { continue; }

    if (isValidIPv4($torIP)) {
        $blockCmd = "/usr/sbin/csf -d $torIP";
        echo $blockCmd . "\n";
        exec($blockCmd);
    }
}

function isValidIPv4($ip) {
    if (filter_var($ip, FILTER_VALIDATE_IP)) {
        return true;
    } else {
        return false;
    }
}

Set cronjon,

crontab -e

Add

5 * * * * /usr/local/bin/php /usr/serverok/block-tor.php > /var/log/tor-block.log 2>&1

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *