js-confuser-vm 0.1.0 → 0.1.2

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 (63) hide show
  1. package/README.md +281 -147
  2. package/dist/build-runtime.js +41 -15
  3. package/dist/compiler.js +714 -265
  4. package/dist/disassembler.js +367 -0
  5. package/dist/index.js +7 -2
  6. package/dist/runtime.js +160 -119
  7. package/dist/template.js +163 -42
  8. package/dist/transforms/bytecode/aliasedOpcodes.js +4 -1
  9. package/dist/transforms/bytecode/concealConstants.js +2 -2
  10. package/dist/transforms/bytecode/controlFlowFlattening.js +569 -0
  11. package/dist/transforms/bytecode/dispatcher.js +15 -111
  12. package/dist/transforms/bytecode/macroOpcodes.js +2 -2
  13. package/{src/transforms/bytecode/resolveContants.ts → dist/transforms/bytecode/resolveConstants.js} +30 -56
  14. package/dist/transforms/bytecode/resolveRegisters.js +23 -4
  15. package/dist/transforms/bytecode/selfModifying.js +88 -21
  16. package/dist/transforms/bytecode/semanticOpcodes.js +162 -0
  17. package/dist/transforms/bytecode/specializedOpcodes.js +23 -12
  18. package/dist/transforms/bytecode/stringConcealing.js +288 -0
  19. package/dist/transforms/runtime/classObfuscation.js +43 -0
  20. package/dist/transforms/runtime/handlerTable.js +91 -0
  21. package/dist/transforms/runtime/semanticOpcodes.js +35 -0
  22. package/dist/transforms/runtime/specializedOpcodes.js +11 -5
  23. package/dist/types.js +1 -1
  24. package/dist/utils/ast-utils.js +75 -0
  25. package/dist/utils/op-utils.js +1 -2
  26. package/dist/utils/pass-utils.js +100 -0
  27. package/dist/utils/profile-utils.js +3 -0
  28. package/package.json +8 -1
  29. package/.gitmodules +0 -4
  30. package/.prettierignore +0 -1
  31. package/CHANGELOG.md +0 -335
  32. package/babel-plugin-inline-runtime.cjs +0 -34
  33. package/babel.config.json +0 -23
  34. package/index.ts +0 -38
  35. package/jest-strip-types.js +0 -10
  36. package/jest.config.js +0 -52
  37. package/src/build-runtime.ts +0 -78
  38. package/src/compiler.ts +0 -2593
  39. package/src/index.ts +0 -14
  40. package/src/minify.ts +0 -21
  41. package/src/options.ts +0 -18
  42. package/src/runtime.ts +0 -923
  43. package/src/template.ts +0 -141
  44. package/src/transforms/bytecode/aliasedOpcodes.ts +0 -148
  45. package/src/transforms/bytecode/concealConstants.ts +0 -52
  46. package/src/transforms/bytecode/dispatcher.ts +0 -398
  47. package/src/transforms/bytecode/macroOpcodes.ts +0 -193
  48. package/src/transforms/bytecode/microOpcodes.ts +0 -291
  49. package/src/transforms/bytecode/resolveLabels.ts +0 -112
  50. package/src/transforms/bytecode/resolveRegisters.ts +0 -221
  51. package/src/transforms/bytecode/selfModifying.ts +0 -121
  52. package/src/transforms/bytecode/specializedOpcodes.ts +0 -153
  53. package/src/transforms/runtime/aliasedOpcodes.ts +0 -191
  54. package/src/transforms/runtime/internalVariables.ts +0 -270
  55. package/src/transforms/runtime/macroOpcodes.ts +0 -138
  56. package/src/transforms/runtime/microOpcodes.ts +0 -93
  57. package/src/transforms/runtime/minify.ts +0 -1
  58. package/src/transforms/runtime/shuffleOpcodes.ts +0 -24
  59. package/src/transforms/runtime/specializedOpcodes.ts +0 -156
  60. package/src/types.ts +0 -93
  61. package/src/utils/op-utils.ts +0 -48
  62. package/src/utils/random-utils.ts +0 -31
  63. package/tsconfig.json +0 -12
