PHP,.htaccess,DDoS和快速请求保护
发布时间:2020-12-13 16:20:04 所属栏目:PHP教程 来源:网络整理
导读:我有一个问题,我建立了这个小脚本,以检查某个IP是否充斥我的网站. 当它,我否认.htaccess文件中的IP.我的问题是,有人可以告诉我这个脚本是完全没用还是值得尝试…脚本在配置文件中调用,因此它在每个页面加载上运行. ?php#get the visitor ip$ip = $_SERVER["R
我有一个问题,我建立了这个小脚本,以检查某个IP是否充斥我的网站.
当它,我否认.htaccess文件中的IP.我的问题是,有人可以告诉我这个脚本是完全没用还是值得尝试…脚本在配置文件中调用,因此它在每个页面加载上运行. <?php #get the visitor ip $ip = $_SERVER["REMOTE_ADDR"]; #start the session @session_start(); #check if the ip is banned if( $_SESSION['~b'] ){ #check if we can open htaccess $fp = @fopen('./.htaccess','a'); if($fp){ #add the ip to htaccess @fwrite($fp,"rndeny from $ip"); #close @fclose($fp); #destroy the session @session_destroy(); @mail("my-email","IP Banned","Ip: $ip"); } #let the user know why we deny him or her access die('To many requests.'); } #get the filename and location $f = './log/'.@ip2long($ip); #check if the file exists if ( @is_file($f) ) { #get the last filetime $a = @filemtime($f); #touch the file,give a new filetime @touch($f,time()); #the ip is not banned $_SESSION['~b'] = false; #add the time diff $_SESSION['~r'] += @time()-$a; #add the latest hit $_SESSION['~h'] += 1; }else{ #create the file if it doesn't exist @file_put_contents($f,''); #size: 0kb #if touch() doesn't work #chmod($ipfile,0755); } #calculate the diff after 10 hits,and ban when the avg is smaller than 0.25 seconds if( $_SESSION['~h'] > 10 && ($_SESSION['~r']/$_SESSION['~h']) < 0.25 ) $_SESSION['~b'] = true; ?> 只是按照建议来避免SESSIONS,所以我把它建立在文件的基础上,而不必依赖于cookie和会话: <?php # get the visitor ip $i = $_SERVER["REMOTE_ADDR"]; # get the filename and location $f = './log/'.ip2long($i).'.dat'; # check if the file exists and we can write if ( is_file($f) ) { # get the last filetime $a = filemtime($f); # get the file content $b = file_get_contents($f); # create array from hits & seconds $d = explode(':',$b); # calculate the new result $h = (int)$d[0] + 1; $s = (int)$d[1] + (time()-$a); # add the new data tot text file file_put_contents($f,"$h:$s",LOCK_EX); unset($d); }else{ # create the file if it doesn't exist hits:seconds file_put_contents($f,"1:1",LOCK_EX); #size: 3kb # to make sure we can write # chmod($f,0755); # set the hits to zero $h = 0; } # create a result var $r = $h > 10 ? (float)$s/$h : (float)1; # calculate the diff after 10 hits,and ban when the avg is smaller than 0.20 seconds (5 hits per second) if( $r < 0.20 ) { # check if we can open htaccess $fp = @fopen('./.htaccess','a'); if($fp){ # add the ip to htaccess @fwrite($fp,"rndeny from $i"); # close @fclose($fp); # mail the admin @mail("email","Ip: $i with $r sbh (Seconds Between Hits)"); } # let the user know why we deny him or her access die('To many requests.'); # remove the file unlink($f); } # if the user leaves,reset if( $r > 30 ) { unlink($f); } echo 'Result: '.$r.'sbh (Seconds Between Hits)'; ?> 解决方法
如果你想阻止临时用户在一定时间内发送太多请求,那么是的,脚本可以工作.打开一个catpcha屏幕,你正在做生意.
但 真正的答案是否定的. 此代码的主要错误取决于会话以确定用户活动的频率.一个“好”的攻击者可以通过禁用cookie的请求来淹没您的服务器,以及欺骗他/她的IP. 阻止攻击的一种方法是转到服务器级别,然后安装iptables.事实上,iptables附带了大多数Linux发行版.它需要很少的配置,并且开箱即用. 另一种方法是,如果您具有对服务器的root访问权限,则将会话处理移动到Memcached.它有一个叫做洪水控制的功能,非常漂亮. 另一种防止DDOS的途径来自第三方服务,例如 有点贵,但它可能适合你. 但PHP本身无法配置为处理DDOS攻击.在转到PHP脚本之前,您需要在所有要审查的请求之前放置某种设备或防火墙. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |