网站被恶意镜像怎么办 php一段代码轻松搞定(全面版)

有时候你会发现,你在搜索引擎输入网站名称的时候,出来的网站信息是你们的,但是域名却是一个陌生的,这种情况可以基本确定网站被镜像了,那么究竟什么叫网站被镜像?

恶意镜像,也叫恶意克隆,恶意解析,是指有人通过域名 A 记录直接解析别人 IP 地址,从而得到一个在访问者眼中完全相同网站的过程。其工作原理基本上是这样子的:有用户访问镜像站点时,程序就会来正版的站点查询数据,并修改相关链接然后呈献给用户,实质上还是在读取原站的数据。严谨一点的解释:通过复制整个网站或部分网页内容并分配以不同域名和服务器,以此欺骗搜索引擎对同一站点或同一页面进行多次索引的行为 。

网站被镜像的危害

通俗的讲,恶意镜像者意图利用自己有一定权重的域名进行威压,通过某些手段复制了你的站点,除了域名不一样之外,其他内容一模一样,用户或许根本无法分辨。甚至对于一些新的站点,搜索引擎都会迷惑到底哪个是真的站点,那么就有可能正牌的网站被删除收录,而盗版的却被搜索引擎青睐。

虽然目前我们还不知道恶意镜像我们的网站到底有什么意图,但肯定对我们没什么好处,如果他这个域名有点什么不健康的信息,那么我们被镜像的站点有可能被污染掉,所以还是要警惕这个现象。

如何知道自己的网站是否被镜像

复制自己网站的完整标题(PS:查看自己站点首页源码,其中 <title> – 分享悲伤;共享快乐</title>),然后在谷歌和百度等搜索引擎里搜索,如搜索:intitle – 分享悲伤;共享快乐,如果有其他网站的网站标题、描述及网站内容跟你的一样,只有域名不一样,那就是被镜像了。

如何处理网站被镜像

这类镜像看似一个完整的站点,其实上是每次用户访问镜像站点,程序就会来正版的站点查询数据,并修改相关链接然后呈献给用户。实质上还是在读取原站的数据。以下就列举几种解决方法,大家自行取舍使用!

方法 1:查清镜像网站的主机 Ip,通过禁止 Ip 来解决

本教程基于 WordPress 程序,其他系统请自测!

1、获取镜像服务器 ip。注:这个 IP 可能不是 ping 到他域名的 IP

复制如下代码,新建一个 php 文件,并命名为“ip.php”上传到你的网站根目录。

<?php
$file = "ip.txt"; //保存的文件名
$ip = $_SERVER['REMOTE_ADDR'];
$handle = fopen($file, 'a');
fwrite($handle, "IP Address:");
fwrite($handle, "$ip");
fwrite($handle, "n");
fclose($handele);
?>

<?php
$file = "ip.txt"; //保存的文件名
$ip = $_SERVER['REMOTE_ADDR'];
$handle = fopen($file, 'a');
fwrite($handle, "IP Address:");
fwrite($handle, "$ip");
fwrite($handle, "n");
fclose($handele);
?>

2、然后访问你网站的镜像站点,在地址后面加…/ip.php,然后你就会在网站根目录找到 ip.txt 文件了,打开复制里面的 ip 地址。

3、然后打开你的.htaccess 文件,在后面加上如下代码(自行修改为刚刚获得的 ip)

#添加IP黑名单
Order Deny,Allow
Deny from 162.158.72.179
#添加IP黑名单
Order Deny,Allow
Deny from 162.158.72.179

当然,如果你使用 CDN,可以直接在 CDN 后台添加 ip 黑名单

这个时候你再刷新一下镜像站点,是不是已经 403 报错了呢?这个时候已经解决了这个镜像站点,然后就等待蜘蛛将其解决掉吧。

此方法的缺点就是如果镜像网站更换了 ip,那我们的屏蔽就失败了

方法 2:JS 来防护

在头部标签:取自 @boke112 导航

<head></head>

<head></head>

里加上下面的 JS 代码:

<script type="text/javascript">
if (document.location.host != "www.zzaxw.com") {
location.href = location.href.replace(document.location.host,'www.zzaxw.com');
}
</script>
<script type="text/javascript">
if (document.location.host != "www.zzaxw.com") {
location.href = location.href.replace(document.location.host,'www.zzaxw.com');
}
</script>

