Ver Fonte

Added `renameGlobal` option

sanex3339 há 7 anos atrás
pai
commit
a59fda5d7c

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 Change Log
 ===
 ===
+v0.11.0
+---
+* **New option:** `renameGlobals` allows to enable obfuscation of global variable and function names with declaration.
+
 v0.10.0
 v0.10.0
 ---
 ---
 * **New option:** `deadCodeInjection`. With this option random blocks of dead code will add to the obfuscated code.
 * **New option:** `deadCodeInjection`. With this option random blocks of dead code will add to the obfuscated code.

+ 12 - 0
README.md

@@ -215,6 +215,7 @@ Following options are available for the JS Obfuscator:
     debugProtectionInterval: false,
     debugProtectionInterval: false,
     disableConsoleOutput: false,
     disableConsoleOutput: false,
     mangle: false,
     mangle: false,
+    renameGlobals: false,
     reservedNames: [],
     reservedNames: [],
     rotateStringArray: true,
     rotateStringArray: true,
     seed: 0,
     seed: 0,
@@ -247,6 +248,7 @@ Following options are available for the JS Obfuscator:
     --debugProtectionInterval <boolean>
     --debugProtectionInterval <boolean>
     --disableConsoleOutput <boolean>
     --disableConsoleOutput <boolean>
     --mangle <boolean>
     --mangle <boolean>
+    --renameGlobals <boolean>
     --reservedNames <list> (comma separated)
     --reservedNames <list> (comma separated)
     --rotateStringArray <boolean>
     --rotateStringArray <boolean>
     --seed <number>
     --seed <number>
@@ -495,6 +497,13 @@ Type: `boolean` Default: `false`
 
 
 Enables mangling of variable names.
 Enables mangling of variable names.
 
 
+### `renameGlobals`
+Type: `boolean` Default: `false`
+
+Enables obfuscation of global variable and function names **with declaration**.
+
+##### :warning: this option can break your code. Enable it only if you know what it does!
+
 ### `reservedNames`
 ### `reservedNames`
 Type: `string[]` Default: `[]`
 Type: `string[]` Default: `[]`
 
 
@@ -632,6 +641,7 @@ Performance will 50-100% slower than without obfuscation
 	debugProtectionInterval: true,
 	debugProtectionInterval: true,
 	disableConsoleOutput: true,
 	disableConsoleOutput: true,
 	mangle: false,
 	mangle: false,
+	renameGlobals: false,
 	rotateStringArray: true,
 	rotateStringArray: true,
 	selfDefending: true,
 	selfDefending: true,
 	stringArray: true,
 	stringArray: true,
@@ -656,6 +666,7 @@ Performance will 30-35% slower than without obfuscation
 	debugProtectionInterval: false,
 	debugProtectionInterval: false,
 	disableConsoleOutput: true,
 	disableConsoleOutput: true,
 	mangle: false,
 	mangle: false,
+	renameGlobals: false,
 	rotateStringArray: true,
 	rotateStringArray: true,
 	selfDefending: true,
 	selfDefending: true,
 	stringArray: true,
 	stringArray: true,
@@ -678,6 +689,7 @@ Performance will slightly slower than without obfuscation
 	debugProtectionInterval: false,
 	debugProtectionInterval: false,
 	disableConsoleOutput: true,
 	disableConsoleOutput: true,
 	mangle: true,
 	mangle: true,
+	renameGlobals: false,
 	rotateStringArray: true,
 	rotateStringArray: true,
 	selfDefending: true,
 	selfDefending: true,
 	stringArray: true,
 	stringArray: true,

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "javascript-obfuscator",
   "name": "javascript-obfuscator",
-  "version": "0.10.0",
+  "version": "0.11.0-beta.1",
   "description": "JavaScript obfuscator",
   "description": "JavaScript obfuscator",
   "keywords": [
   "keywords": [
     "obfuscator",
     "obfuscator",

+ 4 - 0
src/cli/JavaScriptObfuscatorCLI.ts

@@ -175,6 +175,10 @@ export class JavaScriptObfuscatorCLI {
                 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)',
                 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)',
                 (value: string) => value.split(',')
                 (value: string) => value.split(',')
             )
             )
