Blunt Banhammer Blocks Basic Bots Ballistically

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Blunt Banhammer Blocks Basic Bots Ballistically

I cut bot traffic to my site by over 80% with a simple trap. Last month, my hobby website OpenBenches.org transferred more than 2.5TB of data to visitors. As much as I’d like to think the site is enormously popular, it looks like it is just AI bots endlessly crawling the data and slurping up the images. So I’ve built B6 to be an indiscriminate tool to eradicate these unwanted parasites. It is very loosely inspired by Jeff Starr’s Blackhole, but works even on sites with heavy caching. Here’s the high level description of how it works. Create a directory like /honeypot/ on your site Put some code in index.php within the directory Add the honeypot as an explicit “Disallow” in robots.txt Include a hidden link to to the honeypot on every page of your site If something accesses that page, write its IP address to your .htaccess The .htaccess bans the IP address from ever accessing your site again. This blog post shows you how you can build this yourself and discusses some of the problems it might cause.

我通过一个简单的陷阱将网站的机器人流量减少了 80% 以上。上个月,我的业余网站 OpenBenches.org 向访问者传输了超过 2.5TB 的数据。尽管我很想认为该网站非常受欢迎,但看起来这只是人工智能机器人在不断地抓取数据并吸取图像。因此,我构建了 B6,这是一个用于根除这些不受欢迎的寄生虫的无差别工具。它的灵感很大程度上源于 Jeff Starr 的 Blackhole,但即使在缓存严重的网站上也能工作。以下是其工作原理的简要描述:在您的网站上创建一个目录(如 /honeypot/),在该目录的 index.php 中放入一些代码,在 robots.txt 中将该蜜罐明确添加为“禁止访问”(Disallow),在网站的每个页面上包含一个指向该蜜罐的隐藏链接。如果某个对象访问了该页面,则将其 IP 地址写入您的 .htaccess 文件。该 .htaccess 文件会禁止该 IP 地址再次访问您的网站。这篇博文展示了如何自行构建此功能,并讨论了它可能导致的一些问题。

Bad Robots!

The file robots.txt lives at the root of your website. It contains a list of instructions to automated web crawlers. Respectful bots will read it and refuse to look at specific parts of your site. Add these two lines to it: User-agent: * Disallow: /honeypot/ That says bots shouldn’t visit any page in the honeypot directory. Bots generally check for updates every 24 hours, so either wait a day or manually submit it to major crawlers.

坏机器人!

robots.txt 文件位于您网站的根目录下。它包含了一系列针对自动化网络爬虫的指令。守规矩的机器人会读取它并拒绝查看您网站的特定部分。请在其中添加以下两行: User-agent: * Disallow: /honeypot/ 这表示机器人不应访问蜜罐目录中的任何页面。机器人通常每 24 小时检查一次更新,因此您可以等待一天,或者手动将其提交给主要的搜索引擎爬虫。

.htaccess

The .htaccess file contains a list of instructions for how your server should process requests. Find the bit of your .htaccess file which looks like this: RewriteEngine On RewriteBase / Edit it to look like this: RewriteEngine On RewriteBase /

BLOCKED IPs

TEST-NET-1

RewriteCond %{REMOTE_ADDR} “=192.0.2.1” RewriteRule ^ - [R=402,L]

/BLOCKED IPs

It is important that these rules come before any other rewrite rules. Change the end of this IfModule to look like: ErrorDocument 402 “Payment Required - Access Denied.” If an IP address is in the blocked IP list, the server will send an error code and a readable message. You can add whatever plain text you like here.

.htaccess

.htaccess 文件包含了一系列关于服务器应如何处理请求的指令。找到您的 .htaccess 文件中类似以下的部分: RewriteEngine On RewriteBase / 将其修改为如下所示: RewriteEngine On RewriteBase /

BLOCKED IPs

TEST-NET-1

RewriteCond %{REMOTE_ADDR} “=192.0.2.1” RewriteRule ^ - [R=402,L]

/BLOCKED IPs

重要的是,这些规则必须位于任何其他重写规则之前。将此 IfModule 的结尾修改为: ErrorDocument 402 “Payment Required - Access Denied.” 如果某个 IP 地址在被封禁的 IP 列表中,服务器将发送一个错误代码和一条可读的消息。您可以在此处添加任何您喜欢的纯文本。

Wipe Them All Out

Inside the honeypot directory, create a file called index.php (or whatever you like). When this page is visited, it grabs the visitor’s IP address and the name of its User Agent. It then places them in the BLOCKED IPs section.

将它们全部清除

在蜜罐目录内,创建一个名为 index.php(或任何您喜欢的名称)的文件。当此页面被访问时,它会获取访问者的 IP 地址及其用户代理(User Agent)名称,然后将它们放入 BLOCKED IPs 部分。

Setting The Trap

Web crawlers look at a page, find every link on it, then visit those links. So we need to put a link to the honeypot on every page - but in such a way that normal users will not find it or click it. This adds a link, removes it from the accessibility tree, and gives it impossible to click text (a zero width joiner). This CSS hides it from normal viewers and removes the mouse cursor from view. #honey { display: none !important; cursor: none !important; }

设置陷阱

网络爬虫会查看页面,找到上面的每一个链接,然后访问这些链接。因此,我们需要在每个页面上放置一个指向蜜罐的链接,但要以普通用户无法发现或点击的方式。这添加了一个链接,将其从辅助功能树中移除,并为其提供了无法点击的文本(零宽连接符)。 这段 CSS 将其对普通浏览者隐藏,并移除了鼠标光标。 #honey { display: none !important; cursor: none !important; }

Problems This Might Cause

It’s unlikely that a human will find the link, click on it, and be banned. But if they do, consider put a message in the blocking text telling them how they can get unblocked. Some useful bots ignore robots.txt. If they identify themselves via a User-Agent string (and they should) you can manually remove them from .htaccess. You could also code in an allow list exemption for them. But, of course, many bad bots lie about who they really are. Conversely, not all bots crawl every link. This won’t catch bots which respect robots.txt but are nevertheless problematic. If you have thousands of different bots blocked, the performance of your server might slow down while .htaccess is parsed. There’s weak anecdotal evidence that it adds a slight overhead. But, personally, I don’t think that’s worth worrying about. Bots often use hijacked IP addresses. After the bot has moved, an innocent party might be blocked from your site. Each block has a timestamp associated with it. You

这可能导致的问题

人类不太可能找到该链接、点击它并被封禁。但如果发生了这种情况,请考虑在封禁文本中添加一条消息,告诉他们如何解除封禁。一些有用的机器人会忽略 robots.txt。如果它们通过 User-Agent 字符串标识自己(它们应该这样做),您可以手动将它们从 .htaccess 中移除。您也可以为它们编写白名单豁免代码。当然,许多恶意机器人会伪造身份。相反,并非所有机器人都抓取每一个链接。这无法捕获那些遵守 robots.txt 但仍然存在问题的机器人。如果您封禁了数千个不同的机器人,服务器在解析 .htaccess 时性能可能会变慢。有微弱的传闻证据表明这会增加轻微的开销。但就我个人而言,我认为这不值得担心。机器人经常使用被劫持的 IP 地址。当机器人离开后,无辜的第三方可能会被禁止访问您的网站。每个封禁都关联有一个时间戳。您