2012-09-23 34 views
1
<?php 
if (!isset($_SESSION)) { 
     session_start(); 
} 
// anti flood protection 
if($_SESSION['last_session_request'] > time() - 2){ 
     // users will be redirected to this page if it makes requests faster than 2 seconds 
     header("location: http://www.example.com/403.html"); 
     exit; 
} 
$_SESSION['last_session_request'] = time(); 
?> 

我已經測試過這個腳本,因爲你高了一秒,它會無故地重定向到http://www.example.com/403.htmlPHP中的防洪洪水DDoS

有誰能告訴我爲什麼?

+1

調試提示:回聲變量if語句,以確保它是什麼,你認爲它是前。 – sachleen

+0

試着讓你的第一行僅僅是沒有if語句的session_start() – sachleen

+1

我剛剛測試過這個腳本 - 它完美的工作。它不會爲我自己重新定向。 – David

回答

-1

只是改變><

<?php   

if (!isset($_SESSION)) { 
     session_start(); 
} 
// anti flood protection 
if($_SESSION['last_session_request'] < time() - 2){ 
     // users will be redirected to this page if it makes requests faster than 2 seconds 
     header("location: http://www.example.com/403.html"); 
     exit; 
} 
$_SESSION['last_session_request'] = time(); 
?> 
+0

這將重定向請求更慢,然後2秒的用戶。 – xdazz

+0

現在它總是重定向 – Unknown3r

+0

是不是最初的problam? –

8

讓我們想想這個邏輯一秒鐘......

攻擊者的請求已經被髮送到Web服務器,並通過與PHP腳本。 導致DDoS攻擊失敗的瓶頸是網絡服務器。

DDoS攻擊背後的想法就是 - 導致拒絕服務,其中網站/服務器無法處理任何新的請求。因此,在這種情況下,這種方法是不合理的。 您需要爬上請求處理的階梯。

如果您有一臺服務器可供您處理,它更容易。您可以簡單地在內核防火牆/ iptables上實施速率限制規則。 但假設你沒有訪問權限,Apache仍然可以使用 - 儘管效率不高。

在.htaccess中實現規則是更好的解決方案,但仍不完善。 但是,根據DDoS攻擊,開發人員無法解決這個問題。

+0

然後我有我自己的私人服務器,但iptables沒有限制每個ip的6個請求 – Unknown3r

+0

真的嗎?您可以實現一個規則集,該規則集將阻止x個具有特定狀態的x秒內的請求。請參閱:http://www.debian-administration.org/articles/187 – nand

+0

我已經嘗試過這種方式。我需要限制每個ip automaticaly的請求 – Unknown3r

0

什麼spudinksi說仍然是成立的,但是這是你找什麼:

<?php 


if (!isset($_SESSION)) { 
     session_start(); 
} 

if($_SESSION['last_session_request'] > (time() - 5)){ 
    if(empty($_SESSION['last_request_count'])){ 
     $_SESSION['last_request_count'] = 1; 
    }elseif($_SESSION['last_request_count'] < 5){ 
     $_SESSION['last_request_count'] = $_SESSION['last_request_count'] + 1; 
    }elseif($_SESSION['last_request_count'] >= 5){ 
      header("location: http://www.example.com/403.html"); 
      exit; 
     } 
}else{ 
    $_SESSION['last_request_count'] = 1; 
} 

$_SESSION['last_session_request'] = time(); 

?> 
+5

這是行不通的。 Flooders只會刪除cookies – apscience

-1

對於停止的DDos添加一個空的路線該IP,像這樣:

route add -host ???.???.???.??? reject 
+0

這實際上並不「停止」任何意義上的DDoS;它只是忽略它。 – duskwuff

-2

這個代碼不爲這樣的捲曲循環工作。會議將在每個curl exec上重新創建;

for ($i=0;$i<999999999999999;$i++){ 

    /**/ 
    $c=curl_init(); 
    curl_setopt($c,CURLOPT_URL,"URL YOU WANT ATTACK"); 
    curl_setopt($c,CURLOPT_DNS_USE_GLOBAL_CACHE,TRUE);//dns 
    curl_setopt($c,CURLOPT_HEADER,0);//get the header 
    curl_setopt($c,CURLOPT_CONNECTTIMEOUT ,10);//get the header 
    curl_setopt($c,CURLOPT_NOBODY,0);//and *only* get the header 
    curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it 
    curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url 
    curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko Firefox/11.0'); 
    curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded;charset=UTF-8')); 

    echo "\n $i"; 

} 
0

有一個腳本叫IOSec,這個腳本已經很老了,但它可能有幫助。

+1

這種聯繫是不活躍了 –

1

我正在使用一個很好的反洪水腳本,不需要cookie(適用於webservices)。這對於高級DDOS攻擊來說並不完美,但它足以防止初學者攻擊和自動多重請求。

