.htaccess 中的简单速率限制
Simple rate limiting in .htaccess
假设我有一个登录 form
:
<form action="index.php" method="post"><input type="password" name="pwd"><input type="submit" />
我知道应该使用 iptables
或 fail2ban
来进行适当的速率限制,但是没有那么远,我们可以做一些简单的事情,就在 .htaccess
中:
- 如果在最后一分钟内对该页面的请求 >= 10,则将 IP 限制为 24 小时
是否可以仅使用 .htaccess
(或 apache2.conf
)而不使用其他第三方工具(Apache 模块就可以)来完成这样的事情?
我强烈建议不要在您建议的级别上解决此问题。我宁愿选择应用程序层,因为最后很可能 失败 登录尝试是您想要限制的。
由于我们仍然受到 IPV4 和 NAT 的影响,您不会知道有多少不同的合法浏览器隐藏在一个 IP 地址后面。
失败时,您的应用程序可以触发 fail2ban
或 iptables
活动。然而,在分布式攻击和僵尸网络时代,我怀疑这会产生很大的不同。在您投入大量时间之前,我建议您检查日志并确定这是否是常见(或者更确切地说是最常见)的攻击路径。
如果您真的需要一种方法来 'rate limit' 一个特定的 IP,并且您想要一个 Apache 模块,那么这可能适合您:
https://wiki.debian.org/en/Apache/mod_evasive
mod_evasive 是一个检测网络上可能的攻击模式的模块,类似于 IPS(入侵防御系统)。
关于 IPS 功能的基本解释 right here.
设置模块 运行
sudo apt-get install libapache2-mod-evasive
然后用
激活模块
sudo a2enmod evasive
通过 运行ning
验证安装
sudo apachectl -M | grep evasive
预期输出:evasive20_module(共享)
现在我们为我们的 apache 模块创建一个新文件夹,以便它可以向其中写入日志。
sudo mkdir /var/log/mod_evasive
sudo chown www-data:root /var/log/mod_evasive
让我们编辑 mod_evasive 的配置。我在这个例子中使用 nano
sudo nano /etc/apache2/mods-available/evasive.conf
请注意:切勿在 /apache2/mods-enabled/ 中编辑模块配置,而应使用 /apache2/mods-available/。上面提供的例子是正确的。
这是一个示例配置:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
DOSEmailNotify email@yourdomain.com
DOSSystemCommand "echo +%s > /proc/net/xt_recent/badguys" or block them with iptables "su root -c '/sbin/iptables -A INPUT -s %s -j DROP'" ##command that will be sent to the system - %s = attackers ip
DOSLogDir "/var/log/mod_evasive"
DOSWhitelist 127.0.0.1
</IfModule>
如果您想通过 sendmail 获取电子邮件通知,请使用
sudo ln -s /usr/bin/mail /bin/mail
最后使用
重新启动 apache
sudo systemctl restart apache2
要将模块限制在一个位置,请使用
<Directory /var/www/test/>
<IfModule moduleLimitedToTest>
... do something with module
</IfModule>
</Directory>
可以找到有关如何使用容器的更多信息here. You could place this into a site/virtualhost config. This is explained here。
尽管我强烈推荐与 Olaf Kock 相同的东西。
假设我有一个登录 form
:
<form action="index.php" method="post"><input type="password" name="pwd"><input type="submit" />
我知道应该使用 iptables
或 fail2ban
来进行适当的速率限制,但是没有那么远,我们可以做一些简单的事情,就在 .htaccess
中:
- 如果在最后一分钟内对该页面的请求 >= 10,则将 IP 限制为 24 小时
是否可以仅使用 .htaccess
(或 apache2.conf
)而不使用其他第三方工具(Apache 模块就可以)来完成这样的事情?
我强烈建议不要在您建议的级别上解决此问题。我宁愿选择应用程序层,因为最后很可能 失败 登录尝试是您想要限制的。
由于我们仍然受到 IPV4 和 NAT 的影响,您不会知道有多少不同的合法浏览器隐藏在一个 IP 地址后面。
失败时,您的应用程序可以触发 fail2ban
或 iptables
活动。然而,在分布式攻击和僵尸网络时代,我怀疑这会产生很大的不同。在您投入大量时间之前,我建议您检查日志并确定这是否是常见(或者更确切地说是最常见)的攻击路径。
如果您真的需要一种方法来 'rate limit' 一个特定的 IP,并且您想要一个 Apache 模块,那么这可能适合您:
https://wiki.debian.org/en/Apache/mod_evasive
mod_evasive 是一个检测网络上可能的攻击模式的模块,类似于 IPS(入侵防御系统)。
关于 IPS 功能的基本解释 right here.
设置模块 运行
sudo apt-get install libapache2-mod-evasive
然后用
激活模块sudo a2enmod evasive
通过 运行ning
验证安装 sudo apachectl -M | grep evasive
预期输出:evasive20_module(共享)
现在我们为我们的 apache 模块创建一个新文件夹,以便它可以向其中写入日志。
sudo mkdir /var/log/mod_evasive
sudo chown www-data:root /var/log/mod_evasive
让我们编辑 mod_evasive 的配置。我在这个例子中使用 nano
sudo nano /etc/apache2/mods-available/evasive.conf
请注意:切勿在 /apache2/mods-enabled/ 中编辑模块配置,而应使用 /apache2/mods-available/。上面提供的例子是正确的。
这是一个示例配置:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
DOSEmailNotify email@yourdomain.com
DOSSystemCommand "echo +%s > /proc/net/xt_recent/badguys" or block them with iptables "su root -c '/sbin/iptables -A INPUT -s %s -j DROP'" ##command that will be sent to the system - %s = attackers ip
DOSLogDir "/var/log/mod_evasive"
DOSWhitelist 127.0.0.1
</IfModule>
如果您想通过 sendmail 获取电子邮件通知,请使用
sudo ln -s /usr/bin/mail /bin/mail
最后使用
sudo systemctl restart apache2
要将模块限制在一个位置,请使用
<Directory /var/www/test/>
<IfModule moduleLimitedToTest>
... do something with module
</IfModule>
</Directory>
可以找到有关如何使用容器的更多信息here. You could place this into a site/virtualhost config. This is explained here。
尽管我强烈推荐与 Olaf Kock 相同的东西。