javascript-obfuscator-lite 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +88 -0
- package/LICENSE +21 -0
- package/README.md +470 -2
- package/bin/cli.js +325 -0
- package/lib/index.d.ts +57 -0
- package/lib/index.js +140 -0
- package/lib/options.js +159 -0
- package/lib/transforms.js +757 -0
- package/lib/utils.js +452 -0
- package/package.json +56 -4
package/bin/cli.js
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const { obfuscate, version } = require('../lib');
|
|
8
|
+
|
|
9
|
+
const WEBSITE = 'https://js-obfuscator.github.io/';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* CLI flag -> API option mapping.
|
|
13
|
+
*/
|
|
14
|
+
const BOOLEAN_FLAGS = Object.freeze({
|
|
15
|
+
'string-array': 'stringArray',
|
|
16
|
+
'string-reverse': 'stringReverse',
|
|
17
|
+
'string-unicode': 'stringUnicode',
|
|
18
|
+
'member-expression': 'memberExpression',
|
|
19
|
+
'numeric-literal': 'numericLiteral',
|
|
20
|
+
'binary-expression': 'binaryExpression',
|
|
21
|
+
'boolean-literal': 'booleanLiteral',
|
|
22
|
+
'assignment-junk': 'assignmentJunk',
|
|
23
|
+
'zombie-code': 'zombieCode',
|
|
24
|
+
'control-flow': 'controlFlow',
|
|
25
|
+
'rename-globals': 'renameGlobals',
|
|
26
|
+
'rename-locals': 'renameLocals',
|
|
27
|
+
eval: 'eval',
|
|
28
|
+
compact: 'compact',
|
|
29
|
+
comments: 'comments',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const STRING_FLAGS = Object.freeze({
|
|
33
|
+
output: true,
|
|
34
|
+
'parser-plugins': true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const SHORT_FLAGS = Object.freeze({
|
|
38
|
+
o: 'output',
|
|
39
|
+
c: 'compact',
|
|
40
|
+
k: 'comments',
|
|
41
|
+
q: 'quiet',
|
|
42
|
+
h: 'help',
|
|
43
|
+
v: 'version',
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const GLOBAL_BOOLEAN_FLAGS = ['quiet', 'verbose', 'help', 'version'];
|
|
47
|
+
|
|
48
|
+
function isBooleanFlag(name) {
|
|
49
|
+
return (
|
|
50
|
+
Object.prototype.hasOwnProperty.call(BOOLEAN_FLAGS, name) ||
|
|
51
|
+
GLOBAL_BOOLEAN_FLAGS.indexOf(name) !== -1
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isStringFlag(name) {
|
|
56
|
+
return Object.prototype.hasOwnProperty.call(STRING_FLAGS, name);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const HELP = `
|
|
60
|
+
Usage: javascript-obfuscator-lite [options] <input> [output]
|
|
61
|
+
|
|
62
|
+
Obfuscate a JavaScript file. Use "-" for stdin or stdout.
|
|
63
|
+
|
|
64
|
+
Arguments:
|
|
65
|
+
input Input file, or "-" for stdin
|
|
66
|
+
output Output file, or "-" for stdout (default: "-")
|
|
67
|
+
|
|
68
|
+
Options:
|
|
69
|
+
-o, --output <file> Write output to <file>
|
|
70
|
+
-c, --compact Emit compact code
|
|
71
|
+
-k, --comments Preserve comments
|
|
72
|
+
--parser-plugins <p> Comma-separated Babel parser plugins (e.g. jsx)
|
|
73
|
+
--eval Enable eval-based expression encoding
|
|
74
|
+
--no-string-array Disable the string array transform
|
|
75
|
+
--no-string-reverse Disable string reversal
|
|
76
|
+
--no-string-unicode Disable unicode string escaping
|
|
77
|
+
--no-member-expression
|
|
78
|
+
Disable computed property encoding
|
|
79
|
+
--no-numeric-literal Disable numeric literal encoding
|
|
80
|
+
--no-binary-expression
|
|
81
|
+
Disable binary expression wrapping
|
|
82
|
+
--no-boolean-literal Disable boolean literal encoding
|
|
83
|
+
--no-assignment-junk Disable assignment junk code
|
|
84
|
+
--no-zombie-code Disable zombie code injection
|
|
85
|
+
--no-control-flow Disable control-flow flattening
|
|
86
|
+
--no-rename-globals Keep top-level variable/function names
|
|
87
|
+
--no-rename-locals Keep function-scoped names
|
|
88
|
+
-q, --quiet Suppress banner and status messages
|
|
89
|
+
--verbose Print diagnostic details to stderr
|
|
90
|
+
-h, --help Show this help
|
|
91
|
+
-v, --version Show the version
|
|
92
|
+
|
|
93
|
+
Examples:
|
|
94
|
+
javascript-obfuscator-lite input.js -o output.js
|
|
95
|
+
javascript-obfuscator-lite --no-eval --no-control-flow input.js
|
|
96
|
+
cat input.js | javascript-obfuscator-lite - > output.js
|
|
97
|
+
javascript-obfuscator-lite --parser-plugins jsx src/App.jsx -o dist/App.js
|
|
98
|
+
|
|
99
|
+
Website: https://js-obfuscator.github.io/
|
|
100
|
+
`.trimStart();
|
|
101
|
+
|
|
102
|
+
function printHelp(stream = process.stdout) {
|
|
103
|
+
stream.write(HELP);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseCli(argv) {
|
|
107
|
+
const values = {};
|
|
108
|
+
const positionals = [];
|
|
109
|
+
let endOfOptions = false;
|
|
110
|
+
|
|
111
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
112
|
+
const argument = argv[index];
|
|
113
|
+
|
|
114
|
+
if (endOfOptions) {
|
|
115
|
+
positionals.push(argument);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (argument === '--') {
|
|
120
|
+
endOfOptions = true;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (argument === '-') {
|
|
125
|
+
positionals.push(argument);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (argument.slice(0, 2) === '--') {
|
|
130
|
+
const equalsIndex = argument.indexOf('=');
|
|
131
|
+
const name =
|
|
132
|
+
equalsIndex === -1
|
|
133
|
+
? argument.slice(2)
|
|
134
|
+
: argument.slice(2, equalsIndex);
|
|
135
|
+
let value = equalsIndex === -1 ? null : argument.slice(equalsIndex + 1);
|
|
136
|
+
|
|
137
|
+
if (name.slice(0, 3) === 'no-' && isBooleanFlag(name.slice(3))) {
|
|
138
|
+
if (value !== null) {
|
|
139
|
+
throw new Error(`Option '--${name}' does not take an argument`);
|
|
140
|
+
}
|
|
141
|
+
values[name.slice(3)] = false;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (isBooleanFlag(name)) {
|
|
146
|
+
if (value !== null) {
|
|
147
|
+
throw new Error(`Option '--${name}' does not take an argument`);
|
|
148
|
+
}
|
|
149
|
+
values[name] = true;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (isStringFlag(name)) {
|
|
154
|
+
if (value === null) {
|
|
155
|
+
if (index + 1 >= argv.length) {
|
|
156
|
+
throw new Error(`Option '--${name}' requires a value`);
|
|
157
|
+
}
|
|
158
|
+
index += 1;
|
|
159
|
+
value = argv[index];
|
|
160
|
+
}
|
|
161
|
+
values[name] = value;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
throw new Error(`Unknown option '--${name}'`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (argument.charAt(0) === '-') {
|
|
169
|
+
let shortIndex = 1;
|
|
170
|
+
|
|
171
|
+
while (shortIndex < argument.length) {
|
|
172
|
+
const shortName = argument.charAt(shortIndex);
|
|
173
|
+
const longName = SHORT_FLAGS[shortName];
|
|
174
|
+
|
|
175
|
+
if (!longName) {
|
|
176
|
+
throw new Error(`Unknown option '-${shortName}'`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (longName === 'output') {
|
|
180
|
+
let value;
|
|
181
|
+
if (shortIndex + 1 < argument.length) {
|
|
182
|
+
value = argument.slice(shortIndex + 1);
|
|
183
|
+
} else {
|
|
184
|
+
if (index + 1 >= argv.length) {
|
|
185
|
+
throw new Error("Option '-o' requires a value");
|
|
186
|
+
}
|
|
187
|
+
index += 1;
|
|
188
|
+
value = argv[index];
|
|
189
|
+
}
|
|
190
|
+
values.output = value;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
values[longName] = true;
|
|
195
|
+
shortIndex += 1;
|
|
196
|
+
}
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
positionals.push(argument);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return { values, positionals };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function showBanner() {
|
|
207
|
+
process.stderr.write(`javascript-obfuscator-lite v${version}\n`);
|
|
208
|
+
process.stderr.write(`Website: ${WEBSITE}\n`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function readInput(input) {
|
|
212
|
+
if (input === '-') {
|
|
213
|
+
return fs.readFileSync(0, 'utf8');
|
|
214
|
+
}
|
|
215
|
+
return fs.readFileSync(path.resolve(input), 'utf8');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function writeOutput(output, code) {
|
|
219
|
+
const text = code.endsWith('\n') ? code : `${code}\n`;
|
|
220
|
+
if (output === '-') {
|
|
221
|
+
process.stdout.write(text);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const resolved = path.resolve(output);
|
|
226
|
+
fs.mkdirSync(path.dirname(resolved), { recursive: true });
|
|
227
|
+
fs.writeFileSync(resolved, text, 'utf8');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function buildOptions(values) {
|
|
231
|
+
const options = {};
|
|
232
|
+
for (const [flag, key] of Object.entries(BOOLEAN_FLAGS)) {
|
|
233
|
+
if (values[flag] !== undefined) {
|
|
234
|
+
options[key] = values[flag];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (values['parser-plugins']) {
|
|
239
|
+
options.parserPlugins = values['parser-plugins']
|
|
240
|
+
.split(',')
|
|
241
|
+
.map((plugin) => plugin.trim())
|
|
242
|
+
.filter(Boolean);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return options;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function main(argv = process.argv.slice(2)) {
|
|
249
|
+
let parsed;
|
|
250
|
+
try {
|
|
251
|
+
parsed = parseCli(argv);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
process.stderr.write(`error: ${error.message}\n\n`);
|
|
254
|
+
printHelp(process.stderr);
|
|
255
|
+
process.exitCode = 1;
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const { values, positionals } = parsed;
|
|
260
|
+
|
|
261
|
+
if (values.help) {
|
|
262
|
+
printHelp(process.stdout);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (values.version) {
|
|
266
|
+
process.stdout.write(`${version}\n`);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (positionals.length > 2) {
|
|
271
|
+
process.stderr.write('error: too many arguments\n\n');
|
|
272
|
+
printHelp(process.stderr);
|
|
273
|
+
process.exitCode = 1;
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const input = positionals[0] || (process.stdin.isTTY ? null : '-');
|
|
278
|
+
if (!input) {
|
|
279
|
+
printHelp(process.stderr);
|
|
280
|
+
process.exitCode = 1;
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (values.output && positionals[1]) {
|
|
285
|
+
process.stderr.write('error: specify the output either with -o or as a positional argument, not both\n');
|
|
286
|
+
process.exitCode = 1;
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const output = values.output || positionals[1] || '-';
|
|
291
|
+
const quiet = Boolean(values.quiet);
|
|
292
|
+
|
|
293
|
+
if (!quiet) {
|
|
294
|
+
showBanner();
|
|
295
|
+
if (values.verbose) {
|
|
296
|
+
process.stderr.write(`Options: ${JSON.stringify(buildOptions(values), null, 2)}\n`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
const source = readInput(input);
|
|
302
|
+
const result = obfuscate(source, buildOptions(values));
|
|
303
|
+
writeOutput(output, result);
|
|
304
|
+
|
|
305
|
+
if (!quiet) {
|
|
306
|
+
const inputLabel = input === '-' ? '<stdin>' : input;
|
|
307
|
+
const outputLabel = output === '-' ? '<stdout>' : output;
|
|
308
|
+
process.stderr.write(
|
|
309
|
+
`Obfuscated ${inputLabel} -> ${outputLabel} (${source.length} -> ${result.length} bytes)\n`
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
} catch (error) {
|
|
313
|
+
process.stderr.write(`error: ${error.message}\n`);
|
|
314
|
+
if (values.verbose && error.stack) {
|
|
315
|
+
process.stderr.write(`${error.stack}\n`);
|
|
316
|
+
}
|
|
317
|
+
process.exitCode = 1;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (require.main === module) {
|
|
322
|
+
main();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
module.exports = { main, printHelp };
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface ObfuscatorOptions {
|
|
2
|
+
/** Move string literals into a file-level array and look them up by index. */
|
|
3
|
+
stringArray?: boolean;
|
|
4
|
+
/** Replace string literals with reverse/split/join expressions. */
|
|
5
|
+
stringReverse?: boolean;
|
|
6
|
+
/** Escape string literals with \uXXXX sequences. */
|
|
7
|
+
stringUnicode?: boolean;
|
|
8
|
+
/** Convert dot property access to computed hexadecimal string access. */
|
|
9
|
+
memberExpression?: boolean;
|
|
10
|
+
/** Replace small integer literals with XOR expressions. */
|
|
11
|
+
numericLiteral?: boolean;
|
|
12
|
+
/** Wrap binary variable initializers in an IIFE. */
|
|
13
|
+
binaryExpression?: boolean;
|
|
14
|
+
/** Replace boolean literals with `!![]` / `![]`. */
|
|
15
|
+
booleanLiteral?: boolean;
|
|
16
|
+
/** Wrap literal assignment right-hand sides in an IIFE. */
|
|
17
|
+
assignmentJunk?: boolean;
|
|
18
|
+
/** Insert dead variable declarations and assignments. */
|
|
19
|
+
zombieCode?: boolean;
|
|
20
|
+
/** Flatten function bodies into switch-based dispatchers. */
|
|
21
|
+
controlFlow?: boolean;
|
|
22
|
+
/** Encode expressions with `eval(String.fromCharCode(...))`. */
|
|
23
|
+
eval?: boolean;
|
|
24
|
+
/** Rename top-level variables and functions. */
|
|
25
|
+
renameGlobals?: boolean;
|
|
26
|
+
/** Rename function-scoped variables and functions. */
|
|
27
|
+
renameLocals?: boolean;
|
|
28
|
+
/** Emit compact output. */
|
|
29
|
+
compact?: boolean;
|
|
30
|
+
/** Preserve comments. */
|
|
31
|
+
comments?: boolean;
|
|
32
|
+
/** Additional Babel parser plugins, for example `["jsx"]`. */
|
|
33
|
+
parserPlugins?: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export declare const defaultOptions: Readonly<Required<ObfuscatorOptions>>;
|
|
37
|
+
export declare const version: string;
|
|
38
|
+
|
|
39
|
+
export declare function normalizeOptions(
|
|
40
|
+
options?: ObfuscatorOptions | null
|
|
41
|
+
): Required<ObfuscatorOptions>;
|
|
42
|
+
|
|
43
|
+
export declare function obfuscate(
|
|
44
|
+
source: string,
|
|
45
|
+
options?: ObfuscatorOptions
|
|
46
|
+
): string;
|
|
47
|
+
|
|
48
|
+
export declare function obfuscateFile(
|
|
49
|
+
inputPath: string,
|
|
50
|
+
outputPath?: string | null,
|
|
51
|
+
options?: ObfuscatorOptions
|
|
52
|
+
): string;
|
|
53
|
+
|
|
54
|
+
export declare function obfuscateFile(
|
|
55
|
+
inputPath: string,
|
|
56
|
+
options?: ObfuscatorOptions
|
|
57
|
+
): string;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const parser = require('@babel/parser');
|
|
6
|
+
const generate = require('@babel/generator').default;
|
|
7
|
+
|
|
8
|
+
const transforms = require('./transforms');
|
|
9
|
+
const utils = require('./utils');
|
|
10
|
+
const { defaultOptions, normalizeOptions } = require('./options');
|
|
11
|
+
const { version } = require('../package.json');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse source code into a Babel AST.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} source
|
|
17
|
+
* @param {object} options
|
|
18
|
+
* @returns {import('@babel/types').File}
|
|
19
|
+
*/
|
|
20
|
+
function parse(source, options) {
|
|
21
|
+
try {
|
|
22
|
+
return parser.parse(source, {
|
|
23
|
+
sourceType: 'unambiguous',
|
|
24
|
+
plugins: options.parserPlugins,
|
|
25
|
+
allowReturnOutsideFunction: false,
|
|
26
|
+
errorRecovery: false,
|
|
27
|
+
});
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const location = error.loc
|
|
30
|
+
? ` (${error.loc.line}:${error.loc.column})`
|
|
31
|
+
: '';
|
|
32
|
+
const wrapped = new SyntaxError(`Failed to parse input${location}: ${error.message}`);
|
|
33
|
+
wrapped.cause = error;
|
|
34
|
+
throw wrapped;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Obfuscate JavaScript source code.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} source JavaScript source code.
|
|
42
|
+
* @param {object} [options] Obfuscation options.
|
|
43
|
+
* @returns {string} Obfuscated JavaScript code.
|
|
44
|
+
*/
|
|
45
|
+
function obfuscate(source, options) {
|
|
46
|
+
if (typeof source !== 'string') {
|
|
47
|
+
throw new TypeError('Source must be a string.');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const opts = normalizeOptions(options);
|
|
51
|
+
const input = source.replace(/^\uFEFF/, '');
|
|
52
|
+
const ast = parse(input, opts);
|
|
53
|
+
|
|
54
|
+
// Detect export conventions before structural transforms obscure member
|
|
55
|
+
// expressions (for example, `module.exports` becoming `module["exports"]`).
|
|
56
|
+
const context = {
|
|
57
|
+
commonJsExports: utils.hasCommonJsExports(ast),
|
|
58
|
+
exportedNames: utils.collectExportedNames(ast),
|
|
59
|
+
hasWith: utils.hasWithStatement(ast),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (opts.stringReverse) {
|
|
63
|
+
transforms.stringReverse(ast, opts);
|
|
64
|
+
}
|
|
65
|
+
if (opts.zombieCode) {
|
|
66
|
+
transforms.zombieCode(ast, opts, context);
|
|
67
|
+
}
|
|
68
|
+
if (opts.memberExpression) {
|
|
69
|
+
transforms.memberExpression(ast, opts);
|
|
70
|
+
}
|
|
71
|
+
if (opts.assignmentJunk) {
|
|
72
|
+
transforms.assignmentJunk(ast, opts);
|
|
73
|
+
}
|
|
74
|
+
if (opts.numericLiteral) {
|
|
75
|
+
transforms.numericLiteral(ast, opts);
|
|
76
|
+
}
|
|
77
|
+
if (opts.binaryExpression) {
|
|
78
|
+
transforms.binaryExpression(ast, opts);
|
|
79
|
+
}
|
|
80
|
+
if (opts.booleanLiteral) {
|
|
81
|
+
transforms.booleanLiteral(ast, opts);
|
|
82
|
+
}
|
|
83
|
+
if (opts.stringUnicode) {
|
|
84
|
+
transforms.stringUnicode(ast, opts);
|
|
85
|
+
}
|
|
86
|
+
if (opts.controlFlow) {
|
|
87
|
+
transforms.controlFlow(ast, opts, context);
|
|
88
|
+
}
|
|
89
|
+
if (opts.stringArray) {
|
|
90
|
+
transforms.stringArray(ast, opts, context);
|
|
91
|
+
}
|
|
92
|
+
if (opts.renameGlobals || opts.renameLocals) {
|
|
93
|
+
transforms.rename(ast, opts, context);
|
|
94
|
+
}
|
|
95
|
+
if (opts.eval) {
|
|
96
|
+
transforms.evalEncode(ast, opts);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return generate(ast, {
|
|
100
|
+
comments: opts.comments,
|
|
101
|
+
compact: opts.compact,
|
|
102
|
+
importAttributesKeyword: 'with',
|
|
103
|
+
}).code;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Obfuscate a file and optionally write the result to disk.
|
|
108
|
+
*
|
|
109
|
+
* `outputPath` may be omitted. If the second argument is an options object,
|
|
110
|
+
* it is treated as options and the result is only returned.
|
|
111
|
+
*
|
|
112
|
+
* @param {string} inputPath
|
|
113
|
+
* @param {string|object|null} [outputPath]
|
|
114
|
+
* @param {object} [options]
|
|
115
|
+
* @returns {string} Obfuscated JavaScript code.
|
|
116
|
+
*/
|
|
117
|
+
function obfuscateFile(inputPath, outputPath, options) {
|
|
118
|
+
if (outputPath && typeof outputPath === 'object') {
|
|
119
|
+
options = outputPath;
|
|
120
|
+
outputPath = null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const source = fs.readFileSync(inputPath, 'utf8');
|
|
124
|
+
const result = obfuscate(source, options);
|
|
125
|
+
|
|
126
|
+
if (outputPath) {
|
|
127
|
+
fs.mkdirSync(path.dirname(path.resolve(outputPath)), { recursive: true });
|
|
128
|
+
fs.writeFileSync(outputPath, result, 'utf8');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
obfuscate,
|
|
136
|
+
obfuscateFile,
|
|
137
|
+
defaultOptions,
|
|
138
|
+
normalizeOptions,
|
|
139
|
+
version,
|
|
140
|
+
};
|
package/lib/options.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default configuration for all transformations.
|
|
5
|
+
*
|
|
6
|
+
* The defaults are intentionally aggressive, with two safety exceptions:
|
|
7
|
+
* - `eval` is disabled because it can break Content Security Policy and is
|
|
8
|
+
* significantly slower at runtime.
|
|
9
|
+
* - Exported bindings and projects that use CommonJS exports are detected and
|
|
10
|
+
* protected from global renaming by the renamer itself.
|
|
11
|
+
*/
|
|
12
|
+
const defaultOptions = Object.freeze({
|
|
13
|
+
// String transformations
|
|
14
|
+
stringArray: true,
|
|
15
|
+
stringReverse: true,
|
|
16
|
+
stringUnicode: true,
|
|
17
|
+
|
|
18
|
+
// Expression transformations
|
|
19
|
+
memberExpression: true,
|
|
20
|
+
numericLiteral: true,
|
|
21
|
+
binaryExpression: true,
|
|
22
|
+
booleanLiteral: true,
|
|
23
|
+
assignmentJunk: true,
|
|
24
|
+
|
|
25
|
+
// Code-shape transformations
|
|
26
|
+
zombieCode: true,
|
|
27
|
+
controlFlow: true,
|
|
28
|
+
|
|
29
|
+
// Optional runtime-evaluated encoding (disabled by default)
|
|
30
|
+
eval: false,
|
|
31
|
+
|
|
32
|
+
// Identifier renaming
|
|
33
|
+
renameGlobals: true,
|
|
34
|
+
renameLocals: true,
|
|
35
|
+
|
|
36
|
+
// Output
|
|
37
|
+
compact: false,
|
|
38
|
+
comments: false,
|
|
39
|
+
|
|
40
|
+
// Parser
|
|
41
|
+
parserPlugins: Object.freeze([]),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Snake_case option aliases mapped to their camelCase equivalents.
|
|
46
|
+
*/
|
|
47
|
+
const aliases = Object.freeze({
|
|
48
|
+
private_variable_obfuscate: 'renameLocals',
|
|
49
|
+
global_variable_obfuscate: 'renameGlobals',
|
|
50
|
+
private_function_obfuscate: 'renameLocals',
|
|
51
|
+
global_function_obfuscate: 'renameGlobals',
|
|
52
|
+
member_expression_encode: 'memberExpression',
|
|
53
|
+
member_expression: 'memberExpression',
|
|
54
|
+
numberic_literal_encode: 'numericLiteral',
|
|
55
|
+
numeric_literal_encode: 'numericLiteral',
|
|
56
|
+
numeric_literal: 'numericLiteral',
|
|
57
|
+
binary_express_obfuscate: 'binaryExpression',
|
|
58
|
+
binary_expression: 'binaryExpression',
|
|
59
|
+
boolean_encode: 'booleanLiteral',
|
|
60
|
+
boolean_literal: 'booleanLiteral',
|
|
61
|
+
string_unicode_encode: 'stringUnicode',
|
|
62
|
+
string_unicode: 'stringUnicode',
|
|
63
|
+
assignment_junk_code: 'assignmentJunk',
|
|
64
|
+
assignment_junk: 'assignmentJunk',
|
|
65
|
+
zombie_code: 'zombieCode',
|
|
66
|
+
eval_encode: 'eval',
|
|
67
|
+
control_flow: 'controlFlow',
|
|
68
|
+
controlFlowFlattening: 'controlFlow',
|
|
69
|
+
string_reverse: 'stringReverse',
|
|
70
|
+
string_array: 'stringArray',
|
|
71
|
+
rename_globals: 'renameGlobals',
|
|
72
|
+
rename_locals: 'renameLocals',
|
|
73
|
+
parser_plugins: 'parserPlugins',
|
|
74
|
+
compress: 'compact',
|
|
75
|
+
minify: 'compact',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const booleanOptions = new Set([
|
|
79
|
+
'stringArray',
|
|
80
|
+
'stringReverse',
|
|
81
|
+
'stringUnicode',
|
|
82
|
+
'memberExpression',
|
|
83
|
+
'numericLiteral',
|
|
84
|
+
'binaryExpression',
|
|
85
|
+
'booleanLiteral',
|
|
86
|
+
'assignmentJunk',
|
|
87
|
+
'zombieCode',
|
|
88
|
+
'controlFlow',
|
|
89
|
+
'eval',
|
|
90
|
+
'renameGlobals',
|
|
91
|
+
'renameLocals',
|
|
92
|
+
'compact',
|
|
93
|
+
'comments',
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
const validOptions = new Set(Object.keys(defaultOptions));
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Validate and normalize user-supplied options.
|
|
100
|
+
*
|
|
101
|
+
* @param {object|null|undefined} input
|
|
102
|
+
* @returns {object} A complete option object.
|
|
103
|
+
*/
|
|
104
|
+
function normalizeOptions(input) {
|
|
105
|
+
if (input == null) {
|
|
106
|
+
input = {};
|
|
107
|
+
}
|
|
108
|
+
if (typeof input !== 'object' || Array.isArray(input)) {
|
|
109
|
+
throw new TypeError('Options must be a plain object.');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const result = {
|
|
113
|
+
...defaultOptions,
|
|
114
|
+
parserPlugins: [...defaultOptions.parserPlugins],
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
for (const [key, value] of Object.entries(input)) {
|
|
118
|
+
if (value === undefined) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const canonical = aliases[key] || key;
|
|
123
|
+
if (!validOptions.has(canonical)) {
|
|
124
|
+
throw new TypeError(
|
|
125
|
+
`Unknown option "${key}". Valid options are: ${[...validOptions].sort().join(', ')}.`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (booleanOptions.has(canonical)) {
|
|
130
|
+
if (typeof value !== 'boolean') {
|
|
131
|
+
throw new TypeError(`Option "${key}" must be a boolean.`);
|
|
132
|
+
}
|
|
133
|
+
} else if (canonical === 'parserPlugins') {
|
|
134
|
+
if (!Array.isArray(value) || value.some((plugin) => typeof plugin !== 'string')) {
|
|
135
|
+
throw new TypeError('Option "parserPlugins" must be an array of strings.');
|
|
136
|
+
}
|
|
137
|
+
result.parserPlugins = [...value];
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
result[canonical] = value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const plugin of result.parserPlugins) {
|
|
145
|
+
if (plugin === 'typescript' || plugin === 'flow' || plugin === 'estree') {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`Parser plugin "${plugin}" is not supported. Transpile TypeScript/Flow to JavaScript first.`
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
module.exports = {
|
|
156
|
+
defaultOptions,
|
|
157
|
+
aliases,
|
|
158
|
+
normalizeOptions,
|
|
159
|
+
};
|