How to unpack javascript using jsbeautifier in python?

I have this javascript code that I want to unzip and decorate, it looks something like 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 in 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 to use the python library (using jsbeautifier.beautify), it does not seem to decompress 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:

 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
source share
3 answers

You must import this module:

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

I tested this on Windows 32bit, jsbeautifier 1.54.

+1
source

jsbeautifier does not work. Use node-js (or phantomJS) instead. An example of a working example 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 
0
source

I think that / n at the beginning of the line does not allow the unpacker to detect Js as packed. try to do jsbeautifier.beautify (script.strip ())

0
source

Source: https://habr.com/ru/post/1481422/


All Articles