+            .option(
+                '--renameGlobals <boolean>', 'Allows to enable obfuscation of global variable and function names with declaration.',
+                BooleanSanitizer
+            )
             .option(
             .option(
                 '--rotateStringArray <boolean>', 'Disable rotation of unicode array values during obfuscation',
                 '--rotateStringArray <boolean>', 'Disable rotation of unicode array values during obfuscation',
                 BooleanSanitizer
                 BooleanSanitizer

+ 1 - 0
src/interfaces/options/IOptions.d.ts

@@ -12,6 +12,7 @@ export interface IOptions {
     readonly disableConsoleOutput: boolean;
     readonly disableConsoleOutput: boolean;
     readonly domainLock: string[];
     readonly domainLock: string[];
     readonly mangle: boolean;
     readonly mangle: boolean;
+    readonly renameGlobals: boolean;
     readonly reservedNames: string[];
     readonly reservedNames: string[];
     readonly rotateStringArray: boolean;
     readonly rotateStringArray: boolean;
     readonly seed: number;
     readonly seed: number;

+ 1 - 1
src/node-transformers/obfuscating-transformers/FunctionDeclarationTransformer.ts

@@ -81,7 +81,7 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
         const blockScopeOfFunctionDeclarationNode: TNodeWithBlockStatement = NodeUtils
         const blockScopeOfFunctionDeclarationNode: TNodeWithBlockStatement = NodeUtils
             .getBlockScopesOfNode(functionDeclarationNode)[0];
             .getBlockScopesOfNode(functionDeclarationNode)[0];
 
 
-        if (blockScopeOfFunctionDeclarationNode.type === NodeType.Program) {
+        if (!this.options.renameGlobals && blockScopeOfFunctionDeclarationNode.type === NodeType.Program) {
             return functionDeclarationNode;
             return functionDeclarationNode;
         }
         }
 
 

+ 1 - 1
src/node-transformers/obfuscating-transformers/VariableDeclarationTransformer.ts

@@ -81,7 +81,7 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
         const blockScopeOfVariableDeclarationNode: TNodeWithBlockStatement = NodeUtils
         const blockScopeOfVariableDeclarationNode: TNodeWithBlockStatement = NodeUtils
             .getBlockScopesOfNode(variableDeclarationNode)[0];
             .getBlockScopesOfNode(variableDeclarationNode)[0];
 
 
-        if (blockScopeOfVariableDeclarationNode.type === NodeType.Program) {
+        if (!this.options.renameGlobals && blockScopeOfVariableDeclarationNode.type === NodeType.Program) {
             return variableDeclarationNode;
             return variableDeclarationNode;
         }
         }
 
 

+ 6 - 0
src/options/Options.ts

@@ -105,6 +105,12 @@ export class Options implements IOptions {
     @IsBoolean()
     @IsBoolean()
     public readonly mangle: boolean;
     public readonly mangle: boolean;
 
 
+    /**
+     * @type {boolean}
+     */
+    @IsBoolean()
+    public readonly renameGlobals: boolean;
+
     /**
     /**
      * @type {string[]}
      * @type {string[]}
      */
      */

+ 1 - 0
src/options/presets/Default.ts

@@ -14,6 +14,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
     disableConsoleOutput: false,
     disableConsoleOutput: false,
     domainLock: [],
     domainLock: [],
     mangle: false,
     mangle: false,
+    renameGlobals: false,
     reservedNames: [],
     reservedNames: [],
     rotateStringArray: true,
     rotateStringArray: true,
     seed: 0,
     seed: 0,

+ 1 - 0
src/options/presets/NoCustomNodes.ts

@@ -13,6 +13,7 @@ export const NO_CUSTOM_NODES_PRESET: TInputOptions = Object.freeze({
     disableConsoleOutput: false,
     disableConsoleOutput: false,
     domainLock: [],
     domainLock: [],
     mangle: false,
     mangle: false,
+    renameGlobals: false,
     reservedNames: [],
     reservedNames: [],
     rotateStringArray: false,
     rotateStringArray: false,
     seed: 0,
     seed: 0,

+ 48 - 18
test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts

@@ -39,29 +39,59 @@ describe('FunctionDeclarationTransformer', () => {
         });
         });
 
 
         describe('variant #2: `functionDeclaration` parent block scope is a `ProgramNode`', () => {
         describe('variant #2: `functionDeclaration` parent block scope is a `ProgramNode`', () => {
-            const functionNameIdentifierRegExp: RegExp = /function *foo *\(\) *\{/;
-            const functionCallIdentifierRegExp: RegExp = /foo *\( *\);/;
+            describe('variant #1: `renameGlobals` option is disabled', () => {
+                const functionNameIdentifierRegExp: RegExp = /function *foo *\(\) *\{/;
+                const functionCallIdentifierRegExp: RegExp = /foo *\( *\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js');
+                    const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_CUSTOM_NODES_PRESET
+                        }
+                    );
+
+                    obfuscatedCode = obfuscationResult.getObfuscatedCode();
+                });
+
+                it('match #1: shouldn\'t transform function name', () => {
+                    assert.match(obfuscatedCode, functionNameIdentifierRegExp);
+                });
+
+                it('match #2: shouldn\'t transform function name', () => {
+                    assert.match(obfuscatedCode, functionCallIdentifierRegExp);
+                });
+            });
 
 
-            let obfuscatedCode: string;
+            describe('variant #2: `renameGlobals` option is enabled', () => {
+                const functionNameIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
+                const functionCallIdentifierRegExp: RegExp = /(_0x[a-f0-9]{4,6}) *\( *\);/;
 
 
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/input.js');
-                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_CUSTOM_NODES_PRESET
-                    }
-                );
+                let obfuscatedCode: string;
 
 
-                obfuscatedCode = obfuscationResult.getObfuscatedCode();
-            });
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js');
+                    const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_CUSTOM_NODES_PRESET,
+                            renameGlobals: true
+                        }
+                    );
 
 
-            it('match #1: shouldn\'t transform function name', () => {
-                assert.match(obfuscatedCode, functionNameIdentifierRegExp);
-            });
+                    obfuscatedCode = obfuscationResult.getObfuscatedCode();
+                });
+
+                it('match #1: shouldn\'t transform function name', () => {
+                    assert.match(obfuscatedCode, functionNameIdentifierRegExp);
+                });
 
 
-            it('match #2: shouldn\'t transform function name', () => {
-                assert.match(obfuscatedCode, functionCallIdentifierRegExp);
+                it('match #2: shouldn\'t transform function name', () => {
+                    assert.match(obfuscatedCode, functionCallIdentifierRegExp);
+                });
             });
             });
         });
         });
 
 

