-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathxDecoder.js
More file actions
292 lines (250 loc) · 10.4 KB
/
Copy pathxDecoder.js
File metadata and controls
292 lines (250 loc) · 10.4 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* eslint-disable no-cond-assign */
/* eslint-disable no-unused-vars */
module.exports = function () {
const Decoder = {};
Decoder.decode = function decode(type, targetName, code) {
code = this.replaceConstant(targetName, code);
// if the code was generated by updated obfuscator, realTargetName will be found
let realTargetName = this.findRealVar(targetName, code);
if (realTargetName) {
code = this.replaceAllRandomVariable(realTargetName, code, type);
}
switch (type) {
case 0:
return Decoder.decodeType0(targetName, code);
case 1:
return Decoder.decodeType1(realTargetName || targetName, code);
case 2:
return realTargetName ? Decoder.decodeType2_AfterPatch(realTargetName, code) : Decoder.decodeType2(targetName, code);
case 3:
return Decoder.decodeExperimentType(targetName, code);
}
}
Decoder.replaceFormat = function (code) {
/*
Replace format
aaa["bbb"] or aaa['bbb'] to aaa.bbb
*/
while (code.match(/([a-zA-Z_]\w*)\[("|')([a-zA-Z_]\w*)("|')\]/)) {
// replace get["blabla"] to get blabla and set, static too
code = code.replace(/(\W+)(get|set|static)\[("|')(\w+)("|')\]/g, "$1$2 $4");
// normal replace aaa["bbb"] to aaa.bbb
code = code.replace(/([a-zA-Z_]\w*)\[("|')([a-zA-Z_]\w*)("|')\]/g, "$1.$3");
}
return code;
}
Decoder.replaceConstant = function (targetName, code) {
// replace const to var if target func is constant
return code.replace(new RegExp(`const ${targetName}\\s*=`), `var ${targetName}=`);
}
/**
* The Great Escape
* @param {string} code target code
*/
Decoder.greatEscape = function (code) {
// avoid error when the code includes "use strict";
// if it is at the beginning of the code then delete it
// index: 0 means the beginning of the code
const m = code.match(/"use strict";/);
code = m && m.index === 0 ? code.replace(/"use strict";/, "") : code;
// avoid error when the code includes a part like "export default class..."
// maybe split by "export" can be better...? I need to test
code = code.split("export default")[0];
return code;
}
/**
* @param {string} val
*/
Decoder.escapeVal = function (val) {
return val.replace(/'/g, "\\x27").replace(/\$/g, "\\x24").replace(/\n/g, "\\n");
}
/**
* find real decode function's variable name
* @param {string} targetName target variable name
*/
Decoder.findRealVar = function (targetName, code) {
const reg = `${targetName}\\s*=\\s*(\\w+);`;
const regex = new RegExp(reg);
const m = code.match(regex);
console.log(m);
return m && m[1];
}
/**
* delete all "var _0x90129f = _0x3f0b" things
* and replace all random('0x2', 'cFMi') to real('0x2', 'cFMi')
* @param {string} realTargetName decode func name
* @param {string} code obfuscated code
* @param {number} type algo type
*/
Decoder.replaceAllRandomVariable = function (realTargetName, code, type) {
/**
* var some = realTarget
*/
const reg = `var (\\w+)\\s*=\\s*${realTargetName};`;
const regex = new RegExp(reg, 'g');
const matches = code.match(regex); // [ 'var _0x3e8cb6=_0x405d;', 'var _0x5a3785=_0x405d;' ]
if (matches) {
// remove all randoms
code = code.replace(regex, "");
// get random var names
for (let i = 0; i < matches.length; i++) {
const fakeTargetName = matches[i].match(/var (\w+)\s*=/)[1];
const [regArg, replaceValue] = this.getPattern_AfterPatch(type, fakeTargetName, realTargetName);
const regex = new RegExp(regArg, "g");
code = code.replace(regex, replaceValue);
}
}
return code;
}
Decoder.getPattern_AfterPatch = function (type, targetName, replaceName) {
let result = [];
switch (type) {
case 1:
result.push(`([^a-zA-Z0-9])${targetName}\\(([a-zA-Z0-9]+)\\)`);
result.push(`$1${replaceName}($2)`);
break;
case 2:
result.push(`([^a-zA-Z0-9])${targetName}\\(([a-zA-Z0-9]+),\\s*'([^']+)'\\)`);
result.push(`$1${replaceName}($2,'$3')`);
break;
}
return result;
}
Decoder.decodeType0 = function (targetName, code) { // array like _0xf13b[274] not func
eval(`var ${targetName} = null`);
try {
// eval(code.replace(new RegExp(`var ${targetName}=`), `${targetName}=`));
eval(this.greatEscape(code))
} catch (e) {
console.log("Eval err but continue");
}
const stringArray = eval(targetName);
const regArg = `${targetName}\\[([0-9]+)\\]`;
const defaultRegex = new RegExp(regArg);
let regex = new RegExp(regArg);
let m;
while (m = code.match(regex)) {
const val = this.escapeVal(stringArray[m[1]]);
regex = new RegExp(`${targetName}\\[${m[1]}\\]`, 'g');
code = code.replace(regex, `'${val}'`);
regex = defaultRegex;
}
return this.replaceFormat(code);
}
Decoder.decodeType1 = function (targetName, code) { // 1 args like _0xabc('0x00') | _0xabc(0x00)
console.log("[Type] 1")
eval(`var ${targetName} = null`);
try {
eval(this.greatEscape(code));
} catch (e) {
console.log("Eval err but continue");
}
const decode = eval(targetName);
const regArg = `[^a-zA-Z0-9]${targetName}\\(["|']*([a-zA-Z0-9]+)["|']*\\)`;
const defaultRegex = new RegExp(regArg);
let regex = new RegExp(regArg);
let m;
while (m = code.match(regex)) {
const val = this.escapeVal(decode(m[1]));
regex = new RegExp(`${targetName}\\(["|']*${m[1]}["|']*\\)`, 'g');
code = code.replace(regex, `'${val}'`);
regex = defaultRegex;
}
return this.replaceFormat(code);
}
Decoder.decodeType2_AfterPatch = function (targetName, code) { // _0x2d33d8(0x193,'Vdh&') RC4 (after patch)
console.log("[Type] 2 (after patch)");
eval(`var ${targetName} = null`);
try {
eval(this.greatEscape(code));
} catch (e) {
console.log("Eval err but continue");
}
const decode = eval(targetName);
const regArg = `[^a-zA-Z0-9]${targetName}\\(([a-zA-Z0-9]+),\\s*'([^']+)'\\)`;
const defaultRegex = new RegExp(regArg);
const amount = code.match(new RegExp(regArg, "g")).length;
console.log("Target amount: " + amount);
let regex = new RegExp(regArg);
let m;
while ((m = code.match(regex))) {
m[2] = m[2].replace(/"/, "");
// console.log(m[1], m[2])
const val = this.escapeVal(decode(m[1], m[2]));
// console.log(val)
m[2] = m[2]
.replace(/\(/g, "\\(")
.replace(/\)/g, "\\)")
.replace(/\$/g, "\\$")
.replace(/\[/g, "\\[")
.replace(/\]/g, "\\]")
.replace(/\^/g, "\\^")
.replace(/\*/g, "\\*");
regex = new RegExp(`${targetName}\\(${m[1]},\\s*'${m[2]}'\\)`, "g");
code = code.replace(regex, `'${val}'`);
regex = defaultRegex;
}
return this.replaceFormat(code);
}
Decoder.decodeType2 = function (targetName, code, decoderFunc) { // 2 args like _0xabc('asd', 'asd') RC4
eval(`var ${targetName} = null`);
try {
if (decoderFunc) eval(decoderFunc);
else eval(this.greatEscape(code));
} catch (e) {
console.log("Eval err but continue");
}
const decode = eval(targetName);
const quoType = code.match(new RegExp(`${targetName}\\((.)[a-zA-Z0-9]+.,\\s*.[^']+.\\)`))[1];
// there are 2 types like _0x4763("0x120b","EEc$") or _0x4763('0x120b','EEc$')
// so quotation type check is needed
const regArg = `${targetName}\\(.([a-zA-Z0-9]+).,\\s*.([^${quoType}]+).\\)`;
// quotation type check is disabled
// (enabled again, this regex is something wrong maybe ^("|'))
// const regArg = `${targetName}\\(.([^("|')]+).,\\s*.([^("|')]+).\\)`;
const defaultRegex = new RegExp(regArg);
const amount = code.match(new RegExp(regArg, "g")).length;
console.log(amount);
let regex = new RegExp(regArg);
let m;
while ((m = code.match(regex))) {
m[2] = m[2].replace(/"/, "");
// console.log(m[1], m[2])
const val = this.escapeVal(decode(m[1], m[2]));
console.log(val)
m[2] = m[2]
.replace(/\(/g, "\\(")
.replace(/\)/g, "\\)")
.replace(/\$/g, "\\$")
.replace(/\[/g, "\\[")
.replace(/\]/g, "\\]")
.replace(/\^/g, "\\^")
.replace(/\*/g, "\\*");
regex = new RegExp(`${targetName}\\(.${m[1]}.,\\s*.${m[2]}.\\)`, "g");
code = code.replace(regex, `'${val}'`);
regex = defaultRegex;
}
return this.replaceFormat(code);
}
/**
* This is under experimental
* @param {string} code - beautified code
*/
Decoder.decodeExperimentType = function (targetName, code) {
// get decoder func
let importantPart = code.split(`var ${targetName} = function`);
importantPart =
importantPart[0] +
`var ${targetName} = function` +
importantPart[1].split("\n};")[0] +
"};";
// avoid self defending
importantPart =
importantPart
.replace(/[ ]{4}/g, "")
.replace(/\r/g, "")
.replace(/\n/g, "");
return this.decodeType2(targetName, code, importantPart);
}
return Decoder;
}