网站地图    收藏   

主页 > php专栏 > php应用 >

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

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:

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

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

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论