Reputation: 3518
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)
Upvotes: 4
Views: 3097
Reputation: 719
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())
Upvotes: 0
Reputation: 21
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.
Upvotes: 1
Reputation: 816
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
Upvotes: 0