或加上以下的 JS 代码:

<script type="text/javascript">
rthost = window.location.host;
if (rthost != "www.zzaxw.com") {
top.location.href = "https://www.zzaxw.com";
}
</script>
<script type="text/javascript">
rthost = window.location.host;
if (rthost != "www.zzaxw.com") {
top.location.href = "https://www.zzaxw.com";
}
</script>

注意:将上面代码中的www.zzaxw.com改为你网站的首页主地址,如果我上面填写的不是我网站的主地址 www.zzaxw.com,而是 ilxtx.com 的话,就会导致网站一直刷新!

注:经过本站测试,如果镜像站屏蔽了 JS,则该方法失效。所以,最好把方法 2 和方法 3 结合使用!

方法 3:Js 被屏蔽后防止镜像的方法

将以下代码加到网站的 header.php 中:代码取自 @boke112

<div style="display:none;">
<script>proxy2016 = false;</script>
<img src="" onerror='setTimeout(function(){if(typeof(proxy2016)=="undefined"){window.location.host="www.zzaxw.com";}},3000);'>
</div>
<div style="display:none;">
<script>proxy2016 = false;</script>
<img src="" onerror='setTimeout(function(){if(typeof(proxy2016)=="undefined"){window.location.host="www.zzaxw.com";}},3000);'>
</div>

有些网站会屏蔽掉 JS 代码(如下面的代码) :

<script>...</script>
<script>...</script>

所以 <script>proxy2016 = false;</script> 代码将被过滤掉,img 的 onerror 设置超时时间 3000 毫秒,将运行函数部分,检测是否还存在 proxy2016 字符,如果没有找到就会将主机的 URL 改为 www.zzaxw.com;为了安全起见,将 js 部分可以使用 js 代码混淆(本站“JS 代码混淆” 工具 或 站长之家 JS 混淆工具)。
本站的混淆结果如下:

<div style="display:none;">
<script>proxy2016 = false;</script>
<img src=" " onerror='setTimeout(function(){if(typeof(proxy2016)=="undefined"){window["x6cx6fx63x61x74x69x6fx6e"]["x68x6fx73x74"]="x77x77x77x2ex69x6cx78x74x78x2ex63x6fx6d";}},3000);'>
</div>
<div style="display:none;">
<script>proxy2016 = false;</script>
<img src=" " onerror='setTimeout(function(){if(typeof(proxy2016)=="undefined"){window["x6cx6fx63x61x74x69x6fx6e"]["x68x6fx73x74"]="x77x77x77x2ex69x6cx78x74x78x2ex63x6fx6d";}},3000);'>
</div>

经过我的测试,此代码在 Chrome、IE11 和 360 极速浏览器上均有效,会跳转到源站的原文章页!在 Firefox 上则无效果,镜像的文章页并不会跳转到原站…… 将代码中 img 标签的 src 引用地址改为空格或无效的图片地址后,在 Firefox 上也起作用了!

方法 4:借助 Img 的 Onerror 事件

20161119 更新(增加搜狗快照支持):此方法使用了后,会导致百度快照、谷歌快照、必应快照和搜狗快照等跳到 404 页面(360 搜索快照则不会~),奈何不知怎么弄,2016-11-10 再次经过张戈的指导,将原代码中的:if( str1!=str3 ) 改为 :if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ) 。估计要等快照更新时才能知道效果了!

20161127:经过验证,上述更新已经起作用了!具体效果,请点我~

20171022 更新:从张戈那看到,这段代码会因为 onerror 死循环造成浏览网页的电脑高负载(CPU 飙升),因此在代码 onerror 触发事件中加入 onerror 清空机制,即加入this.onerror=null。【博客网页导致电脑 CPU 飙升的问题解决记录】

通过拆分域名链接与镜像站比对,然后用 img 标签 src 空值触发 onerror 来执行 js 比对,比对失败则跳转回源站。

①、WordPress 专用版

