我正在尝试通过将 fail2ban 与我的 nginx 日志一起使用来阻止正在扫描我的站点以查找管理页面的脚本。但是我在我的fail2ban-log中收到了这样的错误(具有不同的请求/用户代理/远程地址):
2015-09-18 14:45:42,137 fail2ban.filter : ERROR No 'host' found in ' +0200 - ***.**.***.*** http://www.example.com - - "GET /admin HTTP/1.1" 404 737 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36"' using '<_sre.SRE_Pattern object at 0xc53590>'
我的 fail2ban-regex 看起来像这样:
failregex = \+0200 - <HOST> https?://.*[Aa]dmin.* ((301)|(404))
我的 nginx-logformat 看起来像这样:
log_format vhosts '$time_local - $remote_addr $scheme://$host - $remote_user "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
当我使用该日志行运行 fail2ban-regex 时,我得到:
fail2ban-regex '18/Sep/2015:14:45:42 +0200 - ***.**.***.*** http://www.example.com - - "GET /admin HTTP/1.1" 404 737 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36"' '\+0200 - <HOST> https?://.*[Aa]dmin.* ((301)|(404))'
Running tests
=============
Use failregex line : \+0200 - <HOST> https?://.*[Aa]dmin.* ((301)|(404))
Use single line : 18/Sep/2015:14:45:42 +0200 - ***.**.***.*** http:/...
Results
=======
Failregex: 1 total
|- #) [# of hits] regular expression
| 1) [1] \+0200 - <HOST> https?://.*[Aa]dmin.* ((301)|(404))
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [1] Day/MONTH/Year:Hour:Minute:Second
`-
Lines: 1 lines, 0 ignored, 1 matched, 0 missed
IP 替换为. ..* . www.example.com 的主机地址。在 fail2ban-regex 中,我使用了真实的 ip 和主机。