【问题标题】:Preventing Brute-Force Attacks When Authenticating A User in Laravel在 Laravel 中对用户进行身份验证时防止暴力攻击
【发布时间】:2014-09-24 20:56:55
【问题描述】:

是否可以使用 Laravel 的 Authenticating A User With Conditions 来防止暴力攻击?

这个answer for PHP 建议在您的数据库中添加两列(TimeOfLastFailedLoginNumberOfFailedAttempts),然后在每次登录尝试时检查这些值。

这是 Laravel 使用条件对用户进行身份验证的语法:

if (Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1)))
{
    // The user is active, not suspended, and exists.
}

有没有办法使用条件参数来检查指定时间段内的尝试次数?例如,过去 60 秒内的请求少于 3 个。

【问题讨论】:

    标签: php laravel brute-force


    【解决方案1】:

    我知道这是一个老问题,但由于它在 Google 上的排名很高,我想澄清一下 ThrottlesLogins 特性自 Laravel 5.1 以来就已经存在,并且确实可以防止暴力攻击。

    默认情况下,它通过特征 AuthenticatesUser 包含在 Auth\LoginController 中。

    文档:https://laravel.com/docs/5.6/authentication#login-throttling

    默认行为示例(参见方法“登录”):https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

    所以如果你使用 Laravel 自带的默认 loginController,那么登录限制的处理会自动完成。

    【讨论】:

    • 如果你添加一个文档链接和一个如何实现的sn-p,我会把它标记为正确答案。
    • 我已经尝试解释更多,但由于它现在是默认行为,我认为没有必要提供实现示例。
    【解决方案2】:

    您可以创建像下面的类一样简单的东西来帮助您防止这种情况:

    class Login {
    
        public function attempt($credentials)
        {
            if ( ! $user = User::where('email' => $credentials['email'])->first())
            {
                //throw new Exception user not found
            }
    
            $user->login_attempts++;
    
            if ($user->login_attempts > 2)
            {
                if (Carbon::now()->diffInSeconds($user->last_login_attempt) < 60)
                {
                    //trow new Exception to wait a while
                }
    
                $user->login_attempts = 0;
            }
    
            if ( ! Auth::attempt($credentials))
            {
                $user->last_login_attempt = Carbon::now();
    
                $user->save();
    
                //trow new Exception wrong password
            }
    
            $user->login_attempts = 0;
    
            $user->save();
    
            return true;
        }
    
    }
    

    或者你可以使用一个包,比如Sentry,它为你控制节流。 Sentry 是开源的。

    【讨论】:

      猜你喜欢
      • 2019-06-20
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 2012-03-05
      • 1970-01-01
      • 2016-10-10
      相关资源
      最近更新 更多