经过 @张戈 童学的不断改进(IE 不支持 window.stop() 函数,所以“20160909 版本”失效…),已经完美的适配 Firefox、Chrome、IE11 和 360 极速浏览器,而且可以跳转至源站的相应文章页,在此衷表感谢!下面 3 段任选一个即可。

代码如下:(复制粘贴到主题的 functions.php 最后一个?>之前)

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) 
*/
add_action('wp_footer','lxtx_deny_mirrored_websites');
function lxtx_deny_mirrored_websites(){
$currentDomain = 'www" + ".zzaxw." + "com';
// $currentDomain = '"zhangge." + "net"';
echo '<img style="display:none" src=" " onerror='this.onerror=null;var str1="'.$currentDomain.'";str2="docu"+"ment.loca"+"tion.host";str3=eval(str2);if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + ""' . $currentDomain .'"" + ")";eval(do_action) }' />';
}
/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版)
*/
add_action('wp_footer','lxtx_deny_mirrored_websites');
function lxtx_deny_mirrored_websites(){
$currentDomain = 'www" + ".zzaxw." + "com';
// $currentDomain = '"zhangge." + "net"';
echo '<img style="display:none" src=" " onerror='this.onerror=null;var str1="'.$currentDomain.'";str2="docu"+"ment.loca"+"tion.host";str3=eval(str2);if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + ""' . $currentDomain .'"" + ")";eval(do_action) }' />';
}

Ps:如果是丢到 wp_head,经过测试发现图片放到 head,浏览器会自动进行错误调整,导致一些本来在 head 的元素被丢到了 body 当中,比如 style.css,估计网页标准中 head 里面就不应该放置图片,所以移到了 footer 当中。

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版)
* https://www.zzaxw.com/mirrored-website.html
* 出自:zhangge.net
*/
add_action('wp_footer','lxtx_deny_mirrored_websites');
function lxtx_deny_mirrored_websites(){
 $currentDomain = "www' + '.zzaxw.' + 'com";
 // $currentDomain = "zhangge' + '.' + 'net";
 echo '<img style="display:none" src="nothing" onerror="this.onerror=null;var str1=''.$currentDomain.'';str2='docu'+'ment.loca'+'tion.host';str3=eval(str2);if( str1!=str3 ){ do_action = 'loca' + 'tion.' + 'href = loca' + 'tion.href' + '.rep' + 'lace(docu' +'ment'+'.loca'+'tion.ho'+'st,' + '\'' . $currentDomain .'\'' + ')';eval(do_action) }" />';
}
/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版)
*/
add_action('wp_footer','lxtx_kimsom_reverse_proxy_defense', 99);
function lxtx_kimsom_reverse_proxy_defense(){
 $currentDomain = '"www." + "zzaxw" + ".com"';
 echo '<img style="display:none" id="inlojv-rpd" src="nothing" data-url="'.home_url().'" onerror='this.onerror=null;var str0=document.getElementById("inlojv-rpd").attributes.getNamedItem("data-url").nodeValue;var ishttps="https:"==document.location.protocol?true:false;if(ishttps){var str1="https"+"://";}else{var str1="http"+"://";}var str2='.$currentDomain.';var str3=str1+str2;if( str0!=str3 ){location.href = location.href.replace(document.location.host,'. $currentDomain .');}'/>';
}

Tips:如果想像“20160909 版本”一样有个提示语,可将上面这段代码改为此

