防止对 MVC 的蛮力攻击

Boj*_*jan 3 c# asp.net security brute-force asp.net-mvc-5

我想弄清楚如何在我的网站上对抗蛮力攻击。根据我所做的所有研究,最佳答案是帐户锁定和验证码。

如果我锁定用户,那么我将拒绝他们提供 x 时间的服务。这意味着如果攻击者要攻击 10 个不同的帐户,他将全部锁定。然后当时间到了他会再次锁定它们。基本上他可以坚持下去并无限期地锁定用户。用户可以联系我,但现在我必须处理 10 张票,如果可能的话,我宁愿避免这项工作。所以我没有完全理解的是这有什么用?攻击者可能不会被考虑在内,但他们会给我和用户带来很多悲伤。

我该如何解决这个问题?禁止 Ip 似乎毫无意义,因为它可以很容易地改变。

Jas*_*son 8

您可以添加一个增量延迟,在每次登录尝试失败后加倍,在几次登录尝试后,延迟变得太长,无法使用暴力破解(例如,20 次尝试后延迟为 6 天)。

[HttpPost]
public async Task<ActionResult> Login(LoginViewModel viewModel, string returnUrl)
{
    // incremental delay to prevent brute force attacks
    int incrementalDelay;
    if (HttpContext.Application[Request.UserHostAddress] != null)
    {
        // wait for delay if there is one
        incrementalDelay = (int)HttpContext.Application[Request.UserHostAddress];
        await Task.Delay(incrementalDelay * 1000);
    }

    if (!ModelState.IsValid)
        return View();

    // authenticate user
    var user = _userService.Authenticate(viewModel.Username, viewModel.Password);

    if (user == null)
    {
        // login failed

        // increment the delay on failed login attempts
        if (HttpContext.Application[Request.UserHostAddress] == null)
        {
            incrementalDelay = 1;
        }
        else
        {
            incrementalDelay = (int)HttpContext.Application[Request.UserHostAddress] * 2;
        }
        HttpContext.Application[Request.UserHostAddress] = incrementalDelay;

        // return view with error
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View();
    }

    // login success

    // reset incremental delay on successful login
    if (HttpContext.Application[Request.UserHostAddress] != null)
    {
        HttpContext.Application.Remove(Request.UserHostAddress);
    }

    // set authentication cookie
    _formsAuthenticationService.SetAuthCookie(
        user.Username,
        viewModel.KeepMeLoggedIn,
        null);

    // redirect to returnUrl
    return Redirect(returnUrl);
}
Run Code Online (Sandbox Code Playgroud)

这篇文章有更多细节