Let’s say I have a login form
:
<form action="index.php" method="post"><input type="password" name="pwd"><input type="submit" />
I know that a proper rate-limiting should be done with iptables
or fail2ban
, but without going so far, can we do something simple, just in the .htaccess
:
- Limit the IP for 24 hours if there has been >= 10 requests to this page during the last minute
Can something like this be done with .htaccess
(or apache2.conf
) only and no other third-party tool (an Apache module is ok)?
2
Answers
I’d strongly advise against solving this problem on the level that you propose. I’d rather go for the application layer, because in the end it’s most likely failed login attempts that you want to limit.
And as we still suffer from IPV4, and with it NAT, you won’t know how many distinct legitimate browsers hide behind a single IP address.
On failure, your application can then trigger
fail2ban
oriptables
activities. However, in times of distributed attacks and botnets I doubt that this will make much of a difference. Before you invest a lot of time, I’d recommend to inspect the logs and find out if this is a common (or rather the most common) attack path.If you really need a way to ‘rate limit’ one particular IP and you want an Apache module for it then this might be for you:
https://wiki.debian.org/en/Apache/mod_evasive
mod_evasive is a module that detects possible attack patterns on the network similar to an IPS (intrusion prevention system).
A basic explanation about what an IPS does it right here.
To setup the module run
Then activate the module with
Verify the installation by running
Expected output: evasive20_module (shared)
Now we create a new folder for our apache module so it can write logs to it.
Let’s edit the config of mod_evasive. I use nano for this example
Please note: Never edit a module configuration in /apache2/mods-enabled/ use /apache2/mods-available/ instead. The example provided above is correct.
This is an example configuration:
If you want to get email notifications via sendmail use
Finally just restart apache using
To limit the module to one location, use
More on how to use containers can be found here. You could place this into a site/virtualhost config. This is explained here.
Though I’d strongly recommend the same thing as Olaf Kock.