| 1 |
# |
| 2 |
# Unpacker for Dean Edward's p.a.c.k.e.r, a part of javascript beautifier |
| 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 |
"""Unpacker for Dean Edward's p.a.c.k.e.r""" |
| 14 |
|
| 15 |
import re |
| 16 |
import string |
| 17 |
import sys |
| 18 |
from jsbeautifier.unpackers import UnpackingError |
| 19 |
|
| 20 |
PRIORITY = 1 |
| 21 |
|
| 22 |
|
| 23 |
def detect(source): |
| 24 |
global beginstr |
| 25 |
global endstr |
| 26 |
beginstr = "" |
| 27 |
endstr = "" |
| 28 |
begin_offset = -1 |
| 29 |
"""Detects whether `source` is P.A.C.K.E.R. coded.""" |
| 30 |
mystr = re.search( |
| 31 |
r"eval[ ]*\([ ]*function[ ]*\([ ]*p[ ]*,[ ]*a[ ]*,[ ]*c[" |
| 32 |
" ]*,[ ]*k[ ]*,[ ]*e[ ]*,[ ]*", |
| 33 |
source, |
| 34 |
) |
| 35 |
if mystr: |
| 36 |
begin_offset = mystr.start() |
| 37 |
beginstr = source[:begin_offset] |
| 38 |
if begin_offset != -1: |
| 39 |
"""Find endstr""" |
| 40 |
source_end = source[begin_offset:] |
| 41 |
if source_end.split("')))", 1)[0] == source_end: |
| 42 |
try: |
| 43 |
endstr = source_end.split("}))", 1)[1] |
| 44 |
except IndexError: |
| 45 |
endstr = "" |
| 46 |
else: |
| 47 |
endstr = source_end.split("')))", 1)[1] |
| 48 |
return mystr is not None |
| 49 |
|
| 50 |
|
| 51 |
def unpack(source): |
| 52 |
"""Unpacks P.A.C.K.E.R. packed js code.""" |
| 53 |
payload, symtab, radix, count = _filterargs(source) |
| 54 |
|
| 55 |
if count != len(symtab): |
| 56 |
raise UnpackingError("Malformed p.a.c.k.e.r. symtab.") |
| 57 |
|
| 58 |
try: |
| 59 |
unbase = Unbaser(radix) |
| 60 |
except TypeError: |
| 61 |
raise UnpackingError("Unknown p.a.c.k.e.r. encoding.") |
| 62 |
|
| 63 |
def lookup(match): |
| 64 |
"""Look up symbols in the synthetic symtab.""" |
| 65 |
word = match.group(0) |
| 66 |
return symtab[unbase(word)] or word |
| 67 |
|
| 68 |
payload = payload.replace("\\\\", "\\").replace("\\'", "'") |
| 69 |
source = re.sub(r"\b\w+\b", lookup, payload, flags=re.ASCII) |
| 70 |
return _replacestrings(source) |
| 71 |
|
| 72 |
|
| 73 |
def _filterargs(source): |
| 74 |
"""Juice from a source file the four args needed by decoder.""" |
| 75 |
juicers = [ |
| 76 |
(r"}\('(.*)', *(\d+|\[\]), *(\d+), *'(.*)'\.split\('\|'\), *(\d+), *(.*)\)\)"), |
| 77 |
(r"}\('(.*)', *(\d+|\[\]), *(\d+), *'(.*)'\.split\('\|'\)"), |
| 78 |
] |
| 79 |
for juicer in juicers: |
| 80 |
args = re.search(juicer, source, re.DOTALL) |
| 81 |
if args: |
| 82 |
a = args.groups() |
| 83 |
if a[1] == "[]": |
| 84 |
a = list(a) |
| 85 |
a[1] = 62 |
| 86 |
a = tuple(a) |
| 87 |
try: |
| 88 |
return a[0], a[3].split("|"), int(a[1]), int(a[2]) |
| 89 |
except ValueError: |
| 90 |
raise UnpackingError("Corrupted p.a.c.k.e.r. data.") |
| 91 |
|
| 92 |
# could not find a satisfying regex |
| 93 |
raise UnpackingError( |
| 94 |
"Could not make sense of p.a.c.k.e.r data (unexpected code structure)" |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
def _replacestrings(source): |
| 99 |
global beginstr |
| 100 |
global endstr |
| 101 |
"""Strip string lookup table (list) and replace values in source.""" |
| 102 |
match = re.search(r'var *(_\w+)\=\["(.*?)"\];', source, re.DOTALL) |
| 103 |
|
| 104 |
if match: |
| 105 |
varname, strings = match.groups() |
| 106 |
startpoint = len(match.group(0)) |
| 107 |
lookup = strings.split('","') |
| 108 |
variable = "%s[%%d]" % varname |
| 109 |
for index, value in enumerate(lookup): |
| 110 |
source = source.replace(variable % index, '"%s"' % value) |
| 111 |
return source[startpoint:] |
| 112 |
return beginstr + source + endstr |
| 113 |
|
| 114 |
|
| 115 |
class Unbaser: |
| 116 |
"""Functor for a given base. Will efficiently convert |
| 117 |
strings to natural numbers.""" |
| 118 |
|
| 119 |
ALPHABET = { |
| 120 |
62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 121 |
95: ( |
| 122 |
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 123 |
"[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" |
| 124 |
), |
| 125 |
} |
| 126 |
|
| 127 |
def __init__(self, base): |
| 128 |
self.base = base |
| 129 |
|
| 130 |
# fill elements 37...61, if necessary |
| 131 |
if 36 < base < 62: |
| 132 |
if not hasattr(self.ALPHABET, self.ALPHABET[62][:base]): |
| 133 |
self.ALPHABET[base] = self.ALPHABET[62][:base] |
| 134 |
# attrs = self.ALPHABET |
| 135 |
# print ', '.join("%s: %s" % item for item in attrs.items()) |
| 136 |
# If base can be handled by int() builtin, let it do it for us |
| 137 |
if 2 <= base <= 36: |
| 138 |
self.unbase = lambda string: int(string, base) |
| 139 |
else: |
| 140 |
# Build conversion dictionary cache |
| 141 |
try: |
| 142 |
self.dictionary = { |
| 143 |
cipher: index for index, cipher in enumerate(self.ALPHABET[base]) |
| 144 |
} |
| 145 |
except KeyError: |
| 146 |
raise TypeError("Unsupported base encoding.") |
| 147 |
|
| 148 |
self.unbase = self._dictunbaser |
| 149 |
|
| 150 |
def __call__(self, string): |
| 151 |
return self.unbase(string) |
| 152 |
|
| 153 |
def _dictunbaser(self, string): |
| 154 |
"""Decodes a value to an integer.""" |
| 155 |
ret = 0 |
| 156 |
for index, cipher in enumerate(string[::-1]): |
| 157 |
ret += (self.base**index) * self.dictionary[cipher] |
| 158 |
return ret |