@@ -1,34 +0,0 @@
1
- const { readFileSync } = require("fs");
2
- const { join } = require("path");
3
- const babel = require("@babel/core");
4
- const { stripTypeScriptTypes } = require("node:module");
5
-
6
- module.exports = function inlineRuntimePlugin({ types: t }) {
7
- const rawContent = readFileSync(join(__dirname, "./src/runtime.ts"), "utf-8");
8
-
9
- const runtimeContent = stripTypeScriptTypes(rawContent);
10
-
11
- return {
12
- name: "inline-runtime",
13
- visitor: {
14
- VariableDeclarator(path) {
15
- if (
16
- path.node.id?.name === "readVMRuntimeFile" &&
17
- (path.node.init?.type === "ArrowFunctionExpression" ||
18
- path.node.init?.type === "FunctionExpression")
19
- ) {
20
- path.node.init = t.arrowFunctionExpression(
21
- [],
22
- t.stringLiteral(runtimeContent),
23
- );
24
- }
25
- },
26
- ImportDeclaration(path) {
27
- const src = path.node.source.value;
28
- if (src === "fs" || src === "path") {
29
- path.remove();
30
- }
31
- },
32
- },
33
- };
34
- };
package/babel.config.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "presets": [
3
- [
4
- "@babel/preset-env",
5
- {
6
- "targets": {
7
- "node": "18"
8
- },
9
- "modules": false
10
- }
11
- ],
12
- ["@babel/preset-typescript"]
13
- ],
14
- "plugins": [
15
- "./babel-plugin-inline-runtime.cjs",
16
- [
17
- "replace-import-extension",
18
- {
19
- "extMapping": { ".ts": ".js" }
20
- }
21
- ]
22
- ]
23
- }
package/index.ts DELETED
@@ -1,38 +0,0 @@
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 the bytecode array?
15
- concealConstants: true, // conceal strings and integers in the constant pool?
16
- dispatcher: true, // create middleman blocks to process jumps?
17
- selfModifying: true, // do self-modifying bytecode for function bodies?
18
- macroOpcodes: true, // create combined opcodes for repeated instruction sequences?
19
- microOpcodes: true, // break opcodes into sub-opcodes?
20
- specializedOpcodes: true, // create specialized opcodes for commonly used opcode+operand pairs?
21
- aliasedOpcodes: true, // create duplicate opcodes for commonly used opcodes?
22
- timingChecks: true, // add timing checks to detect debuggers?
23
- minify: true, // pass final output through Google Closure Compiler? (Renames VM class properties)
24
- });
25
-
26
- writeFileSync("output.original.js", orginalOutput, "utf-8");
27
- writeFileSync("output.js", output, "utf-8");
28
-
29
- // Eval the code like our test suite does
30
- var window = { TEST_OUTPUT: null };
31
- eval(output);
32
- console.log(window.TEST_OUTPUT);
33
-
34
- // Minify using Google Closure Compiler (optional)
35
- // import("./minify.js");
36
- }
37
-
38
- main();
@@ -1,10 +0,0 @@
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 DELETED
@@ -1,52 +0,0 @@
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
- { displayName: "microOpcodes", VM_OPTIONS: { microOpcodes: true } },
10
- {
11
- displayName: "specializedOpcodes",
12
- VM_OPTIONS: { specializedOpcodes: true },
13
- },
14
- {
15
- displayName: "aliasedOpcodes",
16
- VM_OPTIONS: { aliasedOpcodes: true },
17
- },
18
- {
19
- displayName: "concealConstants",
20
- VM_OPTIONS: { concealConstants: true },
21
- },
22
- {
23
- displayName: "dispatcher",
24
- VM_OPTIONS: { dispatcher: true },
25
- },
26
- {
27
- displayName: "all",
28
- VM_OPTIONS: {
29
- randomizeOpcodes: true,
30
- shuffleOpcodes: true,
31
- encodeBytecode: true,
32
- selfModifying: true,
33
- timingChecks: true,
34
- macroOpcodes: true,
35
- microOpcodes: true,
36
- specializedOpcodes: true,
37
- aliasedOpcodes: true,
38
- concealConstants: true,
39
- dispatcher: true,
40
- },
41
- },
42
- ];
43
-
44
- export default {
45
- projects: OPTIONS_MATRIX.map(({ displayName, VM_OPTIONS }) => ({
46
- displayName,
47
- extensionsToTreatAsEsm: [".ts"],
48
- moduleFileExtensions: ["ts", "js", "json"],
49
- transform: { "\\.ts$": "./jest-strip-types.js" },
50
- globals: { VM_OPTIONS },
51
- })),
52
- };
@@ -1,78 +0,0 @@
1
- import { generate } from "@babel/generator";
2
- import { parse } from "@babel/parser";
3
- import type * as t from "@babel/types";
4
- import type { Options } from "./options.ts";
5
- import { applyMacroOpcodes } from "./transforms/runtime/macroOpcodes.ts";
6
- import { applyMicroOpcodes } from "./transforms/runtime/microOpcodes.ts";
7
- import { applyInteralVariablesToRuntime } from "./transforms/runtime/internalVariables.ts";
8
- import { applyShuffleOpcodes } from "./transforms/runtime/shuffleOpcodes.ts";
9
- import { applyMinify } from "./transforms/runtime/minify.ts";
10
- import { Compiler } from "./compiler.ts";
11
- import { applySpecializedOpcodes } from "./transforms/runtime/specializedOpcodes.ts";
12
- import { applyAliasedOpcodes } from "./transforms/runtime/aliasedOpcodes.ts";
13
- import type * as b from "./types.ts";
14
-
15
- export async function obfuscateRuntime(
16
- runtime: string,
17
- bytecode: b.Bytecode,
18
- options: Options,
19
- compiler: Compiler,
20
- generateBytecodeComment,
21
- ) {
22
- let ast: t.File;
23
- try {
24
- ast = parse(runtime, { sourceType: "unambiguous" });
25
- } catch (error) {
26
- throw new Error("VM-Runtime final parsing failed", { cause: error });
27
- }
28
-
29
- // Specialized opcode cases must be applied BEFORE shuffleOpcodes
30
- if (options.specializedOpcodes) {
31
- applySpecializedOpcodes(ast, compiler);
32
- }
33
-
34
- if (options.microOpcodes) {
35
- applyInteralVariablesToRuntime(ast, compiler);
36
- }
37
-
38
- // Micro opcode cases must be applied BEFORE shuffleOpcodes
39
- if (options.microOpcodes && Object.keys(compiler.MICRO_OPS).length > 0) {
40
- applyMicroOpcodes(ast, compiler);
41
- }
42
-
43
- // Macro opcode cases must be applied BEFORE shuffleOpcodes
44
- if (options.macroOpcodes && Object.keys(compiler.MACRO_OPS).length > 0) {
45
- applyMacroOpcodes(ast, compiler);
46
- }
47
-
48
- // Aliased opcode cases must be applied BEFORE shuffleOpcodes
49
- if (options.aliasedOpcodes) {
50
- applyAliasedOpcodes(ast, compiler);
51
- }
52
-
53
- // Shuffle opcode handle order
54
- if (options.shuffleOpcodes) {
55
- applyShuffleOpcodes(ast);
56
- }
57
-
58
- let generated: string;
59
- try {
60
- generated = generate(ast).code;
61
- } catch (error) {
62
- throw new Error("VM-Runtime final generation failed", { cause: error });
63
- }
64
-
65
- // Add comment here for more accurate opcode names
66
- generated = generateBytecodeComment() + "\n" + generated;
67
-
68
- // Minify code?
69
- if (options.minify) {
70
- try {
71
- generated = await applyMinify(generated);
72
- } catch (error) {
73
- throw new Error("VM-Runtime final minification failed", { cause: error });
74
- }
75
- }
76
-
77
- return generated;
78
- }