I am running a web page based on Symfony 2.7. The page uses the FOSUserBundle
for user management and authentication.
I can observe in the log files, that the page is "attacked" quite often by brute force scanners.
There are two types of scans:
HTTP 404
responsesI have been using WordPress before. There exist quite a lot of plugins and tools to automatically recognize and handle such attacks: If the 404 request or denied login attempts reaches a certain threshold, the user/ip is automatically blocked for some time. Usually after a few minutes the user/ip is automatically removed from the block list.
I have not been able to find such a solution for Symfony. Is there any bundle that integrates these functions into Symfony?
Of course it would not be too difficult to implement this functionally on my own. But it makes no sense to re-invent something that is already out there.
If you want to block malicious IP's, you should really look into fail2ban
. This blogs explains it perfectly:
<?php
namespace Your\ExampleBundle\EventHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
{
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if (null !== $this->logger && null !== $request->getClientIp()) {
$this->logger->error(sprintf('Authentication failure for IP: %s', $request->getClientIp()));
}
return parent::onAuthenticationFailure($request, $exception);
}
}
services:
your.examplebundle.authenticationfailurehandler:
class: Your\ExampleBundle\EventHandler\AuthenticationFailureHandler
arguments: ["@http_kernel", "@security.http_utils", {}, "@logger"]
tags:
- { name: 'monolog.logger', channel: 'security' }
# app/config/security.yml
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
failure_handler: your.examplebundle.authenticationfailurehandler
logout: true
anonymous: true
To create a new filter for fail2ban, we'll create a file in /etc/fail2ban/filter.d/symfony.conf with the following contents:
[Definition]
failregex = Authentication\sfailure\sfor\sIP:\s<HOST>\s
That was easy, right? We should create a jail in /etc/fail2ban/jail.local which uses our new filter. The definition for this jail will depend on your configuration, but a basic one could look like this:
[symfony]
enabled = true
filter = symfony
logpath = /var/www/my-project/app/logs/prod.log
port = http,https
bantime = 600
banaction = iptables-multiport
maxretry = 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With