我已经在 fail2ban 配置中尝试了许多正则表达式,但它从未返回任何匹配。
行示例:
[2019-12-10 10:45:38] NOTICE[15077] res_pjsip/pjsip_distributor.c: Request 'INVITE' from '<sip:[email protected]>' failed for '195.154.214.141:53360' (callid: 1570242695-1186607423-1664578181) - No matching endpoint found
Fail2Ban 星号配置:
# Fail2Ban configuration file
#
#
# $Revision: 250 $
#
[INCLUDES]
# Read common prefixes. If any customizations available -- read them from
# common.local
#before = common.conf
[Definition]
#_daemon = asterisk
# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
#
failregex = NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - No matching peer found
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
fail2ban-regex 输出:
Running tests
=============
Use failregex filter file : asterisk, basedir: /etc/fail2ban
Use log file : /var/log/asterisk/messages
Use encoding : UTF-8
Results
=======
Failregex: 0 total
|- #) [# of hits] regular expression
| 1) [0] NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - No matching peer found
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [157969] Year(?P<_sep>[-/.])Month(?P=_sep)Day 24hour:Minute:Second(?:,Microseconds)?
| [0] (?:DAY )?MON Day 24hour:Minute:Second(?:\.Microseconds)?(?: Year)?
| [0] (?:DAY )?MON Day Year 24hour:Minute:Second(?:\.Microseconds)?
| [0] Day(?P<_sep>[-/])Month(?P=_sep)(?:Year|Year2) 24hour:Minute:Second
| [0] Day(?P<_sep>[-/])MON(?P=_sep)Year[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)?
| [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: 159081 lines, 0 ignored, 0 matched, 159081 missed
[processed in 53.87 sec]
我究竟做错了什么?
UPD1
即使表达式是从日志中复制的,也找不到任何东西:
Results
=======
Failregex: 0 total
|- #) [# of hits] regular expression
| 1) [0] \[2019-12-10 10:45:38\] NOTICE\[15077\] res_pjsip/pjsip_distributor\.c: Request 'INVITE' from '<sip:Cant@178\.216\.162\.105>' failed for '<HOST>:53360' \(callid: 1570242695-1186607423-1664578181\) - No matching endpoint found
`-
我的python版本是:
# python -V
Python 2.7.13
UPD2
经过多次尝试,我使用这个正则表达式让它工作:
failregex = NOTICE\[.+?\] res_pjsip/pjsip_distributor\.c: Request '(INVITE|REGISTER)' from '.+?' failed for '<HOST>:.*?' \(callid: .+?\) - .*
答案1
即使表达式是从日志中复制的,也找不到任何东西
datepattern
Fail2ban在应用之前会剪切掉匹配的消息部分failregex
。
顺便说一句,您的正则表达式更糟糕(太多的 catch-all 和非贪婪),并且有点“脆弱”(没有锚定)。最好使用类似这样的内容:
failregex = ^(?:\[\] )?NOTICE\[\d+\] res_pjsip/pjsip_distributor\.c: Request '(?:INVITE|REGISTER)' from '[^']+' failed for '<HOST>
如果您的 fail2ban 版本 >= 0.10 并且您不期望这里使用 DNS 名称(因此仅使用 IP 地址),则请<HOST>
用更精确的方式替换。<ADDR>