소스 검색

Added `renameGlobal` option

sanex3339 7 년 전
부모
커밋
a59fda5d7c

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 ===
+v0.11.0
+---
+* **New option:** `renameGlobals` allows to enable obfuscation of global variable and function names with declaration.
+
 v0.10.0
 ---
 * **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,
     disableConsoleOutput: false,
     mangle: false,
+    renameGlobals: false,
     reservedNames: [],
     rotateStringArray: true,
     seed: 0,
@@ -247,6 +248,7 @@ Following options are available for the JS Obfuscator:
     --debugProtectionInterval <boolean>
     --disableConsoleOutput <boolean>
     --mangle <boolean>
+    --renameGlobals <boolean>
     --reservedNames <list> (comma separated)
     --rotateStringArray <boolean>
     --seed <number>
@@ -495,6 +497,13 @@ Type: `boolean` Default: `false`
 
 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`
 Type: `string[]` Default: `[]`
 
@@ -632,6 +641,7 @@ Performance will 50-100% slower than without obfuscation
 	debugProtectionInterval: true,
 	disableConsoleOutput: true,
 	mangle: false,
+	renameGlobals: false,
 	rotateStringArray: true,
 	selfDefending: true,
 	stringArray: true,
@@ -656,6 +666,7 @@ Performance will 30-35% slower than without obfuscation
 	debugProtectionInterval: false,
 	disableConsoleOutput: true,
 	mangle: false,
+	renameGlobals: false,
 	rotateStringArray: true,
 	selfDefending: true,
 	stringArray: true,
@@ -678,6 +689,7 @@ Performance will slightly slower than without obfuscation
 	debugProtectionInterval: false,
 	disableConsoleOutput: true,
 	mangle: true,
+	renameGlobals: false,
 	rotateStringArray: true,
 	selfDefending: true,
 	stringArray: true,

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.10.0",
+  "version": "0.11.0-beta.1",
   "description": "JavaScript obfuscator",
   "keywords": [
     "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)',
                 (value: string) => value.split(',')
             )
+            .option(
+                '--renameGlobals <boolean>', 'Allows to enable obfuscation of global variable and function names with declaration.',
+                BooleanSanitizer
+            )
             .option(
                 '--rotateStringArray <boolean>', 'Disable rotation of unicode array values during obfuscation',
                 BooleanSanitizer

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

@@ -12,6 +12,7 @@ export interface IOptions {
     readonly disableConsoleOutput: boolean;
     readonly domainLock: string[];
     readonly mangle: boolean;
+    readonly renameGlobals: boolean;
     readonly reservedNames: string[];
     readonly rotateStringArray: boolean;
     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
             .getBlockScopesOfNode(functionDeclarationNode)[0];
 
-        if (blockScopeOfFunctionDeclarationNode.type === NodeType.Program) {
+        if (!this.options.renameGlobals && blockScopeOfFunctionDeclarationNode.type === NodeType.Program) {
             return functionDeclarationNode;
         }
 

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

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

+ 6 - 0
src/options/Options.ts

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

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

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

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

@@ -13,6 +13,7 @@ export const NO_CUSTOM_NODES_PRESET: TInputOptions = Object.freeze({
     disableConsoleOutput: false,
     domainLock: [],
     mangle: false,
+    renameGlobals: false,
     reservedNames: [],
     rotateStringArray: false,
     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`', () => {
-            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', () => {
-        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);
+            });
         });
     });
 

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.