我用Nginx的Auth_Basic模块保护了一个web文件夹。 问题是,我们可以尝试几个密码,直到它工作(蛮力攻击)。 有没有办法限制失败的重试次数?
据我所知, Auth Basic模块不支持这个function,但是你可以通过使用Fail2ban来做到这一点 。
用一个不存在的用户进行testing,你会在错误日志中看到像下面这样的东西:
2012/08/25 10:07:01 [error] 5866#0: *1 no user/password was provided for basic authentication, client: 127.0.0.1, server: localhost, request: "GET /pma HTTP/1.1", host: "localhost:81" 2012/08/25 10:07:04 [error] 5866#0: *1 user "ajfkla" was not found in "/etc/nginx/htpasswd", client: 127.0.0.1, server: localhost, request: "GET /pma HTTP/1.1", host: "localhost:81"
然后创build必要的filter
/etc/fail2ban/filter.d/nginx-auth.conf
[Definition] failregex = no user/password was provided for basic authentication.*client: <HOST> user .* was not found in.*client: <HOST> user .* password mismatch.*client: <HOST> ignoreregex = </host></host></host>
/etc/fail2ban/jail.conf
[nginx-auth] enabled = true filter = nginx-auth action = iptables[name=NoAuthFailures, port=80, protocol=tcp] logpath = /var/log/nginx*/*error*.log bantime = 3600 # 1 hour maxretry = 3
testingFail2Ban规则:
fail2ban-regex /var/log/nginx/localhost.error_log /etc/fail2ban/filter.d/nginx-auth.conf
Failregex |- Regular expressions: | [1] no user/password was provided for basic authentication.*client: <HOST> | [2] user .* was not found in.*client: <HOST> | [3] user .* password mismatch.*client: <HOST> | `- Number of matches: [1] 1 match(es) [2] 2 match(es) [3] 0 match(es) Ignoreregex |- Regular expressions: | `- Number of matches: Summary ======= Addresses found: [1] 127.0.0.1 (Sat Aug 25 10:07:01 2012) [2] 127.0.0.1 (Sat Aug 25 10:07:04 2012) 127.0.0.1 (Sat Aug 25 10:07:07 2012) [3]
PS:由于Fail2ban提取日志文件以禁止,请确保日志path与您的configuration相匹配。
我不相信nginx有任何内部工具来做到这一点。 文档页面不build议这是可能的。
您可以使用Fail2Ban阻止重复失败login尝试的IP地址。
Fail2Ban wiki有一些nginx特定的模式 。
Fail2Ban应该作为大部分发行版的一个包。
我很惊讶没有其他人给了这个解决scheme/解决方法。
Nginx的basic-auth和htpasswd支持使用可选的代价variables来encryption密码。 Bcrypt的devise速度很慢,因此对于尝试使用不同密码的速度提供了一个硬性限制。
创build您的基本身份validation用户名/密码时使用
htpasswd -B -C 12 path/to/users.db <username>
花费12美元,你的服务器很可能无法每秒钟尝试超过几次的密码,增加到14,你可能会看到每个密码尝试约1秒。
使用该configuration,即使攻击者连续多年尝试使用密码,任何合理的密码也不会受到powershell攻击。
例如,在每秒钟10次的密码尝试中,对8个字符的字母数字密码的暴力攻击需要692,351年: 62**8 / (10*3600*24*365) 。
这比configuration“智能”请求限制更容易configuration和更加傻瓜化。