From 2240d2a6a52a4fab966abf31fe03d66de726cf94 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 25 Mar 2012 10:56:45 +1300 Subject: Pretty view now indents Javascript. Thanks to the JSBeautifier project, which is now included in the contrib directory. --- .../contrib/jsbeautifier/unpackers/evalbased.py | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 libmproxy/contrib/jsbeautifier/unpackers/evalbased.py (limited to 'libmproxy/contrib/jsbeautifier/unpackers/evalbased.py') diff --git a/libmproxy/contrib/jsbeautifier/unpackers/evalbased.py b/libmproxy/contrib/jsbeautifier/unpackers/evalbased.py new file mode 100644 index 00000000..b17d926e --- /dev/null +++ b/libmproxy/contrib/jsbeautifier/unpackers/evalbased.py @@ -0,0 +1,39 @@ +# +# Unpacker for eval() based packers, a part of javascript beautifier +# by Einar Lielmanis +# +# written by Stefano Sanfilippo +# +# usage: +# +# if detect(some_string): +# unpacked = unpack(some_string) +# + +"""Unpacker for eval() based packers: runs JS code and returns result. +Works only if a JS interpreter (e.g. Mozilla's Rhino) is installed and +properly set up on host.""" + +from subprocess import PIPE, Popen + +PRIORITY = 3 + +def detect(source): + """Detects if source is likely to be eval() packed.""" + return source.strip().lower().startswith('eval(function(') + +def unpack(source): + """Runs source and return resulting code.""" + return jseval('print %s;' % source[4:]) if detect(source) else source + +# In case of failure, we'll just return the original, without crashing on user. +def jseval(script): + """Run code in the JS interpreter and return output.""" + try: + interpreter = Popen(['js'], stdin=PIPE, stdout=PIPE) + except OSError: + return script + result, errors = interpreter.communicate(script) + if interpreter.poll() or errors: + return script + return result -- cgit v1.2.3