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

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

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

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

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

方法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);
?>

2、然后访问你网站的镜像站点,在地址后面加.../ip.php,然后你就会在网站根目录找到 ip.txt 文件了,打开复制里面的 ip 地址。
3、然后打开你的.htaccess 文件,在后面加上如下代码(自行修改为刚刚获得的 ip)

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

当然,如果你使用 CDN,可以直接在 CDN 后台添加 ip 黑名单
这个时候你再刷新一下镜像站点,是不是已经 403 报错了呢?这个时候已经解决了这个镜像站点,然后就等待蜘蛛将其解决掉吧。

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

方法2:JS 来防护

在头部标签:<head></head>里加上下面的 JS 代码:

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

或加上以下的 JS 代码:

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

注意:将上面代码中的xiaaijun.com改为你网站的首页主地址。

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

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

将以下代码加到网站的 header.php 中:

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

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

本站的混淆结果如下:

<div style="display:none;">
<script>aijun2020 = false;</script>
<img src=" " onerror='setTimeout(function(){if(typeof(aijun2020)=="undefined"){window["\x6c\x6f\x63\x61\x74\x69\x6f\x6e"]["\x68\x6f\x73\x74"]="\x78\x69\x61\x61\x69\x6a\x75\x6e\x2e\x63\x6f\x6d";}},3000);'>
</div>
经测试,此代码在 Chrome、IE11 和 360 极速浏览器上均有效,会跳转到源站的原文章页!将代码中 img 标签的 src 引用地址改为空格或无效的图片地址后,在Firefox 上也起作用了!

方法4:借助 Img 的 Onerror 事件

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

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

add_action('wp_footer','deny_mirrored_websites');
function deny_mirrored_websites(){
    $currentDomain = "xiaaijun." + "com';
    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 当中。
或者:
add_action('wp_footer','deny_mirrored_websites');
function deny_mirrored_websites(){
    $currentDomain = "xiaaijun.' + 'com";
    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','kimsom_reverse_proxy_defense', 99);
function kimsom_reverse_proxy_defense(){
    $currentDomain = '"xiaaijun" + ".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 .');}\'/>';
}

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

<img style="display:none" src=" " onerror='this.onerror=null;var currentDomain="xiaaijun" + ".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="xiaaijun" + ".com"; 自行拆分成自己的域名,避免被镜像代码替换掉,比如:var currentDomain="xiaaijun." + "com";
然后将代码添加到网站的<body>之后即可(不建议放置到<head>里面,具体原因上文已说明),这个版本适合任何网页。

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

PHP 通用版,以及Wordpress 适用版,参考本文原作者:
https://www.ilxtx.com/mirrored-website.html

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