|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionMalicious code writers have many attack vectors. Here, I will introduce a JS class which dissects an encoded JavaScript. I will show you a real life example on a script that tries to hide its actions by using some very common techniques and how to bypass them to uncover the true intent of the code. BackgroundJavaScript is a very flexible OO scripting language which is mostly known for its capability to run inside browsers and manipulate the web pages on the client side. For more information see the Wikipedia entries for JavaScript [1] and prototype based OO programming languages [2]. Some authors even think that JavaScript will be the scripting language of the future [3]. This article assumes that the reader knows the basic constructs of JavaScript. Most of the code hiding techniques are composed of two parts: an encrypted string and a decryptor, which un-mangles and finally evaluates the resulting piece of code. JavaScript (and most of the scripting languages) offers functions that take a string and evaluate it as a piece of code. This process is repeated several times (so the "decrypted" string may actually contain another string to be decrypted). The main goal of this article is to show you how to place hooks on these commonly used functions and to redirect them to a log window instead of execution, where the data can be conveniently interpreted. The hooked functions and the general ideaThe frequently used functions in these routines are: <script language="javascript">
document.write(unescape('%3C%73%63%72%69%70%74%2...
dF('%286FVFULSW%2853odqjxdjh%28...
</script>
It is clear that the first line must somehow define the function function someFunction() {
//...
}
document.write = someFunction;
and all the calls to Remark: One could use Venkman [5], the very powerful JavaScript debugger for Mozilla / Firefox. However this wonderful system doesn't perform really well with self modifying code (after all, which normal programmer would write such a code?!) Implementation detailsThe code is contained entirely in the file "jsdebug.js". It has three big parts: the declaration of the General notes: Ihe implementation was done on Firefox 1.0.6 (the latest stable release while writing this document) and while I've tried to be cross browser compatible, I've never tested on other browsers. Also, if you are going to analyze hostile code, I recommend Firefox since it is a very secure browser and the flaws are patched up very rapidly (and through the automated notify system you get to know about it very fast). During initialization of the system the following things are accomplished:
Now, I will provide a short description of every method and any important implementation quirk it might contain:
One useful trick in the code is the usage of escape / unescape when constructing functions from the strings. Since these functions themselves need to contain strings (delimited by single or normal quotation marks), these signs had to be eliminated. Also, one would have to eliminate the newline characters. Instead of writing a function with several replace methods one after the other I found it easier to wrap / unwrap the strings with the above mentioned function. A sample work session with the codeFor obvious reasons I'm not redistributing the hostile code, however I will describe the session during which it was analyzed to exemplify the usage of the provided class. The first step is to inspect the code very carefully. Tools recommended: an editor which offers syntax highlighting (I personally use jEdit [6]) and Tidy [7]. First I pass the HTML code through Tidy to make it more readable and then open it with jEdit and look very carefully through it (preferably more than once). This is a very important step, since after this you will open the script in a live browser and you can never be sure what exploitable part your browser contains (particularly IE is rated currently "Highly critical" in the Secunia database [8], while Firefox is rated "less critical" [9], but there are claims that there are some undisclosed exploits for it too [10]). In this particular case we see the following code: <script language="javascript">
document.write(unescape('%3C%73%63%72%69%70%74%2...
After looking closer, we see that there are actually two lines that are written as one, most probably to further obfuscate the code. After indenting properly we end up with: <script language="javascript">
document.write(unescape('%3C%73%63%72%69%70%74%2...
dF('%286FVFULSW%2853odqjxdjh%28...
</script>
After carefully looking at it we decide that the first line probably defines the " <script type="text/javascript" language="javascript" src="jsdebug.js"></script>
And fire it up in the browser. We get back the result of the first (and only) function dF(s) {
var s1=unescape(s.substr(0,s.length-1));
var t='';
for(i=0;i<s1.length;i++)
t+=String.fromCharCode(s1.charCodeAt(i)-s.substr(s.length-1,1));
document.write(unescape(t));
}
(The original code was again in a single line, indentation was added by me.) This is a simple routine and its only interaction with the "environment" is through the <SCRIPT language="JScript.Encode">#@~^fAAAAA==@#@&NG1Es+xDRS...
Wow, that looks strange. A little background info: In the year 2003 Microsoft created a little tool called Script encoder [11]. It provides a very weak encryption, which can be broken very quickly and only provides protection against the casual look (it also not compatible with any standard or other browsers!). I used Google with the query "JScript.Encode decode" and then the following website "Decode web pages containing "jscript.encode" sections" (I'm not affiliated to this site). Through it the final result appeared as: document.write(
'<OBJECT classid=XXXX-XXXX-XXX codebase=XXXXXX.cab></OBJECT>');
Searching Google for XXXXXX.cab resulted in the URL of the file. Downloading and unpacking it gave an install script (.inf) and a DLL. After submitting the DLL to VirusTotal [12] we concluded that this threat is detected by many antivirus engines, and now (at least in this case) we can rest. Detecting our implementationIf this technique becomes widely used, future malware authors will try to detect its presence, much the same way as (some) the current compiled ones try to detect the presence of debuggers. The best defense against that is of course very careful inspection of the code before you run it in the browser and its modification so that the detection code gets skipped. I will present three methods that are used to detect the presence of this system and how they can be defeated. I welcome any other suggestions on how this system can be detected and how a particular detection method can be defeated, but I would also like to stress again that the best defense is a deep inspection of the code before running it:
ConclusionToday's scripting malware is going through the same evolutionary path as binary viruses: from proof of concept through polymorphism and ending up with metamorphism (no example yet, but they will appear). To counter this it is necessary to study the techniques which provide the fastest and the most convenient method for analysis of the code to be able to react quickly to the new threats. References
Full disclosure: I’m a junior virus analyst for (SOFTWIN) the makers of the BitDefender antivirus product. However anything that I've written in this article must not be interpreted as an official statement of the company, it is merely a personal opinion.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||