尝试阻止一些使用fail2ban读取Apache访问日志的恶意机器人,并坚持设置正确的正则expression式。 我想在访问日志中匹配的行是:
5.10.83.65 - - [18/Mar/2014:09:06:38 +0400] "GET /catalog/product_compare/ ,,/form_key/QLZ6ZkIwX3FWqme3/ HTTP/1.1" 302 522 "-" " Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)"
而我试图使用的简化正则expression式:
failregex = ^<HOST>*(AhrefsBot)
但是无论我尝试什么都行不通。 在这里find这个职位是我试图实现badbots
这就是我最终做的,不用做一个大的名单就把他们全部封杀,如果这些单词匹配,就要冒着风险,因为这可能会阻止合法的访问者。 我使用ignoreregex来允许好的,并阻止其他机器人,只要他们自称为bot:
failregex = ^<HOST> -.*compatible;.*(Bot|bot) ignoreregex = (Google|Yandex|Mail|bing)
现在这是远远不够理想的,但是阻止99%的不需要的扫描器机器人释放服务器,快速和肮脏。
你几乎在那里, *没有做你认为它是什么,因为它匹配0个或更多的前一个字符尝试
^<HOST> -.*(AhrefsBot)
例如
fail2ban-regex '5.10.83.65 - - [18/Mar/2014:09:06:38 +0400] "GET /catalog/product_compare/,,/form_key/QLZ6ZkIwX3FWqme3/ HTTP/1.1" 302 522 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)"' '^<HOST> -.*(AhrefsBot)' Running tests ============= Use failregex line : ^<HOST> -.*(AhrefsBot) Use single line : 5.10.83.65 - - [18/Mar/2014:09:06:38 +0400] "GET /... Results ======= Failregex: 1 total |- #) [# of hits] regular expression | 1) [1] ^<HOST> -.*(AhrefsBot) `- 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