Posts Tagged ‘snippets’

Unpacking Packed JavaScript Code

// December 30th, 2011 // 1 Comment » // 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!

Recursive Global Search and Replace in Linux

// June 6th, 2011 // No Comments » // Adventures in File Sitting

I was working on a very old project of mine, a MUD called DooMMUD (based off of the popular id Software series of games) and just for shits n giggles, I thought I would move the codebase over to something newer and more stable like the Circle-based tbaMUD codebase just to see if I could get it up and running again.

At some point I needed to change some Variables and Classes (structs really) over the entire codebase (e.g. coins to credits)

No way was I going to bother editing all the files (if I could even find them all), I would almost definitely miss something.

So I used a combination of grep and xargs & sed to recursively go through every directory in the tbaMUD structure and replace every string or replace every phrase that I needed changed.

Behold the majesty of my one line command:

grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

We pipe the recursive results from grep into xargs & sed which takes those results and run a global search and replace within each instance of string1 with string2.

Huzzah!

I remember when I was 8 years old, my father had made me an account on his Sun Microsystem Workstation 3/80 running some later version of SunOS. I used KornShell over a matter of person preference (I don’t think BASH was even around back then) and every day I would look into /usr/bin or /bin to look for new command, run man page on it and tinker around with it.

My father said it best when he said “Its like having a Christmas Present every single day”. The man was right.

Converting XML to Associative Array in PHP

// March 2nd, 2011 // No Comments » // Code Codex

Sometimes there are situations where I have had code that is expecting an Associate Array with its corresponding values to deal with. However, as can often stand the case when propagating over to XML, sometimes its not possible (or digitally healthy) to convert everything all at once and I end up in a situation where I have all my data in single layer XML objects but need to interact with it on an associate array level.

So, as a quick to fix I wrote this small function entitled xml2AssArray() to deal with the children of a single layer XML object and turn it into an array with the children’s names used as the key-names of the array.

function xml2AssArray($xmlobj)
{
    $assarray = array(); // Initialize the Array
    // Loop through the XML object's children
    foreach ($xmlobj->children() as $child)
    {
        // Assign the childs name as the key for the next entry while assigning the value itself as well
        $assarray[$child->getName()] = $child;
    }
    return $assarray; // Return the new Array
}

Let’s take this XML object (called $myxml which we create using SimpleXML) for example:

<?xml version="1.0"?>
<Lead>
    <ID>1213</ID>
    <SSN>123450000</SSN>
    <FullName>Israel Smith</FullName>
</Lead>

When we run it through our new function, the array that is returned, is now an associate array:

> $myarray = xml2AssArray($myxml);
> echo $myarray['FullName'];
Israel Smith
>

Huzzah!