Over the last week, we’ve been working with some interesting malware injections. Developers and malware prevention professionals usually think of hidden iframes that deliver spam-seo or other malware as “easy to spot”. Take this injection, for example (thanks to Sucuri team member, Rafael C., for the sample):
This is not your traditional iframe src=’http://… code, but you can see where the bad code lives in the example above. This is a problem for the malware creators, because if an infection is easily detectable then it’s a relatively straightforward process to write a script to detect and clean it up. That’s why the next step for the malware creator is to hide or obfuscate the injection.
Spotting Obfuscated Code
At first glance, this code looks like a CSS related script that is part of your site’s visual architecture. Usually, you don’t want to touch these files because it could break your website’s look and feel. This, of course, is exactly what the malware creator wants you to think.
Is it Malware?
The tricky thing here is that this function is actually creating a CSS rule. Were we wrong to think that it’s malicious?
What we need to do to find out is look at the content of the rule. To do that, we look for the function call.
The code is defining the background image for the #va selector. When you look closely you can see that String.fromCharCode is not a valid URL. Remember, malware creators need to figure out how to hide their code injections. In this scenario, storing the functions it needs inside a CSS style is ingenious.
Now that we know where the malware lives, we can find out how it is recovering those strings:
Putting It All Together
In the code above, we see that the vkk variable is used as the fromCharCode function and uu variable contains a va string. At this moment, this doesn’t make sense, but it starts to come together as we keep moving through some lines of code.
It’s important to the hacker that nothing is stored in plain sight (if it was, it’d be much easier to clean). In this instance, take the t variable as an example; it contains the number 2. In this case, this value is attributed by subtracting 2 from the number of seconds of a date stored in the knr variable. That’s pretty complex, right?
This t variable is used to multiply all entries of the xt array*
*Some of the content of this variable has been removed to shorten the post. It doesn’t affect the code’s logic.
Next, there is an empty function called g, which is attributed to hhhu variable, and within these parameters the uu is being used to create the function. By concatenating the e, va(the content of uu) and l we end up with, eval! Now we’re finding some malware.
Then, another chain of variables, hhhu, is now attributed to ac with a different function–the one inside the variable ry, which, previously, we saw contains String.fromCharCode. Now it’s eval’ing String.fromCharCode for CharCodes that are stored in the xt variable.
Finally, after all this, it calls the eval again–the hhhu–but now to execute the code inside dwms variable, which was decoded using the for loop from before.
Dissecting Malware is A Full Time Job
That was an illustration of one payload. It’s just one data point that articulates the sort of complex obfuscation we deal with on a daily basis and, we can say without reservation, as we continue to find new ways to detect it more easily, malware creators will find ways to make their obfuscation more and more complex. If you’re having trouble with malware or blacklisting, take a look at the symptoms of malware and ask us to help.