gpt4 book ai didi

symfony - 在 Symfony 中自动阻止/禁止暴力扫描器

转载 作者:行者123 更新时间:2023-12-04 21:09:11 25 4
gpt4 key购买 nike

我正在运行一个基于 Symfony 2.7 的网页。该页面使用 FOSUserBundle用于用户管理和身份验证。

我可以在日志文件中观察到,该页面经常被蛮力扫描器“攻击”。

有两种类型的扫描:

  • 搜索已知漏洞,例如导致 HTTP 404 的 WordPress 文件等回复
  • 使用默认用户凭据尝试登录

  • 我之前一直在使用 WordPress。有很多插件和工具可以自动识别和处理此类攻击:如果 404 请求或拒绝登录尝试达到一定阈值,则用户/IP 将被自动阻止一段时间。通常几分钟后,用户/IP 会自动从阻止列表中删除。

    我一直无法为 Symfony 找到这样的解决方案。有没有将这些功能集成到 Symfony 中的包?

    当然,我自己在功能上实现这一点并不会太难。但是重新发明已经存在的东西是没有意义的。

    最佳答案

    如果你想阻止恶意 IP,你真的应该看看 fail2ban . This blogs完美地解释了它:

    创建身份验证失败处理程序

    <?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

    为 Symfony2 创建自定义的 fail2ban 过滤器

    要为fail2ban 创建一个新过滤器,我们将在/etc/fail2ban/filter.d/symfony.conf 中创建一个包含以下内容的文件:
    [Definition]
    failregex = Authentication\sfailure\sfor\sIP:\s<HOST>\s

    那很容易,对吧?我们应该在/etc/fail2ban/jail.local 中创建一个使用我们新过滤器的 jail 。这个 jail 的定义取决于你的配置,但一个基本的可能是这样的:
    [symfony]
    enabled = true
    filter = symfony
    logpath = /var/www/my-project/app/logs/prod.log
    port = http,https
    bantime = 600
    banaction = iptables-multiport
    maxretry = 3

    关于symfony - 在 Symfony 中自动阻止/禁止暴力扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323675/

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