My Writings. My Thoughts.

Unpacking Packed JavaScript Code

// December 30th, 2011 // No Comments » // Code Codex

I’m sure if you are reading this, you too, as I have many times, come across packed JavaScript code that you needed either to check for maliciousness, modification, curiosity, or perhaps even something more sinister. Don’t judge me, I won’t judge you. Either way, you’ve probably had the same cringing look on your face the first time you saw this.

So, of course, I went about to figure out how to decipher, unscramble, or generally “decrypt” this annoyance. I’ve come across several methods while surfing the Internet, and I’d like to share what I’ve found.

As there are many different methods for JS packing, your mileage may vary greatly on the usefulness of the functionality here for unpacking. However, there are only so many ways to skin a cat and invariably most people have their code packed with the same method.

There are several ways to go about unpacking the code, and I’ll go ahead and list my quick and dirty favorites.

Method 1

The following method (by the guys at http://www.strictly-software.com/unpacker), of course their code needs to be decoded itself by looking at their reformat.js

function unpack()
{
var p = G('txtPacked').value, c="";
if (p != "")
{
c = p;
if (/eval\(+function\(/.test(c))
{
var _e = eval;
var s = "eval = function(v) { c = v; };" + c + "; eval = _e;";
eval(s);
}
c = R(c,{indent_size: 1, indent_char: '\t'});
}

G('txtUnpacked').value = unescape(c);
}

Method 2

Another method supposed is to replace the eval() function call with a document.write() call


eval(code);

to

document.write(code);

Of course, that is just enough to get you started, but it was enough for me, and I hope it helps out others. Good luck, happy hunting & happy hacking!

Kantōrai

// August 13th, 2011 // No Comments » // Poetry


»Kantōrai«

The People are the Castle,
The People are the Stones,
The People are the Moat!

Love for your Comrades,
Hatred for your Enemies!

War is of Men,
Not of Castles.
War lies in the Attack,
Not in the Defense.

-

Lay down your Corpses to be your Walls,
Lay down your Corpses to bridge the Moat.
Attack your enemies on the Corpses of your Comrades.

Victory will be Yours!

Spoken by Ogami Ittō — 16th Century Feudal Japan

PHP Function in_array_by_id Search an array for an elements location or index

// July 11th, 2011 // No Comments » // Code Codex

It seems crazy that there isn’t a function that already does this very simple task, namely, search an array for an element, and return its index (i.e. location) in that array.

  • in_array() will tell you if the element is in the array, but not where it is.
  • search_array() will search an associative array, but not a normal array.

So here is an example array with 7 string elements and what I want to be able to do.


> $myarray = array('Home', 'Control Panel', 'Members', 'Todays Figures', 'History', 'Listing', 'Add New Offer');
> echo in_array_by_id($myarray, 'Control Panel');
2

We passed two variables to our currently non-existent function in_array_by_id(). The array being searched and the element whose index we are search for (i.e. the proverbial needle in the haystack) respectively.

So I put together this sadly simple function:

 function in_array_by_id($array, $needle)
 {
     $array_index = 0;
     foreach ($array as $element)
     {
         if ($needle == $element)
         {
              return $array_index;
         }
         $array_index++;
     }
     return NULL;
 }

As you can see, it returns a NULL value if the element is not found. So when doing checks you’ll want to remember to use the !== or === since you will be dealing with return values of 0 when the element is the first in the array.