通过 access.log 中的用户代理使用 fail2ban 阻止 badbot

ale*_*bal 6 fail2ban

如何使用fail2ban创建过滤器来阻止这些?

    476 Mozilla/5.0 (compatible; BLEXBot/1.0; +http://webmeup-crawler.com/)
    892 ltx71 - (http://ltx71.com/)
    5367 Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)
   6449 Barkrowler/0.9 (+http://www.exensa.com/crawl)
Run Code Online (Sandbox Code Playgroud)

这个列表来自于此:

sudo cat /var/log/apache2/access.log | awk -F\" '{print $6}' | sort | uniq -c | sort -n
Run Code Online (Sandbox Code Playgroud)

我试过 apache-badbot.conf,但它似乎不起作用......

All*_*ial 7

处理烦人的机器人的正确方法是在“robots.txt”中阻止它们。但是您的评论表明他们忽略了该指令。用户代理阻止最终将是一场猫捉老鼠的游戏,但如果您想这样做,您需要以下内容。

因此,如果您还没有启用读取 Apache 访问日志的apache-badbots jail,则需要启用它。创建/etc/fail2ban/jail.d/apache-badbots.local包含以下内容的文件:

[apache-badbots]
enabled = true
Run Code Online (Sandbox Code Playgroud)

apache-badbots jail的主要部分定义在其中,/etc/fail2ban/jail.conf因此您要做的就是启用它。

接下来,修改apache-badbots过滤器以包含您的机器人。编辑/etc/fail2ban/filter.d/apache-badbots.conf。其中有一条用于自定义机器人的特定行:

badbotscustom = EmailCollector|WebEMailExtrac|TrackBack/1\.02|sogou music spider
Run Code Online (Sandbox Code Playgroud)

机器人是使用正则表达式指定的。要么替换那些,要么在用|s分隔的末尾加上你的。

badbotscustom = EmailCollector|WebEMailExtrac|TrackBack/1\.02|sogou music spider|BLEXBot|ltx71|DotBot|Barkrowler
# OR
badbotscustom = BLEXBot|ltx71|DotBot|Barkrowler
Run Code Online (Sandbox Code Playgroud)

接下来,您需要修改该failregex行,以便正则表达式匹配用户代理的任何部分,而不仅仅是整个内容。更改行:

failregex = ^<HOST> -.*"(GET|POST).*HTTP.*"(?:%(badbots)s|%(badbotscustom)s)"$
Run Code Online (Sandbox Code Playgroud)

到(注意另外两个.*):

failregex = ^<HOST> -.*"(GET|POST).*HTTP.*".*(?:%(badbots)s|%(badbotscustom)s).*"$
Run Code Online (Sandbox Code Playgroud)

最后,重新加载 fail2ban 配置。

sudo fail2ban-client reload
Run Code Online (Sandbox Code Playgroud)

此信息可能有助于参考。

查看/etc/fail2ban/filter.d/apache-badbots.conf我拥有的最新 Ubuntu 16.04 服务器的更新,它看起来已经过时了。特别是有这样的评论:

# DEV Notes:
# List of bad bots fetched from http://www.user-agents.org
# Generated on Thu Nov  7 14:23:35 PST 2013 by files/gen_badbots.
Run Code Online (Sandbox Code Playgroud)

我从fail2ban git 存储库生成了一个新的,但它仍然不包含那些机器人(可能源已经过时或不完整)。如果你很好奇,你可以通过以下方式生成一个新的。

# DEV Notes:
# List of bad bots fetched from http://www.user-agents.org
# Generated on Thu Nov  7 14:23:35 PST 2013 by files/gen_badbots.
Run Code Online (Sandbox Code Playgroud)

新文件将在config/filter.d/apache-badbots.conf. 如果你想使用它替换/etc/fail2ban/filter.d/apache-badbots.conf它。

作为参考,这是定义Apache的badbots/etc/fail2ban/jail.conf

git clone https://github.com/fail2ban/fail2ban
cd fail2ban/
./files/gen_badbots
Run Code Online (Sandbox Code Playgroud)

所述%(apache_access_log)s可变来自/etc/fail2ban/paths-debian.conf和被定义为/var/log/apache2/*access.log

作为参考,这是apache-badbots.conf我生成的(未经修改)。

[apache-badbots]
# Ban hosts which agent identifies spammer robots crawling the web
# for email addresses. The mail outputs are buffered.
port     = http,https
logpath  = %(apache_access_log)s
bantime  = 172800
maxretry = 1
Run Code Online (Sandbox Code Playgroud)