PHP攻击网站防御代码-以及攻击代码反译

飘逸的风13年前 (2012-12-10)程序10165
这是我无意中攻击一个网站发现的一个代码PHP有效的拦截住我的DDOS 


<?php  
//查询禁止IP  
$ip =$_SERVER['REMOTE_ADDR'];  
$fileht=".htaccess2";  
if(!file_exists($fileht))file_put_contents($fileht,"");  
$filehtarr=@file($fileht);  
if(in_array($ip."\r\n",$filehtarr))die("Warning:"."<br>"."Your IP address are forbided by some reason, IF you have any question Pls emill to shop@mydalle.com!");  

//加入禁止IP  
$time=time();  
$fileforbid="log/forbidchk.dat";  
if(file_exists($fileforbid))  
{ if($time-filemtime($fileforbid)>60)unlink($fileforbid);  
else{  
$fileforbidarr=@file($fileforbid);  
if($ip==substr($fileforbidarr[0],0,strlen($ip)))  
{  
if($time-substr($fileforbidarr[1],0,strlen($time))>600)unlink($fileforbid);  
elseif($fileforbidarr[2]>600){file_put_contents($fileht,$ip."\r\n",FILE_APPEND);unlink($fileforbid);}  
else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}  
}  
}  
}  
//防刷新  
$str="";  
$file="log/ipdate.dat";  
if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);  
if(!file_exists($file))file_put_contents($file,"");  
$allowTime = 120;//防刷新时间  
$allowNum=10;//防刷新次数  
$uri=$_SERVER['REQUEST_URI'];  
$checkip=md5($ip);  
$checkuri=md5($uri);  
$yesno=true;  
$ipdate=@file($file);  
foreach($ipdate as $k=>$v)  
{ $iptem=substr($v,0,32);  
$uritem=substr($v,32,32);  
$timetem=substr($v,64,10);  
$numtem=substr($v,74);  
if($time-$timetem<$allowTime){  
if($iptem!=$checkip)$str.=$v;  
else{  
$yesno=false;  
if($uritem!=$checkuri)$str.=$iptem.$checkuri.$time."1\r\n";  
elseif($numtem<$allowNum)$str.=$iptem.$uritem.$timetem.($numtem+1)."\r\n";  
else  
{  
if(!file_exists($fileforbid)){$addforbidarr=array($ip."\r\n",time()."\r\n",1);file_put_contents($fileforbid,$addforbidarr);}  
file_put_contents("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."\r\n",FILE_APPEND);  
$timepass=$timetem+$allowTime-$time;  
die("Warning:"."<br>"."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");  
}  
}  
}  
}  
if($yesno) $str.=$checkip.$checkuri.$time."1\r\n";  
file_put_contents($file,$str);  
?> 

相关文章

php_curl扩展模块无法正常加载的处理方法

好不容易通过自己的学习动手写了一套网站自动收录的程序,正当我把源码都传到服务器作演示和大家共享的时候,忽然发现网址无法正常添加,提示错误Fatal error:Call to undefined fu...

提高PHP编程效率的53个要点

用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说...

emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]

emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]

emlog SyntaxHighlighter 代码高亮插件,可以把代码贴在代码框中,确定后自动生成高亮代码。 注:本插件已升级,修复在代码很长换行的时候左边的行数不增加问题;插入代码里有HTML标...

thinkphp 使用U方法自动生成URL超链接

ThinkPHP U方法 U 方法是 ThinkPHP 内置的一个快捷方法,可以根据系统 URL 模式配置动态的生成智能的 URL 地址。 由于 ThinkPHP 支持各种不同的 URL...

分享:PHP获取MAC地址的类库

PHP获取MAC地址的类库代码: <?php /** 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址...

评论列表

飘逸的风
13年前 (2013-06-13)

@akisx:对流量攻击是没有作用的。

akisx
akisx
13年前 (2013-06-12)

这是防御cc攻击的吧,流量攻击没法用代码防。

飘逸的风 回复:
@akisx:对流量攻击是没有作用的。
13年前 (2013-06-13)

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。