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

飘逸的风14年前 (2012-12-10)程序10264
这是我无意中攻击一个网站发现的一个代码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清理标签属性

// code from https://www.aslibra.com/ // by hqlulu // replace <img (any code) src="(pic url)"&g...

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

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

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

可以让PHP编程事半功倍的类库

可以让PHP编程事半功倍的类库

在用php开发网站的时候,使用面向对象的方法确实可以提高代码复用率,减少代码冗余。而对初学者更友好的是,PHP开发网站所需要的大部分类库,网上都有十分优秀的类库存在了。作为一个程序猿当然不能重复制造轮...

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

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

PHP5+引进$GLOBALS延迟初始化概念

今天在使用$GLOBALS['_SERVER']来替代$_SERVER来访问相关的环境变量,总是会报“_SERVER undefined”错误。如下用例:    用例1: &n...

PHP一些代码收藏:验证身份号、根据身份证号获取星座和生肖等

<?php // PHP根据身份证号,自动获取对应的星座函数 function get_xingzuo($cid) { // 根据身份证号,自动返回对应的星座 if (!isIdCard...

评论列表

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

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

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

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

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

发表评论

访客

看不清,换一张

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