2013-11-15 38 views
0

我有一個問題是,如果我跑我的地方wampserver這個腳本則顯示正確的結果,如果它已經被列入黑名單,但如果我在我的服務器上運行等等.../usr/bin/php path-to-phpscript.php然後顯示這個ip被列入所有bls名字中,這是不正確的。DNSBL黑名單查詢

<?php 

$bls = array("b.barracudacentral.org", 
"bl.score.senderscore.com", 
"pbl.spamhaus.org", 
"sbl.spamhaus.org", 
"xbl.spamhaus.org", 
"zen.spamhaus.org", 
"dbl.spamhaus.org", 
"sbl-xbl.spamhaus.org", 
); 

$ ip = '62.213.183.192';

如果(isset($ IP)){

if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) || false === filter_var($ip, FILTER_VALIDATE_URL)) { 

    if (false === filter_var($ip, FILTER_VALIDATE_URL )) { 
     $iptosplit = gethostbyname($ip); 
    } 
    else { 
     $iptosplit = $ip; 
    } 
    $splitip = explode (".", $iptosplit); 
    $iptolookup = "$splitip[3].$splitip[2].$splitip[1].$splitip[0]"; 
    $counter=1; 
    $blList = array(); 
    foreach ($bls as $rbl) { 
     //echo "<tr>"; 
     $rbllookup = $iptolookup.".".$rbl; 
     $lookup = gethostbyname($rbllookup); 

     if ($lookup != $rbllookup || $lookup == $ip) { 
      $qtxtresult = dns_get_record("$rbllookup", DNS_TXT); 
      if (! isset($qtxtresult[0]['txt'])) { 
       $qtxtresult[0]['txt'] = ""; 
      } 
      $blList[$counter]=$ip.' is listed in ('.$rbl.')'; 
      echo '...........Listed in -'.$rbl.'<br />';     
     } 
     echo str_repeat(" ", 24), "\n"; 

     $counter++; 
    } 
} 

} ?>

回答

0

此代碼已被納入DNSBL的源代碼段的任何快速響應將高度可以理解如果IP被列入黑名單.tornevall.org(通過https://dnsbl.tornevall.org/download/下載),並返回一個數組。它也支持ipv6。如果它被列入黑名單,$結果[3]具有正值,其是一個位掩碼,並且位掩碼值取決於類型黑名單型的IP已被標記爲的。

在這個例子中,我使用一個隨機的IP,我知道在域被列入黑名單。不幸的是,該腳本不支持TXT查找,但如果您只需要阻止來自列入黑名單的IP地址的連接,則此腳本也非常基本。

function rblresolve ($ip = '', $rbldomain = '') 
{ 
     if (!$ip) {return false;}      // No data should return nothing 
     if (!$rbldomain) {return false;}  // No rbl = ignore 

     // Old function (during betatesting we want to keep those rows so we can fall back if something fails) 
     // $returnthis = explode('.', gethostbyname(implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain));   // Not ipv6-compatible! 
     // if (implode(".", $returnthis) != implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain) {return $returnthis;} else {return false;} 

     // New ipv6-compatible function 
     $returnthis = (long2ip(ip2long($ip)) != "0.0.0.0" ? explode('.', gethostbyname(implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain)) : explode(".", gethostbyname(v6arpa($ip) . "." . $rbldomain))); 
     // 127-bug-checking 
      if (implode(".", $returnthis) != (long2ip(ip2long($ip)) != "0.0.0.0" ? implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain : v6arpa($ip) . "." . $rbldomain)) {return $returnthis;} else {return false;} 
} 

function v6arpa($ip) 
{ 
     $unpack = unpack('H*hex', inet_pton($ip)); 
     $hex = $unpack['hex']; 
     return implode('', array_reverse(str_split($hex))); 
} 

$result = rblresolve("117.197.11.203", "dnsbl.tornevall.org"); 
print_r($result); 

返回:

Array 
(
    [0] => 127 
    [1] => 0 
    [2] => 0 
    [3] => 67