网站被恶意镜像怎么办 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.ilxtx.com") {
location.href = location.href.replace(document.location.host,'www.ilxtx.com');
}
</script>

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

或加上以下的 JS 代码:

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

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

注意:将上面代码中的www.ilxtx.com改为你网站的首页主地址,如果我上面填写的不是我网站的主地址 www.ilxtx.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.ilxtx.com";}},3000);'>
</div>

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

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

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

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

<div style="display:none;">
<script>proxy2016 = false;</script>
<img src=" " onerror='setTimeout(function(){if(typeof(proxy2016)=="undefined"){window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x6f\x73\x74"]="\x77\x77\x77\x2e\x69\x6c\x78\x74\x78\x2e\x63\x6f\x6d";}},3000);'>
</div>

<div style="display:none;">
<script>proxy2016 = false;</script>
<img src=" " onerror='setTimeout(function(){if(typeof(proxy2016)=="undefined"){window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x6f\x73\x74"]="\x77\x77\x77\x2e\x69\x6c\x78\x74\x78\x2e\x63\x6f\x6d";}},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 段任选一个即可。效果请看这里:http://www.ilxtx.com.3s3s.org/the-shawshank-redemption-1994.html

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

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) - 龙笑天下
* https://www.ilxtx.com/mirrored-website.html
* 最后更新时间:20171022  发布时间:20160912
* 出自:zhangge.net
*/
add_action('wp_footer','lxtx_deny_mirrored_websites');
function lxtx_deny_mirrored_websites(){
  $currentDomain = 'www" + ".ilxtx." + "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) }\' />';
}

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) - 龙笑天下
* https://www.ilxtx.com/mirrored-website.html
* 最后更新时间:20171022  发布时间:20160912
* 出自:zhangge.net
*/
add_action('wp_footer','lxtx_deny_mirrored_websites');
function lxtx_deny_mirrored_websites(){
  $currentDomain = 'www" + ".ilxtx." + "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.ilxtx.com/mirrored-website.html
* 出自:zhangge.net
*/
add_action('wp_footer','lxtx_deny_mirrored_websites');
function lxtx_deny_mirrored_websites(){
 $currentDomain = "www' + '.ilxtx.' + '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) }" />';
}
/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) - 龙笑天下
* https://www.ilxtx.com/mirrored-website.html
* zhangge.net修改
*/
add_action('wp_footer','lxtx_kimsom_reverse_proxy_defense', 99);
function lxtx_kimsom_reverse_proxy_defense(){
 $currentDomain = '"www." + "ilxtx" + ".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." + "ilxtx" + ".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("\u8b66\u544a\uff01\u68c0\u6d4b\u5230\u8be5\u7f51\u7ad9\u4e3a\u6076\u610f\u955c\u50cf\u7ad9\u70b9\uff0c\u5c06\u7acb\u5373\u4e3a\u60a8\u8df3\u8f6c\u5230\u5b98\u65b9\u7ad9\u70b9\uff01");location.href = location.href.replace(document.location.host,'. $currentDomain .');}\'/>';
}
摘自 @曾劲松博客

/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) - 龙笑天下
* https://www.ilxtx.com/mirrored-website.html
*/
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(\'\u8b66\u544a\uff01\u68c0\u6d4b\u5230\u8be5\u7f51\u7ad9\u4e3a\u6076\u610f\u955c\u50cf\u7ad9\u70b9\uff0c\u5c06\u7acb\u5373\u4e3a\u60a8\u8df3\u8f6c\u5230\u5b98\u65b9\u7ad9\u70b9\uff01\');if (!!(window.attachEvent && !window.opera)){document.execCommand(\'stop\');}else{ window.stop();}var str4=\'wind\'+\'ow.loca\'+\'tion.rep\'+\'lace(str3)\';eval(str4);}">';
}
/**
* 网站被恶意镜像怎么办 一段代码轻松搞定(全面版) - 龙笑天下
* https://www.ilxtx.com/mirrored-website.html
*/
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(\'\u8b66\u544a\uff01\u68c0\u6d4b\u5230\u8be5\u7f51\u7ad9\u4e3a\u6076\u610f\u955c\u50cf\u7ad9\u70b9\uff0c\u5c06\u7acb\u5373\u4e3a\u60a8\u8df3\u8f6c\u5230\u5b98\u65b9\u7ad9\u70b9\uff01\');if (!!(window.attachEvent && !window.opera)){document.execCommand(\'stop\');}else{ window.stop();}var str4=\'wind\'+\'ow.loca\'+\'tion.rep\'+\'lace(str3)\';eval(str4);}">';
}

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

阅读剩余部分

相关阅读 >>

wordpress优化头部 去掉版权等信息 wordpress去掉generator

详解wordpress中过滤链接与过滤sql语句的方法

crayon syntax highlighter代码高亮插件与fancybox图片暗箱冲突的解决方法

wordpress更换主题之后出错的解决方法

wordpress 上传附件中文文件名乱码解决办法(for windows)

wordpress中给媒体文件添加分类和标签的php功能实现

centos下如何合并目录centos下合并目录的方法

wordpress与drupal的nginx配置rewrite重写规则示例

wordpress thickbox 点击图片显示下一张图的修改方法

wordpress插件开发设计

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...