corrade-nucleus-nucleons – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | office | 1 | # |
2 | # Unpacker for eval() based packers, a part of javascript beautifier |
||
3 | # by Einar Lielmanis <einar@jsbeautifier.org> |
||
4 | # |
||
5 | # written by Stefano Sanfilippo <a.little.coder@gmail.com> |
||
6 | # |
||
7 | # usage: |
||
8 | # |
||
9 | # if detect(some_string): |
||
10 | # unpacked = unpack(some_string) |
||
11 | # |
||
12 | |||
13 | """Unpacker for eval() based packers: runs JS code and returns result. |
||
14 | Works only if a JS interpreter (e.g. Mozilla's Rhino) is installed and |
||
15 | properly set up on host.""" |
||
16 | |||
17 | from subprocess import PIPE, Popen |
||
18 | |||
19 | PRIORITY = 3 |
||
20 | |||
21 | def detect(source): |
||
22 | """Detects if source is likely to be eval() packed.""" |
||
23 | return source.strip().lower().startswith('eval(function(') |
||
24 | |||
25 | def unpack(source): |
||
26 | """Runs source and return resulting code.""" |
||
27 | return jseval('print %s;' % source[4:]) if detect(source) else source |
||
28 | |||
29 | # In case of failure, we'll just return the original, without crashing on user. |
||
30 | def jseval(script): |
||
31 | """Run code in the JS interpreter and return output.""" |
||
32 | try: |
||
33 | interpreter = Popen(['js'], stdin=PIPE, stdout=PIPE) |
||
34 | except OSError: |
||
35 | return script |
||
36 | result, errors = interpreter.communicate(script) |
||
37 | if interpreter.poll() or errors: |
||
38 | return script |
||
39 | return result |