anyx / login-gate-bundle
Checking brute force attacks on site
Installs: 301 199
Dependents: 0
Suggesters: 0
Security: 0
Stars: 59
Watchers: 2
Forks: 23
Open Issues: 6
Type:symfony-bundle
Requires
- ext-json: *
- symfony/config: ^3.3|^4.0|^5.1|^6.0|^7.0
- symfony/dependency-injection: ^3.3|^4.0|^5.1|^6.0|^7.0
- symfony/security-bundle: ^3.3|^4.0|^5.1|^6.0|^7.0
Requires (Dev)
- php: >=8.2
- ext-ctype: *
- ext-iconv: *
- doctrine/doctrine-bundle: ^2.11
- doctrine/doctrine-fixtures-bundle: ^3.4
- doctrine/doctrine-migrations-bundle: ^3.3
- doctrine/mongodb-odm-bundle: 5.0.x-dev
- doctrine/orm: ^2.17
- escapestudios/symfony2-coding-standard: 3.x-dev
- friendsofphp/php-cs-fixer: ^3.5
- mtdowling/jmespath.php: ^2.7
- phpmd/phpmd: @stable
- phpunit/phpunit: ^9.5
- symfony/browser-kit: ^7.0
- symfony/dotenv: ^7.0
- symfony/flex: ^2
- symfony/framework-bundle: ^7.0
- symfony/monolog-bundle: ^3.0
- symfony/phpunit-bridge: ^6.1
- symfony/runtime: ^7.0
- symfony/security-bundle: ^7.0
- symfony/twig-bundle: ^7.0
- symfony/yaml: ^7.0
- twig/extra-bundle: ^2.12|^3.0
- twig/twig: ^2.12|^3.0
- zenstruck/browser: ^1.6
- dev-master
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.1
- 3.0.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta
- 1.0.1
- 1.0.0
- 0.7.1
- 0.7
- 0.6
- 0.5
- 0.4
- 0.3
- 0.2
- 0.1
- dev-dependabot/composer/symfony/security-http-7.1.8
- dev-dependabot/composer/twig/twig-3.11.2
- dev-dependabot/composer/symfony/process-7.1.7
- dev-dependabot/composer/symfony/http-foundation-7.1.7
- dev-dependabot/composer/symfony/runtime-7.1.7
This package is auto-updated.
Last update: 2024-11-13 18:37:25 UTC
README
⚠️ Bundle is deprecated since similar functionality was introduced in Symfony framework. See https://symfony.com/doc/current/security.html#limiting-login-attempts
This bundle detects brute-force attacks on Symfony applications. It then will disable login for attackers for a certain period of time. This bundle also provides special events to execute custom handlers when a brute-force attack is detected.
Compatibility
The bundle is since version 1.0 compatible with Symfony 5.
Installation
Add this bundle via Composer:
composer require anyx/login-gate-bundle
Configuration:
Add in config/packages/login_gate.yml:
# config/packages/login_gate.yaml login_gate: storages: ['orm'] # Attempts storages. Available storages: ['orm', 'session', 'mongodb'] options: max_count_attempts: 3 timeout: 600 #Ban period watch_period: 3600 #Only for databases storage. Period of actuality attempts
⚠️ Username resolver (optional).
Since Symfony does not provide a common way to retrieve passed username
from LoginFailureEvent
for every possible authentication scenario,
by default this bundle is trying to retrieve username from _username
parameter in request's form data.
It means, that if you are using different authentication scenario (json_login
, for example),
users with same ip addresses will be indistinguishable. To prevent this,
you probably should create own username resolver and register it in username_resolver
option:
<?php namespace App\Service; use Anyx\LoginGateBundle\Service\UsernameResolverInterface; use Symfony\Component\HttpFoundation\Request; /** * Username resolver for json login */ class UsernameResolver implements UsernameResolverInterface { public function resolve(Request $request) { $requestData = json_decode($request->getContent(), true); return is_array($requestData) && array_key_exists('username', $requestData) ? $requestData['username'] : null; } }
# config/packages/login_gate.yaml login_gate: storages: ['orm'] # Attempts storages. Available storages: ['orm', 'session', 'mongodb'] options: max_count_attempts: 3 timeout: 600 #Ban period watch_period: 3600 #Only for databases storage. Period of actuality attempts username_resolver: App\Service\UsernameResolver
Register event handler (optional).
services: acme.brute_force_listener: class: Acme\BestBundle\Listener\BruteForceAttemptListener tags: - { name: kernel.event_listener, event: security.brute_force_attempt, method: onBruteForceAttempt }
Usage
For classic login form authentication we can check count login attempts before showing form:
namespace App\Controller; use Anyx\LoginGateBundle\Service\BruteForceChecker; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; class SecurityController extends AbstractController { /** * @Route("/login", name="login") */ public function formLogin(AuthenticationUtils $authenticationUtils, BruteForceChecker $bruteForceChecker, Request $request): Response { if (!$bruteForceChecker->canLogin($request)) { return new Response('Too many login attempts'); } $error = $authenticationUtils->getLastAuthenticationError(); $lastUsername = $authenticationUtils->getLastUsername(); return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); } }
Also there is ability to clear login attempts for request (it happens after successful authentication by default):
$this->bruteForceChecker->getStorage()->clearCountAttempts($request, $username);
For more examples take a look at the tests.