fail2ban正则expression式filter不能用于nginx日志文件

我一整天都在砸我的脑袋,试图将我的正则expression式筛选器与我的access.log匹配,但没有运气。 我已经在gentoo服务器上安装了fail2ban,运行良好(我手动放了我自己的IP,它工作),但fail2ban正则expression式失败,并返回0结果filter,甚至我的网站有重载攻击现在和最近几天。

顺便说一下,我不使用iptables软件在我的服务器(我需要安装一个为了fail2ban工作?)我猜fail2ban不能读取我的logformat或我的时间格式,我试图调整一切,但没有运气,任何帮助是非常非常感激

这是我的jail.local

[INCLUDES] before = paths-common.conf [DEFAULT] action=%(action_mwl)s ignoreip = 127.0.0.1/8 192.168.99.25 ignorecommand = bantime = 86400 findtime = 300 backend = gamin [wp-login] enabled = true filter = wp-login banaction=iptables-allports logpath = /var/log/nginx/localhost*access_log bantime = 7200 maxretry = 1 [nginx-nohome] enabled=true port=http,https filter=nginx-nohome logpath=/var/log/nginx/localhost.error.log bantime=86400 maxretry=2 [nginx-dos] enabled = true port = http,8090,8080 filter = nginx-dos logpath = /var/log/nginx/localhost.access*log findtime = 30 bantime = 172800 maxretry = 140 [nginx-login] enabled = true filter = nginx-login action = iptables-multiport[name=NoLoginFailures, port="http,https"] logpath = /var/log/messages bantime = 7200 maxretry = 6 [nginx-req-limit] enabled = true filter = nginx-req-limit action = iptables-multiport[name=ReqLimit, port="http,https", protocol=tcp] logpath = /var/log/nginx/localhost.access*log findtime = 600 bantime = 7200 maxretry = 10 

这里是我的filter:

 [Definition]i failregex = limiting requests, excess:.* by zone.*client: <HOST> # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex = ~ [Definition] failregex = ^<HOST> -.*GET */wp-login* HTTP/1\.." ignoreregex = # [Definition] failregex = ^<HOST> -.*GET.*(\.php|\.asp|\.exe|\.pl|\.cgi|\scgi) ignoreregex = [Definition] failregex = ^<HOST> -.*GET .*/~.* ignoreregex = ~ 

在nginx端,这是我的日志格式的语法和输出:

日志格式

  log_format main '[$time_local] - $remote_addr ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" '; 

accesslog输出:

 [01/Oct/2015:09:15:52 +0800] - 60.18.17.206, 113.21.15.23 "POST/httprl_async_function_callback?count=121 HTTP/1.1" 200 1351 "-" "Drupal (+http://drupal.org/)" "-" 

错误日志格式:

 2015/09/22 00:04:06 [error] 7418#0: *287 FastCGI sent in stderr: "Primary scriptunknown", client: 192.168.99.76, server: www.sams.com, request: "GET/city HTTP/1.0", host: "www.sams.com" 

更新:我的网站不使用wordpress,但我得到了数百万wordpress相关链接wp-login.php,我想阻止,有很多积极的恶意search机器人,广告机器人,蜘蛛也打破了我的服务器,我想块

所以看起来你对正则expression式不是很熟悉,你有一个陡峭的学习曲线。 fail2ban实用程序使用Python正则expression式 ,值得一读。

你遇到的问题的一部分是你的failregex的这一部分

 ^<HOST> 

这就是说,在行的开头(或紧跟在一个换行符之后)查找预定义的<HOST>正则expression式,那就是^是for。

看看你的日志示例,它们都以date/时间开始,在将正则expression式应用到行的其余部分之前,这将被fail2ban删除。 该行不是以“^”可以识别的任何东西开始,所以这就是为什么你的正则expression式失败。

一个简单的例子使用你的错误日志条目。 如果你想为scriptunknown错误采取行动(这可能会或可能不是一件好事),你可以使用一个类似failregex

 failregex= scriptunknown", clinet: <HOST> 

你可以通过使用fail2ban-regex(1)运行它来testing它,例如

 fail2ban-regex /path/to/logfile 'scriptunknown", client: <HOST>' Running tests ============= Use failregex line : scriptunknown", client: <HOST> Use log file : /path/to/logfile Use encoding : UTF-8 Results ======= Failregex: 1 total |- #) [# of hits] regular expression | 1) [1] scriptunknown", client: <HOST> `- Ignoreregex: 0 total Date template hits: |- [# of hits] date format | [1] Day(?P<_sep>[-/])MON(?P=_sep)Year[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)? | [1] Year(?P<_sep>[-/.])Month(?P=_sep)Day 24hour:Minute:Second(?:,Microseconds)? `- Lines: 2 lines, 0 ignored, 1 matched, 1 missed [processed in 0.00 sec] |- Missed line(s): | [01/Oct/2015:09:15:52 +0800] - 60.18.17.206, 113.21.15.23 "POST/httprl_async_function_callback?count=121 HTTP/1.1" 200 1351 "-" "Drupal (+http://drupal.org/)" "-" 

好的,可以做你想做的事情,但可能太宽泛了,你必须看看结果,并打这些电话。

顺便说一句,我不使用iptables软件在我的服务器(我需要安装一个为了fail2ban工作?)

您需要某种与安装的fail2ban兼容的防火墙,并在您的系统上运行。 当你testing它和

我手动baned我自己的IP和它的作品

那么我想这里有什么工作。