-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobfuscator.js
344 lines (308 loc) · 11.9 KB
/
obfuscator.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
function obfuscate(code, options) {
const userScriptHeader = extractUserScriptHeader(code);
let obfuscatedCode = code.replace(userScriptHeader, '');
try {
if (options.renameVariables) {
obfuscatedCode = renameVariables(obfuscatedCode);
}
if (options.stringArray) {
if (options.stringInsane) {
obfuscatedCode = insaneStringTransform(obfuscatedCode, {
...options,
stringEncryption: options.stringEncryption
});
} else {
obfuscatedCode = transformStrings(obfuscatedCode, options);
}
} else if (options.stringEncryption) {
obfuscatedCode = encryptStrings(obfuscatedCode);
}
if (options.controlFlowFlattening) {
obfuscatedCode = flattenControlFlow(obfuscatedCode);
}
if (options.deadCodeInjection) {
obfuscatedCode = injectDeadCode(obfuscatedCode);
}
if (options.debugProtection) {
obfuscatedCode = addDebugProtection(obfuscatedCode);
}
if (options.functionReordering) {
obfuscatedCode = reorderFunctions(obfuscatedCode);
}
if (options.constantFolding) {
obfuscatedCode = foldConstants(obfuscatedCode);
}
if (options.codeSplitting) {
obfuscatedCode = splitCode(obfuscatedCode);
}
} catch (error) {
console.error("Error during obfuscation:", error);
return "Error: " + error.message;
}
// Return the header and obfuscated code separately
return { header: userScriptHeader, code: obfuscatedCode };
}
function extractUserScriptHeader(code) {
const headerRegex = /\/\/ ==UserScript==[\s\S]*?\/\/ ==\/UserScript==/;
const match = code.match(headerRegex);
return match ? match[0] + '\n' : '';
}
function renameVariables(code) {
const variableRegex = /\b(var|let|const)\s+(\w+)\b/g;
const variables = new Set();
code.replace(variableRegex, (_, __, varName) => variables.add(varName));
const variableMap = new Map([...variables].map((v, i) => [v, `_${i}`]));
return code.replace(/\b(\w+)\b/g, match => variableMap.get(match) || match);
}
function encryptStrings(code) {
return code.replace(/"([^"]*)"/g, (_, str) => {
const encrypted = btoa(str);
return `atob("${encrypted}")`;
});
}
function flattenControlFlow(code) {
const statements = code.split(';').filter(s => s.trim() !== '');
const flattened = `
(function(){
var _index = 0;
var _fns = [
${statements.map((stmt, i) => `function(){${stmt}; _index = ${i + 1};}`).join(',\n')}
];
while (_index < _fns.length) {
_fns[_index]();
}
})();
`;
return flattened;
}
function injectDeadCode(code) {
const deadCode = `
if (false) {
console.log("This code will never run");
alert("Unreachable");
}
`;
return deadCode + code;
}
function addDebugProtection(code) {
const protection = `
(function(){
var _0x3d88=['debug','exception','trace','log','warn','error','table'];
(function(_0x36bc23,_0x3d8889){
var _0x2a4c4d=function(_0x44e8da){
while(--_0x44e8da){
_0x36bc23['push'](_0x36bc23['shift']());
}
};
_0x2a4c4d(++_0x3d8889);
}(_0x3d88,0x1d3));
var _0x2a4c=function(_0x36bc23,_0x3d8889){
_0x36bc23=_0x36bc23-0x0;
var _0x2a4c4d=_0x3d88[_0x36bc23];
return _0x2a4c4d;
};
(function(){
var _0x1ab6ea=function(){
var _0x54c45b;
try{
_0x54c45b=Function('return\\x20(function()\\x20'+'{}.constructor(\\x22return\\x20this\\x22)(\\x20)'+');')();
}catch(_0x21f569){
_0x54c45b=window;
}
return _0x54c45b;
};
var _0x2c5f2b=_0x1ab6ea();
var _0x5de1fd=['log',_0x2a4c('0x0'),_0x2a4c('0x1'),_0x2a4c('0x2'),_0x2a4c('0x3'),_0x2a4c('0x4'),_0x2a4c('0x5')];
for(var _0x1e0f66=0x0;_0x1e0f66<_0x5de1fd[_0x2a4c('0x6')];_0x1e0f66++){
(function(_0x1d6e0){
var _0x2f3ec9=_0x2c5f2b[_0x1d6e0]||_0x2c5f2b['console'][_0x1d6e0];
Object['defineProperty'](_0x2c5f2b['console'],_0x1d6e0,{'value':function(){
debugger;
return _0x2f3ec9['apply'](_0x2c5f2b['console'],arguments);
},'configurable':!![]});
}(_0x5de1fd[_0x1e0f66]));
}
}());
})();
`;
return protection + code;
}
function reorderFunctions(code) {
const functionRegex = /function\s+(\w+)\s*\([^)]*\)\s*\{[^}]*\}/g;
const functions = code.match(functionRegex) || [];
const shuffledFunctions = functions.sort(() => Math.random() - 0.5);
return code.replace(functionRegex, () => shuffledFunctions.pop());
}
function foldConstants(code) {
return code.replace(/(\d+)\s*([+\-*/])\s*(\d+)/g, (_, a, op, b) => {
return eval(`${a}${op}${b}`);
});
}
function splitCode(code) {
const parts = code.split(';');
const shuffledParts = parts.sort(() => Math.random() - 0.5);
const reconstructor = `
(function(){
var parts = ${JSON.stringify(shuffledParts)};
var code = parts.join(';');
eval(code);
})();
`;
return reconstructor;
}
function getIndex(index, options, arrayLength) {
const shiftedIndex = (index + (options.stringArrayIndexShift || 0)) % arrayLength;
if (options.stringArrayIndexesType === 'hexadecimal') {
return `[0x${shiftedIndex.toString(16)}]`;
}
return `[${shiftedIndex}]`;
}
function transformStrings(code, options) {
if (!options.stringArray) return code;
const strings = new Set();
code = code.replace(/"([^"]*)"/g, (match, str) => {
if (str.length >= options.stringArrayThreshold) {
strings.add(str);
return `_0x${strings.size.toString(16)}`;
}
return match;
});
let stringArray = Array.from(strings);
if (options.stringArrayShuffle) {
stringArray = stringArray.sort(() => Math.random() - 0.5);
}
if (options.stringEncryption) {
stringArray = stringArray.map(str => btoa(str));
}
const stringMap = new Map(stringArray.map((str, i) => [str, i]));
const arrayName = `_0x${Math.random().toString(36).substr(2, 6)}`;
const arrayDeclaration = `var ${arrayName} = ${JSON.stringify(stringArray)};`;
code = code.replace(/_0x([0-9a-f]+)/g, (match, index) => {
const originalIndex = parseInt(index, 16) - 1;
const str = stringArray[originalIndex];
const arrayAccess = `${arrayName}${getIndex(stringMap.get(str), options, stringArray.length)}`;
return options.stringEncryption ? `atob(${arrayAccess})` : arrayAccess;
});
return arrayDeclaration + code;
}
function encryptAndTransformStrings(code, options) {
const strings = new Set();
code = code.replace(/"([^"]*)"/g, (match, str) => {
if (str.length >= options.stringArrayThreshold) {
const encrypted = btoa(str);
strings.add(encrypted);
return `_0x${strings.size.toString(16)}`;
}
return match;
});
let stringArray = Array.from(strings);
if (options.stringArrayShuffle) {
stringArray = stringArray.sort(() => Math.random() - 0.5);
}
const stringMap = new Map(stringArray.map((str, i) => [str, i]));
const indexShift = options.stringArrayIndexShift || 0;
const getIndex = (index) => {
const shiftedIndex = (index + indexShift) % stringArray.length;
if (options.stringArrayIndexesType === 'hexadecimal') {
return `0x${shiftedIndex.toString(16)}`;
}
return shiftedIndex;
};
const arrayName = `_0x${Math.random().toString(36).substr(2, 6)}`;
const arrayDeclaration = `var ${arrayName} = ${JSON.stringify(stringArray)};`;
code = code.replace(/_0x([0-9a-f]+)/g, (match, index) => {
const originalIndex = parseInt(index, 16) - 1;
const str = stringArray[originalIndex];
return `atob(${arrayName}[${getIndex(stringMap.get(str))}])`;
});
return arrayDeclaration + code;
}
function insaneStringTransform(code, options) {
const threshold = options.stringInsaneThreshold || 2;
const dictionary = new Set();
const strings = new Set();
const arrayName = `_0x${Math.random().toString(36).substr(2, 6)}`;
function padBase64(str) {
while (str.length % 4) {
str += '=';
}
return str;
}
// First pass: build dictionary
code = code.replace(/"([^"]*)"/g, (match, str) => {
if (str.length >= options.stringArrayThreshold) {
for (let i = 0; i < str.length; i += threshold) {
const chunk = str.substr(i, threshold);
const encodedChunk = options.stringEncryption ? btoa(chunk) : chunk;
dictionary.add(encodedChunk);
}
strings.add(str);
return `_0x${strings.size.toString(16)}`;
}
return match;
});
let dictionaryArray = Array.from(dictionary);
if (options.stringArrayShuffle) {
dictionaryArray = dictionaryArray.sort(() => Math.random() - 0.5);
}
const stringMap = new Map();
// Second pass: transform strings
code = code.replace(/_0x([0-9a-f]+)/g, (match, index) => {
const str = Array.from(strings)[parseInt(index, 16) - 1];
if (str === undefined) {
console.warn(`String not found for index: ${index}`);
return match; // Return the original match if string not found
}
if (!stringMap.has(str)) {
const transformed = str.match(new RegExp(`.{1,${threshold}}`, 'g'))
.map(chunk => {
const encryptedChunk = options.stringEncryption ? btoa(chunk) : chunk;
const chunkIndex = dictionaryArray.indexOf(encryptedChunk);
return `${arrayName}${getIndex(chunkIndex, options, dictionaryArray.length)}`;
})
.join(' + ');
stringMap.set(str, transformed);
}
const transformedStr = stringMap.get(str);
if (options.stringEncryption) {
return `(function(s) { var b = findBase64Strings(padBase64(s)); return b && b.length ? b.map(x => atob(x)).join('') : s; })(${transformedStr})`;
} else {
return transformedStr;
}
});
const arrayDeclaration = `var ${arrayName} = ${JSON.stringify(dictionaryArray)};`;
const padFunction = `function padBase64(str) { while (str.length % 4) { str += '='; } return str; }`;
const isBase64Function = `
function isBase64(str) {
try {
return btoa(atob(str)) === str;
} catch (e) {
return false;
}
}`;
const findBase64StringsFunction = `
function findBase64Strings(text) {
if (typeof text !== 'string') return [];
const base64Pattern = /(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?/g;
const potentialBase64Strings = text.match(base64Pattern) || [];
return potentialBase64Strings.filter(str => str.length > 0 && isBase64(str));
}`;
return arrayDeclaration + padFunction + isBase64Function + findBase64StringsFunction + code;
}
function isBase64(str) {
try {
if (str.length % 4 !== 0) {
return false;
}
atob(str);
return true;
} catch (e) {
return false;
}
}
function findBase64Strings(text) {
const base64Pattern = /(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?/g;
const potentialBase64Strings = text.match(base64Pattern) || [];
const validBase64Strings = potentialBase64Strings.filter(str => str.length > 0 && isBase64(str));
return validBase64Strings;
}