我用 Nginx 的 Auth_Basic 模块保护了一个 web 文件夹。问题是,我们可以尝试多个密码直到它起作用(暴力攻击)。有没有办法限制失败的重试次数?
qua*_*nta 35
据我所知,Auth Basic模块不支持此功能,但您可以使用Fail2ban来实现。
使用不存在的用户进行测试,您将在错误日志中看到类似以下内容:
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"
然后创建必要的过滤器:
/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>
Run Code Online (Sandbox Code Playgroud)
/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
Run Code Online (Sandbox Code Playgroud)
测试 Fail2Ban 规则:
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]
Run Code Online (Sandbox Code Playgroud)
PS:由于 Fail2ban 获取要禁止的日志文件,请确保logpath
与您的配置匹配。
小智 9
我很惊讶其他人没有给出这个解决方案/解决方法。
Nginx 基本身份验证并htpasswd
支持带有可选成本变量的 bcrypt 密码加密。Bcrypt 的设计速度很慢,因此对尝试不同密码的速度提供了硬性限制。
创建基本身份验证用户名/密码时使用
htpasswd -B -C 12 path/to/users.db <username>
Run Code Online (Sandbox Code Playgroud)
成本为 12,您的服务器可能无法每秒尝试密码超过几次,将其增加到 14,您可能会看到每次密码尝试大约 1 秒。
有了这个配置,任何合理的密码都将免受暴力攻击,即使攻击者连续多年尝试密码。
例如,每秒 10 次密码尝试对 8 个字符的字母数字密码进行强力攻击需要 692,351 年:62**8 / (10*3600*24*365)
.
这比设置“智能”请求限制更容易配置和更简单。
归档时间: |
|
查看次数: |
16842 次 |
最近记录: |