diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 635547b33..9f85a9b02 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,7 +47,20 @@ jobs: - run: yarn run test:mocha-coverage - run: yarn run test:mocha-coverage:report - name: Coveralls - uses: coverallsapp/github-action@master + uses: coverallsapp/github-action@v2 with: github-token: ${{ secrets.GITHUB_TOKEN }} - path-to-lcov: './coverage/lcov.info' \ No newline at end of file + path-to-lcov: './coverage/lcov.info' + parallel: true + flag-name: node-${{ matrix.node-version }}-${{ matrix.os }} + + coveralls-finish: + needs: build + if: always() + runs-on: ubuntu-latest + steps: + - name: Coveralls Finished + uses: coverallsapp/github-action@v2 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + parallel-finished: true \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 670232a2f..839cef024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,38 @@ Change Log +v5.4.2 +--- +* Fixed obfuscated code hanging in Bun when `selfDefending` is enabled. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1404 + +v5.4.1 +--- +* Fixed `Utils.nodeRequire` causing `ReferenceError: require is not defined` in browser build by making it lazy-evaluated +* Fixed missing space between keywords (`return`, `throw`, `typeof`) and Unicode surrogate pair identifiers in compact mode. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1112 +* Fixed `domainLock` being case-sensitive — domain values are now normalized to lowercase. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1182 +* Removed `source-map-support` runtime dependency. Use `node --enable-source-maps` instead. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1149 + +v5.4.0 +--- +* Add support for `import attributes`. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1256 +* Add `renameProperties` support for private class fields and methods (`#foo`, `#bar()`). Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1220 +* Fixed `reservedNames` not preserving class method and property names when `stringArray` or `deadCodeInjection` is enabled. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1279 +* Fixed infinite loop / stack overflow when `reservedNames` patterns match all generated identifier names. Now throws a descriptive error instead. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1382 +* Fixed `transformObjectKeys` changing evaluation order when object expression is inside a sequence expression with preceding side effects (e.g. `return aux(ys), { min }`). Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1246 +* Fixed destructuring patterns inside class static blocks not being renamed when `renameGlobals` is disabled. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1141 +* Fixed CLI `--options-preset` not applying preset values for options not explicitly set via command line (e.g. `splitStrings` from `high-obfuscation` preset was ignored). Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1236 +* Replaced `mkdirp` dependency with native `fs.mkdirSync({ recursive: true })`. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1275. Thank you https://github.com/roli-lpci! +* Updated reserved DOM properties list, fixing `renameProperties` breaking modern built-in methods like `Array.prototype.at()`. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1066 +* Replaced `conf` dependency with custom implementation using `env-paths` and native `fs` + +v5.3.1 +--- +* Fixed class expression name references inside class body being incorrectly resolved to an import binding with the same name, causing broken code at runtime. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1386 + +v5.3.0 +--- +* Add Pro API support to CLI +* Add large files upload support to Pro API + v5.2.1 --- * Fixed `transformObjectKeys` incorrectly hoisting object literal outside of loop when loop body is a single statement without braces, causing all iterations to share the same object reference. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1300 diff --git a/README.md b/README.md index 128eaba72..937bc642a 100644 --- a/README.md +++ b/README.md @@ -15,18 +15,32 @@ Huge thanks to all supporters! --- -### :rocket: JavaScript Obfuscator Pro with VM Obfuscation is out! +### :rocket: Obfuscator.io with VM Obfuscation -**JavaScript Obfuscator Pro** features **VM-based bytecode obfuscation** — the most advanced code protection available. Your JavaScript functions are transformed into custom bytecode running on an embedded virtual machine, making reverse engineering extremely difficult. +**Obfuscator.io** adds **VM-based bytecode obfuscation** to this package - your JavaScript functions are compiled to custom bytecode that runs on an embedded virtual machine. Each build produces unique opcodes and VM structure, making reverse engineering and automated deobfuscation dramatically harder. -[Try it at obfuscator.io](https://obfuscator.io) +| Protection goal | Free (this package) | [obfuscator.io](https://obfuscator.io) | +| --- |-----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Rename identifiers | ✅ variable/function renaming | ✅ + VM-local symbols never exposed as JavaScript | +| Obscure strings | ✅ string array + base64/rc4 | ✅ + strings embedded in bytecode constants | +| Obscure control flow | ✅ control flow flattening | ✅ full bytecode virtualization, [`vmJumpsEncoding`](#vmjumpsencoding) (runtime-computed jump targets), [`vmDeadCodeInjection`](#vmdeadcodeinjection) (fake bytecode sequences) | +| Resist decompilation | ⚠️ output is still JavaScript | ✅ custom opcodes, [`vmStatefulOpcodes`](#vmstatefulopcodes) (position-dependent opcode mapping), [`vmMacroOps`](#vmmacroops) (fused instructions), [`vmDecoyOpcodes`](#vmdecoyopcodes) (fake opcode handlers) | +| Resist automated LLM-based analysis | ❌ fully vulnerable (no LLM-specific defenses) | ✅ bytecode encryption + anti-LLM defenses in [`vmSelfDefending`](#vmselfdefending) and [`vmDebugProtection`](#vmdebugProtection) | +| Encryption | ✅ [`stringArrayEncoding`](#stringarrayencoding) (base64/rc4 on extracted strings) | ✅ [`vmBytecodeEncoding`](#vmbytecodeencoding) (per-instruction encoding), [`vmBytecodeArrayEncoding`](#vmbytecodeArrayEncoding) (whole bytecode array as single block) | +| Anti-debugging | ✅ `debugProtection` (freezes browser DevTools) | ✅ [`vmDebugProtection`](#vmdebugProtection) (multi-layered anti-debugging and anti-analysis defenses) | +| Tamper detection | ✅ `selfDefending` (breaks if beautified) | ✅ [`vmSelfDefending`](#vmselfdefending) (multi-layered tamper detection, anti-hooking, anti-reverse-engineering protection) | +| Runs offline, no network | ✅ | ❌ uses obfuscator.io API (requires token) | + +[Visit Obfuscator.io](https://obfuscator.io) · [Pro API methods](#shield-pro-api-methods-vm-obfuscation) + +This package provides access to Obfuscator.io API via CLI and Node.js API. --- JavaScript Obfuscator is a powerful free obfuscator for JavaScript, containing a variety of features which provide protection for your source code. **Key features:** -- VM bytecode obfuscation (via [JavaScript Obfuscator Pro](https://obfuscator.io/)) +- VM bytecode obfuscation (via [Obfuscator.io](https://obfuscator.io/)) - variables renaming - strings extraction and encryption - dead code injection @@ -49,6 +63,7 @@ The example of obfuscated code: [github.com](https://github.com/javascript-obfus * Malta: [malta-js-obfuscator](https://github.com/fedeghe/malta-js-obfuscator) * Netlify plugin: [netlify-plugin-js-obfuscator](https://www.npmjs.com/package/netlify-plugin-js-obfuscator) * Snowpack plugin: [snowpack-javascript-obfuscator](https://www.npmjs.com/package/snowpack-javascript-obfuscator) +* Vite plugin: [vite-plugin-bundle-obfuscator](https://github.com/z0ffy/vite-plugin-bundle-obfuscator) [![npm version](https://badge.fury.io/js/javascript-obfuscator.svg)](https://badge.fury.io/js/javascript-obfuscator) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjavascript-obfuscator%2Fjavascript-obfuscator.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjavascript-obfuscator%2Fjavascript-obfuscator?ref=badge_shield) @@ -300,7 +315,6 @@ const result = await JavaScriptObfuscator.obfuscatePro( `function hello() { console.log("Hello World"); }`, { vmObfuscation: true, // Required! - vmObfuscationThreshold: 1, compact: true }, { @@ -314,17 +328,17 @@ console.log(result.getObfuscatedCode()); **Parameters:** * `sourceCode` (`string`) – source code to obfuscate -* `options` (`Object`) – obfuscation options. **Must include `vmObfuscation: true`** +* `options` (`Object`) – obfuscation options. **Must include at least one Pro feature: `vmObfuscation: true` or `parseHtml: true`** * `apiConfig` (`Object`) – Pro API configuration: * `apiToken` (`string`, required) – your API token from obfuscator.io * `timeout` (`number`, optional) – request timeout in ms (default: `300000` - 5 minutes) - * `version` (`string`, optional) – JavaScript Obfuscator Pro version to use (e.g., `'5.0.0-beta.20'`). Defaults to latest version if not specified. + * `version` (`string`, optional) – Obfuscator.io version to use (e.g., `'5.0.3'`). Defaults to latest version if not specified. * `onProgress` (`function`, optional) – callback for progress updates during obfuscation **Returns:** `Promise` **Throws:** `ApiError` if: -- `vmObfuscation` is not enabled in options +- No Pro features (`vmObfuscation` or `parseHtml`) are enabled in options - API token is invalid or expired - API request fails @@ -336,12 +350,11 @@ You can specify which obfuscator version to use via the `version` option: const result = await JavaScriptObfuscator.obfuscatePro( sourceCode, { - vmObfuscation: true, - vmObfuscationThreshold: 1 + vmObfuscation: true }, { apiToken: 'your_javascript_obfuscator_pro_api_token', - version: '5.0.0-beta.20' // Use specific version + version: '5.0.3' // Use specific version } ); ``` @@ -354,8 +367,7 @@ The API uses streaming mode to provide real-time progress updates during obfusca const result = await JavaScriptObfuscator.obfuscatePro( sourceCode, { - vmObfuscation: true, - vmObfuscationThreshold: 1 + vmObfuscation: true }, { apiToken: 'your_javascript_obfuscator_pro_api_token' @@ -367,6 +379,28 @@ const result = await JavaScriptObfuscator.obfuscatePro( ); ``` +### Checking for Pro Features + +Use `ProApiClient.hasProFeatures()` to check if options require the Pro API: + +```javascript +const { ProApiClient } = require('javascript-obfuscator'); + +const options = { vmObfuscation: true, compact: true }; + +if (ProApiClient.hasProFeatures(options)) { + // Use obfuscatePro() - requires API token + const result = await JavaScriptObfuscator.obfuscatePro(sourceCode, options, { apiToken }); +} else { + // Use regular obfuscate() - no API token needed + const result = JavaScriptObfuscator.obfuscate(sourceCode, options); +} +``` + +Pro features include: +- `vmObfuscation: true` – VM-based bytecode obfuscation +- `parseHtml: true` – HTML parsing with inline JavaScript obfuscation + ### Error Handling ```javascript @@ -383,6 +417,36 @@ try { } ``` +### CLI Usage with Pro API + +You can also use Pro API features directly from the CLI by providing your API token: + +```sh +javascript-obfuscator input.js --pro-api-token YOUR_API_TOKEN --vm-obfuscation true -o output.js +``` + +With a specific obfuscator version: + +```sh +javascript-obfuscator input.js --pro-api-token YOUR_API_TOKEN --pro-api-version 5.0.3 --vm-obfuscation true -o output.js +``` + +**CLI Options:** +- `--pro-api-token ` – Your API token from [obfuscator.io](https://obfuscator.io) +- `--pro-api-version ` – Obfuscator.io version to use (optional, defaults to latest) + +The CLI automatically detects when Pro features (`vmObfuscation` or `parseHtml`) are enabled and routes the request through the Pro API. + +### Large File Uploads + +For files larger than ~4MB, the Pro API uses client-side uploads to Vercel Blob storage. To enable this feature, install the optional `@vercel/blob` package: + +```sh +npm install @vercel/blob +``` + +Without this package, large file obfuscation will fail with an error message prompting you to install it. + --- ## CLI usage @@ -462,6 +526,8 @@ When using CLI this prefix will be added automatically. ## JavaScript Obfuscator Options +> :shield: **Looking for VM obfuscation?** Options like `vmObfuscation`, `parseHtml`, and every `vm*` option are Pro-only and require an API token from [obfuscator.io](https://obfuscator.io). Use them via the [`obfuscatePro()`](#shield-pro-api-methods-vm-obfuscation) method, or the `--pro-api-token` CLI flag — see [Pro API Methods](#shield-pro-api-methods-vm-obfuscation). + Following options are available for the JS Obfuscator: #### options: @@ -583,6 +649,37 @@ Following options are available for the JS Obfuscator: --target [browser, browser-no-eval, node] --transform-object-keys --unicode-escape-sequence + --pro-api-token + --pro-api-version + --vm-obfuscation + --vm-obfuscation-threshold + --vm-preprocess-identifiers + --vm-dynamic-opcodes + --vm-target-functions '' (comma separated) + --vm-exclude-functions '' (comma separated) + --vm-target-functions-mode [root, comment] + --vm-wrap-top-level-initializers + --vm-opcode-shuffle + --vm-bytecode-encoding + --vm-bytecode-array-encoding + --vm-bytecode-array-encoding-key + --vm-bytecode-array-encoding-key-getter + --vm-instruction-shuffle + --vm-jumps-encoding + --vm-decoy-opcodes + --vm-dead-code-injection + --vm-split-dispatcher + --vm-macro-ops + --vm-debug-protection + --vm-runtime-opcode-derivation + --vm-stateful-opcodes + --vm-stack-encoding + --vm-randomize-keys + --vm-indirect-dispatch + --vm-compact-dispatcher + --vm-bytecode-format [binary, json] + --parse-html + --strict-mode ``` @@ -1766,9 +1863,9 @@ The performance will be at a relatively normal level -## JavaScript Obfuscator Pro Options +## Obfuscator.io Pro Options -> :warning: **The following VM obfuscation/Pro options are available only via the [JavaScript Obfuscator Pro API](https://obfuscator.io/).** +> :warning: **The following VM obfuscation/Pro options are available only via the [Obfuscator.io Pro API](https://obfuscator.io/).** > > To use these options, you need a Pro API token from [obfuscator.io](https://obfuscator.io) and must call the `obfuscatePro()` method instead of `obfuscate()`. See the [Pro API Methods](#shield-pro-api-methods-vm-obfuscation) section for details. @@ -1780,18 +1877,6 @@ Enables VM-based bytecode obfuscation. When enabled, JavaScript functions are co **Example:** Your readable code like `return qty * price` becomes a list of numbers like `[0x15,0x03,0x17,...]` that only the embedded VM interpreter can execute. The original logic is no longer visible as JavaScript. -### `vmObfuscationThreshold` -Type: `number` Default: `1` - -Controls what percentage of your root-level functions get VM protection. - -### `vmPreprocessIdentifiers` -Type: `boolean` Default: `true` - -Renames all non-global identifiers to unique hexadecimal names before VM obfuscation. This eliminates variable shadowing that can cause scope resolution issues in the VM bytecode. - -**When to disable:** Only disable this if you encounter specific compatibility issues. The preprocessing step ensures correct variable resolution in complex nested scopes. - ### `vmTargetFunctions` Type: `string[]` Default: `[]` @@ -1800,8 +1885,8 @@ Specify exactly which root-level functions should get VM protection by name. **Example:** ```javascript { - vmObfuscation: true, - vmTargetFunctions: ['someFunctionName'] + vmObfuscation: true, + vmTargetFunctions: ['someFunctionName'] } ``` @@ -1815,8 +1900,8 @@ Specify root-level functions that should never get VM protection. Takes preceden **Example:** ```javascript { - vmObfuscation: true, - vmExcludeFunctions: ['someFunctionName'] + vmObfuscation: true, + vmExcludeFunctions: ['someFunctionName'] } ``` @@ -1836,28 +1921,28 @@ Controls how functions/methods are selected for VM obfuscation. ```javascript // Source code function regularFunction() { - return 'not virtualized'; + return 'not virtualized'; } /* javascript-obfuscator:vm */ function sensitiveFunction() { - return 'this will be VM-protected'; + return 'this will be VM-protected'; } function outer() { - /* javascript-obfuscator:vm */ - function nestedSensitive() { - return 'nested but still VM-protected'; - } - return nestedSensitive(); + /* javascript-obfuscator:vm */ + function nestedSensitive() { + return 'nested but still VM-protected'; + } + return nestedSensitive(); } ``` ```javascript // Obfuscator options { - vmObfuscation: true, - vmTargetFunctionsMode: 'comment' + vmObfuscation: true, + vmTargetFunctionsMode: 'comment' } ``` @@ -1900,11 +1985,6 @@ Makes the VM interpreter smaller and unique for each build. As the result - smaller output and each build looks different. -### `vmOpcodeShuffle` -Type: `boolean` Default: `false` - -Randomizes the numeric values assigned to each opcode. For example, the `LOAD` instruction might be `1` in one build and `47` in another. - ### `vmBytecodeEncoding` Type: `boolean` Default: `false` @@ -1915,64 +1995,70 @@ Type: `boolean` Default: `false` Encodes the entire bytecode array as a single block. The array is decoded once at startup before execution begins. Use together with `vmBytecodeEncoding` for two layers of protection. -### `vmJumpsEncoding` -Type: `boolean` Default: `false` +### `vmBytecodeArrayEncodingKey` +Type: `string` Default: `''` -Encodes jump targets in the bytecode. Jump offsets are calculated at runtime, hiding the control flow structure (`if`/`else`, loops, etc.) from static analysis. +Custom encryption key for bytecode array encoding. When set, this key is used instead of the default environment-derived key. The key must be provided at runtime via `vmBytecodeArrayEncodingKeyGetter`. -### `vmDecoyOpcodes` -Type: `boolean` Default: `false` +This option externalizes the encryption key - it's not embedded in the obfuscated code itself. While the key is still accessible at runtime (and thus not truly secret), this separation prevents static analysis tools from finding the key by examining the code alone. -Adds fake opcode handlers to the VM dispatcher that are never called. For example, if the VM uses 20 real opcodes, this might add 30 fake handlers, making the interpreter appear more complex than it really is. +**Important:** The key must be available **synchronously** when the obfuscated code loads. Use synchronous storage like cookies, localStorage, sessionStorage, global variables, or DOM elements (e.g., server-injected meta tags). Async methods like `fetch()` cannot be used directly in the key getter expression. -### `vmDeadCodeInjection` -Type: `boolean` Default: `false` +### `vmBytecodeArrayEncodingKeyGetter` +Type: `string` Default: `''` -Injects fake bytecode sequences that are never executed. These look like real instructions but are skipped during runtime, confusing analysis tools that process them. +**Synchronous** JavaScript expression that **returns** the encryption key at runtime. This expression is evaluated when the obfuscated code loads, and must return the same key that was provided in `vmBytecodeArrayEncodingKey`. -### `vmSplitDispatcher` -Type: `boolean` Default: `false` +**The obfuscated code will only work when the key getter returns exactly the same key that was used during obfuscation.** If the keys don't match, decryption will fail and the code will produce garbage or errors. If the key getter returns `undefined`, `null`, or an empty string, the code will throw an error: "VM decryption key not available". -Splits the VM dispatcher into multiple smaller switch statements organized by opcode category, instead of one large monolithic switch. Each category (stack, arithmetic, control flow, etc.) gets its own switch, routed by if/else range checks. +**Important:** The key should NOT be defined in the same JavaScript file/script as the obfuscated code. Doing so defeats the purpose of key externalization, as static analysis could still find the key. Store the key in a separate source: server-set cookies, localStorage populated by another script, server-injected HTML meta tags, or a global variable set by a different script that loads before the obfuscated code. -This option supports `vmDynamicOpcodes` in both modes: `true` (shuffle first, then split into groups) and `false`. +Examples: +```ts +// From cookie +vmBytecodeArrayEncodingKeyGetter: "document.cookie.match(/vmKey=([^;]+)/)?.[1]" -> :warning: When `vmIndirectDispatch` is enabled, this option is ignored. Prefer `vmIndirectDispatch` as it provides better obfuscation with similar performance. +// From localStorage +vmBytecodeArrayEncodingKeyGetter: "localStorage.getItem('vmKey')" -### `vmIndirectDispatch` -Type: `boolean` Default: `false` +// From global variable +vmBytecodeArrayEncodingKeyGetter: "window.__VM_KEY__" -Uses compile-time generated handler functions for opcode dispatch instead of switch statements. Handlers are generated at compile-time with inlined opcode logic and shuffled positions. +// From meta tag (server-injected) +vmBytecodeArrayEncodingKeyGetter: "document.querySelector('meta[name=\"vm-key\"]').content" -Instead of: -```javascript -switch(op) { - case 0: /* handle opcode 0 */ break; - case 1: /* handle opcode 1 */ break; -} +// From nested object +vmBytecodeArrayEncodingKeyGetter: "window.config.encryption.key" ``` -It generates: -```javascript -var _hm = {0:42, 1:17, ...}; // opcode → handler index mapping -var _h = [handler0, handler1, ...]; // shuffled handler array -_h[_hm[op]](arg); // single lookup + function call +**Usage example:** +```ts +// Build time +JavaScriptObfuscator.obfuscate(code, { + vmObfuscation: true, + vmBytecodeArrayEncoding: true, + vmBytecodeArrayEncodingKey: 'mySecretKey123', + vmBytecodeArrayEncodingKeyGetter: 'window.__VM_KEY__' +}); + +// Runtime - key must be set before obfuscated code runs +window.__VM_KEY__ = 'mySecretKey123'; ``` -This option supports `vmDynamicOpcodes` in both modes. +### `vmJumpsEncoding` +Type: `boolean` Default: `false` -> :warning: When enabled, this takes priority over `vmSplitDispatcher`. Both options cannot be active simultaneously. +Encodes jump targets in the bytecode. Jump offsets are calculated at runtime, hiding the control flow structure (`if`/`else`, loops, etc.) from static analysis. -### `vmCompactDispatcher` +### `vmDecoyOpcodes` Type: `boolean` Default: `false` -Uses a single unified dispatcher (generator-based) for both sync and async/generator code execution. By default (`false`), the VM generates two separate dispatchers: a non-generator version for sync code (faster) and a generator version for async/generator code. When enabled, only the generator-based dispatcher is used for all execution. +Adds fake opcode handlers to the VM dispatcher that are never called. For example, if the VM uses 20 real opcodes, this might add 30 fake handlers, making the interpreter appear more complex than it really is. -**Trade-offs:** -- `false` (default): Larger code size due to dual dispatchers, but faster sync execution (no generator overhead) -- `true`: Smaller code size with single dispatcher, but sync code has generator protocol overhead +### `vmDeadCodeInjection` +Type: `boolean` Default: `false` -Use this when code size is more important than sync execution speed. +Injects fake bytecode sequences that are never executed. These look like real instructions but are skipped during runtime, confusing analysis tools that process them. ### `vmMacroOps` Type: `boolean` Default: `false` @@ -1982,12 +2068,16 @@ Combines common instruction sequences into single "macro" opcodes. For example, ### `vmDebugProtection` Type: `boolean` Default: `false` -Adds anti-debugging measures to the VM runtime. Detects debugger presence and alters behavior when debugging is detected. +Adds multi-layered anti-debugging, anti-analysis, and anti-LLM defenses to the VM runtime. For best results, allow `unsafe-eval` in your Content Security Policy. Works best with `browser`/`browser-no-eval` targets. -### `vmRuntimeOpcodeDerivation` +### `vmSelfDefending` Type: `boolean` Default: `false` -Derives the opcode mapping table at runtime from a seed value instead of hardcoding it. The seed is stored in the bytecode and used to generate the opcode-to-handler mapping via Fisher-Yates shuffle during execution. +Adds multi-layered tamper detection, anti-hooking, and anti-reverse-engineering protection to the VM runtime. + +> :warning: This option force-enables [`vmBytecodeArrayEncoding`](#vmbytecodeArrayEncoding). + +Strongly recommended to use together with [`vmDebugProtection`](#vmDebugProtection), [`vmBytecodeArrayEncodingKey`](#vmbytecodeArrayEncodingKey), and [`vmBytecodeArrayEncodingKeyGetter`](#vmbytecodeArrayEncodingKeyGetter). ### `vmStatefulOpcodes` Type: `boolean` Default: `false` @@ -2001,19 +2091,25 @@ Encrypts values on the VM stack during execution. Values are encoded when pushed This option heavily affects performance. -### `vmRandomizeKeys` +### `vmCompactDispatcher` Type: `boolean` Default: `false` -Randomizes the property key names used in bytecode objects. Standard keys like `i` (instructions), `c` (constants) become random 2-character identifiers, making the bytecode structure different for each build. +Uses a single VM executor instead of dual executors (sync + generator). Reduces obfuscated code size but adds ~20% performance overhead on recursion-heavy code. + +- `false` (default): dual executors — optimal performance, larger output +- `true`: single executor — smaller output, slightly slower -### `vmBytecodeFormat` -Type: `string` Default: `binary` +### `vmStringArrayBytecodeOnly` +Type: `boolean` Default: `false` -Controls how bytecode is stored in the output. +When enabled, the string array will **only** extract strings from bytecode data — no other strings in the code are transformed. This force-enables `stringArray` even if it's not explicitly set. + +**Why use this:** Extracting all VM runtime strings to a string array is slow. This option targets only bytecode content for string array extraction, improving performance while still protecting bytecode constants. + +- When `vmBytecodeArrayEncoding: false` — strings inside bytecode constant pools (`c` arrays) are extracted +- When `vmBytecodeArrayEncoding: true` — top-level base64 encoded bytecode strings are extracted +- `stringArrayThreshold` still controls what percentage of those bytecode strings are extracted -**Options:** -- `binary` - Compact binary format. Smaller size, recommended for production. -- `json` - Human-readable JSON format. Larger size, useful for debugging. ### `strictMode` Type: `boolean | null` Default: `null` @@ -2025,6 +2121,62 @@ Available values: * `true` - force strict mode treatment for all code, even without explicit `'use strict'` directive. Use this when your code will run in strict mode context (e.g., in ES modules, bundlers, or modern frameworks). * `false` - only explicit strict mode indicators (`'use strict'`, ES modules, class methods) are treated as strict. Parent scope inheritance still applies per JS spec. +### `parseHtml` +Type: `boolean` Default: `false` + +Enables obfuscation of JavaScript within HTML ` + + + + +`; + +JavaScriptObfuscator.obfuscate(html, { + parseHtml: true, + stringArray: true +}); + +// output: HTML with only the marked script obfuscated +``` + ## Frequently Asked Questions ### What javascript versions are supported? diff --git a/bin/javascript-obfuscator b/bin/javascript-obfuscator index 0946b4521..144f7b8ad 100755 --- a/bin/javascript-obfuscator +++ b/bin/javascript-obfuscator @@ -1,3 +1,6 @@ #!/usr/bin/env node -require('../dist/index.cli').obfuscate(process.argv); \ No newline at end of file +require('../dist/index.cli').obfuscate(process.argv).catch((error) => { + console.error(error.message); + process.exit(1); +}); \ No newline at end of file diff --git a/package.json b/package.json index 4baf28c3f..3c29944bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "javascript-obfuscator", - "version": "5.2.1", + "version": "5.4.2", "description": "JavaScript obfuscator", "keywords": [ "obfuscator", @@ -21,26 +21,26 @@ }, "types": "typings/index.d.ts", "dependencies": { - "@javascript-obfuscator/escodegen": "2.3.1", + "@javascript-obfuscator/escodegen": "2.4.1", "@javascript-obfuscator/estraverse": "5.4.0", + "@vercel/blob": ">=0.23.0", "acorn": "8.15.0", + "acorn-import-attributes": "^1.9.5", "assert": "2.1.0", "chalk": "4.1.2", "chance": "1.1.13", "class-validator": "0.14.3", "commander": "12.1.0", - "conf": "15.0.2", + "env-paths": "4.0.0", "eslint-scope": "8.4.0", "eslint-visitor-keys": "4.2.1", "fast-deep-equal": "3.1.3", "inversify": "6.1.4", "js-string-escape": "1.0.1", "md5": "2.3.0", - "mkdirp": "3.0.1", "multimatch": "5.0.0", "process": "0.11.10", "reflect-metadata": "0.2.2", - "source-map-support": "0.5.21", "string-template": "1.0.0", "stringz": "2.1.0", "tslib": "2.8.1" @@ -57,7 +57,6 @@ "@types/js-beautify": "1.14.3", "@types/js-string-escape": "1.0.3", "@types/md5": "2.3.6", - "@types/mkdirp": "1.0.2", "@types/mocha": "10.0.10", "@types/multimatch": "4.0.0", "@types/node": "22.10.2", @@ -85,11 +84,13 @@ "js-beautify": "1.15.4", "mocha": "11.7.4", "nyc": "17.1.0", + "parse5": "^8.0.0", "pjson": "1.0.9", "prettier": "3.6.2", "rimraf": "6.0.1", "sinon": "19.0.2", "source-map-resolve": "0.6.0", + "source-map-support": "0.5.21", "terser": "5.44.0", "threads": "1.7.0", "ts-loader": "9.5.4", diff --git a/src/ASTParserFacade.ts b/src/ASTParserFacade.ts index 8e3c27bfb..ade6007c4 100644 --- a/src/ASTParserFacade.ts +++ b/src/ASTParserFacade.ts @@ -1,6 +1,9 @@ import * as acorn from 'acorn'; import * as ESTree from 'estree'; import chalk, { Chalk } from 'chalk'; +import { importAttributesOrAssertions } from 'acorn-import-attributes'; + +const AcornParser = acorn.Parser.extend(importAttributesOrAssertions); /** * Facade over AST parser `acorn` @@ -67,7 +70,7 @@ export class ASTParserFacade { sourceType }; - const program: acorn.Node & ESTree.Program = acorn.parse(sourceCode, config); + const program: acorn.Node & ESTree.Program = AcornParser.parse(sourceCode, config); if (comments.length) { program.comments = comments; diff --git a/src/JavaScriptObfuscatorCLIFacade.ts b/src/JavaScriptObfuscatorCLIFacade.ts index 09a66604b..075f21be5 100644 --- a/src/JavaScriptObfuscatorCLIFacade.ts +++ b/src/JavaScriptObfuscatorCLIFacade.ts @@ -6,11 +6,12 @@ class JavaScriptObfuscatorCLIFacade { /** * @param {string[]} argv */ - public static obfuscate(argv: string[]): void { + public static async obfuscate(argv: string[]): Promise { const javaScriptObfuscatorCLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI(argv); javaScriptObfuscatorCLI.initialize(); - javaScriptObfuscatorCLI.run(); + + return javaScriptObfuscatorCLI.run(); } } diff --git a/src/JavaScriptObfuscatorFacade.ts b/src/JavaScriptObfuscatorFacade.ts index d5128d0ee..91356e7c3 100644 --- a/src/JavaScriptObfuscatorFacade.ts +++ b/src/JavaScriptObfuscatorFacade.ts @@ -11,12 +11,10 @@ import { IInversifyContainerFacade } from './interfaces/container/IInversifyCont import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator'; import { IObfuscationResult } from './interfaces/source-code/IObfuscationResult'; import { IProApiConfig, IProObfuscationResult, TProApiProgressCallback } from './interfaces/pro-api/IProApiClient'; -import { ApiError } from './pro-api/ApiError'; import { InversifyContainerFacade } from './container/InversifyContainerFacade'; import { Options } from './options/Options'; import { Utils } from './utils/Utils'; -import { ProApiClient } from './pro-api/ProApiClient'; class JavaScriptObfuscatorFacade { /** @@ -94,6 +92,7 @@ class JavaScriptObfuscatorFacade { /** * Obfuscate code using the Pro API (obfuscator.io) * This method requires a valid API token from obfuscator.io and only works with VM obfuscation. + * Only available in Node.js environment. * * @param {string} sourceCode - Source code to obfuscate * @param {TInputOptions} inputOptions - Obfuscation options (must include vmObfuscation: true) @@ -108,13 +107,13 @@ class JavaScriptObfuscatorFacade { proApiConfig: IProApiConfig, onProgress?: TProApiProgressCallback ): Promise { - if (!inputOptions.vmObfuscation) { - throw new ApiError( - 'obfuscatePro method works only with VM obfuscation. Set vmObfuscation: true in options.', - 400 - ); + if (typeof window !== 'undefined') { + const { ApiError } = await import('./pro-api/ApiError'); + + throw new ApiError('obfuscatePro is only available in Node.js environment', 500); } + const { ProApiClient } = await import('./pro-api/ProApiClient'); const client = new ProApiClient(proApiConfig); return client.obfuscate(sourceCode, inputOptions, onProgress); @@ -123,4 +122,4 @@ class JavaScriptObfuscatorFacade { export { JavaScriptObfuscatorFacade as JavaScriptObfuscator }; export { ApiError } from './pro-api/ApiError'; -export type { IProApiConfig, TProApiProgressCallback } from './interfaces/pro-api/IProApiClient'; +export type { IProApiConfig, IProObfuscationResult, TProApiProgressCallback } from './interfaces/pro-api/IProApiClient'; diff --git a/src/analyzers/scope-analyzer/ScopeAnalyzer.ts b/src/analyzers/scope-analyzer/ScopeAnalyzer.ts index b4001cf6d..1c001e376 100644 --- a/src/analyzers/scope-analyzer/ScopeAnalyzer.ts +++ b/src/analyzers/scope-analyzer/ScopeAnalyzer.ts @@ -238,7 +238,11 @@ export class ScopeAnalyzer implements IScopeAnalyzer { (definition: eslintScope.Definition) => definition.type === 'ClassName' ); - return isValidClassNameVariable && variable.name === classNameVariable.name; + const isImportBinding: boolean = variable.defs.some( + (definition: eslintScope.Definition) => definition.type === 'ImportBinding' + ); + + return isValidClassNameVariable && variable.name === classNameVariable.name && !isImportBinding; } ); diff --git a/src/cli/JavaScriptObfuscatorCLI.ts b/src/cli/JavaScriptObfuscatorCLI.ts index 9d905accd..95eb5b78d 100644 --- a/src/cli/JavaScriptObfuscatorCLI.ts +++ b/src/cli/JavaScriptObfuscatorCLI.ts @@ -4,10 +4,13 @@ import * as path from 'path'; import { TInputCLIOptions } from '../types/options/TInputCLIOptions'; import { TInputOptions } from '../types/options/TInputOptions'; +import { TOptionsPreset } from '../types/options/TOptionsPreset'; import { IFileData } from '../interfaces/cli/IFileData'; import { IInitializable } from '../interfaces/IInitializable'; import { IObfuscationResult } from '../interfaces/source-code/IObfuscationResult'; +import { ProApiClient } from '../pro-api/ProApiClient'; +import { IProObfuscationResult } from '../interfaces/pro-api/IProApiClient'; import { initializable } from '../decorators/Initializable'; @@ -22,11 +25,10 @@ import { StringArrayEncoding } from '../enums/node-transformers/string-array-tra import { StringArrayIndexesType } from '../enums/node-transformers/string-array-transformers/StringArrayIndexesType'; import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType'; -import { DEFAULT_PRESET } from '../options/presets/Default'; - import { ArraySanitizer } from './sanitizers/ArraySanitizer'; import { BooleanSanitizer } from './sanitizers/BooleanSanitizer'; +import { Options } from '../options/Options'; import { CLIUtils } from './utils/CLIUtils'; import { IdentifierNamesCacheFileUtils } from './utils/IdentifierNamesCacheFileUtils'; import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade'; @@ -34,6 +36,9 @@ import { Logger } from '../logger/Logger'; import { ObfuscatedCodeFileUtils } from './utils/ObfuscatedCodeFileUtils'; import { SourceCodeFileUtils } from './utils/SourceCodeFileUtils'; import { Utils } from '../utils/Utils'; +import { VMTargetFunctionsMode } from '../pro-api/enums/VMTargetFunctionsMode'; +import { VMBytecodeFormat } from '../pro-api/enums/VMBytecodeFormat'; +import { StrictModeSanitizer } from './sanitizers/StrictModeSanitizer'; export class JavaScriptObfuscatorCLI implements IInitializable { /** @@ -107,26 +112,36 @@ export class JavaScriptObfuscatorCLI implements IInitializable { /** * @param {TInputCLIOptions} inputOptions + * @param {commander.Command} command * @returns {TInputOptions} */ - private static buildOptions(inputOptions: TInputCLIOptions): TInputOptions { - const inputCLIOptions: TInputOptions = JavaScriptObfuscatorCLI.filterOptions(inputOptions); + private static buildOptions(inputOptions: TInputCLIOptions, command: commander.Command): TInputOptions { + const inputCLIOptions: TInputOptions = JavaScriptObfuscatorCLI.filterOptions(inputOptions, command); const configFilePath: string | undefined = inputOptions.config; const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : ''; const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {}; + const presetName: TOptionsPreset = + inputCLIOptions.optionsPreset ?? configFileOptions.optionsPreset ?? OptionsPreset.Default; + const presetOptions: TInputOptions = Options.getOptionsByPreset(presetName); + return { - ...DEFAULT_PRESET, + ...presetOptions, ...configFileOptions, ...inputCLIOptions }; } /** + * Filters out options that were not explicitly set by the user. + * Commander.js sets default values for all options, which would + * override preset values. Only user-provided options should be kept. + * * @param {TObject} options + * @param {commander.Command} command * @returns {TInputOptions} */ - private static filterOptions(options: TInputCLIOptions): TInputOptions { + private static filterOptions(options: TInputCLIOptions, command: commander.Command): TInputOptions { const filteredOptions: TInputOptions = {}; Object.keys(options).forEach((option: keyof TInputCLIOptions) => { @@ -134,6 +149,10 @@ export class JavaScriptObfuscatorCLI implements IInitializable { return; } + if (command.getOptionValueSource(String(option)) === 'default') { + return; + } + filteredOptions[option] = options[option]; }); @@ -147,7 +166,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable { this.configureHelp(); this.inputPath = path.normalize(this.commands.args[0] || ''); - this.inputCLIOptions = JavaScriptObfuscatorCLI.buildOptions(this.commands.opts()); + this.inputCLIOptions = JavaScriptObfuscatorCLI.buildOptions(this.commands.opts(), this.commands); this.sourceCodeFileUtils = new SourceCodeFileUtils(this.inputPath, this.inputCLIOptions); this.obfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(this.inputPath, this.inputCLIOptions); this.identifierNamesCacheFileUtils = new IdentifierNamesCacheFileUtils( @@ -155,7 +174,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable { ); } - public run(): void { + public async run(): Promise { const canShowHelp: boolean = !this.arguments.length || this.arguments.includes('--help'); if (canShowHelp) { @@ -166,7 +185,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable { const sourceCodeData: IFileData[] = this.sourceCodeFileUtils.readSourceCode(); - this.processSourceCodeData(sourceCodeData); + await this.processSourceCodeData(sourceCodeData); } private configureCommands(): void { @@ -210,7 +229,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable { ) .option( '--domain-lock-redirect-url ', - 'Allows the browser to be redirected to a passed URL if the source code isn\'t run on the domains specified by --domain-lock' + "Allows the browser to be redirected to a passed URL if the source code isn't run on the domains specified by --domain-lock" ) .option( '--exclude (comma separated, without whitespaces)', @@ -390,6 +409,153 @@ export class JavaScriptObfuscatorCLI implements IInitializable { 'Allows to enable/disable string conversion to unicode escape sequence', BooleanSanitizer ) + .option( + '--pro-api-token ', + 'API token for Pro obfuscation via obfuscator.io (enables VM obfuscation via cloud API)' + ) + .option('--pro-api-version ', 'Obfuscator version to use with Pro API (e.g., "5.0.0")') + .option( + '--vm-obfuscation ', + 'Enables VM-based bytecode obfuscation for functions', + BooleanSanitizer + ) + .option( + '--vm-obfuscation-threshold ', + 'The probability that VM obfuscation will be applied to a function (Default: 1, Min: 0, Max: 1)', + parseFloat + ) + .option( + '--vm-preprocess-identifiers ', + 'Preprocesses identifiers before VM transformation (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-dynamic-opcodes ', + 'Dynamically assembles VM dispatcher with shuffled case order and filters unused opcodes based on code analysis', + BooleanSanitizer + ) + .option( + '--vm-target-functions (comma separated, without whitespaces)', + 'List of specific function names to apply VM obfuscation to (comma separated)', + ArraySanitizer + ) + .option( + '--vm-exclude-functions (comma separated, without whitespaces)', + 'List of function names to exclude from VM obfuscation (comma separated)', + ArraySanitizer + ) + .option( + '--vm-target-functions-mode ', + 'Controls how functions are selected for VM obfuscation. ' + + `Values: ${CLIUtils.stringifyOptionAvailableValues(VMTargetFunctionsMode)}. ` + + `Default: ${VMTargetFunctionsMode.Root}` + ) + .option( + '--vm-wrap-top-level-initializers ', + 'Wraps top-level variable initializers in IIFEs so they can be VM-obfuscated (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-opcode-shuffle ', + 'Randomizes the numeric values assigned to each opcode (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-bytecode-encoding ', + 'Enables bytecode encryption with per-function keys (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-bytecode-array-encoding ', + 'Enables encrypted bytecode array with lazy decryption (Default: false)', + BooleanSanitizer + ) + .option('--vm-bytecode-array-encoding-key ', 'Custom static key for bytecode array encoding') + .option( + '--vm-bytecode-array-encoding-key-getter ', + 'Custom key getter function code for bytecode array encoding' + ) + .option( + '--vm-instruction-shuffle ', + 'Shuffles instruction order within basic blocks (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-jumps-encoding ', + 'Enables jump target encoding to prevent CFG reconstruction (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-decoy-opcodes ', + 'Enables insertion of decoy opcodes and dead instructions (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-dead-code-injection ', + 'Enables dead code injection with opaque predicates in bytecode (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-split-dispatcher ', + 'Splits the VM interpreter into multiple category-based dispatchers (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-macro-ops ', + 'Enables macro-op fusion to combine common instruction sequences (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-debug-protection ', + 'Enables anti-debugging measures with state corruption (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-runtime-opcode-derivation ', + 'Enables runtime opcode derivation from seeds instead of static mappings (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-stateful-opcodes ', + 'Enables position-based stateful opcode decoding to prevent pattern matching (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-stack-encoding ', + 'Enables stack value encoding to prevent stack inspection (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-randomize-keys ', + 'Randomizes bytecode property keys to prevent pattern matching (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-indirect-dispatch ', + 'Uses indirect dispatch via handler function table instead of switch statement (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-compact-dispatcher ', + 'Uses a single unified dispatcher for both sync and generator execution, reducing code size (Default: false)', + BooleanSanitizer + ) + .option( + '--vm-bytecode-format ', + 'Sets the bytecode storage format. ' + + `Values: ${CLIUtils.stringifyOptionAvailableValues(VMBytecodeFormat)}. ` + + `Default: ${VMBytecodeFormat.Binary}` + ) + .option( + '--parse-html ', + 'Enables obfuscation of JavaScript within HTML