Httpd – How to block all HEAD requests to urls that contain a substring on Apache

apache-2.4Apache2blockhttpdhttpd.conf

A server I administer is being pounded with a poorly coded AWS-built bot that switches IPs constantly and appears to be stuck in a recursive encoding loop. The only consistent fingerprint I can see is that each request is only a HEAD request and each request appears to re-encode the previous one. So http://someurl.com/?foo=%25bar becomes ..%2525.. becomes ..%252525..%2525252525252525...x1000.

Here's an example of the types of requests I see:

HEAD http://example.com/?foo=%25bar
HEAD http://example.com/?foo=%2525bar
HEAD http://example.com/?foo=%252525bar
HEAD http://example.com/?foo=%25252525bar
HEAD http://example.com/?foo=%2525252525bar
HEAD http://example.com/?foo=%2525252525...25bar (x1000)

So far I've been using Cloudflare firewalls to block each IP, but they keep switching IPs.

How can I simply block all HEAD requests containing a substring (say %25252525)?

I'm running Apache/2.4.6 (CentOS).

Best Answer

How about using mod_rewrite in your .htaccess?

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_METHOD} HEAD
    RewriteCond %{QUERY_STRING} 25252525
    RewriteRule .* - [F,L]
</IfModule>

That will block all HEAD requests with a query string containing "25252525". Obviously you can tune this more as you see fit!