Blocking libwww-perl with Apache .htaccess

Testing a website recently with one of the online SEO tool, amongst the results was the comment:

Your server appears to allow access from User-agent Libwww-perl. Botnet scripts that automatically look for vulnerabilities in your software are sometimes identified as User-Agent libwww-perl. By blocking access from libwww-perl you can eliminate many simpler attacks.

So how do I block the User-Agent libwww-perl?

The libwww-perl user agent may be blocked through the file .htaccess or you may find support for blocking by user agent in your installed WordPress security plugin.

Blocking using the .htaccess file allows a core configuration to be created which can easily be copied to other websites. Much simpler to maintain.

Implementing within the .htaccess file actions the inhibit using the Apache web server, whilst actioning via a security plugin imposes a greater burden on the server as it is done through the CMS.

The user agent to be blocked will be added to the .htaccess file. This is a file associated with the Apache web server. It’s to be found at the root of the public website files.

Access the file either via ftp or online through your domain account control panel. Often the public website files are to be found one level in from the FTP root. For example a directory called public_html.

To block a website visitor add the following to the .htaccess file, ensuring its wrapped within the mod_rewrite section

<IfModule mod_rewrite.c>
BrowserMatchNoCase "libwww-perl" bots
BrowserMatchNoCase "Zeus" bots

Order Allow,Deny
Allow from ALL
Deny from env=bots

</ifModule>

Alternatively

RewriteCond %{HTTP_USER_AGENT} ^libwww-perl [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus

RewriteRule ^.* - [F,L] 

In the example above the match is using regular expressions to test for the user agent with the given string. I’ve also included an exclusion for the Zeus bot.

The final line RewriteRule ^.* – [F,L], forbids access and throws a 403 server response.

As always when making changes to your .htaccess file, check to ensure that you haven’t blocked those users and robots which you wish to visit.

References

Immotion hosting: Block unwanted users using htaccess

Apache: mod_setenvif

Hostgator: apache htaccess user agent blocks