Home >Backend Development >PHP Tutorial >PHP anti-CC attack implementation code summary_PHP tutorial

PHP anti-CC attack implementation code summary_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:11:14776browse

CC attack means that the other party uses programs or some agents to continuously access your website, causing your website to be unable to process and in a crashed state. Below we will summarize some PHP example codes to prevent CC attacks. Friends, you can refer to.

Example 1

The code is as follows Copy code
 代码如下 复制代码

//代理IP直接退出
empty($_SERVER['HTTP_VIA']) or exit('Access Denied');
//防止快速刷新
session_start();
$seconds = '3'; //时间段[秒]
$refresh = '5'; //刷新次数
//设置监控变量
$cur_time = time();
if(isset($_SESSION['last_time'])){
    $_SESSION['refresh_times'] += 1;
}else{
    $_SESSION['refresh_times'] = 1;
    $_SESSION['last_time'] = $cur_time;
}
//处理监控结果
if($cur_time - $_SESSION['last_time'] < $seconds){
if($_SESSION['refresh_times'] >= $refresh){
        //跳转至攻击者服务器地址
        header(sprintf('Location:%s', 'http://127.0.0.1'));
        exit('Access Denied');
    }
}else{
    $_SESSION['refresh_times'] = 0;
    $_SESSION['last_time'] = $cur_time;
}

//Exit the proxy IP directly
empty($_SERVER['HTTP_VIA']) or exit('Access Denied');
//Prevent fast refresh
session_start();
$seconds = '3'; //Time period [seconds]
$refresh = '5'; //Number of refreshes
//Set monitoring variables
$cur_time = time();
if(isset($_SESSION['last_time'])){
$_SESSION['refresh_times'] += 1;
}else{
$_SESSION['refresh_times'] = 1;
$_SESSION['last_time'] = $cur_time;
}
//Process monitoring results
if($cur_time - $_SESSION['last_time'] < $seconds){
If($_SESSION['refresh_times'] >= $refresh){
//Jump to the attacker's server address
header(sprintf('Location:%s', 'http://127.0.0.1'));
exit('Access Denied');
}  
}else{
$_SESSION['refresh_times'] = 0;
$_SESSION['last_time'] = $cur_time;
}

Example 2

 代码如下 复制代码

$P_S_T = $t_array[0] + $t_array[1];
$timestamp = time();

session_start();
$ll_nowtime = $timestamp ;
if (session_is_registered('ll_lasttime')){
$ll_lasttime = $_SESSION['ll_lasttime'];
$ll_times = $_SESSION['ll_times'] + 1;
$_SESSION['ll_times'] = $ll_times;
}else{
$ll_lasttime = $ll_nowtime;
$ll_times = 1;
$_SESSION['ll_times'] = $ll_times;
$_SESSION['ll_lasttime'] = $ll_lasttime;
}
if (($ll_nowtime - $ll_lasttime)<3){
if ($ll_times>=5){
header(sprintf("Location: %s",'http://127.0.0.1'));
exit;
}
}else{
$ll_times = 0;
$_SESSION['ll_lasttime'] = $ll_nowtime;
$_SESSION['ll_times'] = $ll_times;
}

The code is as follows Copy code
$P_S_T = $t_array[0] + $t_array[1];
$timestamp = time(); session_start();
$ll_nowtime = $timestamp;
if (session_is_registered('ll_lasttime')){
$ll_lasttime = $_SESSION['ll_lasttime'];
$ll_times = $_SESSION['ll_times'] + 1;
$_SESSION['ll_times'] = $ll_times;
}else{
$ll_lasttime = $ll_nowtime;
$ll_times = 1;
$_SESSION['ll_times'] = $ll_times;
$_SESSION['ll_lasttime'] = $ll_lasttime;
}
if (($ll_nowtime - $ll_lasttime)<3){
if ($ll_times>=5){
header(sprintf("Location: %s",'http://127.0.0.1'));
exit;
}
}else{
$ll_times = 0;
$_SESSION['ll_lasttime'] = $ll_nowtime;
$_SESSION['ll_times'] = $ll_times;
}

An example I personally tested

Log Analysis

