Debian Codegraph @

js/src/unpackers/javascriptobfuscator_unpacker.js

raw
1 /*
2
3 The MIT License (MIT)
4
5 Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
6
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation files
9 (the "Software"), to deal in the Software without restriction,
10 including without limitation the rights to use, copy, modify, merge,
11 publish, distribute, sublicense, and/or sell copies of the Software,
12 and to permit persons to whom the Software is furnished to do so,
13 subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 */
27
28 //
29 // simple unpacker/deobfuscator for scripts messed up with javascriptobfuscator.com
30 // written by Einar Lielmanis <[email protected]>
31 //
32 // usage:
33 //
34 // if (JavascriptObfuscator.detect(some_string)) {
35 // var unpacked = JavascriptObfuscator.unpack(some_string);
36 // }
37 //
38 //
39
40 /*jshint strict:false */
41
42 var JavascriptObfuscator = {
43 detect: function(str) {
44 return /^var _0x[a-f0-9]+ ?\= ?\[/.test(str);
45 },
46
47 unpack: function(str) {
48 if (JavascriptObfuscator.detect(str)) {
49 var matches = /var (_0x[a-f\d]+) ?\= ?\[(.*?)\];/.exec(str);
50 if (matches) {
51 var var_name = matches[1];
52 var strings = JavascriptObfuscator._smart_split(matches[2]);
53 str = str.substring(matches[0].length);
54 for (var k in strings) {
55 str = str.replace(new RegExp(var_name + '\\[' + k + '\\]', 'g'),
56 JavascriptObfuscator._fix_quotes(JavascriptObfuscator._unescape(strings[k])));
57 }
58 }
59 }
60 return str;
61 },
62
63 _fix_quotes: function(str) {
64 var matches = /^"(.*)"$/.exec(str);
65 if (matches) {
66 str = matches[1];
67 str = "'" + str.replace(/'/g, "\\'") + "'";
68 }
69 return str;
70 },
71
72 _smart_split: function(str) {
73 var strings = [];
74 var pos = 0;
75 while (pos < str.length) {
76 if (str.charAt(pos) === '"') {
77 // new word
78 var word = '';
79 pos += 1;
80 while (pos < str.length) {
81 if (str.charAt(pos) === '"') {
82 break;
83 }
84 if (str.charAt(pos) === '\\') {
85 word += '\\';
86 pos++;
87 }
88 word += str.charAt(pos);
89 pos++;
90 }
91 strings.push('"' + word + '"');
92 }
93 pos += 1;
94 }
95 return strings;
96 },
97
98
99 _unescape: function(str) {
100 // inefficient if used repeatedly or on small strings, but wonderful on single large chunk of text
101 for (var i = 32; i < 128; i++) {
102 str = str.replace(new RegExp('\\\\x' + i.toString(16), 'ig'), String.fromCharCode(i));
103 }
104 str = str.replace(/\\x09/g, "\t");
105 return str;
106 },
107
108 run_tests: function(sanity_test) {
109 var t = sanity_test || new SanityTest();
110
111 t.test_function(JavascriptObfuscator._smart_split, "JavascriptObfuscator._smart_split");
112 t.expect('', []);
113 t.expect('"a", "b"', ['"a"', '"b"']);
114 t.expect('"aaa","bbbb"', ['"aaa"', '"bbbb"']);
115 t.expect('"a", "b\\\""', ['"a"', '"b\\\""']);
116 t.test_function(JavascriptObfuscator._unescape, 'JavascriptObfuscator._unescape');
117 t.expect('\\x40', '@');
118 t.expect('\\x10', '\\x10');
119 t.expect('\\x1', '\\x1');
120 t.expect("\\x61\\x62\\x22\\x63\\x64", 'ab"cd');
121 t.test_function(JavascriptObfuscator.detect, 'JavascriptObfuscator.detect');
122 t.expect('', false);
123 t.expect('abcd', false);
124 t.expect('var _0xaaaa', false);
125 t.expect('var _0xaaaa = ["a", "b"]', true);
126 t.expect('var _0xaaaa=["a", "b"]', true);
127 t.expect('var _0x1234=["a","b"]', true);
128 return t;
129 }
130
131
132 };