4

我有这个要解包和美化的 javascript 代码,它看起来类似于:

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,{}))

当我将其放入 jsbeautifier.org 时,我得到:

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

但是当我尝试使用 python 库(使用 jsbeautifier.beautify)时,它似乎没有正确解包:

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, {}));

我究竟做错了什么?

编辑:Python代码是:

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

3 回答 3

1

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.

于 2015-01-16T20:08:24.867 回答
0

jsbeautifier 坏了。请改用 node-js(或 phantomJS)。下面的工作示例代码:

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
于 2014-01-16T16:42:41.750 回答
0

我认为字符串开头的 /n 避免了解包器将 Js 检测为已打包。尝试做 jsbeautifier.beautify(script.strip())

于 2015-06-04T08:51:48.890 回答