add_action('wp_footer','lxtx_kimsom_reverse_proxy_defense', 99);
function lxtx_kimsom_reverse_proxy_defense(){
 $currentDomain = '"www." + "zzaxw" + ".com"';
 echo '<img style="display:none" id="inlojv-rpd" src="nothing" data-url="'.home_url().'" onerror='this.onerror=null;var str0=document.getElementById("inlojv-rpd").attributes.getNamedItem("data-url").nodeValue;var ishttps="https:"==document.location.protocol?true:false;if(ishttps){var str1="https"+"://";}else{var str1="http"+"://";}var str2='.$currentDomain.';var str3=str1+str2;if( str0!=str3 ){alert("u8b66u544auff01u68c0u6d4bu5230u8be5u7f51u7ad9u4e3au6076u610fu955cu50cfu7ad9u70b9uff0cu5c06u7acbu5373u4e3au60a8u8df3u8f6cu5230u5b98u65b9u7ad9u70b9uff01");location.href = location.href.replace(document.location.host,'. $currentDomain .');}'/>';
}
摘自 @曾劲松博客
/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版)
*/
add_action('wp_footer','lxtx_kimsom_reverse_proxy_defense');
function lxtx_kimsom_reverse_proxy_defense(){
$domain_arr = explode('//',home_url());
$domain = $domain_arr[1];
 echo '<img style="display:none" id="inlojv-rpd" src="nothing" data-url="'.home_url().'" onerror="this.onerror=null;var str0=document.getElementById('inlojv-rpd').attributes.getNamedItem('data-url').nodeValue;var ishttps='https:'==document.location.protocol?true:false;if(ishttps){var str1='https'+'://';}else{var str1='http'+'://';}var str2=''.$domain.'';var str3=str1+str2;if( str0!=str3 ){alert('u8b66u544auff01u68c0u6d4bu5230u8be5u7f51u7ad9u4e3au6076u610fu955cu50cfu7ad9u70b9uff0cu5c06u7acbu5373u4e3au60a8u8df3u8f6cu5230u5b98u65b9u7ad9u70b9uff01');if (!!(window.attachEvent && !window.opera)){document.execCommand('stop');}else{ window.stop();}var str4='wind'+'ow.loca'+'tion.rep'+'lace(str3)';eval(str4);}">';
}
/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版)
*/
add_action('wp_footer','lxtx_kimsom_reverse_proxy_defense');
function lxtx_kimsom_reverse_proxy_defense(){
$domain_arr = explode('//',home_url());
$domain = $domain_arr[1];
 echo '<img style="display:none" id="inlojv-rpd" src="nothing" data-url="'.home_url().'" onerror="this.onerror=null;var str0=document.getElementById('inlojv-rpd').attributes.getNamedItem('data-url').nodeValue;var ishttps='https:'==document.location.protocol?true:false;if(ishttps){var str1='https'+'://';}else{var str1='http'+'://';}var str2=''.$domain.'';var str3=str1+str2;if( str0!=str3 ){alert('u8b66u544auff01u68c0u6d4bu5230u8be5u7f51u7ad9u4e3au6076u610fu955cu50cfu7ad9u70b9uff0cu5c06u7acbu5373u4e3au60a8u8df3u8f6cu5230u5b98u65b9u7ad9u70b9uff01');if (!!(window.attachEvent && !window.opera)){document.execCommand('stop');}else{ window.stop();}var str4='wind'+'ow.loca'+'tion.rep'+'lace(str3)';eval(str4);}">';
}

添加以上代码之后,再打开镜像站就会弹出提示:“警告!检测到该网站为恶意镜像站点,将立即为您跳转到官方站点!”,并在关闭或确定此提示后直接跳转到被镜像的网站。经过本站测试,本方法防止网站被镜像目前有效。

此方法在 IE11 上,会弹出提示框,但点击“确定”按钮后,网页并不会跳转。。。Firefox、Chrome 和 360 极速浏览器上则没此问题!

②、HTML 通用版

既然是利用 js 代码,那么就能用到如何 html 页面当中了。要不是为了可以放到 wp 的 functions.php,都没必要写成 php 的模式,直接用 html 代码即可:

<img style="display:none" src=" " onerror='this.onerror=null;var currentDomain="www." + "zzaxw" + ".com"; var str1=currentDomain; str2="docu"+"ment.loca"+"tion.host"; str3=eval(str2) ;if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + "currentDomain" + ")";eval(do_action) }' />
<img style="display:none" src=" " onerror='this.onerror=null;var currentDomain="www." + "zzaxw" + ".com"; var str1=currentDomain; str2="docu"+"ment.loca"+"tion.host"; str3=eval(str2) ;if( str1!=str3 && str3!="cache.baiducontent.com" && str3!="webcache.googleusercontent.com" && str3!="c.360webcache.com" && str3!="cncc.bingj.com" && str3!="snapshot.sogoucdn.com" ){ do_action = "loca" + "tion." + "href = loca" + "tion.href" + ".rep" + "lace(docu" +"ment"+".loca"+"tion.ho"+"st," + "currentDomain" + ")";eval(do_action) }' />

