2015-06-12 31 views
0

在我的服務器上發出一個不間斷的分佈式smtp auth攻擊波,提示我禁止非指定的IP連接到我的服務器上的smtp並通過它發送郵件。非常有效。 (說明:http://sysadmintips.in/advanced/csf/exim在cPanel/WHM Centos服務器上將Google Access授權給SMTP,而不通過SMTP認證?

但是我現在無法使用雙因素身份驗證(這很痛苦,因爲我爲此設置了遠程設置,因此我無法使用Google Mail(Gmail)爲新帳戶「發送郵件」客戶端)或在我的服務器上切換smtp身份驗證。

我的其他選項是將Google Mail的IP地址白名單。

谷歌搜索發現這種方式來檢索使用這些方針的東西(我已經從該頁面複製:https://support.google.com/a/answer/60764?hl=en):在當前谷歌的IP範圍

nslookup -q=TXT _spf.google.com 8.8.8.8 

這將返回域的列表包括在Google的SPF記錄中,例如: _netblocks.google.com,_netblocks2.google.com,_netblocks3.google.com

現在查看與這些域相關的DNS記錄,一次一個,如下所示:

nslookup -q=TXT _netblocks.google.com 8.8.8.8 
nslookup -q=TXT _netblocks2.google.com 8.8.8.8 
nslookup -q=TXT _netblocks3.google.com 8.8.8.8 

這些命令的結果包含當前的地址範圍。

我可以使用這些輸出來爲/etc/csf/csf.smtpauth生成有用的內容嗎?

我可以編寫一些代碼在PHP中執行此操作,並以root身份將其作爲cron任務運行,但什麼格式可以接受? csf.smtpauth是否接受IP範圍聲明?它是否支持IPV6 IP?

任何更改後,我還需要強制自動重新啓動csf和lfd,以便新IP正在使用中。 PHP可以以root身份運行嗎?

謝謝!

回答

0

已解決。

我已經編碼了以下PHP,它查詢Google的SPF記錄,然後僅在需要時才用新的SMTP取代現有的SMTP Auth塊。然後它創建一個文件作爲bash腳本的標誌來重新啓動防火牆。

請注意,/etc/csf/csf.smtpauth接受IPV4和IPV6地址和CIDR地址範圍。

// Grab current Google SPF IPs... 
$dns = dns_get_record('_spf.google.com', DNS_TXT); 
if (!$dns) 
{ 
    echo "FAILED TO RETRIEVE DNS RECORD<br />\n"; 
    exit; 
} 

// The variable in which to store the results 
$ranges = array(); 

// Of interest in particular to us is... 
$val = $dns[0]['txt']; 

preg_match_all("/include:[^\s]+\s/", $val, $matches); 

if (sizeof($matches[0]) <= 0) 
{ 
    echo "BAD DATA RECEIVED OR FAILED TO DECODE DATA<br />\n"; 
    exit; 
} 

foreach ($matches[0] as $match) 
{ 
    $match = trim($match); 
    $domain = trim(preg_replace("/include\:/", "", $match)); 

    // Now do it all again for this domain to get the IP range 
    $dns = dns_get_record($domain, DNS_TXT); 

    if (!$dns) 
    { 
     echo "DNS LOOKUP FAILURE AT PASS 2<br />\n"; 
     exit; 
    } 

    $val = $dns[0]['txt']; 
    preg_match_all("/ip\d:[^\s]+\s/", $val, $ips); 

    if (sizeof($ips[0])<=0) 
    { 
     // At time of writing this is entirely possible as _netblocks3.google.com 
     // currently holds NO IP ranges 
    } 
    else 
    { 
     foreach ($ips[0] as $ip) 
     { 
      $ip = trim($ip); 
      if ($ip <> '') 
      { 
       $ip = preg_replace("/ip\d\:/", "", $ip); 
       $ranges[] = $ip; 
      } 
     } 
    } 
} 

// To be here means we made it without a major problem. Form the new IP range for 
// the smtp auth file (/etc/csf/csf.smtpauth) and compare with the existing. Update only if there has 
// been a change. Also update only if there are at least N ranges found. 
// When I wrote this there were 11 IPV4 ranges and 6 IPV6 ranges so setting 
// low limit to 10 
$limit = 10; 
$filename = '/etc/csf/csf.smtpauth'; 

if (sizeof($ranges) < $limit) 
{ 
    echo "NOT UPDATING RANGES, TOO FEW DISCOVERED, PROBLEM?"; 
    exit; 
} 

$filerange = "# GOOGLE SPF RESULTS START\n"; 
$filerange .= join("\n", $ranges); 
$filerange .= "\n# GOOGLE SPF RESULTS END"; 

// Read in existing conf file 
$econf = file_get_contents($filename); 
if (sizeof($econf)<=0) 
{ 
    echo "FAILED TO READ $filename<br />\n"; 
    exit; 
} 

// Extract the block 
if (!preg_match("/\# GOOGLE SPF RESULTS START.+\# GOOGLE SPF RESULTS END/s", $econf, $matches)) 
{ 
    echo "FAILED TO FIND EXISTING BLOCK. CORRUPT AUTH FILE?<br />\n"; 
    exit; 
} 

if ($filerange == $matches[0]) 
{ 
    // IT'S THE SAME DO NOT UPDATE IT!; 
    exit; 
} 

// Replace the block entirely 
$econf = preg_replace("/\# GOOGLE SPF RESULTS START.+\# GOOGLE SPF RESULTS END/s", $filerange, $econf); 

// Write out the new file contents 
file_put_contents($filename, $econf); 

// Trigger a CSF/LFD restart by creating trigger file. 
touch("restartcsflfd"); 

然後創建的每個上面運行時一個cron任務後不久,運行這個shell腳本和:

#!/bin/bash 
if [ -f /path-to-file/restartcsflfd ]; 
then 
    csf -r 
    /etc/init.d/lfd restart 
    rm -f restartcsflfd 
    echo "RE-STARTED CSF and LFD" 
fi