[2011-04-16 03:03:13] [client 61.217.192.39] /index.php
[2011-04-16 03:03:13] [client 61.217.192.39] /index.php
[2011-04-16 03:03:13] [client 61.217.192.39] /index.php
[2011-04-16 03:03:13] [client 61.217.192.39] /index.php
[2011-04-16 03:03:12] [client 61.217.192.39] /index.php
[2011-04-16 03:03:12] [client 61.217.192.39] /index.php
[2011-04-16 03:03:12] [client 61.217.192.39] /index.php
[2011-04-16 03:03:11] [client 61.217.192.39] /index.php
[2011-04-16 03:03:11] [client 61.217.192.39] /index.php
[2011-04-16 03:03:11] [client 61.217.192.39] /index.php
[2011-04-16 03:03:10] [client 61.217.192.39] /index.php
[2011-04-16 03:03:10] [client 61.217.192.39] /index.php

The following is the PHP method: save the following code as a php file, and then include the first line into your common.php file.

The code is as follows Copy code

/*
* Anti-CC attack, depressed to death, immortal version.
*
* If the website is refreshed more than 2 times per second, access will be delayed for 5 seconds.
*/

$cc_min_nums = '1'; // times, refresh times
$cc_url_time = '5'; //Seconds, delay time
//$cc_log = 'cc_log.txt'; //Enable this behavior log
$cc_forward = 'http://localhost'; //Release to URL

//---------------------------------------------

//Return URL
$cc_uri = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
$site_url = 'http://'.$_SERVER ['HTTP_HOST'].$cc_uri;

//Enable session
if( !isset( $_SESSION ) ) session_start();
$_SESSION["visiter"] = true;
if ($_SESSION["visiter"] <> true){
echo "<script>setTimeout("window.location.href ='$cc_forward';", 1);</script>";
//header("Location: ".$cc_forward);
exit;
}

$timestamp = time();
$cc_nowtime = $timestamp ;
if (session_is_registered('cc_lasttime')){
$cc_lasttime = $_SESSION['cc_lasttime'];
$cc_times = $_SESSION['cc_times'] + 1;
$_SESSION['cc_times'] = $cc_times;
}else{
$cc_lasttime = $cc_nowtime;
$cc_times = 1;
$_SESSION['cc_times'] = $cc_times;
$_SESSION['cc_lasttime'] = $cc_lasttime;
}

//Get the real IP
if (isset($_SERVER)){
$real_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$real_ip = getenv("HTTP_X_FORWARDED_FOR");
}

//print_r($_SESSION);

//Release IP
if (($cc_nowtime - $cc_lasttime)<=0){
if ($cc_times>=$cc_min_nums){ 
if(!empty($cc_log)) cc_log(get_ip(), $real_ip, $cc_log, $cc_uri); //Generate log
echo "Wait please, try again later!<script>setTimeout("window.location.href ='$site_url';", 5000);</script>";
//printf('Your refresh is too fast, please wait.');
//header("Location: ".$cc_forward);
exit;
}
}else{
$cc_times = 0;
$_SESSION['cc_lasttime'] = $cc_nowtime;
$_SESSION['cc_times'] = $cc_times;
}

//记录cc日志
function cc_log($client_ip, $real_ip, $cc_log, $cc_uri){   
 $temp_time = date("Y-m-d H:i:s", time() + 3600*8);
 
 $temp_result = "[".$temp_time."] [client ".$client_ip."] ";   
 if($real_ip) $temp_result .= " [real ".$real_ip."] ";
 $temp_result .= $cc_uri . "rn";
 
 $handle = fopen ("$cc_log", "rb");
 $oldcontent = fread($handle,filesize("$cc_log"));
 fclose($handle);
 
 $newcontent = $temp_result . $oldcontent;
 $fhandle=fopen("$cc_log", "wb");
 fwrite($fhandle,$newcontent,strlen($newcontent));
 fclose($fhandle);
}

//获取在线IP
function get_ip() {
 global $_C;
 
 if(empty($_C['client_ip'])) {
 if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
 $client_ip = getenv('HTTP_CLIENT_IP');
 } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
 $client_ip = getenv('HTTP_X_FORWARDED_FOR');
 } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
 $client_ip = getenv('REMOTE_ADDR');
 } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
 $client_ip = $_SERVER['REMOTE_ADDR'];
 }
 $_C['client_ip'] = $client_ip ? $client_ip : 'unknown';
 }
 return $_C['client_ip'];
}
?>

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629601.htmlTechArticleCC攻击就是对方利用程序或一些代理对您的网站进行不间断的访问,造成您的网站处理不了而处于当机状态,下面我们来总结一些防CC攻击的...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn