当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP防CC攻击实现代码总结

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-22 11:31:24 浏览: 评论:0 

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

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

Tags: PHP防CC攻击 PHP防止快速刷新

分享到: