simple-javascript-obf 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 (43) hide show
  1. package/.github/workflows/node.js.yml +31 -0
  2. package/LICENSE +21 -0
  3. package/README.md +92 -0
  4. package/THIRD_PARTY_NOTICES.md +82 -0
  5. package/bin/js-obf +228 -0
  6. package/bin/obf.sh +257 -0
  7. package/package.json +26 -0
  8. package/src/index.js +106 -0
  9. package/src/options.js +128 -0
  10. package/src/pipeline.js +56 -0
  11. package/src/plugins/antiHook.js +123 -0
  12. package/src/plugins/controlFlowFlatten.js +203 -0
  13. package/src/plugins/deadCode.js +82 -0
  14. package/src/plugins/encodeMembers.js +44 -0
  15. package/src/plugins/entry.js +31 -0
  16. package/src/plugins/rename.js +100 -0
  17. package/src/plugins/stringEncode.js +494 -0
  18. package/src/plugins/vm/ast-utils.js +58 -0
  19. package/src/plugins/vm/compiler.js +113 -0
  20. package/src/plugins/vm/constants.js +72 -0
  21. package/src/plugins/vm/emit.js +916 -0
  22. package/src/plugins/vm/encoding.js +252 -0
  23. package/src/plugins/vm/index.js +366 -0
  24. package/src/plugins/vm/mapping.js +24 -0
  25. package/src/plugins/vm/normalize.js +692 -0
  26. package/src/plugins/vm/runtime.js +1145 -0
  27. package/src/plugins/vm.js +1 -0
  28. package/src/utils/names.js +55 -0
  29. package/src/utils/reserved.js +57 -0
  30. package/src/utils/rng.js +55 -0
  31. package/src/utils/stream.js +97 -0
  32. package/src/utils/string.js +13 -0
  33. package/test/bench-runner.js +78 -0
  34. package/test/benchmark-source.js +35 -0
  35. package/test/benchmark-vm.js +160 -0
  36. package/test/dist/bench.obf.js +1 -0
  37. package/test/dist/bench.original.js +35 -0
  38. package/test/dist/bench.vm.js +1 -0
  39. package/test/dist/sample-input.obf.js +1 -0
  40. package/test/dist/sample-input.vm.js +1 -0
  41. package/test/generate-obf.js +38 -0
  42. package/test/obf-smoke.js +129 -0
  43. package/test/sample-input.js +23 -0
