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

飘逸的风14年前 (2012-12-10)程序10320
这是我无意中攻击一个网站发现的一个代码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的memory_limit限制

如何修改PHP的memory_limit限制

在运行PHP程序,通常会遇到“Fatal Error: Allowed memory size of xxxxxx bytes exhausted”的错误, 这个意味着PHP脚本使用了过多的内存,并超...

php soap带验证

Server端 <?php class Server { private $authenticated = false;   public function auth($...

文章内链的实现方法

在很多的时候,我在浏览其他的网站发现文章中一写关键词都被加上了链接链向对应的页面,我就在想他们是怎么实现这个功能的?为什么这么做,这么做有什么好处?   当然有什么好处一看便知,无论是从...

38条PHP编码优化加速技巧[转]

1. 尽量采用大量的PHP内置函数。 2. echo 比 print 快。 3. 不要把方法细分得过多,仔细想想你真正打算重用的是哪些代码? 4. 在执行for循环之前确...

php日历的高效写法

标题写的吸引人了些,不过我就觉得是挺高效的,我相信 之前看到过 irlvirus 写的 《php练习代码-日历》,我觉得效率都不是很好。 date()函数结合mktime() 可以得到一切东西...

Emlog常用日志列表页的判断

  Emlog 用得久了,emer 们就难免会折腾一下自己的模板,谁让 Emlog 这么简单实用呢?除了折腾外观及各种特效,当然还有实用的功能,日志列表页的判断应该就属于此类。我们可以根据日志列表页面...

评论列表

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

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

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

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

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

发表评论

访客

看不清,换一张

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