| 1 |
# |
| 2 |
# deobfuscator for scripts messed up with myobfuscate.com |
| 3 |
# by Einar Lielmanis <[email protected]> |
| 4 |
# |
| 5 |
# written by Stefano Sanfilippo <[email protected]> |
| 6 |
# |
| 7 |
# usage: |
| 8 |
# |
| 9 |
# if detect(some_string): |
| 10 |
# unpacked = unpack(some_string) |
| 11 |
# |
| 12 |
|
| 13 |
# CAVEAT by Einar Lielmanis |
| 14 |
|
| 15 |
# |
| 16 |
# You really don't want to obfuscate your scripts there: they're tracking |
| 17 |
# your unpackings, your script gets turned into something like this, |
| 18 |
# as of 2011-08-26: |
| 19 |
# |
| 20 |
# var _escape = 'your_script_escaped'; |
| 21 |
# var _111 = document.createElement('script'); |
| 22 |
# _111.src = 'http://api.www.myobfuscate.com/?getsrc=ok' + |
| 23 |
# '&ref=' + encodeURIComponent(document.referrer) + |
| 24 |
# '&url=' + encodeURIComponent(document.URL); |
| 25 |
# var 000 = document.getElementsByTagName('head')[0]; |
| 26 |
# 000.appendChild(_111); |
| 27 |
# document.write(unescape(_escape)); |
| 28 |
# |
| 29 |
|
| 30 |
"""Deobfuscator for scripts messed up with MyObfuscate.com""" |
| 31 |
|
| 32 |
import re |
| 33 |
import base64 |
| 34 |
from urllib.parse import unquote |
| 35 |
|
| 36 |
from jsbeautifier.unpackers import UnpackingError |
| 37 |
|
| 38 |
PRIORITY = 1 |
| 39 |
|
| 40 |
CAVEAT = """// |
| 41 |
// Unpacker warning: be careful when using myobfuscate.com for your projects: |
| 42 |
// scripts obfuscated by the free online version call back home. |
| 43 |
// |
| 44 |
|
| 45 |
""" |
| 46 |
|
| 47 |
SIGNATURE = ( |
| 48 |
r'["\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' |
| 49 |
r"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x61\x62\x63\x64\x65" |
| 50 |
r"\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75" |
| 51 |
r"\x76\x77\x78\x79\x7A\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x2B" |
| 52 |
r'\x2F\x3D","","\x63\x68\x61\x72\x41\x74","\x69\x6E\x64\x65\x78' |
| 53 |
r'\x4F\x66","\x66\x72\x6F\x6D\x43\x68\x61\x72\x43\x6F\x64\x65","' |
| 54 |
r'\x6C\x65\x6E\x67\x74\x68"]' |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def detect(source): |
| 59 |
"""Detects MyObfuscate.com packer.""" |
| 60 |
return SIGNATURE in source |
| 61 |
|
| 62 |
|
| 63 |
def unpack(source): |
| 64 |
"""Unpacks js code packed with MyObfuscate.com""" |
| 65 |
if not detect(source): |
| 66 |
return source |
| 67 |
payload = unquote(_filter(source)) |
| 68 |
match = re.search(r"^var _escape\='<script>(.*)<\/script>'", payload, re.DOTALL) |
| 69 |
polished = match.group(1) if match else source |
| 70 |
return CAVEAT + polished |
| 71 |
|
| 72 |
|
| 73 |
def _filter(source): |
| 74 |
"""Extracts and decode payload (original file) from `source`""" |
| 75 |
try: |
| 76 |
varname = re.search(r"eval\(\w+\(\w+\((\w+)\)\)\);", source).group(1) |
| 77 |
reverse = re.search(r"var +%s *\= *'(.*)';" % varname, source).group(1) |
| 78 |
except AttributeError: |
| 79 |
raise UnpackingError("Malformed MyObfuscate data.") |
| 80 |
try: |
| 81 |
return base64.b64decode(reverse[::-1].encode("utf8")).decode("utf8") |
| 82 |
except TypeError: |
| 83 |
raise UnpackingError("MyObfuscate payload is not base64-encoded.") |