Wednesday, September 2, 2009

Obfuscated code examples

In this post I will give to some examples of JavaScript Obfuscation.
Example 1:
This following code is obfuscated using online JavaScript Obfuscator on http://www.javascriptobfuscator.com/
Plain text Code:
var a="Hello World!";
function MsgBox(msg)
{
    alert(msg+"\n"+a);
}
MsgBox("OK");
Obfuscated code:
var _0x8e48=["\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x21","\x0A","\x4F\x4B"];var a=_0x8e48[0];function MsgBox(_0xab5dx3){alert(_0xab5dx3+_0x8e48[1]+a);} ;MsgBox(_0x8e48[2]);
 
You can apply multiple level of obfuscation by repeatedly obfuscating the obfuscated code.
Following obfuscated code is 2 level obfuscation of plain text code given above.  That is, obfuscation of obfuscated code.
Obfuscated code of the Obfuscated code given above:
var _0x1c08=["\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x21","\x0A","\x4F\x4B"];var _0x1bae=[_0x1c08[0],_0x1c08[1],_0x1c08[2]];var a=_0x1bae[0];function MsgBox(_0xc9d7x4){alert(_0xc9d7x4+_0x1bae[1]+a);} ;MsgBox(_0x1bae[2]);

Example 2:
The following code is obfuscated using  online JavaScript Obfuscator. http://www.virtualpromote.com/tools/javascript-encrypt/
Plain text code:
document.write("hello, world");
 
Obfuscated code:
var enkripsi="'1Aqapkrv'1G'2Cfmawoglv,upkvg'0:'00jgnnm'0A'02umpnf'00'0;'1@'2C'1A-qapkrv'1G";
teks="";
teksasli="";
var panjang;
panjang=enkripsi.length;
for (i=0;i
  teks+=String.fromCharCode(enkripsi.charCodeAt(i)^2)
}
teksasli=unescape(teks);
document.write(teksasli);

In the second example, document.write is used. So during parsing of the code in the browser (for example, Firefox), DOM nodes will be get created for obfuscated code and for each level of  de-obfuscated code.

1 comment: