snh*_*_nl 3 firewall nginx fail2ban
使用Nginx蜜罐并使用黑名单、防火墙阻止ip或fail2ban
所以我们有这台服务器,每天都会看到 1000 个探测器。有趣的是,他们都“尝试”至少相同的基本 uri,例如\admin
and \wp-admin
and \control
and \mysqladmin
.... 我们自己的人和用户永远不会输入这些命令。
过去,我们已向这些 uri 位置匹配发送了全部拒绝,我们的块如下所示
location ~* ^/(admin|wp-admin|control)/?$
deny all;
}
Run Code Online (Sandbox Code Playgroud)
但对于某些 uri 请求,我们 100% 确定这是一个网站探针/黑客/爬虫爬行不存在的过度逻辑不安全的 uri ...而不是使用deny all;
我希望 IP 被阻止...永久或至少24小时
问题:如何像蜜罐一样 uri 匹配请求,然后通过 nginx conf 使用我们的防火墙阻止此 IP?
结果会是这样的
location ~* ^/(admin|wp-admin|control)/?$
ban the ip permanently;
or
ban the ip 24hours;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
如果deny all
在 error.log 中产生错误条目,您可以使用名为 nginx-http-auth 的fail2ban监狱,因此将其添加到您的/etc/fail2ban/jail.local
:
[nginx-http-auth]
enabled = true
Run Code Online (Sandbox Code Playgroud)
让fail2ban重新启动(或fail2ban-client reload
fail2ban v.0.10或更高版本),它们将被禁止。
http
在nginx 配置部分创建新的访问日志格式(nginx 默认值有点差) :
log_format badlogfmt '$time_local : $remote_addr : $status : $body_bytes_sent : $request_method : $remote_user : '
'"$request" "$http_referer" "$http_user_agent"';
Run Code Online (Sandbox Code Playgroud)
然后编写一个新条目,将所有“错误”请求记录在单独的日志文件中:
map $status $loggable {
404 0; # ignore page not found (404).
499 0; # ignore canceled/closed requests.
~^[45] 1; # all other requests with status starting with 4 or 5.
default 0;
}
# log the bad requests:
access_log /var/log/nginx/access_bad.log badlogfmt if=$loggable;
# all other requests:
access_log /var/log/nginx/access.log combined;
Run Code Online (Sandbox Code Playgroud)
这将导致所有“坏”请求进入不同的日志,如下所示:
11/Jan/2020:06:27:59 +0100 : 192.0.2.1 : 403 : 154 : GET : - : "GET /admin/ HTTP/1.1" ...
11/Jan/2020:06:28:00 +0100 : 192.0.2.2 : 400 : 166 : - : - : "145.ll|'|'|SGFjS2...
Run Code Online (Sandbox Code Playgroud)
你的监狱可能与此类似:
[nginx-ban-bots]
port = http,https
logpath = /var/log/nginx/access_bad.log
backend = auto
filter =
# ban all but ignore 401 Unauthorized for empty user (: - :) and 404 (and 499 cancel request)...
failregex = ^\s*: <HOST> :(?: (?!40[14]|499)[45]\d{2} :| 401 : \d+ : \S* (?!: - :))
enabled = true
Run Code Online (Sandbox Code Playgroud)
重新加载fail2ban,所有机器人都会消失。
有关更多信息,请参见fail2ban/wiki/Best-practice#reduce-parasitic-log-traffic。
如果您的页面足够安全,那么您可以说“我们的页面上没有损坏的链接”,因此您几乎没有 404 错误,一个聪明的技巧也可能是禁止入侵者对每个不存在的页面进行大规模尝试。
因此,您可以404
在map $status ...
指令中进行注释或删除,以及4
从[14]
failregex中删除,并可能增加maxretry
(和findtime
) 以避免一些误报。
使用此策略,您不需要为任何人工 URL 创建所有 nginx 位置来匹配每个机器人尝试(如果有利可图的 URL 列表明天发生变化,它也会起作用)...
bantime.increment
并将初始设置bantime
为一些较低的值,例如30s
(为了可能的误报),但所有被称为“坏”入侵者的禁令将在每个下一个更长的时间禁止(经过多次尝试)。
归档时间: |
|
查看次数: |
2307 次 |
最近记录: |