2012-03-21 50 views
1

隨着我阻止來自尼日利亞的垃圾郵件發送者的持續戰鬥,我已經使用GeoIP阻止了我的htaccess文件中的國家代碼,但仍有一些仍然能夠通過。若要從尼日利亞添加另一層我想通過IP的下面的列表迭代範圍,並阻止他們:有沒有更快的方式來檢查IP與IP塊

41.75.192.0 41.75.207.255 
41.138.160.0 41.138.191.255 
41.139.64.0 41.139.127.255 
41.155.0.0 41.155.127.255 
41.184.0.0 41.184.255.255 
41.189.0.0 41.189.31.255 
41.190.0.0 41.190.31.255 
41.203.64.0 41.203.95.255 
41.203.96.0 41.203.127.255 
41.204.224.0 41.204.255.255 
41.205.160.0 41.205.191.255 
41.206.0.0 41.206.31.255 
41.206.224.0 41.206.255.255 
41.211.192.0 41.211.255.255 
41.216.160.0 41.216.175.255 
41.217.0.0 41.217.127.255 
41.219.128.0 41.219.191.255 
41.219.192.0 41.219.255.255 
41.220.64.0 41.220.79.255 
41.221.112.0 41.221.127.255 
41.221.160.0 41.221.175.255 
62.173.32.0 62.173.63.255 
62.193.160.0 62.193.191.255 
80.248.0.0 80.248.15.255 
80.250.32.0 80.250.47.255 
81.18.32.0 81.18.47.255 
82.128.0.0 82.128.127.255 
195.166.224.0 195.166.255.255 
196.1.176.0 196.1.191.255 
196.29.208.0 196.29.223.255 
196.45.48.0 196.45.63.255 
196.45.192.0 196.45.255.255 
196.200.0.0 196.200.15.255 
196.200.64.0 196.200.79.255 
196.200.112.0 196.200.127.255 
196.207.0.0 196.207.15.255 
196.220.0.0 196.220.31.255 
212.100.64.0 212.100.95.255 
217.14.80.0 217.14.95.255 
217.117.0.0 217.117.15.255 

使用下面的代碼檢查每個範圍內有沒有更有效的方法或這樣的方式?

$range_start = ip2long("41.75.192.0"); 
$range_end = ip2long("41.75.207.255"); 
$ip   = ip2long($_SERVER['REMOTE_ADDR']); 
if ($ip >= $range_start && $ip <= $range_end) { 
    // blocked 
} 
+2

比單個'if'更高效嗎?你有什麼考慮? – Jon 2012-03-21 20:02:36

+0

在大多數情況下,在〜40個元素中搜索可以。如果你真的想優化它 - 使用二進制搜索。 – strkol 2012-03-21 20:05:48

+0

我必須遍歷39個IP塊,所以我想我可以做到這一點...我希望有一些光鮮的PHP技巧:) – Paul 2012-03-21 20:06:07

回答

2

如果您有權訪問.htaccess,您可以在配置級別上阻止它們,並且不需要在腳本中擔心它們。

order allow,deny 
deny from 41.75.192.0 
deny from 41.75.207.255 

... 

deny from 217.117.15.255 
allow from all 

讓Apache讓他們完全保持它們將省去一些麻煩。

+0

我想我對你的例子感到困惑,Apache如何拒絕IP範圍... – Paul 2012-03-21 20:18:00

+0

你需要在它自己的行上填寫每個IP。我只是用一個'...'顯示了一些IP,表示我正在跳過,而不是顯示列出的所有IP地址。您必須手動列出所有內容,但服務器將在完成所有工作後完成。 – 2012-03-21 20:20:44

+0

當你說全部,你的意思是所有的子網,如41.75.192.0,41.75.192.1,41.75.192.2等? – Paul 2012-03-21 20:26:15

0

做120(3x40)呼叫ip2long和40 ifs幾乎不會成爲任何服務器的瓶頸。你的問題的答案是:不,你不應該打擾它。

在網站的其他部分,您可能會遇到更大的性能問題。

1

隨着.htaccess它更容易

order allow,deny 
deny from 41.75.192. 
deny from 41.75.193. 
... 
deny from 41.75.207. 
allow from all 

PHP的解決方案:

