js-confuser-vm 0.1.0 → 0.1.1

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +75 -94
  3. package/bench.ts +146 -0
  4. package/disassemble.ts +12 -0
  5. package/dist/build-runtime.js +41 -15
  6. package/dist/compiler.js +134 -60
  7. package/dist/disassembler.js +317 -0
  8. package/dist/index.js +7 -2
  9. package/dist/runtime.js +68 -46
  10. package/dist/template.js +116 -0
  11. package/dist/transforms/bytecode/aliasedOpcodes.js +4 -1
  12. package/dist/transforms/bytecode/controlFlowFlattening.js +451 -0
  13. package/dist/transforms/bytecode/dispatcher.js +13 -109
  14. package/dist/transforms/bytecode/macroOpcodes.js +2 -2
  15. package/dist/transforms/bytecode/resolveConstants.js +100 -0
  16. package/dist/transforms/bytecode/resolveRegisters.js +4 -0
  17. package/dist/transforms/bytecode/semanticOpcodes.js +162 -0
  18. package/dist/transforms/bytecode/specializedOpcodes.js +18 -10
  19. package/dist/transforms/bytecode/stringConcealing.js +110 -0
  20. package/dist/transforms/runtime/classObfuscation.js +43 -0
  21. package/dist/transforms/runtime/handlerTable.js +91 -0
  22. package/dist/transforms/runtime/semanticOpcodes.js +35 -0
  23. package/dist/transforms/runtime/specializedOpcodes.js +11 -5
  24. package/dist/types.js +1 -1
  25. package/dist/utils/ast-utils.js +14 -0
  26. package/dist/utils/op-utils.js +0 -2
  27. package/dist/utils/pass-utils.js +100 -0
  28. package/dist/utils/profile-utils.js +3 -0
  29. package/index.ts +22 -17
  30. package/jest.config.js +14 -2
  31. package/output.disassembled.js +41 -0
  32. package/package.json +2 -1
  33. package/src/build-runtime.ts +113 -78
  34. package/src/compiler.ts +2703 -2593
  35. package/src/disassembler.ts +329 -0
  36. package/src/index.ts +12 -2
  37. package/src/options.ts +7 -1
  38. package/src/runtime.ts +84 -51
  39. package/src/template.ts +125 -1
  40. package/src/transforms/bytecode/aliasedOpcodes.ts +4 -1
  41. package/src/transforms/bytecode/controlFlowFlattening.ts +566 -0
  42. package/src/transforms/bytecode/dispatcher.ts +19 -125
  43. package/src/transforms/bytecode/macroOpcodes.ts +2 -2
  44. package/src/transforms/bytecode/resolveRegisters.ts +5 -0
  45. package/src/transforms/bytecode/specializedOpcodes.ts +22 -11
  46. package/src/transforms/bytecode/stringConcealing.ts +130 -0
  47. package/src/transforms/runtime/classObfuscation.ts +59 -0
  48. package/src/transforms/runtime/specializedOpcodes.ts +14 -9
  49. package/src/types.ts +42 -1
  50. package/src/utils/ast-utils.ts +19 -0
  51. package/src/utils/op-utils.ts +0 -2
  52. package/src/utils/pass-utils.ts +126 -0
  53. package/src/utils/profile-utils.ts +3 -0
  54. package/tsconfig.json +1 -1
  55. package/src/transforms/bytecode/microOpcodes.ts +0 -291
  56. package/src/transforms/runtime/internalVariables.ts +0 -270
  57. package/src/transforms/runtime/microOpcodes.ts +0 -93
  58. /package/src/transforms/bytecode/{resolveContants.ts → resolveConstants.ts} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ # Planned:
2
+
3
+ - - Planned: Shuffle the order of constructor and method parameters
4
+ - - Planned: Alias variables, and other function related obfuscations
5
+
6
+ ## `0.1.1` Control Flow Flattening, String Concealing, and more
7
+
8
+ - Added new option `controlFlowFlattening` which flattens the control flow of your program into a convoluted state machine
9
+ - Added new option `stringConcealing` which involves encoding strings to conceal plain-text values.
10
+
11
+ - Added new API method `JSConfuserVM.disassemble(sourceCode)` which returns a partial JS representation
12
+ - - This only works if the parameter `sourceCode` contains the original bytecode comment
13
+ - - This shouldn't be used with any options enabled, as the disassembler only supports default patterns
14
+
15
+ - Added new option `classObfuscation` which obfuscates the VM runtime classes:
16
+ - - Shuffles the order of declarations and methods
17
+
18
+ - Added new option `verbose` which `console.log`'s useful info for debugging purposes.
19
+
20
+ - Removed option `microOpcodes` - this option introduced scratch registers which left runtime values exposed, and were easily traceable by deobfuscators. The cons outweighed the pros and since it conflicts with the other obfuscations already present, it was removed.
21
+
22
+ - Added support for rest parameters (ES6 feature)
23
+
1
24
  ## `0.1.0` Dispatcher, Virtual Registers, and more
