-
Notifications
You must be signed in to change notification settings - Fork 11
/
JavaScriptUnpacker.php
263 lines (248 loc) · 8.12 KB
/
JavaScriptUnpacker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* JavaScriptUnpacker.php
* @author Andrey Izman <[email protected]>
* @link https://github.com/mervick/JavaScriptUnpacker
* @license MIT
*/
/**
* Class JavaScriptUnpacker
*/
class JavaScriptUnpacker
{
/**
* @var string
*/
protected static $JS_FUNC = 'eval(function(p,a,c,k,e,';
/**
* @var string
*/
protected $script;
/**
* @param $script
* @return string
*/
public static function unpack($script)
{
return (new self($script))->deobfuscate();
}
/**
* @param $script
*/
protected function __construct($script)
{
$this->script = $script;
}
/**
* @return string
*/
protected function deobfuscate()
{
if (self::hasPackedCode($this->script, $start) &&
(($body = $this->findBlock('{', '}', $start + strlen(self::$JS_FUNC), $pos))) &&
(($params = $this->findBlock('(', ')', $pos + strlen($body), $pos))) &&
(($end = strpos($this->script, ')', $pos + strlen($params)))) &&
(($packed = self::findString($params, 1, $pos, $quote))) &&
(($keywords = self::findString($params, $offset = $pos + strlen($packed) + 2, $pos))) &&
(preg_match('/^,([0-9]+),([0-9]+),$/', preg_replace('/[\x03-\x20]+/', '',
substr($params, $offset, $pos - $offset)), $matches)))
{
list(, $ascii, $count) = $matches;
$packed = str_replace('\\' . $quote, $quote, $packed);
$decode = 'decode' . self::detectEncoding($body);
$script = $this->$decode($packed, $ascii, $count, explode('|', $keywords));
$script = str_replace('\\\\', '\\', $script);
if (self::isDoubleEscaped($script)) {
$script = str_replace(['\\\'', '\\"', '\\\\\'', '\\\\"', '\\\\'],
['\'', '"', '\\\\\'', '\\\\"', '\\'], $script);
}
$script = self::replaceSpecials($script);
return substr($this->script, 0, $start) . self::unpack($script) . self::unpack(substr($this->script, $end + 1));
}
return $this->script;
}
/**
* @param string $str
* @return string
*/
protected static function replaceSpecials($str)
{
$replace = function($str) {
return str_replace(['\n', '\r', '\t'], ["\n", "\r", "\t"], $str);
};
$pieces = [];
for ($offset = 0; ($string = self::findString($str, $offset, $pos, $quote)) !== false;) {
$pieces[] = $replace(substr($str, $offset, $pos - $offset));
$pieces[] = $quote . $string . $quote;
$offset = $pos + strlen($string) + 2;
}
$pieces[] = $replace(substr($str, $offset));
return implode('', $pieces);
}
/**
* @param string $str
* @return bool
*/
protected static function isDoubleEscaped($str)
{
$result = true;
foreach (["'", '"'] as $quote) {
$matches = [];
foreach (['', '\\'] as $i => $slash) {
for ($matches[$i] = $j = 0, $find = "{$slash}{$quote}", $len = strlen($find);
($pos = strpos($str, $find, $j)) !== false; $j = $pos + $len, $matches[$i] ++);
}
list($x, $y) = $matches;
if ($x !== $y) {
return false;
}
$result = $result && $x;
}
return $result;
}
/**
* @param string $packed
* @param int $ascii
* @param int $count
* @param string $keywords
* @return string
*/
protected function decode62($packed, $ascii, $count, $keywords)
{
$packed = " $packed ";
$base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$encode = function ($count) use (&$encode, $ascii, $base) {
return ($count < $ascii ? '' : $encode(intval($count / $ascii))) . $base{$count % $ascii};
};
$split = '([^a-zA-Z0-9_])';
while ($count--) {
if (!empty($keywords[$count])) {
$pattern = '/' . $split . preg_quote($encode($count)) . $split . '/';
$packed = preg_replace_callback($pattern, function($matches) use ($keywords, $count) {
return $matches[1] . $keywords[$count] . $matches[2];
}, $packed);
}
}
return substr($packed, 1, -1);
}
/**
* @param string $packed
* @param int $ascii
* @param int $count
* @param string $keywords
* @return string
*/
protected function decode95($packed, $ascii, $count, $keywords)
{
$encode = function ($count) use (&$encode, $ascii) {
return ($count < $ascii ? '' : $encode(intval($count / $ascii))) .
mb_convert_encoding(pack('N', $count % $ascii + 161), 'UTF-8', 'UCS-4BE');
};
$decoded = [];
while ($count--) {
$encoded = $encode($count);
$decoded[$encoded] = !empty($keywords[$count]) ? $keywords[$count] : $encoded;
}
return preg_replace_callback('/([\xa1-\xff]+)/', function($match) use ($decoded) {
return isset($decoded[$match[1]]) ? $decoded[$match[1]] : $match[1];
}, $packed);
}
/**
* @param string $buf
* @param int $index
* @param int $len
* @return bool
*/
protected static function isSlashed($buf, $index, $len)
{
if ($buf{$index} === '\\') {
if ($len > 1 && $buf{$index - 1} === '\\') {
return self::isSlashed($buf, $index - 2, $len - 2);
}
return true;
}
return false;
}
/**
* @param string $buf
* @param int $offset
* @param null|int $start
* @param null|string $quote
* @return bool|string
*/
protected static function findString($buf, $offset, &$start=null, &$quote=null)
{
for ($start = $offset, $len = strlen($buf); $start < $len; $start++) {
foreach (['"', "'"] as $quote) {
if ($buf{$start} === $quote) {
for ($i = $start + 1; $i < $len; $i++) {
if ($buf{$i} === $quote && !self::isSlashed($buf, $i - 1, $i - $start - 1)) {
break;
}
}
if ($i === $len) {
return false;
}
return substr($buf, $start + 1, $i - $start - 1);
}
}
}
return false;
}
/**
* @param string $open
* @param string $close
* @param int $offset
* @param int $start
* @return string|false
*/
protected function findBlock($open, $close, $offset, &$start)
{
$buf = substr($this->script, $offset);
$len = strlen($buf);
for ($start = 0; $start < $len && $buf{$start} !== $open; $start++);
for ($i = $start + 1, $skip = 0; $i < $len; $i++) {
if ($buf{$i} === $close && 0 === $skip--) {
break;
}
foreach (['"', "'"] as $quote) {
if ($buf{$i} === $quote) {
for ($i++; $i < $len && ($buf{$i} !== $quote || $buf{$i - 1} === '\\'); $i++);
}
}
if ($buf{$i} === $open) {
$skip++;
}
}
if ($start === $len || $i === $len) {
return false;
}
$block = substr($buf, $start, $i - $start + 1);
$start += $offset;
return $block;
}
/**
* @param string $body
* @return int
*/
protected static function detectEncoding($body)
{
return strpos($body, '161') ? 95 : 62;
}
/**
* @param string $str
* @param null|int $start
* @return bool
*/
public static function hasPackedCode($str, &$start=null)
{
if (($pos = strpos(strtolower(preg_replace('/[\x03-\x20]+/', '', $str)), self::$JS_FUNC)) !== false) {
$start = -1;
do {
while (preg_match('/[\x03-\x20]/', $str{++$start}));
} while (0 < $pos--);
return true;
}
return false;
}
}