@@ -0,0 +1,31 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ "main" ]
9
+ pull_request:
10
+ branches: [ "main" ]
11
+
12
+ jobs:
13
+ build:
14
+
15
+ runs-on: ubuntu-latest
16
+
17
+ strategy:
18
+ matrix:
19
+ node-version: [18.x, 20.x, 22.x]
20
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
+
22
+ steps:
23
+ - uses: actions/checkout@v4
24
+ - name: Use Node.js ${{ matrix.node-version }}
25
+ uses: actions/setup-node@v4
26
+ with:
27
+ node-version: ${{ matrix.node-version }}
28
+ cache: 'npm'
29
+ - run: npm ci
30
+ - run: npm run build --if-present
31
+ - run: npm test
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 js-obf contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # js-obf
2
+
3
+ A modular JavaScript obfuscation CLI with control-flow flattening (CFF) and optional VM virtualization.
4
+
5
+ ## Features
6
+ - CLI input/output for Node.js and browser-targeted bundles
7
+ - AST-level obfuscation: variable renaming, string encryption (Base64 + custom stream cipher / polymorphic variants), dead-code injection, control-flow flattening
8
+ - Always runs Terser compression on output
9
+ - Optional VM virtualization (covers common syntax; see limitations)
10
+ - Optional anti-hook runtime guard (detects tampering of common built-ins)
11
+ - VM opcode mapping randomization and mask obfuscation, fake opcode injection (configurable via `vm.opcodeShuffle` / `vm.fakeOpcodes`)
12
+ - VM bytecode runtime decryption (can be disabled via `vm.bytecodeEncrypt`)
13
+ - VM const pool runtime decryption (can be disabled via `vm.constsEncrypt`)
14
+ - Highly modular plugin architecture for easy extension
15
+
16
+ ## Install
17
+ ```
18
+ npm install
19
+ ```
20
+
21
+ ## Usage
22
+ ```
23
+ node bin/js-obf input.js -o output.js --preset high --vm --seed my-seed
24
+ ```
25
+
26
+ Supports stdin (use `-` as input):
27
+ ```
28
+ cat input.js | node bin/js-obf - -o output.js
29
+ ```
30
+
31
+ ### Interactive script
32
+ Use the interactive script to choose features and specify input file/directory and output directory:
33
+ ```
34
+ bash bin/obf.sh
35
+ ```
36
+ If an input directory is provided, it will recursively obfuscate all `.js` files and overwrite them, automatically ignoring `node_modules` (also tolerates common misspellings of that directory name).
37
+
38
+ ## CLI options
39
+ - `-o, --output <file>` Output file
40
+ - `--preset <high|balanced|low>` Preset strength (default `high`)
41
+ - `--no-rename` Disable variable renaming
42
+ - `--no-strings` Disable string encryption
43
+ - `--no-cff` Disable control-flow flattening
44
+ - `--no-dead` Disable dead-code injection
45
+ - `--vm` Enable VM virtualization (see limitations)
46
+ - `--vm-include name1,name2` Only virtualize the specified function names
47
+ - `--vm-opcode-shuffle` Enable VM opcode random mapping (default on)
48
+ - `--no-vm-opcode-shuffle` Disable VM opcode random mapping
49
+ - `--vm-fake-opcodes <0-1>` Fake opcode injection probability (default 0.15)
50
+ - `--vm-bytecode` Enable VM bytecode runtime decryption (default on)
51
+ - `--no-vm-bytecode` Disable VM bytecode runtime decryption
52
+ - `--vm-consts` Enable VM const pool runtime decryption (default on)
53
+ - `--no-vm-consts` Disable VM const pool runtime decryption
54
+ - `--vm-downlevel` Allow VM to downlevel `let/const` to `var`
55
+ - `--anti-hook` Enable anti-hook runtime guard
56
+ - `--anti-hook-lock` Enable anti-hook and freeze built-in prototype chains
57
+ - `--seed <value>` PRNG seed
58
+ - `--ecma <version>` Terser output ECMAScript version (default 2015)
59
+ - `--sourcemap` Emit source map
60
+ - `--compact` Compact output
61
+
62
+ Default output is ES2015 to preserve `let/const`. For ES5, set `--ecma 5`.
63
+
64
+ ## VM virtualization coverage
65
+ VM supports most common syntax while keeping performance and memory overhead reasonable:
66
+ - Control flow: `if/else`, `for/while/do-while`, `switch`, `break/continue`
67
+ - Exceptions: `try/catch/finally` (supports `throw`)
68
+ - `async/await` (only inside `async` functions)
69
+ - Variables: `var` (`let/const` are skipped by default; use `--vm-downlevel` to force)
70
+ - Parameters: defaults, rest params, parameter destructuring (object/array)
71
+ - Expressions: literals, member access, function calls, object/array literals, `new`, template strings, etc.
72
+
73
+ ### VM limitations
74
+ - Generator function bodies are not virtualized (functions containing `yield` are skipped, but you can declare generators inside normal functions)
75
+ - Optional chaining / nullish coalescing (`?.` / `??`) not supported
76
+ - `spread` (call/array/object) and object rest destructuring not supported
77
+ - Destructuring assignment expressions are not supported (only parameter/variable declaration destructuring)
78
+ - Complex or unsupported nodes are skipped for that function
79
+ - VM uses the `Function` constructor to create closures; CSP-restricted environments may not run
80
+
81
+ ## Structure
82
+ - `src/index.js`: core API
83
+ - `src/pipeline.js`: plugin pipeline
84
+ - `src/plugins/*`: obfuscation plugins
85
+ - `src/utils/*`: utilities and RNG
86
+ - `bin/js-obf`: CLI entry
87
+
88
+ ## License
89
+ MIT License. See `LICENSE`.
90
+
91
+ ## Third-Party Notices
92
+ Some dependencies require preserving author and license notices. See `THIRD_PARTY_NOTICES.md`.
@@ -0,0 +1,82 @@
1
+ # Third-Party Notices
2
+
3
+ This project depends on third-party packages that require preservation of
4
+ copyright notices and license terms. The following notices are provided to
5
+ comply with those requirements.
6
+
7
+ ## Babel packages (@babel/generator, @babel/traverse, @babel/types)
8
+
9
+ MIT License
10
+
11
+ Copyright (c) 2014-present Sebastian McKenzie and other contributors
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining
14
+ a copy of this software and associated documentation files (the
15
+ "Software"), to deal in the Software without restriction, including
16
+ without limitation the rights to use, copy, modify, merge, publish,
17
+ distribute, sublicense, and/or sell copies of the Software, and to
18
+ permit persons to whom the Software is furnished to do so, subject to
19
+ the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be
22
+ included in all copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+
32
+ ## @babel/parser
33
+
34
+ Copyright (C) 2012-2014 by various contributors (see AUTHORS)
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining a copy
37
+ of this software and associated documentation files (the "Software"), to deal
38
+ in the Software without restriction, including without limitation the rights
39
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40
+ copies of the Software, and to permit persons to whom the Software is
41
+ furnished to do so, subject to the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be included in
44
+ all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52
+ THE SOFTWARE.
53
+
54
+ ## terser
55
+
56
+ Copyright 2012-2018 (c) Mihai Bazon <mihai.bazon@gmail.com>
57
+
58
+ Redistribution and use in source and binary forms, with or without
59
+ modification, are permitted provided that the following conditions
60
+ are met:
61
+
62
+ * Redistributions of source code must retain the above
63
+ copyright notice, this list of conditions and the following
64
+ disclaimer.
65
+
66
+ * Redistributions in binary form must reproduce the above
67
+ copyright notice, this list of conditions and the following
68
+ disclaimer in the documentation and/or other materials
69
+ provided with the distribution.
70
+
71
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
72
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
74
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
75
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
76
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
77
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
78
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
79
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
80
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
81
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82
+ SUCH DAMAGE.
package/bin/js-obf ADDED
@@ -0,0 +1,228 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { obfuscate } = require("../src/index");
6
+
7
+ function printHelp() {
8
+ const text = `
9
+ Usage:
10
+ js-obf <input.js | -> [options]
11
+
12
+ Options:
13
+ -o, --output <file> Output file
14
+ --preset <high|balanced|low> Preset strength (default: high)
15
+ --no-rename Disable identifier renaming
16
+ --no-strings Disable string encoding
17
+ --no-cff Disable control flow flattening
18
+ --no-dead Disable dead code injection
19
+ --vm Enable VM virtualization (see README limits)
20
+ --vm-include <a,b,c> Only virtualize specified function names
21
+ --vm-opcode-shuffle Enable VM opcode shuffle (default: on)
22
+ --no-vm-opcode-shuffle Disable VM opcode shuffle
23
+ --vm-fake-opcodes <0-1> Fake opcode insertion probability (default: 0.15)
24
+ --vm-bytecode Encrypt VM bytecode at runtime (default: on)
25
+ --no-vm-bytecode Disable VM bytecode encryption
26
+ --vm-consts Encrypt VM const pool at runtime (default: on)
27
+ --no-vm-consts Disable VM const pool encryption
28
+ --vm-downlevel Allow VM to downlevel let/const to var
29
+ --anti-hook Enable anti-hook runtime guard
30
+ --anti-hook-lock Freeze built-in prototypes after guard
31
+ --seed <value> RNG seed
32
+ --ecma <version> Terser output ECMAScript version (default: 2015)
33
+ --sourcemap Emit source map
34
+ --compact Compact output
35
+ -h, --help Show help
36
+ `;
37
+ process.stdout.write(text);
38
+ }
39
+
40
+ function parseArgs(argv) {
41
+ const args = argv.slice(2);
42
+ const options = {
43
+ preset: "high",
44
+ };
45
+ const positional = [];
46
+
47
+ for (let i = 0; i < args.length; i += 1) {
48
+ const arg = args[i];
49
+ if (arg === "-h" || arg === "--help") {
50
+ options.help = true;
51
+ continue;
52
+ }
53
+ if (arg === "-o" || arg === "--output") {
54
+ options.output = args[i + 1];
55
+ i += 1;
56
+ continue;
57
+ }
58
+ if (arg === "--preset") {
59
+ options.preset = args[i + 1] || "high";
60
+ i += 1;
61
+ continue;
62
+ }
63
+ if (arg === "--seed") {
64
+ options.seed = args[i + 1];
65
+ i += 1;
66
+ continue;
67
+ }
68
+ if (arg === "--ecma") {
69
+ options.ecma = args[i + 1];
70
+ i += 1;
71
+ continue;
72
+ }
73
+ if (arg === "--sourcemap") {
74
+ options.sourcemap = true;
75
+ continue;
76
+ }
77
+ if (arg === "--compact") {
78
+ options.compact = true;
79
+ continue;
80
+ }
81
+ if (arg === "--vm") {
82
+ options.vm = true;
83
+ continue;
84
+ }
85
+ if (arg === "--vm-include") {
86
+ options.vmInclude = (args[i + 1] || "").split(",").filter(Boolean);
87
+ i += 1;
88
+ continue;
89
+ }
90
+ if (arg === "--vm-opcode-shuffle") {
91
+ options.vmOpcodeShuffle = true;
92
+ continue;
93
+ }
94
+ if (arg === "--no-vm-opcode-shuffle") {
95
+ options.vmOpcodeShuffle = false;
96
+ continue;
97
+ }
98
+ if (arg === "--vm-fake-opcodes") {
99
+ options.vmFakeOpcodes = args[i + 1];
100
+ i += 1;
101
+ continue;
102
+ }
103
+ if (arg === "--vm-bytecode") {
104
+ options.vmBytecode = true;
105
+ continue;
106
+ }
107
+ if (arg === "--no-vm-bytecode") {
108
+ options.vmBytecode = false;
109
+ continue;
110
+ }
111
+ if (arg === "--vm-consts") {
112
+ options.vmConsts = true;
113
+ continue;
114
+ }
115
+ if (arg === "--no-vm-consts") {
116
+ options.vmConsts = false;
117
+ continue;
118
+ }
119
+ if (arg === "--vm-downlevel") {
120
+ options.vmDownlevel = true;
121
+ continue;
122
+ }
123
+ if (arg === "--no-rename") {
124
+ options.rename = false;
125
+ continue;
126
+ }
127
+ if (arg === "--no-strings") {
128
+ options.strings = false;
129
+ continue;
130
+ }
131
+ if (arg === "--no-cff") {
132
+ options.cff = false;
133
+ continue;
134
+ }
135
+ if (arg === "--no-dead") {
136
+ options.dead = false;
137
+ continue;
138
+ }
139
+ if (arg === "--anti-hook") {
140
+ options.antiHook = true;
141
+ continue;
142
+ }
143
+ if (arg === "--anti-hook-lock") {
144
+ options.antiHook = true;
145
+ options.antiHookLock = true;
146
+ continue;
147
+ }
148
+ positional.push(arg);
149
+ }
150
+
151
+ return { options, positional };
152
+ }
153
+
154
+ function readInput(file) {
155
+ if (!file || file === "-") {
156
+ return fs.readFileSync(0, "utf8");
157
+ }
158
+ return fs.readFileSync(file, "utf8");
159
+ }
160
+
161
+ function writeOutput(file, code) {
162
+ if (!file) {
163
+ process.stdout.write(code);
164
+ return;
165
+ }
166
+ fs.writeFileSync(file, code, "utf8");
167
+ }
168
+
169
+ async function main() {
170
+ const { options, positional } = parseArgs(process.argv);
171
+ if (options.help || positional.length === 0) {
172
+ printHelp();
173
+ process.exit(options.help ? 0 : 1);
174
+ }
175
+
176
+ const inputPath = positional[0];
177
+ const source = readInput(inputPath);
178
+ const outputPath = options.output
179
+ ? options.output
180
+ : inputPath === "-"
181
+ ? null
182
+ : path.join(
183
+ path.dirname(inputPath),
184
+ `${path.basename(inputPath, path.extname(inputPath))}.obf.js`
185
+ );
186
+
187
+ const result = await obfuscate(source, {
188
+ preset: options.preset,
189
+ rename: options.rename,
190
+ strings: options.strings,
191
+ cff: options.cff,
192
+ dead: options.dead,
193
+ antiHook: options.antiHook
194
+ ? {
195
+ enabled: true,
196
+ lock: Boolean(options.antiHookLock),
197
+ }
198
+ : undefined,
199
+ vm: {
200
+ enabled: Boolean(options.vm),
201
+ include: options.vmInclude,
202
+ opcodeShuffle: options.vmOpcodeShuffle,
203
+ fakeOpcodes: options.vmFakeOpcodes,
204
+ bytecodeEncrypt: options.vmBytecode,
205
+ constsEncrypt: options.vmConsts,
206
+ downlevel: options.vmDownlevel,
207
+ },
208
+ seed: options.seed,
209
+ ecma: options.ecma,
210
+ sourceMap: options.sourcemap,
211
+ compact: options.compact,
212
+ filename: inputPath === "-" ? "stdin.js" : inputPath,
213
+ });
214
+
215
+ let outputCode = result.code;
216
+ if (result.map && outputPath) {
217
+ const mapPath = `${outputPath}.map`;
218
+ fs.writeFileSync(mapPath, JSON.stringify(result.map), "utf8");
219
+ outputCode += `\n//# sourceMappingURL=${path.basename(mapPath)}\n`;
220
+ }
221
+
222
+ writeOutput(outputPath, outputCode);
223
+ }
224
+
225
+ main().catch((err) => {
226
+ process.stderr.write(`${err && err.stack ? err.stack : err}\n`);
227
+ process.exit(1);
228
+ });