2
25
 
3
26
  - Added new option `dispatcher` which creates a middleman block to process jumps.
package/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # JS Confuser VM
2
2
 
3
- [![NPM](https://img.shields.io/badge/NPM-%23000000.svg?style=for-the-badge&logo=npm&logoColor=white)](https://npmjs.com/package/js-confuser-vm) [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/MichaelXF/js-confuser-vm) [![Netlify](https://img.shields.io/badge/netlify-%23000000.svg?style=for-the-badge&logo=netlify&logoColor=#00C7B7)](https://development--confuser.netlify.app/vm)
3
+ [![NPM](https://img.shields.io/badge/NPM-%23000000.svg?style=for-the-badge&logo=npm&logoColor=white)](https://npmjs.com/package/js-confuser-vm) [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/MichaelXF/js-confuser-vm) [![Netlify](https://img.shields.io/badge/netlify-%23000000.svg?style=for-the-badge&logo=netlify&logoColor=#00C7B7)](https://js-confuser.com/vm)
4
4
 
5
5
 
6
6
  - **Requires Node v24.13.1 or higher**
7
7
  - ES5 support only. No complex features: async, generator, and even try..finally aren't supported.
8
8
  - Experimental. Expect issues.
9
- - [Try the web version.](https://development--confuser.netlify.app/vm)
9
+ - [Try the web version.](https://js-confuser.com/vm)
10
10
 
11
11
  ### Installation
12
12
 
@@ -42,10 +42,10 @@ JsConfuserVM.obfuscate(`
42
42
  dispatcher: true, // create middleman blocks to process jumps?
43
43
  selfModifying: true, // do self-modifying bytecode for function bodies?
44
44
  macroOpcodes: true, // create combined opcodes for repeated instruction sequences?
45
- microOpcodes: true, // break opcodes into sub-opcodes?
46
45
  specializedOpcodes: true, // create specialized opcodes for commonly used opcode+operand pairs?
47
46
  aliasedOpcodes: true, // create duplicate opcodes for commonly used opcodes?
48
47
  timingChecks: true, // add timing checks to detect debuggers?
48
+ classObfuscation: true, // obfuscate the VM runtime classes?
49
49
  minify: true // pass final output through Google Closure Compiler? (Renames VM class properties)
50
50
  }).then(result => {
51
51
  console.log(result.code)
@@ -116,6 +116,7 @@ A(new p(function(a){a=typeof Buffer!=="undefined"?Buffer.from(a,"base64"):Uint8A
116
116
  - [x] getter/setters
117
117
  - [x] debugger;
118
118
  - [x] template literals (**ES6**)
119
+ - [x] rest parameters (**ES6**)
119
120
 
120
121
  ### Missing
121
122
 
@@ -132,7 +133,6 @@ A(new p(function(a){a=typeof Buffer!=="undefined"?Buffer.from(a,"base64"):Uint8A
132
133
  - [ ] dead handlers
133
134
  - [ ] dead bytecode insertion
134
135
  - [x] macro opcodes (Combine multiple opcodes into a "macro opcode")
135
- - [x] micro opcodes (Break opcodes into sub-opcodes)
136
136
  - [x] specialized opcodes (Create specific opcodes for opcode+operand pairs)
137
137
  - [x] aliased opcodes (Create duplicate opcodes, including variants with shuffled operand order)
138
138
  - [x] encoded bytecode array
@@ -181,30 +181,83 @@ var CONSTANTS = [/* 0 */"console", /* 1 */"log", /* 2 */"Hello world!", /* 3 */u
181
181
  var CONSTANTS = [/* 0 */"DaQApB6kAqQdpB+kEaQ=", /* 1 */"TCFOIUUh", /* 2 */"kKK8orait6Kzov2iqaKwopKijaKGosKi", /* 3 */undefined];
182
182
  ```
183
183
 
184
- #### `dispatcher` (true/false)
184
+ #### `controlFlowFlattening` (true/false)
185
185
 
186
- - Creates a middleman block to process jumps.
186
+ Flattens the control flow of your program into a convoluted state machine.
187
187
 
188
188
  ```js
189
189
  // Input Code
190
+ var message;
190
191
  if (true) {
191
- console.log("Hello world!");
192
+ message = "Hello World";
192
193
  }
193
194
 
194
195
  // Before
195
196
  // fn_0_0:
196
- // [0, 0, 0, 0], LOAD_CONST reg[0] = true 1:4-1:8
197
- // [40, 0, 29], JUMP_IF_FALSE [0, if_else_1] 1:0-3:1
198
- // [2, 0, 1, 0], LOAD_GLOBAL reg[0] = console 2:2-2:9
199
- // [0, 1, 2, 0], LOAD_CONST reg[1] = "log" 2:2-2:29
200
- // [8, 2, 0, 1], GET_PROP reg[2] = reg[0][reg[1]] 2:2-2:29
201
- // [0, 1, 3, 0], LOAD_CONST reg[1] = "Hello world!" 2:14-2:28
202
- // [43, 3, 0, 2, 1, 1], CALL_METHOD reg[3] = reg[2](recv=reg[0], 1 args) 2:2-2:29
197
+ r0 = undefined
198
+ r1 = true
199
+ if (!r1) goto: if_else_1
200
+ r1 = "Hello World"
201
+ r0 = r1
202
+ // if_else_1:
203
+ r1 = undefined
204
+ return r1
205
+
206
+ // After
207
+ // fn_0_0:
208
+ r1 = 969
209
+ r2 = r1
210
+ // while_top_5:
211
+ r3 = 4439
212
+ r4 = r2 !== r3
213
+ if (!r4) goto: while_exit_6
214
+ r5 = 969
215
+ r6 = r2 === r5
216
+ if (!r6) goto: if_else_7
217
+ goto: cff_block_2
218
+ // if_else_7:
219
+ r7 = 1317
220
+ r8 = r2 === r7
221
+ if (!r8) goto: if_else_8
222
+ goto: cff_block_3
223
+ // if_else_8:
224
+ r9 = 58894
225
+ r10 = r2 === r9
226
+ if (!r10) goto: if_else_9
227
+ goto: if_else_1
228
+ // if_else_9:
229
+ goto: while_top_5
230
+ // while_exit_6:
231
+ // cff_block_3:
232
+ r11 = "Hello World"
233
+ r0 = r11
234
+ r2 = 58894
235
+ goto: while_top_5
203
236
  // if_else_1:
204
- // [0, 0, 4, 0], LOAD_CONST reg[0] = undefined
205
- // [45, 0], RETURN reg[0]
237
+ r11 = undefined
238
+ return r11
239
+ // cff_block_2:
240
+ r0 = undefined
241
+ r11 = true
242
+ if (r11) goto: cff_skip_10
243
+ r2 = 58894
244
+ goto: while_top_5
245
+ // cff_skip_10:
246
+ r2 = 1317
247
+ goto: while_top_5
248
+ ```
249
+
250
+ #### `dispatcher` (true/false)
251
+
252
+ Creates a middleman block to process jumps.
253
+
254
+ ```js
255
+ // Input Code
256
+ if (true) {
257
+ console.log("Hello world!");
258
+ }
206
259
 
207
- // What this looks like decompiled:
260
+ // Before
208
261
  // fn_0_0:
209
262
  r0 = true
210
263
  if (!r0) goto if_else_1
@@ -219,34 +272,6 @@ if (!r0) goto if_else_1
219
272
 
220
273
  // After
221
274
  // fn_0_0:
222
- // [47, 2, 57, 2, 5, 0], MAKE_CLOSURE reg[2] PC=fn_2_3 (params=2 regs=5 upvalues=0)
223
- // [0, 3, 0, 0], LOAD_CONST reg[3] = true 1:4-1:8
224
- // [41, 3, 21], JUMP_IF_TRUE [3, if_else_1_skip_5]
225
- // [1, 0, 43020], LOAD_INT reg[0] = if_else_1
226
- // [1, 1, 40151], LOAD_INT reg[1] = 40151
227
- // [39, 49], JUMP dispatcher_4
228
- // if_else_1_skip_5:
229
- // [2, 3, 1, 0], LOAD_GLOBAL reg[3] = console 2:2-2:9
230
- // [0, 4, 2, 0], LOAD_CONST reg[4] = "log" 2:2-2:29
231
- // [8, 5, 3, 4], GET_PROP reg[5] = reg[3][reg[4]] 2:2-2:29
232
- // [0, 4, 3, 0], LOAD_CONST reg[4] = "Hello world!" 2:14-2:28
233
- // [43, 6, 3, 5, 1, 4], CALL_METHOD reg[6] = reg[5](recv=reg[3], 1 args) 2:2-2:29
234
- // if_else_1:
235
- // [0, 3, 4, 0], LOAD_CONST reg[3] = undefined
236
- // [45, 3], RETURN reg[3]
237
- // dispatcher_4:
238
- // [42, 0, 2, 2, 0, 1], CALL reg[0] = reg[2](reg[0], reg[1])
239
- // [58, 0], JUMP_REG PC = reg[0]
240
- // fn_2_3:
241
- // [18, 2, 0, 1], BXOR [2, 0, 1]
242
- // [0, 3, 5, 0], LOAD_CONST reg[3] = 52048
243
- // [11, 4, 2, 3], ADD [4, 2, 3]
244
- // [0, 2, 6, 0], LOAD_CONST reg[2] = 65535
245
- // [16, 3, 4, 2], BAND [3, 4, 2]
246
- // [45, 3], RETURN reg[3]
247
-
248
- // What this looks like decompiled:
249
- // fn_0_0:
250
275
  r2 = MakeClosure(fn_2_3, params=2)
251
276
  r3 = true
252
277
  if (r3) goto if_else_1_skip
@@ -340,53 +365,6 @@ case 5074:
340
365
  break;
341
366
  ```
342
367
 
343
- #### `microOpcodes` (true/false)
344
-
345
- Breaks opcodes into mulitple sub-opcodes.
346
-
347
- ```js
348
- // Input Code
349
- console.log("Hello world!");
350
-
351
- // Before
352
- // [2, 1, 0, 0], LOAD_GLOBAL reg[1] = console 1:0-1:7
353
- // [0, 2, 1, 0], LOAD_CONST reg[2] = "log" 1:0-1:27
354
- // [8, 3, 1, 2], GET_PROP reg[3] = reg[1][reg[2]] 1:0-1:27
355
- // [0, 4, 2, 0], LOAD_CONST reg[4] = "Hello world!" 1:12-1:26
356
- // [43, 5, 1, 3, 1, 4], CALL_METHOD reg[5] = reg[3](recv=reg[1], 1 args) 1:0-1:27
357
-
358
- // What the opcode "LOAD_CONST" looks like:
359
- case OP.LOAD_CONST:
360
- var dst = this._operand();
361
- frame.regs[dst] = this._constant();
362
- break;
363
-
364
- // After
365
- // [60, 1], MICRO_LOAD_GLOBAL_0 1 1:0-1:7
366
- // [61, 0, 0], MICRO_LOAD_GLOBAL_1 [0, 0]
367
- // [62], MICRO_LOAD_GLOBAL_2
368
- // [63], MICRO_LOAD_GLOBAL_3
369
- // [58, 2], MICRO_LOAD_CONST_0 2 1:0-1:27
370
- // [59, 1, 0], MICRO_LOAD_CONST_1 [1, 0]
371
- // [64, 3], MICRO_GET_PROP_0 3 1:0-1:27
372
- // [65, 1], MICRO_GET_PROP_1 1
373
- // [66, 2], MICRO_GET_PROP_2 2
374
- // [67], MICRO_GET_PROP_3
375
- // [58, 4], MICRO_LOAD_CONST_0 4 1:12-1:26
376
- // [59, 2, 0], MICRO_LOAD_CONST_1 [2, 0]
377
- // [43, 5, 1, 3, 1, 4], CALL_METHOD reg[5] = reg[3](recv=reg[1], 1 args) 1:0-1:27
378
-
379
- // What the opcodes "MICRO_LOAD_CONST_0" (58) and "MICRO_LOAD_CONST_1" (59) look like:
380
- case 58:
381
- // MICRO_LOAD_CONST_0
382
- this._internals[0] = this._operand();
383
- break;
384
- case 59:
385
- // MICRO_LOAD_CONST_1
386
- frame.regs[this._internals[0]] = this._constant();
387
- break;
388
- ```
389
-
390
368
  #### `specializedOpcodes` (true/false)
391
369
 
392
370
  Creates specialized opcodes for commonly used opcode+operand pairs.
@@ -519,6 +497,9 @@ console.log("Hello, world!");
519
497
 
520
498
  Detects the use of debuggers by checking for >1second pauses. May break code with slow sync tasks.
521
499
 
500
+ ### `classObfuscation` (true/false)
501
+
502
+ Obfuscates the VM runtime classes by shuffling the order of declarations and methods.
522
503
 
523
504
  #### `minify` (true/false)
524
505
 
@@ -596,8 +577,8 @@ main().catch(console.error);
596
577
 
597
578
  ### WIP
598
579
 
599
- - 178 tests, 91.18% coverage
600
- - [Test262 (es5-tests)](https://github.com/tc39/test262/tree/es5-tests) percentage: 66.67%
580
+ - 202 tests, 91.18% coverage
581
+ - [Test262 (es5-tests)](https://github.com/tc39/test262/tree/es5-tests) percentage: 78.58%
601
582
 
602
583
  ### Made with AI
603
584
 
package/bench.ts ADDED
@@ -0,0 +1,146 @@
1
+ import vm from "vm";
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ const args = process.argv.slice(2);
6
+ if (args.length === 0) {
7
+ console.log("Usage: node bench.js <file> [baseFile]");
8
+ console.log(" node bench.js output.js - benchmark a single file");
9
+ console.log(
10
+ " node bench.js output.js base.js - compare output.js against base.js",
11
+ );
12
+ process.exit(1);
13
+ }
14
+
15
+ const ITERATIONS = 5_000;
16
+
17
+ function getFileSize(filePath) {
18
+ return fs.statSync(filePath).size;
19
+ }
20
+
21
+ function formatBytes(bytes) {
22
+ if (bytes < 1024) return bytes + " B";
23
+ return (bytes / 1024).toFixed(2) + " KB";
24
+ }
25
+
26
+ function runBenchmark(filePath) {
27
+ const resolved = path.resolve(filePath);
28
+ if (!fs.existsSync(resolved)) {
29
+ console.error(`File not found: ${resolved}`);
30
+ process.exit(1);
31
+ }
32
+
33
+ const code = fs.readFileSync(resolved, "utf-8");
34
+ const script = new vm.Script(code, { filename: resolved });
35
+
36
+ // Verify the file runs successfully first
37
+ try {
38
+ const testCtx = vm.createContext({
39
+ window: {},
40
+ console,
41
+ process,
42
+ performance,
43
+ });
44
+ script.runInContext(testCtx);
45
+ } catch (e) {
46
+ console.error(`\nFile failed to execute: ${resolved}`);
47
+ console.error(e.message);
48
+ process.exit(1);
49
+ }
50
+
51
+ const times = [];
52
+
53
+ // Warmup
54
+ for (let i = 0; i < 50; i++) {
55
+ const ctx = vm.createContext({ window: {}, console, process, performance });
56
+ script.runInContext(ctx);
57
+ }
58
+
59
+ for (let i = 0; i < ITERATIONS; i++) {
60
+ const ctx = vm.createContext({ window: {}, console, process, performance });
61
+ const start = performance.now();
62
+ script.runInContext(ctx);
63
+ const end = performance.now();
64
+ times.push(end - start);
65
+
66
+ if ((i + 1) % 1000 === 0) {
67
+ process.stdout.write(`\r Running... ${i + 1}/${ITERATIONS}`);
68
+ }
69
+ }
70
+ process.stdout.write("\r" + " ".repeat(40) + "\r");
71
+
72
+ times.sort((a, b) => a - b);
73
+
74
+ const median = times[Math.floor(times.length / 2)];
75
+ const q1 = times[Math.floor(times.length * 0.25)];
76
+ const q3 = times[Math.floor(times.length * 0.75)];
77
+ const min = times[0];
78
+ const max = times[times.length - 1];
79
+ const mean = times.reduce((a, b) => a + b, 0) / times.length;
80
+ const fileSize = getFileSize(resolved);
81
+
82
+ return { median, q1, q3, min, max, mean, fileSize, filePath: resolved };
83
+ }
84
+
85
+ function formatMs(ms) {
86
+ return ms.toFixed(3) + " ms";
87
+ }
88
+
89
+ function pctChange(base, compare) {
90
+ const pct = ((compare - base) / base) * 100;
91
+ const sign = pct >= 0 ? "+" : "";
92
+ return sign + pct.toFixed(1) + "%";
93
+ }
94
+
95
+ function printResult(label, result) {
96
+ console.log(` ${label}`);
97
+ console.log(` ${"=".repeat(label.length)}`);
98
+ console.log(` File: ${result.filePath}`);
99
+ console.log(` File Size: ${formatBytes(result.fileSize)}`);
100
+ console.log(` Iterations: ${ITERATIONS.toLocaleString()}`);
101
+ console.log();
102
+ console.log(` Median: ${formatMs(result.median)}`);
103
+ console.log(` Mean: ${formatMs(result.mean)}`);
104
+ console.log(` Q1: ${formatMs(result.q1)}`);
105
+ console.log(` Q3: ${formatMs(result.q3)}`);
106
+ console.log(` Min: ${formatMs(result.min)}`);
107
+ console.log(` Max: ${formatMs(result.max)}`);
108
+ console.log(` IQR: ${formatMs(result.q3 - result.q1)}`);
109
+ }
110
+
111
+ function printComparison(base, compare) {
112
+ console.log(" Comparison (vs Base)");
113
+ console.log(" " + "=".repeat(21));
114
+ console.log(` Median: ${pctChange(base.median, compare.median)}`);
115
+ console.log(` Mean: ${pctChange(base.mean, compare.mean)}`);
116
+ console.log(` Q1: ${pctChange(base.q1, compare.q1)}`);
117
+ console.log(` Q3: ${pctChange(base.q3, compare.q3)}`);
118
+ console.log(` File Size: ${pctChange(base.fileSize, compare.fileSize)}`);
119
+ }
120
+
121
+ console.log();
122
+ console.log(" JS-Confuser VM Benchmark");
123
+ console.log(" " + "-".repeat(26));
124
+ console.log();
125
+
126
+ if (args.length === 1) {
127
+ const result = runBenchmark(args[0]);
128
+ printResult(path.basename(args[0]), result);
129
+ console.log();
130
+ } else {
131
+ const baseFile = args[1];
132
+ const compareFile = args[0];
133
+
134
+ console.log(` Benchmarking base: ${path.basename(baseFile)}`);
135
+ const baseResult = runBenchmark(baseFile);
136
+ console.log(` Benchmarking compare: ${path.basename(compareFile)}`);
137
+ const compareResult = runBenchmark(compareFile);
138
+
139
+ console.log();
140
+ printResult("Base: " + path.basename(baseFile), baseResult);
141
+ console.log();
142
+ printResult("Compare: " + path.basename(compareFile), compareResult);
143
+ console.log();
144
+ printComparison(baseResult, compareResult);
145
+ console.log();
146
+ }
package/disassemble.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import JsConfuserVM from "./src/index.ts";
3
+
4
+ const fileContents = await readFile(process.argv[2], "utf-8");
5
+
6
+ const outputFile =
7
+ process.argv[3] || process.argv[2].replace(/\.js$/, ".disassembled.js");
8
+
9
+ const output = await JsConfuserVM.disassemble(fileContents);
10
+ console.log(output);
11
+
12
+ await writeFile(outputFile, output, "utf-8");
@@ -1,13 +1,14 @@
1
1
  import { generate } from "@babel/generator";
2
2
  import { parse } from "@babel/parser";
3
3
  import { applyMacroOpcodes } from "./transforms/runtime/macroOpcodes.js";
4
- import { applyMicroOpcodes } from "./transforms/runtime/microOpcodes.js";
5
- import { applyInteralVariablesToRuntime } from "./transforms/runtime/internalVariables.js";
6
4
  import { applyShuffleOpcodes } from "./transforms/runtime/shuffleOpcodes.js";
7
5
  import { applyMinify } from "./transforms/runtime/minify.js";
8
6
  import { applySpecializedOpcodes } from "./transforms/runtime/specializedOpcodes.js";
9
7
  import { applyAliasedOpcodes } from "./transforms/runtime/aliasedOpcodes.js";
10
- export async function obfuscateRuntime(runtime, bytecode, options, compiler, generateBytecodeComment) {
8
+ import { applyClassObfuscation } from "./transforms/runtime/classObfuscation.js";
9
+ import { getSwitchStatement } from "./utils/ast-utils.js";
10
+ import { now } from "./utils/profile-utils.js";
11
+ export async function buildRuntime(runtime, bytecode, options, compiler, generateBytecodeComment) {
11
12
  let ast;
12
13
  try {
13
14
  ast = parse(runtime, {
@@ -18,34 +19,52 @@ export async function obfuscateRuntime(runtime, bytecode, options, compiler, gen
18
19
  cause: error
19
20
  });
20
21
  }
22
+ const switchStatement = getSwitchStatement(ast);
23
+ const getHandlerCount = () => {
24
+ return switchStatement.cases.length;
25
+ };
26
+ const timings = {};
27
+ function runAndTime(pass, name) {
28
+ const startedAt = now();
29
+ compiler.log(`Running runtime pass ${name}...`);
30
+ pass(ast, compiler);
31
+ const endedAt = now();
32
+ const elapsedMs = endedAt - startedAt;
33
+ timings[name] = elapsedMs;
34
+ compiler.profileData.transforms[name] = {
35
+ fileSize: null,
36
+ // TODO: Add option as doing 'generate(ast).code.length' is slow
37
+ transformTime: elapsedMs,
38
+ handlerCount: getHandlerCount()
39
+ };
40
+ compiler.log(`Runtime pass ${name} completed in ${Math.floor(elapsedMs)}ms`);
41
+ }
21
42
 
22
43
  // Specialized opcode cases must be applied BEFORE shuffleOpcodes
23
44
  if (options.specializedOpcodes) {
24
- applySpecializedOpcodes(ast, compiler);
25
- }
26
- if (options.microOpcodes) {
27
- applyInteralVariablesToRuntime(ast, compiler);
28
- }
29
-
30
- // Micro opcode cases must be applied BEFORE shuffleOpcodes
31
- if (options.microOpcodes && Object.keys(compiler.MICRO_OPS).length > 0) {
32
- applyMicroOpcodes(ast, compiler);
45
+ runAndTime(applySpecializedOpcodes, "applySpecializedOpcodes");
33
46
  }
34
47
 
35
48
  // Macro opcode cases must be applied BEFORE shuffleOpcodes
36
49
  if (options.macroOpcodes && Object.keys(compiler.MACRO_OPS).length > 0) {
37
- applyMacroOpcodes(ast, compiler);
50
+ runAndTime(applyMacroOpcodes, "applyMacroOpcodes");
38
51
  }
39
52
 
40
53
  // Aliased opcode cases must be applied BEFORE shuffleOpcodes
41
54
  if (options.aliasedOpcodes) {
42
- applyAliasedOpcodes(ast, compiler);
55
+ runAndTime(applyAliasedOpcodes, "applyAliasedOpcodes");
43
56
  }
44
57
 
45
58
  // Shuffle opcode handle order
46
59
  if (options.shuffleOpcodes) {
47
- applyShuffleOpcodes(ast);
60
+ runAndTime(applyShuffleOpcodes, "applyShuffleOpcodes");
61
+ }
62
+
63
+ // Shuffle top-level var declarations and prototype method definitions
64
+ if (options.classObfuscation) {
65
+ runAndTime(applyClassObfuscation, "applyClassObfuscation");
48
66
  }
67
+ compiler.profileData.handlerCount = getHandlerCount();
49
68
  let generated;
50
69
  try {
51
70
  generated = generate(ast).code;
@@ -61,7 +80,14 @@ export async function obfuscateRuntime(runtime, bytecode, options, compiler, gen
61
80
  // Minify code?
62
81
  if (options.minify) {
63
82
  try {
83
+ let startedAt = now();
84
+ compiler.log("Running minify...");
64
85
  generated = await applyMinify(generated);
86
+ let elapsedMs = now() - startedAt;
87
+ compiler.log(`Minify completed in ${Math.floor(elapsedMs)}ms`);
88
+ compiler.profileData.transforms["minify"] = {
89
+ transformTime: elapsedMs
90
+ };
65
91
  } catch (error) {
66
92
  throw new Error("VM-Runtime final minification failed", {
67
93
  cause: error