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

飘逸的风13年前 (2012-12-10)程序10093
这是我无意中攻击一个网站发现的一个代码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 soap带验证

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

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

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

ECShop设置模板的原理以及一些模板设置函数说明(个人观点)

ECShop设置模板的原理以及一些模板设置函数说明(个人观点) admin/includes/lib_template.php 一些函数及变量说明 $template_files...

PHP购物车类,简单易用,移植CodeIgniter,并进行一些优化[附带实例]

个人感觉CodeIgniter用起来方便,但有时候做电子商城网站的时候,没有使用CodeIgniter框架,但想用CodeIgniter里的购物车程序,那么就需要对其购物车类进行修改。但CodeIgn...

可以提高PHP编程效率的一些方法

1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。 2、$row[’id’] 的速度是$row[id]的7倍。 3、echo 比 print 快,并且使用e...

评论列表

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

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

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

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

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

发表评论

访客

看不清,换一张

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