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

飘逸的风13年前 (2012-12-10)程序10220
这是我无意中攻击一个网站发现的一个代码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的计划(定时)任务和暂停任务

如果在服务器做计划任务是件很简单的事情,但是有的时候收条件限制无法使用服务器自带的计划任务,比如买的空间,这时候想要让网站定时执行某些操作(处理数据、生成静态文件、清除缓存...),该怎么办呢? &...

MemCache安装使用

Windows下的Memcache安装: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c:\me...

一些需要禁用的PHP危险函数

phpinfo() 功能描述:输出 PHP 环境信息以及相关的模块、WEB 环境等信息。 危险等级:中 passthru() 功能描述:允许执行一个外部程序并回显输出,类似于 exec()。...

龙圆 php 判断是否为搜索引擎蜘蛛函数 转载

/**  *    判断是否为搜索引擎蜘蛛  *  *    @author&nbs...

强制下载文件的代码(解决了IE下中文文件名乱码问题)

以下这段代码作用是:浏览器提交excel格式的数据和文件名到服务器上,PHP将请求转化为可下载的excel文件,并要求浏览器弹出文件下载提示窗口。中间遇到一个问题是提交的中文文件名直接放到header...

评论列表

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

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

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

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

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

发表评论

访客

看不清,换一张

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