将以上代码中的: var currentDomain="www." + "zzaxw" + ".com"; 自行拆分成自己的域名,避免被镜像代码替换掉,比如: var currentDomain="zhangge." + "net";

然后将代码添加到网站的 <body> 之后即可(不建议放置到 <head> 里面,具体原因上文已说明),这个版本适合任何网页。

方法 5:通过禁止某些 User Agent 特征来防

服务器反爬虫攻略:Apache/Nginx/PHP 禁止某些 User Agent 抓取网站
我们都知道网络上的爬虫非常多,有对网站收录有益的,比如百度蜘蛛(Baiduspider),也有不但不遵守 robots 规则对服务器造成压力,还不能为网站带来流量的无用爬虫,比如 YY 蜘蛛(Yis…

参考上面这篇文章来禁止 UA 为 PHP 的抓取网页,从而达到防镜像的目的!

①、PHP 通用版:

将下面的代码贴到网站入口文件 index.php 中的第一个 <?php之后即可:

//防止恶意HTTP_USER_AGENT采集
$ua = $_SERVER['HTTP_USER_AGENT'];
$now_ua = array('FeedDemon ','BOT/0.1 (BOT for JCE)','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','lightDeckReports Bot','PHP');
 if(!$ua) {
header("Content-type: text/html; charset=utf-8");

die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');
}else{

foreach($now_ua as $value )

if(eregi($value,$ua)) {

header("Content-type: text/html; charset=utf-8");

die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');

}
}

//防止恶意HTTP_USER_AGENT采集
$ua = $_SERVER['HTTP_USER_AGENT'];
$now_ua = array('FeedDemon ','BOT/0.1 (BOT for JCE)','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','lightDeckReports Bot','PHP');
 if(!$ua) {
header("Content-type: text/html; charset=utf-8");

die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');
}else{

foreach($now_ua as $value )

if(eregi($value,$ua)) {

header("Content-type: text/html; charset=utf-8");

die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');

}
}

②、Wordpress 适用版

如果使用上面的 php 版本,WordPress 每次更新就会需要操作 index.php,比较麻烦,因此弄个专版。

将下面的代码贴到 functions.php 中的最后一个 ?>之前即可:

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版)
*/
//防止恶意HTTP_USER_AGENT采集
add_action('wp_head', 'lxtx_deny_mirrored_request', 0);
function lxtx_deny_mirrored_request()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
$now_ua = array('FeedDemon ','BOT/0.1 (BOT for JCE)','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','lightDeckReports Bot','PHP');
 if(!$ua) {
header("Content-type: text/html; charset=utf-8");
wp_die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');
}else{

foreach($now_ua as $value )

if(eregi($value,$ua)) {

header("Content-type: text/html; charset=utf-8");

wp_die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');

}
}
}

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) 
*/
//防止恶意HTTP_USER_AGENT采集
add_action('wp_head', 'lxtx_deny_mirrored_request', 0);
function lxtx_deny_mirrored_request()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
$now_ua = array('FeedDemon ','BOT/0.1 (BOT for JCE)','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','lightDeckReports Bot','PHP');
 if(!$ua) {
header("Content-type: text/html; charset=utf-8");
wp_die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');
}else{

foreach($now_ua as $value )

if(eregi($value,$ua)) {

header("Content-type: text/html; charset=utf-8");

wp_die('请勿采集本站,采集者木有小JJ!请正常访问,并认准【网】官方网址!');

}
}
}

经过测试,在 functions.php 中加入此代码后,打开镜像站后显示“Internal Server Error”,强制刷新后显示我们设置好的提示文字“请勿采集本站,采集者木有小 JJ!请正常访问,并认准【网】官方网址!”。

本站目前发现的恶意镜像域名

dijicat.com
lapaleo.com
iaroex.com
disauvi.com
3s3s.org
ytlqpo.com
s3.gvirabi.com
hdtmail.com
dimyapi.com

更多镜像网站等你提供~

在这些域名前面加上你们自己的的域名,看看有没有被恶意镜像。

友情提示:建议方法 2 和方法 3 一起使用!方法 4 包含方法 2 和方法 3~

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论