gpt4 book ai didi

PHP、.htaccess、DDoS 和快速请求保护

转载 作者:可可西里 更新时间:2023-10-31 23:32:37 27 4
gpt4 key购买 nike

我有一个问题,我构建了这个小脚本来检查某个 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,"\r\ndeny 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 和 session :

<?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,"\r\ndeny from $i");
# close
@fclose($fp);
# mail the admin
@mail("email","IP Banned","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)';
?>

最佳答案

如果您想阻止临时用户在特定时间内发送过多请求,那么,该脚本可以工作。调出一个验证码屏幕,您就可以开始工作了。

但是

真正的答案是

此代码的主要错误是根据 session 来确定用户事件的频率。 “好”的攻击者可以用禁用 cookie 的请求淹没您的服务器,并欺骗他/她的 IP。

阻止攻击的一种方法是进入服务器级别,并安装 iptables。事实上,iptables 与大多数 Linux 发行版一起提供。它几乎不需要配置,开箱即用。

另一种方法,如果您对服务器具有根访问权限,则将 session 处理移至 Memcached。有个功能叫防洪,挺BOSS的。

防止 DDOS 的另一种方法是来自第三方服务,例如 block dos http://www.blockdos.net/

有点贵,但它对你有用。

但是 PHP 本身无法配置为处理 DDOS 攻击。在转到 PHP 脚本之前,您需要在所有要审查的请求前放置某种设备或防火墙。

关于PHP、.htaccess、DDoS 和快速请求保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14087117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com