php专区

 首页 > php专区 > PHP应用 > 常用功能 > PHP防CC攻击实现代码总结 - php高级应用

PHP防CC攻击实现代码总结 - php高级应用

分享到:
【字体:
导读:
          CC攻击就是对方利用程序或一些代理对您的网站进行不间断的访问,造成您的网站处理不了而处于当机状态,下面我们来总结一些防CC攻击的php实例代码,各位朋友可参考.例1,代码如下://代理...

PHP防CC攻击实现代码总结

CC攻击就是对方利用程序或一些代理对您的网站进行不间断的访问,造成您的网站处理不了而处于当机状态,下面我们来总结一些防CC攻击的php实例代码,各位朋友可参考.

例1,代码如下:

  1. //代理IP直接退出  
  2. emptyempty($_SERVER['HTTP_VIA']) or exit('Access Denied');  
  3. //防止快速刷新  
  4. session_start();  
  5. $seconds = '3'//时间段[秒]  
  6. $refresh = '5'//刷新次数  
  7. //设置监控变量  
  8. $cur_time = time();  
  9. if(isset($_SESSION['last_time'])){  
  10.     $_SESSION['refresh_times'] += 1;  
  11. }else{  
  12.     $_SESSION['refresh_times'] = 1;  
  13.     $_SESSION['last_time'] = $cur_time;  
  14. }  
  15. //处理监控结果  
  16. if($cur_time - $_SESSION['last_time'] < $seconds){  
  17.     if($_SESSION['refresh_times'] >= $refresh){  
  18.         //跳转至攻击者服务器地址  
  19.         header(sprintf('Location:%s''http://127.0.0.1'));  
  20.         exit('Access Denied');  
  21.     } //开源代码phpfensi.com 
  22. }else{  
  23.     $_SESSION['refresh_times'] = 0;  
  24.     $_SESSION['last_time'] = $cur_time;  

例二,代码如下:

  1. $P_S_T = $t_array[0] + $t_array[1];  
  2. $timestamp = time(); 
  3.  
  4. session_start();  
  5. $ll_nowtime = $timestamp ;  
  6. if (session_is_registered('ll_lasttime')){  
  7. $ll_lasttime = $_SESSION['ll_lasttime'];  
  8. $ll_times = $_SESSION['ll_times'] + 1;  
  9. $_SESSION['ll_times'] = $ll_times;  
  10. }else{  
  11. $ll_lasttime = $ll_nowtime;  
  12. $ll_times = 1;  
  13. $_SESSION['ll_times'] = $ll_times;  
  14. $_SESSION['ll_lasttime'] = $ll_lasttime;  
  15. }  
  16. if (($ll_nowtime - $ll_lasttime)<3){  
  17. if ($ll_times>=5){  
  18. header(sprintf("Location: %s",'http://127.0.0.1'));  
  19. exit;  
  20. }  
  21. }else{  
  22. $ll_times = 0;  
  23. $_SESSION['ll_lasttime'] = $ll_nowtime;  
  24. $_SESSION['ll_times'] = $ll_times;  

一个实例我自己亲测的,日志分析:

  1. [2011-04-16 03:03:13] [client 61.217.192.39] /index.php 
  2. [2011-04-16 03:03:13] [client 61.217.192.39] /index.php 
  3. [2011-04-16 03:03:13] [client 61.217.192.39] /index.php 
  4. [2011-04-16 03:03:13] [client 61.217.192.39] /index.php 
  5. [2011-04-16 03:03:12] [client 61.217.192.39] /index.php 
  6. [2011-04-16 03:03:12] [client 61.217.192.39] /index.php 
  7. [2011-04-16 03:03:12] [client 61.217.192.39] /index.php 
  8. [2011-04-16 03:03:11] [client 61.217.192.39] /index.php 
  9. [2011-04-16 03:03:11] [client 61.217.192.39] /index.php 
  10. [2011-04-16 03:03:11] [client 61.217.192.39] /index.php 
  11. [2011-04-16 03:03:10] [client 61.217.192.39] /index.php 
  12. [2011-04-16 03:03:10] [client 61.217.192.39] /index.php 

下面是PHP方法,将以下代码另存为php文件,然后首行include入你的common.php文件中,代码如下:

  1. /* 
  2.  * 防CC攻击,不死版. 
  3.  * 
  4.  * 如果每秒内网站刷新次数超过2次,延迟5秒后访问。 
  5.  */ 
  6.  
  7. $cc_min_nums = '1';                    //次,刷新次数 
  8. $cc_url_time = '5';                    //秒,延迟时间 
  9. //$cc_log = 'cc_log.txt';                //启用本行为记录日志 
  10. $cc_forward = 'http://localhost';    //释放到URL 
  11.  
  12. //-------------------------------------------- 
  13.  
  14. //返回URL 
  15. $cc_uri = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']); 
  16. $site_url = 'http://'.$_SERVER ['HTTP_HOST'].$cc_uri
  17.  
  18. //启用session 
  19. if( !isset( $_SESSION ) ) session_start(); 
  20. $_SESSION["visiter"] = true; 
  21. if ($_SESSION["visiter"] <> true){ 
  22.  echo ""
  23.  //header("Location: ".$cc_forward); 
  24.  exit
  25.  
  26. $timestamp = time();  
  27. $cc_nowtime = $timestamp ; 
  28. if (session_is_registered('cc_lasttime')){ 
  29.  $cc_lasttime = $_SESSION['cc_lasttime']; 
  30.  $cc_times = $_SESSION['cc_times'] + 1; 
  31.  $_SESSION['cc_times'] = $cc_times
  32. }else
  33.  $cc_lasttime = $cc_nowtime
  34.  $cc_times = 1; 
  35.  $_SESSION['cc_times'] = $cc_times
  36.  $_SESSION['cc_lasttime'] = $cc_lasttime
  37.  
  38. //获取真实IP 
  39. if (isset($_SERVER)){ 
  40.  $real_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
  41. }else
  42.  $real_ip = getenv("HTTP_X_FORWARDED_FOR"); 
  43.  
  44. //print_r($_SESSION); 
  45.  
  46. //释放IP 
  47. if (($cc_nowtime - $cc_lasttime)<=0){ 
  48.  if ($cc_times>=$cc_min_nums){         
  49.  if(!emptyempty($cc_log))    cc_log(get_ip(), $real_ip$cc_log$cc_uri);    //产生log 
  50.  echo "Wait please, try again later!"
  51.  //printf('您的刷新过快,请稍后。'); 
  52.  //header("Location: ".$cc_forward); 
  53.  exit
  54.  } 
  55. }else
  56.  $cc_times = 0; 
  57.  $_SESSION['cc_lasttime'] = $cc_nowtime
  58.  $_SESSION['cc_times'] = $cc_times
  59.  
  60. //记录cc日志 
  61. function cc_log($client_ip$real_ip$cc_log$cc_uri){     
  62.  $temp_time = date("Y-m-d H:i:s", time() + 3600*8); 
  63.  
  64.  $temp_result = "[".$temp_time."] [client ".$client_ip."] ";     
  65.  if($real_ip$temp_result .= " [real ".$real_ip."] "
  66.  $temp_result .= $cc_uri . "rn"
  67.  
  68.  $handle = fopen ("$cc_log""rb"); 
  69.  $oldcontent = fread($handle,filesize("$cc_log")); 
  70.  fclose($handle); 
  71.  
  72.  $newcontent = $temp_result . $oldcontent
  73.  $fhandle=fopen("$cc_log""wb"); 
  74.  fwrite($fhandle,$newcontent,strlen($newcontent)); 
  75.  fclose($fhandle); 
  76.  
  77. //获取在线IP 
  78. function get_ip() { 
  79.  global $_C
  80.  
  81.  if(emptyempty($_C['client_ip'])) { 
  82.  if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { 
  83.  $client_ip = getenv('HTTP_CLIENT_IP'); 
  84.  } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { 
  85.  $client_ip = getenv('HTTP_X_FORWARDED_FOR'); 
  86.  } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { 
  87.  $client_ip = getenv('REMOTE_ADDR'); 
  88.  } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { 
  89.  $client_ip = $_SERVER['REMOTE_ADDR']; 
  90.  } 
  91.  $_C['client_ip'] = $client_ip ? $client_ip : 'unknown'
  92.  } 
  93.  return $_C['client_ip']; 
  94. ?> 

这样就可以基础工业防止了,但是如果更高级占的就没办法,大家可尝试使用相关硬件防火强来设置.

分享到:
关闭php执行系统指令函数 - php高级应用
关闭php执行系统指令函数 php的四个函数exec,shell_exec,system,passthru可以执行系统指令,对系统安全构成威胁,如果不用的话可以将其关闭,代码如下: vim /etc/php.ini 去掉disable_functions前注释,编辑内容如下: disable_functions = exec,shell_exec,system,passthru,popen 友情提示,有...
php网站被挂木马修复方法总结 - php高级...
php网站被挂木马修复方法总结 php网站被挂木马修复是次要的最要的是怎么修复之后不再让木马再注入到你的网站才是重要的,下面我来总结一下php网站被挂木马修复与之后防止网站再次给挂木马的方法. 在linux中我们可以使用命令来搜查木马文件,到代码安装目录执行下面命令: find ./ -iname "*.ph...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……