aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-03-25 10:56:45 +1300
committerAldo Cortesi <aldo@nullcube.com>2012-03-25 10:56:45 +1300
commit2240d2a6a52a4fab966abf31fe03d66de726cf94 (patch)
treeac59fc38dcd3eb95e76e69c8ef072ccff3cd4118 /libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py
parent74c51df5806e98046a1abea72c377781434810d8 (diff)
downloadmitmproxy-2240d2a6a52a4fab966abf31fe03d66de726cf94.tar.gz
mitmproxy-2240d2a6a52a4fab966abf31fe03d66de726cf94.tar.bz2
mitmproxy-2240d2a6a52a4fab966abf31fe03d66de726cf94.zip
Pretty view now indents Javascript.
Thanks to the JSBeautifier project, which is now included in the contrib directory.
Diffstat (limited to 'libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py')
-rw-r--r--libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py b/libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py
new file mode 100644
index 00000000..aa4344a3
--- /dev/null
+++ b/libmproxy/contrib/jsbeautifier/unpackers/javascriptobfuscator.py
@@ -0,0 +1,58 @@
+#
+# simple unpacker/deobfuscator for scripts messed up with
+# javascriptobfuscator.com
+#
+# written by Einar Lielmanis <einar@jsbeautifier.org>
+# rewritten in Python by Stefano Sanfilippo <a.little.coder@gmail.com>
+#
+# Will always return valid javascript: if `detect()` is false, `code` is
+# returned, unmodified.
+#
+# usage:
+#
+# if javascriptobfuscator.detect(some_string):
+# some_string = javascriptobfuscator.unpack(some_string)
+#
+
+"""deobfuscator for scripts messed up with JavascriptObfuscator.com"""
+
+import re
+
+PRIORITY = 1
+
+def smartsplit(code):
+ """Split `code` at " symbol, only if it is not escaped."""
+ strings = []
+ pos = 0
+ while pos < len(code):
+ if code[pos] == '"':
+ word = '' # new word
+ pos += 1
+ while pos < len(code):
+ if code[pos] == '"':
+ break
+ if code[pos] == '\\':
+ word += '\\'
+ pos += 1
+ word += code[pos]
+ pos += 1
+ strings.append('"%s"' % word)
+ pos += 1
+ return strings
+
+def detect(code):
+ """Detects if `code` is JavascriptObfuscator.com packed."""
+ # prefer `is not` idiom, so that a true boolean is returned
+ return (re.search(r'^var _0x[a-f0-9]+ ?\= ?\[', code) is not None)
+
+def unpack(code):
+ """Unpacks JavascriptObfuscator.com packed code."""
+ if detect(code):
+ matches = re.search(r'var (_0x[a-f\d]+) ?\= ?\[(.*?)\];', code)
+ if matches:
+ variable = matches.group(1)
+ dictionary = smartsplit(matches.group(2))
+ code = code[len(matches.group(0)):]
+ for key, value in enumerate(dictionary):
+ code = code.replace(r'%s[%s]' % (variable, key), value)
+ return code