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;  
  25. } 

例二,代码如下:

  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;  
  25. } 

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

  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.  
  27. $timestamp = time();  
  28. $cc_nowtime = $timestamp ; 
  29. if (session_is_registered('cc_lasttime')){ 
  30.  $cc_lasttime = $_SESSION['cc_lasttime']; 
  31.  $cc_times = $_SESSION['cc_times'] + 1; 
  32.  $_SESSION['cc_times'] = $cc_times; 
  33. }else{ 
  34.  $cc_lasttime = $cc_nowtime; 
  35.  $cc_times = 1; 
  36.  $_SESSION['cc_times'] = $cc_times; 
  37.  $_SESSION['cc_lasttime'] = $cc_lasttime; 
  38. } 
  39.  
  40. //获取真实IP 
  41. if (isset($_SERVER)){ 
  42.  $real_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
  43. }else{ 
  44.  $real_ip = getenv("HTTP_X_FORWARDED_FOR"); 
  45. } 
  46.  
  47. //print_r($_SESSION); 
  48.  
  49. //释放IP 
  50. if (($cc_nowtime - $cc_lasttime)<=0){ 
  51.  if ($cc_times>=$cc_min_nums){         
  52.  if(!emptyempty($cc_log))    cc_log(get_ip(), $real_ip, $cc_log, $cc_uri);    //产生log 
  53.  echo "Wait please, try again later!"; 
  54.  //printf('您的刷新过快,请稍后。'); 
  55.  //header("Location: ".$cc_forward); 
  56.  exit; 
  57.  } 
  58. }else{ 
  59.  $cc_times = 0; 
  60.  $_SESSION['cc_lasttime'] = $cc_nowtime; 
  61.  $_SESSION['cc_times'] = $cc_times; 
  62. } 
  63.  
  64. //记录cc日志 
  65. function cc_log($client_ip, $real_ip, $cc_log, $cc_uri){     
  66.  $temp_time = date("Y-m-d H:i:s", time() + 3600*8); 
  67.  
  68.  $temp_result = "[".$temp_time."] [client ".$client_ip."] ";     
  69.  if($real_ip) $temp_result .= " [real ".$real_ip."] "; 
  70.  $temp_result .= $cc_uri . "rn"; 
  71.  
  72.  $handle = fopen ("$cc_log", "rb"); 
  73.  $oldcontent = fread($handle,filesize("$cc_log")); 
  74.  fclose($handle); 
  75.  
  76.  $newcontent = $temp_result . $oldcontent; 
  77.  $fhandle=fopen("$cc_log", "wb"); 
  78.  fwrite($fhandle,$newcontent,strlen($newcontent)); 
  79.  fclose($fhandle); 
  80. } 
  81.  
  82. //获取在线IP 
  83. function get_ip() { 
  84.  global $_C; 
  85.  
  86.  if(emptyempty($_C['client_ip'])) { 
  87.  if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { 
  88.  $client_ip = getenv('HTTP_CLIENT_IP'); 
  89.  } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { 
  90.  $client_ip = getenv('HTTP_X_FORWARDED_FOR'); 
  91.  } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { 
  92.  $client_ip = getenv('REMOTE_ADDR'); 
  93.  } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { 
  94.  $client_ip = $_SERVER['REMOTE_ADDR']; 
  95.  } 
  96.  $_C['client_ip'] = $client_ip ? $client_ip : 'unknown'; 
  97.  } 
  98.  return $_C['client_ip']; 
  99. } 
  100. ?> 

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

分享到:
关闭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技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……