5

I have this javascript code which I want to unpack and beautify, it looks similar to this:

eval(function(p,a,c,k,e,r){e=String;if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'^$'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('document.body.innerHTML="<iframe width=\'100%\' scrolling=\'no\' height=\'2500\' frameborder=\'0\' src=\'http://www.exmaple.com\'>";',[],1,''.split('|'),0,{}))

When I put this into jsbeautifier.org I get:

document.body.innerHTML = "<iframe width='100%' scrolling='no' height='2500' frameborder='0' src='http://www.example.com'>";

But when I try and use the python library (using jsbeautifier.beautify) it doesn't seem to unpack properly:

print al(function (p, a, c, k, e, r) {
    e = String;
    if ('0'.replace(0, e) == 0) {
        while (c--) r[e(c)] = k[c];
        k = [
            function (e) {
                return r[e] || e
            }
        ];
        e = function () {
            return '^$'
        };
        c = 1
    };
    while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]);
    return p
}('document.body.innerHTML="<iframe width=\'100%\' scrolling=\'no\' height=\'2500\' frameborder=\'0\' src=\'http://www.example.com\'>";', [], 1, ''.split('|'), 0, {}));

What am I doing wrong?

EDIT: Python code is:

import jsbeautifier
#script needs to have '\n' at the beginning otherwise jsbeautifier throws an error
script = """\neval(function(p,a,c,k,e,r){e=String;if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'^$'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('document.body.innerHTML="<iframe width=\'100%\' scrolling=\'no\' height=\'2500\' frameborder=\'0\' src=\'http://www.example.com\'>";',[],1,''.split('|'),0,{}))"""
jsbeautifier.beautify(script)
4
  • What is your Python code? Commented May 17, 2013 at 16:02
  • It looks like it's a problem with the Python port. Commented May 17, 2013 at 18:14
  • Ah ok, that's annoying. I'll have to file my issues with them. Commented May 17, 2013 at 19:03
  • To unpack it in javascript is: eval("String" + packed_string.substr(4)) Commented Sep 16, 2013 at 2:29

3 Answers 3

3

You should import this module:

import jsbeautifier.unpackers.packer as packer
unpack = packer.unpack(some_packed_code)

I have tested this in Windows 32bit, jsbeautifier 1.54.

Sign up to request clarification or add additional context in comments.

Comments

0

jsbeautifier is broken. Use node-js (or phantomJS) instead. Working example code below:

import subprocess
import StringIO

data = r"""eval(function(p,a,c,k,e,r){e=String;if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'^$'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('document.body.innerHTML="<iframe width=\'100%\' scrolling=\'no\' height=\'2500\' frameborder=\'0\' src=\'http://www.exmaple.com\'>";',[],1,''.split('|'),0,{}))"""

data = 'console.log' + data[4:]
p = subprocess.Popen(['node'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = p.communicate(data)
print stdout

Comments

0

I think that the /n in the beginning of the string avoid the unpacker to detect the Js as packed. try to do jsbeautifier.beautify(script.strip())

1 Comment

Your answer is unclear, could you elaborate on your suggestion and how it solves the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.