Skip to content

Instantly share code, notes, and snippets.

@N3X15
Last active August 29, 2015 14:15
Show Gist options
  • Save N3X15/47b3eeb9434d084187c5 to your computer and use it in GitHub Desktop.
Save N3X15/47b3eeb9434d084187c5 to your computer and use it in GitHub Desktop.
(Old) Tor DNSBL for PHP
$bl = new DNSBL();
$bl->no_silence=true;
$bl->dnsbl_list=array(
'Tor'=>
array(
'url'=>'tor.dnsbl.sectoor.de',
'returns'=>array(
'127.0.0.1'=>'tor exit node'
)
),
'SORBS HTTP'=>
array(
'url'=>'http.dnsbl.sorbs.net',
'returns'=>array(
'127.0.0.2'=>'http Proxy',
'127.0.0.3'=>'socks Proxy',
'127.0.0.4'=>'misc Proxy'
)
),
'DNSEL'=>
array(
'url'=>'80.'.$bl->GetMyIP(true).'.ip-port.exitlist.torproject.org',
'returns'=>array(
'127.0.0.2'=>'tor exit node'
)
)
);
$bl->check(true);
<?php
/* MIT license, (c) 2008-2015 Rob Nelson.
This is older than Christ, so may or may not work.
Older than composer, so you need ADODB and Savant2 for the verbatim version.
*/
include_once('adodb/adodb.inc.php');
include_once('Savant2.php');
class DNSBL
{
public $dnsbl_list = array();
private $whitelist = array();
function DNSBL()
{
$this->ip = $_SERVER['REMOTE_ADDR'];
// ADODB connection
$this->sql =& NewADOCOnnection('mysql');
$this->sql->PConnect("HOST",'USER','PASSWORD','SCHEMA');
if(!$this->sql)
{
die('<h1>MySQL Connection Error.</h1>');
}
$this->savant= new Savant2();
$this->debug[]= "Initialized ".date("r",fileatime(__FILE__));
}
function GetMyIP($reversed=false)
{
$ipa="YOUR.IP.HERE";
$this->debug[]=$_SERVER['HTTP_HOST'].' => '.$ipa.' or '.var_export($lol,true);
$ipa=explode('.',$ipa);
return "{$ipa[3]}.{$ipa[2]}.{$ipa[1]}.{$ipa[0]}";
}
function block()
{
if($this->no_silence===true) {
$this->savant->assign('blockreason',$this->blockreason);
$this->savant->assign('ip',$this->ip);
$this->savant->addPath('template','/host/PHP_INCLUDE/_templates/');
$this->savant->display('banned.template.php');
}
exit();
}
function whitelist($ip)
{
$this->whitelisted[]=$ip;
$this->debug[]='Added '.$ip.' to whitelist.';
}
function check($autoblock=true)
{
$this->debug[]= "Checking {$this->ip}...<br />";
if(in_array($this->ip,$this->whitelist)) return true;
$res=$this->sql->Execute("SELECT * FROM WebDNSBL WHERE ip='{$this->ip}'");
$reccount=$res->RecordCount();
$this->debug[]="Number of records for IP {$this->ip}: $reccount";
if($reccount>0)
{
$ipd=$res->FetchRow();
$this->ipd=$ipd;
if($ipd['proxy']>0)
{
$this->debug[]="FAILED. {$this->ip} is a {$ipd['proxy']}";
$this->blockreason=$ipd['reason'];
if($autoblock===true) $this->block(); // Already in cache. Blockit.
return false;
}
$this->debug[]="Passed.";
return true;
} else {
$ipa=explode('.',$this->ip);
if($ipa[0].$ipa[1]!="192168"){
$rip="{$ipa[3]}.{$ipa[2]}.{$ipa[1]}.{$ipa[0]}";
$proxy=0;
foreach($this->dnsbl_list as $dnsbl)
{
$qstr="{$rip}.{$dnsbl['url']}";
$tor=gethostbyname($qstr);
$this->debug[]=$qstr.' => '.$tor;
if($tor==$qstr)
{
$this->debug[]='Not a proxy. '.$tor;
//$proxy=0;
} else {
$this->blockreason=$dnsbl['returns'][$tor];
$this->debug[]='PROXY FOUND. '.$this->blockreason;
$proxy=1;
}
}
$qry="INSERT INTO WebDNSBL SET ip='%s',date=%d,proxy=%d,reason='%s'";
$q=sprintf($qry,$this->ip,time(),$proxy,$this->blockreason);
$this->sql->Execute($q);
$this->debug[]='SQL: '.$q;
if($proxy>0 && $autoblock===true) $this->block();
if($proxy>0) return false;
$this->debug[]='Passed.';
return true;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment