User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。这些信息会保存在服务器日志里面,因此当网站有人恶意访问时,可以查看服务器日志里面的user-agent信息,并将恶意访问者的user-agent屏蔽掉。
最近我们就发现一些客户的网站同时被大量不同IP访问,导致网站使用过多服务器进程出现500 Internal Server Error错误,我们查看服务器日志发现这些访问都来自不同的IP,但是user-agent却完全相同,很明显是恶意访问该网站对其攻击,user-agent里面都含有“Mozilla/4.0”,因此我们在.htaccess里面添加上一下代码对其进行屏蔽:
SetEnvIfNoCase User-Agent "Mozilla/4.0" bad_user
Order Allow,Deny
Allow from all
Deny from env=bad_user
RewriteCond %{HTTP_USER_AGENT} Mozilla\/4\.0 [NC]
RewriteRule .* - [R=503,L]
.htaccess文件里面加上以上代码后,网站马上就可以正常访问了。不过这样做也可能会导致低版本的浏览器,如IE 6等无法网站网站,所以当攻击停止后建议尽快删除.htaccess里面添加的那些代码。
下面是只允许搜索引擎访问
SetEnvIfNoCase User-Agent .*google.* search_robotSetEnvIfNoCase User-Agent .*yahoo.* search_robotSetEnvIfNoCase User-Agent .*bot.* search_robotSetEnvIfNoCase User-Agent .*ask.* search_robotOrder Deny,AllowDeny from AllAllow from env=search_robot
下面是另一个例子
<IfModule mod_rewrite.c>SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOTDeny from env=HTTP_SAFE_BADBOT</ifModule>
阻止 User Agent 的所有请求
## .htaccess Code :: BEGIN
## Block Bad Bots by user-Agent
SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Java.* [NC,OR]
SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]
SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]
SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]
SetEnvIfNoCase user-Agent ^Zeus [NC]
Order Allow,Deny
Allow from all
Deny from env=bad_bot
## .htaccess Code :: END
Allow from and Rewrite* are directives from two different Apache's modules.
The first one is mod_authz_host and the other from mod_rewrite.
You can use mod_rewrite to do what you want:
RewriteEngine onRewriteCond %{HTTP_USER_AGENT} !=myuseragentRewriteRule .* - [F,L]
联系客服