paint-brush
ボットはサーバーのリソースを無駄にするのが大好き - 対抗する方法はこれだ@wasteofserver
新しい歴史

ボットはサーバーのリソースを無駄にするのが大好き - 対抗する方法はこれだ

Frankie6m2025/02/20
Read on Terminal Reader

長すぎる; 読むには

HAProxy を使用して、404 URL でサーバーにアクセスし続けるボットのレートを制限します。
featured image - ボットはサーバーのリソースを無駄にするのが大好き - 対抗する方法はこれだ
Frankie HackerNoon profile picture
0-item

公開 Web サーバーを管理している場合、ボットが存在しない多数のページにアクセスしようとしていることに気付くでしょう。HAProxy を活用して、ボットを阻止しましょう。


注: この記事のリンクには、Amazon へのアフィリエイト リンクが含まれています。これらのリンクをクリックしても追加料金は発生しませんが、これらのリンクのいずれかから何かを購入すると、私をサポートすることになります。ありがとうございます!


ログには、 /.DS_Store/backup.sql/.vscode/sftp.json 、その他多数の URL の 404 ヒットが記録されています。これらのリクエストはほとんど無害ですが、もちろん、サーバーが実際にそれらの場所で何か提供できるものでない限り、ボットをなだめる必要があります。


なぜ?


サーバーにアクセスするのはリソースを大量に消費するタスクであり、ボットにはさまざまな URL の膨大なリストがあるため、役立つキャッシュ メカニズムはありません。また、ボットを停止することは常に安全対策となります。


これまで、Wordpress ログイン ページへの攻撃を軽減するために HAProxy を使用してきましたが、そのアプローチを拡張して 404 エラーもカバーしようという考えです。


Bots will try their best to create havoc in your server

ボットはあなたのサーバーに大混乱をもたらそうと全力を尽くします

私はSasa Tekovic からインスピレーションを得ました。具体的には、実際の検索エンジン クローラーをブロックせず、静的リソースで 404 を許可して、実際に欠落しているリソース (つまり、ユーザー側のエラー) によって正当なユーザーがブロックされないようにするということです。


実装する前に、ローカル テスト環境を立ち上げておくのが常に良いことです。Docker Docker使用してHAProxyApacheを起動してみましょう。404 404返す実際のバックエンド サーバーが必要です。


 version : '3' services: haproxy: image: haproxy:3.1.3-alpine ports: - "8100:80" volumes: - "./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg" networks: - webnet apache: image: httpd:latest container_name: apache1 ports: - "8080:80" volumes: - ./html:/usr/local/apache2/htdocs/ networks: - webnet networks: webnet:


次に、 docker-compose upを実行するだけで、ブラウザでlocalhost:8100アクセスできるようになります。


haproxy.cfgファイルの内容はほぼ一目瞭然です:


 global log stdout format raw daemon debug defaults log global mode http frontend main bind *:80 acl static_file path_end .css .js .jpg .jpeg .gif .ico .png .bmp .webp .csv .ttf .woff .svg .svgz acl excluded_user_agent hdr_reg(user-agent) -i (yahoo|yandex|kagi|(google|bing)bot) # tracks IPs but exclude hits on static files and search engine crawlers http-request track-sc0 src table mock_404_tracking if !static_file !excluded_user_agent # increment gpc0 if response code was 404 http-response sc-inc-gpc0(0) if { status 404 } # checks if the 404 error rate limit was exceeded http-request deny deny_status 403 content-type text/html lf-string "404 abuse" if { sc0_gpc0_rate(mock_404_tracking) ge 5 } # whatever backend you're using use_backend apache_servers backend apache_servers server apache1 apache1:80 maxconn 32 # mock backend to hold a stick table backend mock_404_tracking stick-table type ip size 100k expire 10m store gpc0,gpc0_rate(1m)


1 分間に 404 リクエストのヒットが 5 回を超えると、ボットは 10 分間禁止されます。




現状では、この設定により、過剰な 404 を生成するボットが効果的にレート制限されます。ただし、 HAProxyを使用してWordPressへの攻撃をブロックした以前の例と統合することも必要です。


 global log stdout format raw daemon debug defaults log global mode http frontend main bind *:80 # We may, or may not, be running this with Cloudflare acting as a CDN. # If Cloudflare is in front of our servers, user/bot IP will be in # 'CF-Connecting-IP', otherwise user IP with be in 'src'. So we make # sure to set a variable 'txn.actual_ip' that has the IP, no matter what http-request set-var(txn.actual_ip) hdr_ip(CF-Connecting-IP) if { hdr(CF-Connecting-IP) -m found } http-request set-var(txn.actual_ip) src if !{ hdr(CF-Connecting-IP) -m found } # gets the actual IP on logs log-format "%ci\ %hr\ %ft\ %b/%s\ %Tw/%Tc/%Tt\ %B\ %ts\ %r\ %ST\ %Tr IP:%{+Q}[var(txn.actual_ip)]" # common static files where we may get 404 errors and also common search engine # crawlers that we don't want blocked acl static_file path_end .css .js .jpg .jpeg .gif .ico .png .bmp .webp .csv .ttf .woff .svg .svgz acl excluded_user_agent hdr_reg(user-agent) -i (yahoo|yandex|kagi|google|bing) # paths where we will rate limit users to prevent Wordpress abuse acl is_wp_login path_end -i /wp-login.php /xmlrpc.php /xmrlpc.php acl is_post method POST # 404 abuse blocker # track IPs but exclude hits on static files and search engine crawlers # increment gpc0 counter if response status was 404 and deny if rate exceeded http-request track-sc0 var(txn.actual_ip) table mock_404_track if !static_file !excluded_user_agent http-response sc-inc-gpc0(0) if { status 404 } http-request deny deny_status 403 content-type text/html lf-string "404 abuse" if { sc0_gpc0_rate(mock_404_track) ge 5 } # wordpress abuse blocker # track IPs if the request hits one of the monitored paths with a POST request # increment gpc1 counter if path was hit and deny if rate exceeded http-request track-sc1 var(txn.actual_ip) table mock_wplogin_track if is_wp_login is_post http-request sc-inc-gpc1(1) if is_wp_login is_post http-request deny deny_status 403 content-type text/html lf-string "login abuse" if { sc1_gpc1_rate(mock_wplogin_track) ge 5 } # your backend, here using apache for demonstration purposes use_backend apache_servers backend apache_servers server apache1 apache1:80 maxconn 32 # mock backends for storing sticky tables backend mock_404_track stick-table type ip size 100k expire 10m store gpc0,gpc0_rate(1m) backend mock_wplogin_track stick-table type ip size 100k expire 10m store gpc1,gpc1_rate(1m)


2 つのstick tablesを持って走り、両方の脅威を阻止します。


これで完了です。HAProxy は、単純なリバース プロキシとしてだけでなく、さまざまな用途に使用できます。これは、小さなスイス ナイフです。



このヘッドランプは修理作業において画期的なものでした。


私も持っていましたが、壊れたときに買い替えるのをためらって、携帯電話の懐中電灯に頼りました。確かに、それは機能しますが、両手が自由になる便利さを一度体験すると、もう元には戻れません。信頼できるハンズフリーの照明が必要な場合は、これは必需品です。


この投稿は元々 https://wasteofserver.com/stop-404-prying-bots-with-haproxy/に投稿されたもので、新しいリビジョンや追加のコメントはそちらで見つかるかもしれません。