+ 6 - 0
test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/fixtures/parent-block-scope-is-program-node.js

@@ -0,0 +1,6 @@
+function foo () {
+}
+
+if (true) {
+    foo();
+}

+ 47 - 17
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts

@@ -38,29 +38,59 @@ describe('VariableDeclarationTransformer', () => {
     });
     });
 
 
     describe('variant #2: parent block scope node is `Program` node', () => {
     describe('variant #2: parent block scope node is `Program` node', () => {
-        const variableDeclarationRegExp: RegExp = /var *test *= *0xa;/;
-        const variableCallRegExp: RegExp = /console\['log'\]\(test\);/;
+        describe('variant #1: `renameGlobals` option is disabled', () => {
+            const variableDeclarationRegExp: RegExp = /var *test *= *0xa;/;
+            const variableCallRegExp: RegExp = /console\['log'\]\(test\);/;
 
 
-        let obfuscatedCode: string;
+            let obfuscatedCode: string;
 
 
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js');
-            const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_CUSTOM_NODES_PRESET
-                }
-            );
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_CUSTOM_NODES_PRESET
+                    }
+                );
 
 
-            obfuscatedCode = obfuscationResult.getObfuscatedCode();
-        });
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
 
 
-        it('match #1: shouldn\'t transform `variableDeclaration` node', () => {
-            assert.match(obfuscatedCode, variableDeclarationRegExp);
+            it('match #1: shouldn\'t transform `variableDeclaration` node', () => {
+                assert.match(obfuscatedCode, variableDeclarationRegExp);
+            });
+
+            it('match #2: shouldn\'t transform `variableDeclaration` node', () => {
+                assert.match(obfuscatedCode, variableCallRegExp);
+            });
         });
         });
 
 
-        it('match #2: shouldn\'t transform `variableDeclaration` node', () => {
-            assert.match(obfuscatedCode, variableCallRegExp);
+        describe('variant #2: `renameGlobals` option is enabled', () => {
+            const variableDeclarationRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0xa;/;
+            const variableCallRegExp: RegExp = /console\['log'\]\(_0x([a-f0-9]){4,6}\);/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_CUSTOM_NODES_PRESET,
+                        renameGlobals: true
+                    }
+                );
+
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
+
+            it('match #1: should transform `variableDeclaration` node', () => {
+                assert.match(obfuscatedCode, variableDeclarationRegExp);
+            });
+
+            it('match #2: should transform `variableDeclaration` node', () => {
+                assert.match(obfuscatedCode, variableCallRegExp);
+            });
         });
         });
     });
     });
 
 

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff