Please enable Javascript to view this site.
 
 
  • Coding

  • Technology

  • Japanese

  • Chinese

  • Miscellaneous

Unpacking Javascript

March 29, 2011 | Tags: | Leave a comment

If you’ve worked with numerous javascript-heavy open source web applications, you’ve probably come across javascript files that look like they were encoded into a single large block of gibberish. Whether the purpose of this obfuscation was to protect the original code or to minimize the download size, it becomes a pain if you are trying to debug or modify a piece of javascript code that looks something like this:

eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,
String)){while(c--){d[c]=k[c]||c}k=[(function(e){return d[e]})];
e=(function(){return'\\w+'});c=1};while(c--){if(k[c]){p=p.replace(
new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('3.0(\'1 2!\');',
4,4,'write|Hello|World|document'.split('|'),0,{}))

Well, it turns out this “encoding” is the result of one of several “packers” (which you may have realized if you noticed the “cute” arguments of the first function), the most famous of which is probably Dean Edward’s packer.

How it works is like this. The characters that make up the content of the code are all contained at the end of the block, albeit jumbled together in a seemingly random order. The eval() function at the beginning of the block contains the logic to rearrange those characters into the correct order necessary to create the original javascript code.

This means you could actually run the code in your browser and it should output the correct javascript code for you. Of course, you’d have to escape out certain html entities (or dump it into a textbox), and there wouldn’t be any whitespace formatting. The easiest solution by far, however, is to simply use an online “decoder” or “unpacker” such as jsbeautifier.org.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.