js-confuser-vm 0.0.3 → 0.0.5
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 +125 -28
- package/LICENSE +21 -21
- package/README.MD +370 -196
- package/babel-plugin-inline-runtime.cjs +34 -34
- package/babel.config.json +23 -23
- package/dist/build-runtime.js +53 -0
- package/dist/compiler.js +107 -117
- package/dist/runtime.js +78 -84
- package/dist/transforms/bytecode/macroOpcodes.js +152 -0
- package/dist/transforms/{resolveContants.js → bytecode/resolveContants.js} +16 -6
- package/dist/transforms/bytecode/resolveLabels.js +80 -0
- package/dist/transforms/{selfModifying.js → bytecode/selfModifying.js} +33 -33
- package/dist/transforms/bytecode/specializedOpcodes.js +103 -0
- package/dist/transforms/runtime/macroOpcodes.js +88 -0
- package/dist/transforms/runtime/minify.js +1 -0
- package/dist/transforms/runtime/shuffleOpcodes.js +20 -0
- package/dist/transforms/runtime/specializedOpcodes.js +102 -0
- package/dist/transforms/utils/op-utils.js +25 -0
- package/dist/{random.js → transforms/utils/random-utils.js} +3 -3
- package/dist/types.js +4 -2
- package/index.ts +34 -22
- package/jest-strip-types.js +10 -10
- package/jest.config.js +35 -28
- package/package.json +49 -48
- package/src/build-runtime.ts +57 -0
- package/src/compiler.ts +2069 -2066
- package/src/index.ts +14 -14
- package/src/minify.ts +21 -21
- package/src/options.ts +14 -12
- package/src/runtime.ts +771 -779
- package/src/transforms/bytecode/macroOpcodes.ts +177 -0
- package/src/transforms/bytecode/resolveContants.ts +62 -0
- package/src/transforms/bytecode/resolveLabels.ts +107 -0
- package/src/transforms/{selfModifying.ts → bytecode/selfModifying.ts} +37 -40
- package/src/transforms/bytecode/specializedOpcodes.ts +118 -0
- package/src/transforms/runtime/macroOpcodes.ts +111 -0
- package/src/transforms/runtime/minify.ts +1 -0
- package/src/transforms/runtime/shuffleOpcodes.ts +24 -0
- package/src/transforms/runtime/specializedOpcodes.ts +146 -0
- package/src/transforms/utils/op-utils.ts +26 -0
- package/src/{random.ts → transforms/utils/random-utils.ts} +31 -31
- package/src/types.ts +33 -24
- package/src/utilts.ts +3 -3
- package/tsconfig.json +12 -12
- package/dist/runtimeObf.js +0 -56
- package/dist/transforms/controlFlowFlattening.js +0 -22
- package/dist/transforms/resolveLabels.js +0 -59
- package/src/runtimeObf.ts +0 -62
- package/src/transforms/controlFlowFlattening.ts +0 -30
- package/src/transforms/resolveContants.ts +0 -42
- package/src/transforms/resolveLabels.ts +0 -83
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { SOURCE_NODE_SYM } from "../../compiler.js";
|
|
2
|
+
import { nextFreeSlot, U16_MAX } from "../utils/op-utils.js";
|
|
3
|
+
|
|
4
|
+
// Creates specialized opcodes for the most frequent (OPCODE + single_integer_operand) pairs.
|
|
5
|
+
// Example: [OP.LOAD_CONST, 1] becomes [SPECIALIZED_LOAD_CONST_1].
|
|
6
|
+
// Only instructions with *exactly one numeric operand* are considered.
|
|
7
|
+
// MAKE_CLOSURE and any instruction with zero / multiple operands are skipped.
|
|
8
|
+
// Runs after selfModifying but before resolveLabels (operands stay plain numbers).
|
|
9
|
+
export function specializedOpcodes(bc, compiler) {
|
|
10
|
+
// ── Collect used opcodes exactly as specified ─────────────────────────────
|
|
11
|
+
const usedOpcodes = new Set(Object.keys(compiler.OP_NAME).map(k => parseInt(k, 10)).filter(v => !isNaN(v)));
|
|
12
|
+
if (usedOpcodes.size > U16_MAX) return {
|
|
13
|
+
bytecode: bc
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// ── Step 1: count frequency of eligible (op, operand) pairs ───────────────
|
|
17
|
+
const freqMap = new Map();
|
|
18
|
+
for (const instr of bc) {
|
|
19
|
+
const op = instr[0];
|
|
20
|
+
if (op === null || op === compiler.OP.MAKE_CLOSURE) continue;
|
|
21
|
+
|
|
22
|
+
// Must have exactly one operand and it must be a plain number
|
|
23
|
+
if (instr.length !== 2) continue;
|
|
24
|
+
const operand = instr[1];
|
|
25
|
+
const key = `${op},${operand}`;
|
|
26
|
+
const entry = freqMap.get(key);
|
|
27
|
+
if (entry) {
|
|
28
|
+
entry.count++;
|
|
29
|
+
} else {
|
|
30
|
+
freqMap.set(key, {
|
|
31
|
+
op,
|
|
32
|
+
operand,
|
|
33
|
+
count: 1
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ── Step 2: keep combinations that appear >= 2 times, sort by frequency ───
|
|
39
|
+
const candidates = Array.from(freqMap.values()).filter(e => e.count >= 1).sort((a, b) => b.count - a.count);
|
|
40
|
+
if (candidates.length === 0) return {
|
|
41
|
+
bytecode: bc
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// ── Step 3: assign free opcode slots to the best candidates ───────────────
|
|
45
|
+
const sigToSpecial = new Map();
|
|
46
|
+
const specializedOps = {};
|
|
47
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
48
|
+
const specialOp = nextFreeSlot(usedOpcodes);
|
|
49
|
+
if (specialOp === -1) break;
|
|
50
|
+
const {
|
|
51
|
+
op: originalOp,
|
|
52
|
+
operand
|
|
53
|
+
} = candidates[i];
|
|
54
|
+
const key = `${originalOp},${JSON.stringify(operand)}`;
|
|
55
|
+
sigToSpecial.set(key, specialOp);
|
|
56
|
+
specializedOps[specialOp] = {
|
|
57
|
+
originalOp,
|
|
58
|
+
operand
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Register a human-readable name for disassembly / debugging
|
|
62
|
+
const originalName = compiler.OP_NAME[originalOp] ?? `OP_${originalOp}`;
|
|
63
|
+
compiler.OP_NAME[specialOp] = `${originalName}_${JSON.stringify(operand)}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Store mapping so the interpreter knows how to dispatch the specialized op
|
|
67
|
+
compiler.SPECIALIZED_OPS = specializedOps;
|
|
68
|
+
|
|
69
|
+
// ── Step 4: replace matching instructions with the new single-byte opcode ─
|
|
70
|
+
const result = [];
|
|
71
|
+
for (const instr of bc) {
|
|
72
|
+
const op = instr[0];
|
|
73
|
+
// Only consider instructions with exactly one numeric operand
|
|
74
|
+
if (op === null || instr.length !== 2 || op === compiler.OP.MAKE_CLOSURE) {
|
|
75
|
+
result.push(instr);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const operand = instr[1];
|
|
79
|
+
const key = `${op},${JSON.stringify(operand)}`;
|
|
80
|
+
if (sigToSpecial.has(key)) {
|
|
81
|
+
const specialOpCode = sigToSpecial.get(key);
|
|
82
|
+
const operandAsObject = typeof operand === "object" && operand ? operand : {
|
|
83
|
+
type: "number",
|
|
84
|
+
value: operand,
|
|
85
|
+
resolvedValue: operand
|
|
86
|
+
};
|
|
87
|
+
const newOperand = {
|
|
88
|
+
...operandAsObject,
|
|
89
|
+
placeholder: true
|
|
90
|
+
};
|
|
91
|
+
const newInstr = [specialOpCode, newOperand];
|
|
92
|
+
|
|
93
|
+
// Preserve source-node information for error reporting
|
|
94
|
+
newInstr[SOURCE_NODE_SYM] = instr[SOURCE_NODE_SYM];
|
|
95
|
+
result.push(newInstr);
|
|
96
|
+
} else {
|
|
97
|
+
result.push(instr);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
bytecode: result
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import traverseImport from "@babel/traverse";
|
|
3
|
+
import { ok } from "assert";
|
|
4
|
+
const traverse = traverseImport.default || traverseImport;
|
|
5
|
+
|
|
6
|
+
// Extract the real statement list from a SwitchCase consequent, normalising
|
|
7
|
+
// the two forms that appear in the runtime:
|
|
8
|
+
// • A single wrapping BlockStatement → use its .body
|
|
9
|
+
// • Statements listed directly → use as-is
|
|
10
|
+
// In both cases trailing BreakStatement / EmptyStatement are filtered out.
|
|
11
|
+
function extractCaseBody(switchCase) {
|
|
12
|
+
let stmts;
|
|
13
|
+
if (switchCase.consequent.length === 1 && t.isBlockStatement(switchCase.consequent[0])) {
|
|
14
|
+
stmts = switchCase.consequent[0].body;
|
|
15
|
+
} else {
|
|
16
|
+
stmts = switchCase.consequent;
|
|
17
|
+
}
|
|
18
|
+
return stmts.filter(s => !t.isBreakStatement(s) && !t.isEmptyStatement(s));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Append a generated switch case for every entry in compiler.MACRO_OPS.
|
|
22
|
+
// Each case inlines the constituent case bodies directly — no operand stack,
|
|
23
|
+
// no substitution needed. Because every opcode handler now reads its own
|
|
24
|
+
// operands via this._operand(), those calls naturally consume the inline
|
|
25
|
+
// operands that macroOpcodes.ts embedded on the macro instruction.
|
|
26
|
+
// Must be called BEFORE applyShuffleOpcodes so the new cases get shuffled.
|
|
27
|
+
export function applyMacroOpcodes(ast, compiler) {
|
|
28
|
+
let switchStatement = null;
|
|
29
|
+
traverse(ast, {
|
|
30
|
+
SwitchStatement(path) {
|
|
31
|
+
if (path.node.leadingComments?.some(c => c.value.includes("@SWITCH"))) {
|
|
32
|
+
switchStatement = path.node;
|
|
33
|
+
path.stop();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
ok(switchStatement, "Could not find @SWITCH statement for macro opcodes");
|
|
38
|
+
|
|
39
|
+
// Build a map opName → SwitchCase from the existing OP.xxx case tests.
|
|
40
|
+
const nameToCaseMap = new Map();
|
|
41
|
+
for (const sc of switchStatement.cases) {
|
|
42
|
+
const test = sc.test;
|
|
43
|
+
if (test && t.isMemberExpression(test) && t.isIdentifier(test.object, {
|
|
44
|
+
name: "OP"
|
|
45
|
+
}) && t.isIdentifier(test.property)) {
|
|
46
|
+
nameToCaseMap.set(test.property.name, sc);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
for (const [macroOpStr, constituentOps] of Object.entries(compiler.MACRO_OPS)) {
|
|
50
|
+
const macroOpCode = Number(macroOpStr);
|
|
51
|
+
const N = constituentOps.length;
|
|
52
|
+
|
|
53
|
+
// Resolve each constituent op value → case node via OP_NAME lookup.
|
|
54
|
+
const constituentCases = [];
|
|
55
|
+
let allFound = true;
|
|
56
|
+
for (const opVal of constituentOps) {
|
|
57
|
+
const opName = compiler.OP_NAME[opVal];
|
|
58
|
+
if (!opName) {
|
|
59
|
+
allFound = false;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
const found = nameToCaseMap.get(opName);
|
|
63
|
+
if (!found) {
|
|
64
|
+
allFound = false;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
constituentCases.push(found);
|
|
68
|
+
}
|
|
69
|
+
if (!allFound) continue;
|
|
70
|
+
const opNames = constituentOps.map(v => compiler.OP_NAME[v] ?? `OP_${v}`);
|
|
71
|
+
|
|
72
|
+
// ── Build the macro case body ──────────────────────────────────────────
|
|
73
|
+
// Clone and inline each sub-instruction's case body directly.
|
|
74
|
+
// No operand substitution needed: each body already calls this._operand()
|
|
75
|
+
// to read its own operands, which will consume the inline operands that
|
|
76
|
+
// macroOpcodes.ts embedded on the macro instruction in order.
|
|
77
|
+
const bodyStmts = [];
|
|
78
|
+
for (let i = 0; i < N; i++) {
|
|
79
|
+
const subStmts = extractCaseBody(constituentCases[i]).map(s => t.cloneNode(s, true));
|
|
80
|
+
if (subStmts.length > 0) {
|
|
81
|
+
t.addComment(subStmts[0], "leading", ` ${opNames[i]}`, true);
|
|
82
|
+
bodyStmts.push(...subStmts);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
bodyStmts.push(t.breakStatement());
|
|
86
|
+
switchStatement.cases.push(t.switchCase(t.numericLiteral(macroOpCode), [t.blockStatement(bodyStmts)]));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { minify as applyMinify } from "../../minify.js";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import traverseImport from "@babel/traverse";
|
|
2
|
+
import { ok } from "assert";
|
|
3
|
+
import { shuffle } from "../utils/random-utils.js";
|
|
4
|
+
const traverse = traverseImport.default || traverseImport;
|
|
5
|
+
|
|
6
|
+
// Randomly reorder the switch cases inside the @SWITCH statement so the
|
|
7
|
+
// opcode handler order varies per build.
|
|
8
|
+
export function applyShuffleOpcodes(ast) {
|
|
9
|
+
let switchStatement = null;
|
|
10
|
+
traverse(ast, {
|
|
11
|
+
SwitchStatement(path) {
|
|
12
|
+
if (path.node.leadingComments?.some(c => c.value.includes("@SWITCH"))) {
|
|
13
|
+
switchStatement = path.node;
|
|
14
|
+
path.stop();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
ok(switchStatement, "Could not find opcode handlers switch statement");
|
|
19
|
+
switchStatement.cases = shuffle(switchStatement.cases);
|
|
20
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import traverseImport from "@babel/traverse";
|
|
3
|
+
import { ok } from "assert";
|
|
4
|
+
const traverse = traverseImport.default || traverseImport;
|
|
5
|
+
|
|
6
|
+
// Extract the real statement list from a SwitchCase consequent (identical to the
|
|
7
|
+
// helper used by applyMacroOpcodes so the two files stay in sync).
|
|
8
|
+
function extractCaseBody(switchCase) {
|
|
9
|
+
let stmts;
|
|
10
|
+
if (switchCase.consequent.length === 1 && t.isBlockStatement(switchCase.consequent[0])) {
|
|
11
|
+
stmts = switchCase.consequent[0].body;
|
|
12
|
+
} else {
|
|
13
|
+
stmts = switchCase.consequent;
|
|
14
|
+
}
|
|
15
|
+
return stmts.filter(s => !t.isBreakStatement(s) && !t.isEmptyStatement(s));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Inline a fixed numeric operand in place of every `this._operand()` call.
|
|
19
|
+
// Because specialized opcodes are only created for instructions that have
|
|
20
|
+
// *exactly one* numeric operand, every `_operand()` call inside the original
|
|
21
|
+
// handler is replaced by the constant value that was baked into the opcode.
|
|
22
|
+
function inlineFixedOperand(bodyStmts, resolvedValue) {
|
|
23
|
+
// Wrap the statements in a temporary BlockStatement so traverse has a root.
|
|
24
|
+
// The replacement mutates the original statement objects in place.
|
|
25
|
+
traverse(t.blockStatement(bodyStmts), {
|
|
26
|
+
noScope: true,
|
|
27
|
+
CallExpression(path) {
|
|
28
|
+
const callee = path.node.callee;
|
|
29
|
+
if (t.isMemberExpression(callee) && t.isThisExpression(callee.object) && t.isIdentifier(callee.property, {
|
|
30
|
+
name: "_operand"
|
|
31
|
+
}) && path.node.arguments.length === 0) {
|
|
32
|
+
path.replaceWith(t.numericLiteral(resolvedValue));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Append a generated switch case for every entry in compiler.SPECIALIZED_OPS.
|
|
39
|
+
// Each case is a clone of the original opcode’s handler with `this._operand()`
|
|
40
|
+
// replaced by the constant integer that was captured at compile time.
|
|
41
|
+
// Must be called AFTER applyMacroOpcodes (so the original cases exist) but
|
|
42
|
+
// BEFORE applyShuffleOpcodes so the new specialized cases also get shuffled.
|
|
43
|
+
export function applySpecializedOpcodes(ast, bytecode, compiler) {
|
|
44
|
+
let switchStatement = null;
|
|
45
|
+
traverse(ast, {
|
|
46
|
+
SwitchStatement(path) {
|
|
47
|
+
if (path.node.leadingComments?.some(c => c.value.includes("@SWITCH"))) {
|
|
48
|
+
switchStatement = path.node;
|
|
49
|
+
path.stop();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
ok(switchStatement, "Could not find @SWITCH statement for specialized opcodes");
|
|
54
|
+
|
|
55
|
+
// Build a map opName → SwitchCase from the existing OP.xxx case tests.
|
|
56
|
+
const nameToCaseMap = new Map();
|
|
57
|
+
for (const sc of switchStatement.cases) {
|
|
58
|
+
const test = sc.test;
|
|
59
|
+
if (test && t.isMemberExpression(test) && t.isIdentifier(test.object, {
|
|
60
|
+
name: "OP"
|
|
61
|
+
}) && t.isIdentifier(test.property)) {
|
|
62
|
+
nameToCaseMap.set(test.property.name, sc);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (!compiler.SPECIALIZED_OPS) return;
|
|
66
|
+
for (const [specialOpStr, info] of Object.entries(compiler.SPECIALIZED_OPS)) {
|
|
67
|
+
const specialOpCode = Number(specialOpStr);
|
|
68
|
+
const {
|
|
69
|
+
originalOp,
|
|
70
|
+
operand
|
|
71
|
+
} = info;
|
|
72
|
+
const newName = compiler.OP_NAME[specialOpCode];
|
|
73
|
+
const originalName = compiler.OP_NAME[originalOp];
|
|
74
|
+
if (!originalName) continue;
|
|
75
|
+
const originalCase = nameToCaseMap.get(originalName);
|
|
76
|
+
if (!originalCase) continue;
|
|
77
|
+
|
|
78
|
+
// Clone the original handler body
|
|
79
|
+
const bodyStmts = extractCaseBody(originalCase).map(s => t.cloneNode(s, true));
|
|
80
|
+
const placedOperand = info.resolvedOperand || {
|
|
81
|
+
resolvedValue: 1337
|
|
82
|
+
};
|
|
83
|
+
ok(placedOperand !== undefined, `Could not find operand for original opcode ${newName}`);
|
|
84
|
+
const resolvedValue = placedOperand?.resolvedValue ?? placedOperand;
|
|
85
|
+
if (typeof resolvedValue !== "number") {
|
|
86
|
+
console.error(resolvedValue);
|
|
87
|
+
}
|
|
88
|
+
ok(typeof resolvedValue === "number", "Expected a numeric operand value");
|
|
89
|
+
|
|
90
|
+
// Replace this._operand() with the baked-in constant
|
|
91
|
+
inlineFixedOperand(bodyStmts, resolvedValue);
|
|
92
|
+
|
|
93
|
+
// Add a leading comment so the generated source stays readable
|
|
94
|
+
if (bodyStmts.length > 0) {
|
|
95
|
+
t.addComment(bodyStmts[0], "leading", ` ${compiler.OP_NAME[specialOpCode]} (specialized)`, true);
|
|
96
|
+
}
|
|
97
|
+
bodyStmts.push(t.breakStatement());
|
|
98
|
+
|
|
99
|
+
// Insert the new specialized case into the big switch
|
|
100
|
+
switchStatement.cases.push(t.switchCase(t.numericLiteral(specialOpCode), [t.blockStatement(bodyStmts)]));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getRandomInt } from "./random-utils.js";
|
|
2
|
+
export const U16_MAX = 0xffff; // bytecode operands are u16
|
|
3
|
+
|
|
4
|
+
/** Returns the next free opcode slot, or -1 when the space is exhausted. */
|
|
5
|
+
export function nextFreeSlot(usedOpcodes) {
|
|
6
|
+
if (usedOpcodes.size > U16_MAX) return -1;
|
|
7
|
+
let attempts = 0;
|
|
8
|
+
while (attempts++ < 512) {
|
|
9
|
+
const candidate = getRandomInt(0, U16_MAX);
|
|
10
|
+
if (!usedOpcodes.has(candidate)) {
|
|
11
|
+
usedOpcodes.add(candidate);
|
|
12
|
+
return candidate;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// Fallback: linear scan from a random start
|
|
16
|
+
const start = getRandomInt(0, U16_MAX);
|
|
17
|
+
for (let i = 0; i <= U16_MAX; i++) {
|
|
18
|
+
const v = start + i & U16_MAX;
|
|
19
|
+
if (!usedOpcodes.has(v)) {
|
|
20
|
+
usedOpcodes.add(v);
|
|
21
|
+
return v;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return -1;
|
|
25
|
+
}
|
|
@@ -14,9 +14,9 @@ export function getRandomInt(min, max) {
|
|
|
14
14
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Shuffles an array in-place using the Fisher-Yates algorithm.
|
|
19
|
-
* @param array - The array to shuffle (mutated)
|
|
17
|
+
/**
|
|
18
|
+
* Shuffles an array in-place using the Fisher-Yates algorithm.
|
|
19
|
+
* @param array - The array to shuffle (mutated)
|
|
20
20
|
*/
|
|
21
21
|
export function shuffle(array) {
|
|
22
22
|
for (let i = array.length - 1; i > 0; i--) {
|
package/dist/types.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// Bytecode supports both real instructions and IR pseudo-instructions
|
|
2
|
-
// Real instruction: [OP.ADD, 5]
|
|
2
|
+
// Real instruction: [OP.ADD, 5] or multi-operand: [OP.MAKE_CLOSURE, labelRef, 2, 3, 0]
|
|
3
3
|
// IR instruction: [null, { type: "defineLabel", label: "FN_ENTRY_1" }]
|
|
4
4
|
|
|
5
5
|
// IR instructions are used to hold symbolic information during compilation
|
|
6
|
-
// All "null" instructions are dropped before assembly time
|
|
6
|
+
// All "null" instructions are dropped before assembly time.
|
|
7
|
+
// Instructions may carry any number of operands; the flat output serializes
|
|
8
|
+
// each operand as a separate u16 slot in the bytecode array.
|
|
7
9
|
|
|
8
10
|
export function constantOperand(value) {
|
|
9
11
|
return {
|
package/index.ts
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
|
-
import JsConfuserVM from "./src/index.ts";
|
|
2
|
-
import { readFileSync, writeFileSync } from "fs";
|
|
3
|
-
|
|
4
|
-
async function main() {
|
|
5
|
-
// Compile and write the output to a file
|
|
6
|
-
const sourceCode = readFileSync("input.js", "utf-8");
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import JsConfuserVM from "./src/index.ts";
|
|
2
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
3
|
+
|
|
4
|
+
async function main() {
|
|
5
|
+
// Compile and write the output to a file
|
|
6
|
+
const sourceCode = readFileSync("input.js", "utf-8");
|
|
7
|
+
|
|
8
|
+
const { code: orginalOutput } = await JsConfuserVM.obfuscate(sourceCode, {});
|
|
9
|
+
|
|
10
|
+
const { code: output } = await JsConfuserVM.obfuscate(sourceCode, {
|
|
11
|
+
target: "browser", // or "node"
|
|
12
|
+
// randomizeOpcodes: true, // randomize the opcode numbers?
|
|
13
|
+
// shuffleOpcodes: true, // shuffle order of opcode handlers in the runtime?
|
|
14
|
+
// encodeBytecode: true, // encode bytecode? when off, comments for instructions are added
|
|
15
|
+
// selfModifying: true, // do self-modifying bytecode for function bodies?
|
|
16
|
+
// macroOpcodes: true, // create combined opcodes for repeated instruction sequences?
|
|
17
|
+
// specializedOpcodes: true, // create specialized opcodes for commonly used opcode+operand pairs?
|
|
18
|
+
// timingChecks: true, // add timing checks to detect debuggers?
|
|
19
|
+
// minify: true, // pass final output through Google Closure Compiler? (
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
writeFileSync("output.original.js", orginalOutput, "utf-8");
|
|
23
|
+
writeFileSync("output.js", output, "utf-8");
|
|
24
|
+
|
|
25
|
+
// Eval the code like our test suite does
|
|
26
|
+
var window = { TEST_OUTPUT: null };
|
|
27
|
+
eval(output);
|
|
28
|
+
console.log(window.TEST_OUTPUT);
|
|
29
|
+
|
|
30
|
+
// Minify using Google Closure Compiler (optional)
|
|
31
|
+
// import("./minify.js");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
main();
|
package/jest-strip-types.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { stripTypeScriptTypes } from 'node:module';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
process(code, filePath) {
|
|
5
|
-
if (filePath.endsWith('.ts')) {
|
|
6
|
-
return { code: stripTypeScriptTypes(code) };
|
|
7
|
-
}
|
|
8
|
-
return { code };
|
|
9
|
-
},
|
|
10
|
-
};
|
|
1
|
+
import { stripTypeScriptTypes } from 'node:module';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
process(code, filePath) {
|
|
5
|
+
if (filePath.endsWith('.ts')) {
|
|
6
|
+
return { code: stripTypeScriptTypes(code) };
|
|
7
|
+
}
|
|
8
|
+
return { code };
|
|
9
|
+
},
|
|
10
|
+
};
|
package/jest.config.js
CHANGED
|
@@ -1,28 +1,35 @@
|
|
|
1
|
-
const OPTIONS_MATRIX = [
|
|
2
|
-
{ displayName: "default", VM_OPTIONS: {} },
|
|
3
|
-
{ displayName: "randomizeOpcodes", VM_OPTIONS: { randomizeOpcodes: true } },
|
|
4
|
-
{ displayName: "shuffleOpcodes", VM_OPTIONS: { shuffleOpcodes: true } },
|
|
5
|
-
{ displayName: "encodeBytecode", VM_OPTIONS: { encodeBytecode: true } },
|
|
6
|
-
{ displayName: "selfModifying", VM_OPTIONS: { selfModifying: true } },
|
|
7
|
-
{ displayName: "timingChecks", VM_OPTIONS: { timingChecks: true } },
|
|
8
|
-
{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
1
|
+
const OPTIONS_MATRIX = [
|
|
2
|
+
{ displayName: "default", VM_OPTIONS: {} },
|
|
3
|
+
{ displayName: "randomizeOpcodes", VM_OPTIONS: { randomizeOpcodes: true } },
|
|
4
|
+
{ displayName: "shuffleOpcodes", VM_OPTIONS: { shuffleOpcodes: true } },
|
|
5
|
+
{ displayName: "encodeBytecode", VM_OPTIONS: { encodeBytecode: true } },
|
|
6
|
+
{ displayName: "selfModifying", VM_OPTIONS: { selfModifying: true } },
|
|
7
|
+
{ displayName: "timingChecks", VM_OPTIONS: { timingChecks: true } },
|
|
8
|
+
{ displayName: "macroOpcodes", VM_OPTIONS: { macroOpcodes: true } },
|
|
9
|
+
{
|
|
10
|
+
displayName: "specializedOpcodes",
|
|
11
|
+
VM_OPTIONS: { specializedOpcodes: true },
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
displayName: "all",
|
|
15
|
+
VM_OPTIONS: {
|
|
16
|
+
randomizeOpcodes: true,
|
|
17
|
+
shuffleOpcodes: true,
|
|
18
|
+
encodeBytecode: true,
|
|
19
|
+
selfModifying: true,
|
|
20
|
+
timingChecks: true,
|
|
21
|
+
macroOpcodes: true,
|
|
22
|
+
specializedOpcodes: true,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
projects: OPTIONS_MATRIX.map(({ displayName, VM_OPTIONS }) => ({
|
|
29
|
+
displayName,
|
|
30
|
+
extensionsToTreatAsEsm: [".ts"],
|
|
31
|
+
moduleFileExtensions: ["ts", "js", "json"],
|
|
32
|
+
transform: { "\\.ts$": "./jest-strip-types.js" },
|
|
33
|
+
globals: { VM_OPTIONS },
|
|
34
|
+
})),
|
|
35
|
+
};
|
package/package.json
CHANGED
|
@@ -1,48 +1,49 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "js-confuser-vm",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"main": "dist/index.js",
|
|
5
|
-
"scripts": {
|
|
6
|
-
"build": "babel src --out-dir dist --extensions
|
|
7
|
-
"index": "NODE_OPTIONS=\"--disable-warning=ExperimentalWarning\" node index.ts",
|
|
8
|
-
"test": "NODE_OPTIONS=\"--experimental-vm-modules --experimental-strip-types --disable-warning=ExperimentalWarning\" jest --coverage --coverageReporters=html --selectProjects default",
|
|
9
|
-
"test-all": "NODE_OPTIONS=\"--experimental-vm-modules --experimental-strip-types --disable-warning=ExperimentalWarning\" jest --coverage --coverageReporters=html",
|
|
10
|
-
"test262": "NODE_OPTIONS=\"--experimental-vm-modules --experimental-strip-types --disable-warning=ExperimentalWarning\" node test262-scripts/run-test262.ts",
|
|
11
|
-
"prepublishOnly": "npm run build && npm run test"
|
|
12
|
-
},
|
|
13
|
-
"type": "module",
|
|
14
|
-
"keywords": [
|
|
15
|
-
"vm",
|
|
16
|
-
"virtual machine",
|
|
17
|
-
"obfuscator",
|
|
18
|
-
"obfuscation",
|
|
19
|
-
"uglify",
|
|
20
|
-
"code protection",
|
|
21
|
-
"javascript obfuscator",
|
|
22
|
-
"js obfuscator"
|
|
23
|
-
],
|
|
24
|
-
"author": "MichaelXF",
|
|
25
|
-
"license": "MIT",
|
|
26
|
-
"description": "",
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"@babel/generator": "^7.29.1",
|
|
29
|
-
"@babel/parser": "^7.29.0",
|
|
30
|
-
"@babel/traverse": "^7.29.0",
|
|
31
|
-
"@babel/types": "^7.29.0",
|
|
32
|
-
"google-closure-compiler": "^20260216.0.0"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"@babel/
|
|
37
|
-
"@babel/
|
|
38
|
-
"@babel/preset-
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
"babel-plugin-
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "js-confuser-vm",
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "babel src --out-dir dist --extensions \".ts,.js\"",
|
|
7
|
+
"index": "cross-env NODE_OPTIONS=\"--disable-warning=ExperimentalWarning\" node index.ts",
|
|
8
|
+
"test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --experimental-strip-types --disable-warning=ExperimentalWarning\" jest --coverage --coverageReporters=html --selectProjects default",
|
|
9
|
+
"test-all": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --experimental-strip-types --disable-warning=ExperimentalWarning\" jest --coverage --coverageReporters=html",
|
|
10
|
+
"test262": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --experimental-strip-types --disable-warning=ExperimentalWarning\" node test262-scripts/run-test262.ts",
|
|
11
|
+
"prepublishOnly": "npm run build && npm run test"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"vm",
|
|
16
|
+
"virtual machine",
|
|
17
|
+
"obfuscator",
|
|
18
|
+
"obfuscation",
|
|
19
|
+
"uglify",
|
|
20
|
+
"code protection",
|
|
21
|
+
"javascript obfuscator",
|
|
22
|
+
"js obfuscator"
|
|
23
|
+
],
|
|
24
|
+
"author": "MichaelXF",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"description": "",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@babel/generator": "^7.29.1",
|
|
29
|
+
"@babel/parser": "^7.29.0",
|
|
30
|
+
"@babel/traverse": "^7.29.0",
|
|
31
|
+
"@babel/types": "^7.29.0",
|
|
32
|
+
"google-closure-compiler": "^20260216.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@babel/cli": "^7.28.6",
|
|
36
|
+
"@babel/core": "^7.29.0",
|
|
37
|
+
"@babel/preset-env": "^7.29.0",
|
|
38
|
+
"@babel/preset-typescript": "^7.28.5",
|
|
39
|
+
"@types/node": "^25.3.0",
|
|
40
|
+
"babel-plugin-module-resolver": "^5.0.2",
|
|
41
|
+
"babel-plugin-replace-import-extension": "^1.1.5",
|
|
42
|
+
"cross-env": "^10.1.0",
|
|
43
|
+
"glob": "^13.0.6",
|
|
44
|
+
"jest": "^30.2.0"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.0.0"
|
|
48
|
+
}
|
|
49
|
+
}
|