简短的总结:
我正在尝试在监控 docker 容器中的流量的主机上配置 fail2ban。我的fail2ban 匹配,fail2ban 确实禁止了IP 地址。但是它禁止的IP地址是错误的?
设置和诊断
- 通过这个 repo 运行一个带有letsencrypt docker容器的nginx:https ://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion on digitalocean。
- 在宿主机上安装了fail2ban(即不在容器中)
- 我还必须对过滤器正则表达式进行自定义修改,因为 docker 日志在 json 中,我将在下面概述:
示例片段jail.local
[php-custom]
enabled = true
port = http,https
filter = php-custom
logpath = /var/lib/docker/containers/*/*-json.log
maxrety = 0
bantime = 8640000
我的自定义php-custom.conf
过滤器规则:(我正在尝试禁止任何 php,因为我正在运行 .net 应用程序):
[Definition]
failregex = ^{"log":".*<HOST>.*(GET|POST).*(.php).*$
ignoreregex =
我试图阻止/禁止的可怕垃圾邮件流量:
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:40 +0000] \"GET /phpMyadmin_bak/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:40.24318153Z"}
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:40 +0000] \"GET /www/phpMyAdmin/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:40.823999106Z"}
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:42 +0000] \"GET /tools/phpMyAdmin/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:42.495745595Z"}
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:42 +0000] \"GET /phpmyadmin-old/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:42.686355079Z"}
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:42 +0000] \"GET /phpMyAdminold/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:42.876219111Z"}
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:43 +0000] \"GET /phpMyAdmin.old/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:43.0685648Z"}
{"log":"127.0.0.1 47.95.1.195 - - [05/Jul/2018:21:42:43 +0000] \"GET /pma-old/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-05T21:42:43.258384519Z"}
当我用 测试它时fail2ban-regex
,见下文,NB 127.0.0.1 不是我的真实 IP 地址。
fail2ban-regex '{"log":"127.0.0.1 118.24.11.172 - - [07/Jul/2018:06:15:10 +0000] \"GET /mysql-admin/index.php HTTP/1.1\" 503 213 \"-\" \"Mozilla/5.0\"\n","stream":"stdout","time":"2018-07-07T06:15:10.68 3403757Z"}' '^{"log":".*<HOST>.*(GET|POST).*(.php).*$'
我得到的输出:
Running tests
=============
Use failregex line : ^{"log":".*<HOST>.*(GET|POST).*(.php).*$
Use single line : {"log":"127.0.0.1 118.24.11.172 - - [07/Jul/2...
Results
=======
Failregex: 1 total
|- #) [# of hits] regular expression
| 1) [1] ^{"log":".*<HOST>.*(GET|POST).*(.php).*$
| 0.0.0.2 Sat Jul 07 06:15:10 2018
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [1] Day(?P<_sep>[-/])MON(?P=_sep)Year[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)?
| [0] (?:DAY )?MON Day 24hour:Minute:Second(?:\.Microseconds)?(?: Year)?
| [0] Year(?P<_sep>[-/.])Month(?P=_sep)Day 24hour:Minute:Second(?:,Microseconds)?
| [0] Day(?P<_sep>[-/])Month(?P=_sep)(?:Year|Year2) 24hour:Minute:Second
| [0] Month/Day/Year:24hour:Minute:Second
| [0] Month-Day-Year 24hour:Minute:Second\.Microseconds
| [0] TAI64N
| [0] Epoch
| [0] Year-Month-Day[T ]24hour:Minute:Second(?:\.Microseconds)?(?:Zone offset)?
| [0] ^24hour:Minute:Second
| [0] ^<Month/Day/Year2@24hour:Minute:Second>
| [0] ^Year2MonthDay ?24hour:Minute:Second
| [0] MON Day, Year 12hour:Minute:Second AMPM
| [0] ^MON-Day-Year2 24hour:Minute:Second
`-
Lines: 1 lines, 0 ignored, 1 matched, 0 missed [processed in 0.00 sec]
它似乎匹配,但似乎说匹配的 ip 地址是0.0.0.2 Sat Jul 07 06:15:10 2018
??
我让这个设置运行了一段时间,因为我认为它正在工作,当通过运行检查状态时,fail2ban-client status php-custom
我得到以下信息:
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- File list: /var/lib/docker/containers/016ef4731565527d407a552af9bfe5cf3ec3623117b40a34ed09e9fb5b2ffb00/ 016ef4731565527d407a552af9bfe5cf3ec3623117b40a34ed09e9fb5b2ffb00-json.log
`- Actions
|- Currently banned: 5
|- Total banned: 5
`- Banned IP list: 0.0.0.1 0.0.0.2 0.0.0.4 0.0.0.8 0.0.0.9
ip地址似乎都是0.0.0.1
等等0.0.0.2
?
我希望它应该是实际的 IP 地址,因为我仍然收到垃圾邮件流量。
任何建议或帮助,因为我对 fail2ban 和 docker 很陌生,将不胜感激。