使用它之前,需要創建一個「ctrl」文件和一個「鎖定」子文件夾的「洪水」文件夾。還需要設置正確的權限。

已經由我測試過。

define("SCRIPT_ROOT", dirname(__FILE__)); 

// number of allowed page requests for the user 
define("CONTROL_MAX_REQUESTS", 3); 
// time interval to start counting page requests (seconds) 
define("CONTROL_REQ_TIMEOUT", 2); 
// seconds to punish the user who has exceeded in doing requests 
define("CONTROL_BAN_TIME", 5); 
// writable directory to keep script data 
define("SCRIPT_TMP_DIR", SCRIPT_ROOT."/flood"); 
// you don't need to edit below this line 
define("USER_IP", $_SERVER["REMOTE_ADDR"]); 
define("CONTROL_DB", SCRIPT_TMP_DIR."/ctrl"); 
define("CONTROL_LOCK_DIR", SCRIPT_TMP_DIR."/lock"); 
define("CONTROL_LOCK_FILE", CONTROL_LOCK_DIR."/".md5(USER_IP)); 
@mkdir(CONTROL_LOCK_DIR); 
@mkdir(SCRIPT_TMP_DIR); 


if (file_exists(CONTROL_LOCK_FILE)) { 
    if (time()-filemtime(CONTROL_LOCK_FILE) > CONTROL_BAN_TIME) { 
     // this user has complete his punishment 
     unlink(CONTROL_LOCK_FILE); 
    } else { 
     // too many requests 
     echo "<h1>DENIED</h1>"; 
     echo "Please try later."; 
     touch(CONTROL_LOCK_FILE); 
     die; 
    } 
} 

function antiflood_countaccess() { 
    // counting requests and last access time 
    $control = Array(); 

    if (file_exists(CONTROL_DB)) { 
     $fh = fopen(CONTROL_DB, "r"); 
     $control = array_merge($control, unserialize(fread($fh, filesize(CONTROL_DB)))); 
     fclose($fh); 
    } 

    if (isset($control[USER_IP])) { 
     if (time()-$control[USER_IP]["t"] < CONTROL_REQ_TIMEOUT) { 
      $control[USER_IP]["c"]++; 
     } else { 
      $control[USER_IP]["c"] = 1; 
     } 
    } else { 
     $control[USER_IP]["c"] = 1; 
    } 
    $control[USER_IP]["t"] = time(); 

    if ($control[USER_IP]["c"] >= CONTROL_MAX_REQUESTS) { 
     // this user did too many requests within a very short period of time 
     $fh = fopen(CONTROL_LOCK_FILE, "w"); 
     fwrite($fh, USER_IP); 
     fclose($fh); 
    } 
    // writing updated control table 
    $fh = fopen(CONTROL_DB, "w"); 
    fwrite($fh, serialize($control)); 
    fclose($fh); 
} 

從這裏摘自:https://github.com/damog/planetalinux/blob/master/www/principal/suscripcion/lib/antiflood.hack.php

-2

會議可能無法正常工作,因爲我們沒有會話coockie。

我推薦這樣

$load = sys_getloadavg(); 
if ($load[0] > 20) { 
    header('HTTP/1.1 503 Too busy, try again later'); 
    die('Server too busy. Please try again later.'); 
} 

或者你可以

shell_exec('/sbin/iptables -I INPUT -j DROP -s ' . $ip); 

爲ddosing $ IP

+0

因此,在這種情況下,一旦服務器的負載超過20%......沒有人會訪問您的服務器....哦,我倒覺得不是 – Mayhem

+0

@MayhemРазберись,чтотакое平均負載и вчемонизмеряется! –

0

這將計數重新加載頁面&又節省時間在3秒後.... 如果它給出了問題或容易讓新手繞過然後發表評論..

if(empty($_SESSION['AFsys_time']) || $_SESSION['AFsys_time'] == '0') { 
    $tGoal = time() + 3; // Pluss Seconds 
    $_SESSION['AFsys_time'] = $tGoal; 
} 

if(empty($_SESSION['AFsys_pReloads']) || $_SESSION['AFsys_pReloads'] == 0) { $_SESSION['AFsys_pReloads'] = 1; } else { $_SESSION['AFsys_pReloads']++; }; 

if($_SESSION['AFsys_time'] < time()){ 
    $_SESSION['AFsys_time'] = 0; // Session Reset 
    $_SESSION['AFsys_pReloads'] = 0; // Session Reset 
} 

if($_SESSION['AFsys_pReloads'] > '5' && $_SESSION['AFsys_time'] > time()){ 
    $_SESSION['AFsys_time'] = 0; // Session Reset 
    $_SESSION['AFsys_pReloads'] = 0; // Session Reset 
    header("location: http://www.example.com/403.html"); 
    exit; 
}