- Tạo thư mục anti_ddos và "ném" file index.php dưới đây vào:
index.php(Dùng để dò tìm các cuộc tấn công DDOS):
PHP Code:
<?php
$ad_ddos_query=10; //số lượng những yêu cầu trên giây để nhận diện sự tấn công DDOS
$ad_check_file='check.txt'; //Quá trình theo dõi được lưu lại trong file này
$ad_temp_file='all_ip.txt'; //Lưu lại tất cả các địa chỉ IP tạm thời
$ad_black_file='black_ip.txt'; //Địa chỉ IP của các máy có chứa Zombie
$ad_white_file='white_ip.txt'; //Địa chỉ IP sạch (Visitor)
$ad_dir='anti_ddos'; //Đây là thư mục chứa mã Anti-DDOS
$ad_num_query=0; // current number of queries per second from the file $check_file
$ad_sec_query=0; // Seconds from the file $check_file
$ad_end_defense=0; // Time ending protection from file $check_file
$ad_sec=date("s"); // current second
$ad_date=date("mdHis"); // current time
$ad_defense_time=10000; // Upon discovery of ddos attack, in seconds which stops monitoring?
if(!file_exists("{$ad_dir}/{$ad_check_file}") or !file_exists("{$ad_dir}/{$ad_temp_file}") or !file_exists("{$ad_dir}/{$ad_black_file}") or !file_exists("{$ad_dir}/{$ad_white_file}") or !file_exists("{$ad_dir}/anti_ddos.php")){
die("Not enough File.");
}
require("{$ad_dir}/{$ad_check_file}");
if ($ad_end_defense and $ad_end_defense>$ad_date){
require("{$ad_dir}/anti_ddos.php");
} else {
if($ad_sec==$ad_sec_query){
$ad_num_query++;
} else {
$ad_num_query='1';
}
if ($ad_num_query>=$ad_ddos_query){
$ad_file=fopen("{$ad_dir}/{$ad_check_file}","w");
$ad_end_defense=$ad_date+$ad_defense_time;
$ad_string='<?php $ad_end_defense='.$ad_end_defense.'; ?>';
fputs($ad_file,$ad_string);
fclose($ad_fp);
} else {
$ad_file=fopen("{$ad_dir}/{$ad_check_file}","w");
$ad_string='<?php $ad_num_query='.$ad_num_query.'; $ad_sec_query='.$ad_sec.'; ?>';
fputs($ad_file,$ad_string);
fclose($ad_fp);
}
}
?>
PHP Code:
<?php
function getIP() {
if(getenv("HTTP_X_FORWARDED_FOR") and preg_match("/^[0-9\.]*?[0-9\.]+$/is",getenv("HTTP_X_FORWARDED_FOR")) and getenv("HTTP_X_FORWARDED_FOR")!='127.0.0.1') {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else {
$ip = getenv("REMOTE_ADDR");
}
return $ip;
}
$ad_ip=getIP();
$ad_source=file("{$ad_dir}/{$ad_black_file}");
$ad_source=explode(' ',$ad_source[0]);
if (in_array($ad_ip,$ad_source)){die();}
$ad_source=file("{$ad_dir}/{$ad_white_file}");
$ad_source=explode(' ',$ad_source[0]);
if (!in_array($ad_ip,$ad_source)){
$ad_source=file("{$ad_dir}/{$ad_temp_file}");
$ad_source=explode(' ',$ad_source[0]);
if (!in_array($ad_ip,$ad_source)){
$ad_file=fopen("{$ad_dir}/{$ad_temp_file}","a+");
$ad_string=$ad_ip.' ';
fputs($ad_file,"$ad_string");
fclose($ad_fp);
?>
The site is currently being subjected DDOS attack if you are not machine-zombie attacker's site, click on the button, otherwise you IP (<?=$ad_ip?>) will be blocked!!!
<form method="post">
<input type="submit" name="ad_white_ip" value="Tranla12a@yahoo.com">
</form>
<?php
die();
}
elseif ($_POST['ad_white_ip']){
$ad_file=fopen("{$ad_dir}/{$ad_white_file}","a+");
$ad_string=$ad_ip.' ';
fputs($ad_file,"$ad_string");
fclose($ad_fp);
}
else {
$ad_file=fopen("{$ad_dir}/{$ad_black_file}","a+");
$ad_string=$ad_ip.' ';
fputs($ad_file,"$ad_string");
fclose($ad_fp);
die();
}
}
?>
- Bạn có thể dùng thêm cả .htaccess nếu Server của bạn chạy trên Linux hỗ trợ .htaccess
Deny from 111.111.111.111 222.222.222.222
- Xong rồi bây giờ bạn chỉ việc Upload (FTP) thư mục anti_ddos lên Host
Và quan trọng nhất là thêm vào file cần bảo vệ đoạn code sau
require ( "anti_ddos/index.php");