$range_start = 192; 
$range_end = 207; 
$ip = preg_match('/41\.75\.(\d+)\.(\d+)/', $_SERVER['REMOTE_ADDR'], $m); 
if(intval($m[1]) >= $range_start && intval($m[1]) <= $range_end) { 
    //blocked 
} 
1

下面是用二進制搜索的例子,請記住,在$ IPS的元素必須進行排序。如果您在生產中使用它,請將$ ips中的ip2long('..')替換爲整數值 - 您無需每次都計算它們。

$ips = array(
      array(ip2long('41.75.192.0'), ip2long('41.75.207.255')), 
      array(ip2long('41.138.160.0'), ip2long('41.138.191.255')), 
      array(ip2long('41.139.64.0'), ip2long('41.139.127.255')) 
); 

$ip = '41.138.160.1'; 

function binary_search(array $a, $ip) { 
    $low = 0; 
    $high = count($a) - 1; 

    while ($low <= $high) { 
     $mid = ($low + $high)/2; 
     if ($a[$mid][0] > $ip) { 
      $high = $mid - 1; 
     } else if ($a[$mid][1] < $ip) { 
      $low = $mid + 1; 
     } else { 
       return true; 
     } 
    }   
    return false; 
} 

var_dump(binary_search($ips, ip2long($ip))); 

對40個元素數組使用二進制搜索,最多隻需要執行6次迭代。使用線性搜索 - 40.

0

有合法用戶試圖從已知垃圾郵件的IPS訪問您的網站。例如,Tor網絡允許您將您的流量通過3-4 ips來保護您的隱私。大多數用戶將此用於惡意目的,因此大多數ips都通過垃圾郵件列表列出。

最有效的方法是在用戶嘗試在您的網站上執行需要驗證的操作時,針對這些垃圾郵件數據庫之一檢查用戶的信息。支票不僅包括IP,還包括用戶名和電子郵件。我在我的網站上發現了垃圾郵件問題,並在使用其中一個鏈接後大幅下降。現在它幾乎降到零。我使用和Botscout。他們都有示例代碼和用於主要開源軟件的插件。

0

比任何編程語言版本或apache更高效的方法。 你可以做一個系統調用來添加或刪除php中的條目。

創建一套

ipset create test hash:net 

填充集!

ipset add test 212.100.64.0/19 
ipset add test 41.211.192.0/18 
ipset add test 196.200.112.0/20 
ipset add test 41.220.64.0/20 
ipset add test 41.206.0.0/19 
ipset add test 41.203.64.0/19 
ipset add test 62.173.32.0/19 
ipset add test 62.193.160.0/19 
ipset add test 41.138.160.0/19 
ipset add test 196.200.0.0/20 
ipset add test 217.14.80.0/20 
ipset add test 80.250.32.0/20 
ipset add test 195.166.224.0/19 
ipset add test 41.221.112.0/20 
ipset add test 41.216.160.0/20 
ipset add test 196.200.64.0/20 
ipset add test 41.139.64.0/18 
ipset add test 41.206.224.0/19 
ipset add test 41.221.160.0/20 
ipset add test 196.45.192.0/18 
ipset add test 41.217.0.0/17 
ipset add test 82.128.0.0/17 
ipset add test 41.203.96.0/19 
ipset add test 41.184.0.0/16 
ipset add test 41.205.160.0/19 
ipset add test 41.219.128.0/18 
ipset add test 41.204.224.0/19 
ipset add test 217.117.0.0/20 
ipset add test 196.220.0.0/19 
ipset add test 41.155.0.0/17 
ipset add test 196.1.176.0/20 
ipset add test 41.190.0.0/19 
ipset add test 80.248.0.0/20 
ipset add test 196.29.208.0/20 
ipset add test 41.189.0.0/19 
ipset add test 41.75.192.0/20 
ipset add test 81.18.32.0/20 
ipset add test 41.219.192.0/18 
ipset add test 196.45.48.0/20 
ipset add test 196.207.0.0/20 

使用set

iptables -I INPUT 1 -m set --match-set test src -j DROP 
iptables -I FORWARD 1 -m set --match-set test src -j DROP 

保存設置

ipset save -f /somewhere/something.txt 

裝入集

ipset load -f /somewhere/something.txt 

保存iptables的

iptables-save >/somewhere/something.txt 

負載的iptables

iptables-restore < /somewhere/something.txt 

的Cron或systemd自動保存/加載處理。

相關問題