一段程式碼輕鬆解決網站被惡意鏡像

網站被惡意鏡像

有時候你會發現,在搜尋引擎輸入網站名稱時,出來的網站資訊是你們的,但網域卻是陌生的,這種情況基本上就可以確定網站被鏡像了。那到底什麼叫網站被鏡像?

惡意鏡像,也叫惡意複製、惡意解析,是指有人把自己的網域 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 這個字串還在不在,找不到就把主機網址改成 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~

發表評論

評論 暫無評論

文章目錄