sanex3339 9 éve
szülő
commit
0e8b610f53

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.1.4",
+  "version": "0.1.5",
   "description": "JavaScript obfuscator",
   "main": "index.js",
   "dependencies": {

+ 16 - 0
src/NodeUtils.js

@@ -0,0 +1,16 @@
+"use strict";
+class NodeUtils {
+    static getParentNodeWithType(node, types, limitNodeTypes = [], deep = 0) {
+        if (node.parentNode.type === 'Program' || limitNodeTypes.indexOf(node.parentNode.type) >= 0) {
+            return node.parentNode;
+        }
+        if (types.indexOf(node.parentNode.type) < 0) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, deep);
+        }
+        if (deep > 0) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, --deep);
+        }
+        return node.parentNode;
+    }
+}
+exports.NodeUtils = NodeUtils;

+ 23 - 0
src/NodeUtils.ts

@@ -0,0 +1,23 @@
+export class NodeUtils {
+    /**
+     * @param node
+     * @param types
+     * @param limitNodeTypes
+     * @param deep
+     */
+    public static getParentNodeWithType (node: any, types: string[], limitNodeTypes: string[] = [], deep: number = 0): any {
+        if (node.parentNode.type === 'Program' || limitNodeTypes.indexOf(node.parentNode.type) >= 0) {
+            return node.parentNode;
+        }
+
+        if (types.indexOf(node.parentNode.type) < 0) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, deep);
+        }
+
+        if (deep > 0) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, --deep);
+        }
+
+        return node.parentNode;
+    }
+}

+ 11 - 4
src/Obfuscator.js

@@ -1,8 +1,8 @@
 "use strict";
 const AppendState_1 = require('./enums/AppendState');
+const CatchClauseObfuscator_1 = require("./node-obfuscators/CatchClauseObfuscator");
 const FunctionDeclarationObfuscator_1 = require('./node-obfuscators/FunctionDeclarationObfuscator');
 const FunctionObfuscator_1 = require('./node-obfuscators/FunctionObfuscator');
-const LiteralObfuscator_1 = require('./node-obfuscators/LiteralObfuscator');
 const MemberExpressionObfuscator_1 = require('./node-obfuscators/MemberExpressionObfuscator');
 const MethodDefinitionObfuscator_1 = require('./node-obfuscators/MethodDefinitionObfuscator');
 const ObjectExpressionObfuscator_1 = require('./node-obfuscators/ObjectExpressionObfuscator');
@@ -16,6 +16,7 @@ class Obfuscator {
         this.nodes = new Map();
         this.nodeObfuscators = new Map([
             ['ClassDeclaration', [FunctionDeclarationObfuscator_1.FunctionDeclarationObfuscator]],
+            ['CatchClause', [CatchClauseObfuscator_1.CatchClauseObfuscator]],
             ['FunctionDeclaration', [
                     FunctionDeclarationObfuscator_1.FunctionDeclarationObfuscator,
                     FunctionObfuscator_1.FunctionObfuscator
@@ -26,7 +27,6 @@ class Obfuscator {
             ['VariableDeclaration', [VariableDeclarationObfuscator_1.VariableDeclarationObfuscator]],
             ['ObjectExpression', [ObjectExpressionObfuscator_1.ObjectExpressionObfuscator]],
             ['MemberExpression', [MemberExpressionObfuscator_1.MemberExpressionObfuscator]],
-            ['Literal', [LiteralObfuscator_1.LiteralObfuscator]]
         ]);
         this.options = {
             rotateUnicodeArray: true
@@ -42,7 +42,8 @@ class Obfuscator {
         }
         this.beforeObfuscation(node);
         estraverse.replace(node, {
-            leave: (node, parent) => this.nodeController(node, parent)
+            enter: (node, parent) => this.nodeControllerEnter(node, parent),
+            leave: (node, parent) => this.nodeControllerLeave(node, parent)
         });
         this.afterObfuscation(node);
     }
@@ -70,7 +71,13 @@ class Obfuscator {
         });
     }
     ;
-    nodeController(node, parent) {
+    nodeControllerEnter(node, parent) {
+        switch (node.type) {
+            default:
+                node.parentNode = parent;
+        }
+    }
+    nodeControllerLeave(node, parent) {
         switch (node.type) {
             default:
                 this.initializeNodeObfuscators(node, parent);

+ 17 - 3
src/Obfuscator.ts

@@ -4,6 +4,7 @@ import { INodesGroup } from './interfaces/INodesGroup';
 
 import { AppendState } from './enums/AppendState';
 
+import { CatchClauseObfuscator } from "./node-obfuscators/CatchClauseObfuscator";
 import { FunctionDeclarationObfuscator } from './node-obfuscators/FunctionDeclarationObfuscator';
 import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator';
 import { LiteralObfuscator } from './node-obfuscators/LiteralObfuscator';
@@ -30,6 +31,7 @@ export class Obfuscator {
      */
     private nodeObfuscators: Map <string, Function[]> = new Map <string, Function[]> ([
         ['ClassDeclaration', [FunctionDeclarationObfuscator]],
+        ['CatchClause', [CatchClauseObfuscator]],
         ['FunctionDeclaration', [
             FunctionDeclarationObfuscator,
             FunctionObfuscator
@@ -40,7 +42,7 @@ export class Obfuscator {
         ['VariableDeclaration', [VariableDeclarationObfuscator]],
         ['ObjectExpression', [ObjectExpressionObfuscator]],
         ['MemberExpression', [MemberExpressionObfuscator]],
-        ['Literal', [LiteralObfuscator]]
+        //['Literal', [LiteralObfuscator]]
     ]);
 
     /**
@@ -73,7 +75,8 @@ export class Obfuscator {
         this.beforeObfuscation(node);
 
         estraverse.replace(node, {
-            leave: (node, parent) => this.nodeController(node, parent)
+            enter: (node, parent) => this.nodeControllerEnter(node, parent),
+            leave: (node, parent) => this.nodeControllerLeave(node, parent)
         });
 
         this.afterObfuscation(node);
@@ -125,7 +128,18 @@ export class Obfuscator {
      * @param node
      * @param parent
      */
-    private nodeController (node, parent): void {
+    private nodeControllerEnter (node, parent): void {
+        switch (node.type) {
+            default:
+                node.parentNode = parent;
+        }
+    }
+
+    /**
+     * @param node
+     * @param parent
+     */
+    private nodeControllerLeave (node, parent): void {
         switch (node.type) {
             default:
                 this.initializeNodeObfuscators(node, parent);

+ 1 - 1
src/Utils.js

@@ -25,7 +25,7 @@ class Utils {
     }
     static getRandomVariableName(length = 6) {
         const prefix = '_0x';
-        return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 9999999))).substr(0, length)}`;
+        return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 99999999))).substr(0, length)}`;
     }
     static stringToUnicode(string) {
         return `'${string.replace(/[\s\S]/g, (escape) => {

+ 1 - 1
src/Utils.ts

@@ -50,7 +50,7 @@ export class Utils {
     public static getRandomVariableName (length: number = 6): string {
         const prefix = '_0x';
 
-        return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 9999999))).substr(0, length)}`;
+        return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 99999999))).substr(0, length)}`;
     }
 
     /**

+ 33 - 0
src/node-obfuscators/CatchClauseObfuscator.js

@@ -0,0 +1,33 @@
+"use strict";
+const NodeObfuscator_1 = require('./NodeObfuscator');
+const Utils_1 = require('../Utils');
+let estraverse = require('estraverse');
+class CatchClauseObfuscator extends NodeObfuscator_1.NodeObfuscator {
+    constructor(...args) {
+        super(...args);
+        this.catchClauseParam = new Map();
+    }
+    obfuscateNode(catchClauseNode) {
+        this.replaceCatchClauseParam(catchClauseNode);
+        this.replaceCatchClauseParamInBlock(catchClauseNode);
+    }
+    replaceCatchClauseParam(catchClauseNode) {
+        estraverse.replace(catchClauseNode.param, {
+            leave: (node, parentNode) => {
+                if (node.type !== 'Identifier') {
+                    return;
+                }
+                this.catchClauseParam.set(node.name, Utils_1.Utils.getRandomVariableName());
+                node.name = this.catchClauseParam.get(node.name);
+            }
+        });
+    }
+    replaceCatchClauseParamInBlock(catchClauseNode) {
+        estraverse.replace(catchClauseNode.body, {
+            leave: (node, parentNode) => {
+                this.replaceNodeIdentifierByNewValue(node, parentNode, this.catchClauseParam);
+            }
+        });
+    }
+}
+exports.CatchClauseObfuscator = CatchClauseObfuscator;

+ 54 - 0
src/node-obfuscators/CatchClauseObfuscator.ts

@@ -0,0 +1,54 @@
+import { NodeObfuscator } from './NodeObfuscator';
+import { Utils } from '../Utils';
+
+let estraverse = require('estraverse');
+
+/**
+ * replaces:
+ *     try {} catch (e) { console.log(e); };
+ *
+ * by:
+ *     try {} catch (_0x12d45f) { console.log(_0x12d45f); };
+ *
+ */
+export class CatchClauseObfuscator extends NodeObfuscator {
+    /**
+     * @type {Map<string, string>}
+     */
+    private catchClauseParam: Map <string, string> = new Map <string, string> ();
+
+    /**
+     * @param catchClauseNode
+     */
+    public obfuscateNode (catchClauseNode: any): void {
+        this.replaceCatchClauseParam(catchClauseNode);
+        this.replaceCatchClauseParamInBlock(catchClauseNode);
+    }
+
+    /**
+     * @param catchClauseNode
+     */
+    private replaceCatchClauseParam (catchClauseNode: any): void {
+        estraverse.replace(catchClauseNode.param, {
+            leave: (node, parentNode) => {
+                if (node.type !== 'Identifier') {
+                    return;
+                }
+
+                this.catchClauseParam.set(node.name, Utils.getRandomVariableName());
+                node.name = this.catchClauseParam.get(node.name);
+            }
+        });
+    }
+
+    /**
+     * @param catchClauseNode
+     */
+    private replaceCatchClauseParamInBlock (catchClauseNode: any): void {
+        estraverse.replace(catchClauseNode.body, {
+            leave: (node, parentNode) => {
+                this.replaceNodeIdentifierByNewValue(node, parentNode, this.catchClauseParam);
+            }
+        });
+    }
+}

+ 1 - 1
src/node-obfuscators/NodeObfuscator.ts

@@ -6,7 +6,7 @@ import { Utils } from '../Utils';
 
 export abstract class NodeObfuscator implements INodeObfuscator {
     /**
-     * @param Map <string, Node>
+     * @type Map <string, Node>
      */
     protected nodes: Map <string, INode>;
 

+ 14 - 6
src/node-obfuscators/VariableDeclarationObfuscator.js

@@ -1,6 +1,7 @@
 "use strict";
 const NodeObfuscator_1 = require('./NodeObfuscator');
 const Utils_1 = require('../Utils');
+const NodeUtils_1 = require("../NodeUtils");
 let estraverse = require('estraverse');
 class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
     constructor(...args) {
@@ -12,17 +13,17 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
             return;
         }
         this.replaceVariableName(variableDeclarationNode);
-        this.replaceVariableCalls(parentNode);
+        this.replaceVariableCalls(variableDeclarationNode, parentNode);
     }
     replaceVariableName(variableDeclarationNode) {
         variableDeclarationNode.declarations.forEach((declarationNode) => {
             estraverse.replace(declarationNode, {
-                leave: (node) => {
+                enter: (node) => {
                     if (node.type !== 'VariableDeclarator') {
                         return;
                     }
                     estraverse.replace(node.id, {
-                        leave: (node) => {
+                        enter: (node) => {
                             this.variableName.set(node.name, Utils_1.Utils.getRandomVariableName());
                             node.name = this.variableName.get(node.name);
                         }
@@ -31,9 +32,16 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
             });
         });
     }
-    replaceVariableCalls(variableParentNode) {
-        estraverse.replace(variableParentNode, {
-            leave: (node, parentNode) => {
+    replaceVariableCalls(variableDeclarationNode, variableParentNode) {
+        let scopeNode, statementNode;
+        if (variableDeclarationNode.kind === 'var') {
+            scopeNode = NodeUtils_1.NodeUtils.getParentNodeWithType(variableDeclarationNode, ['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'MethodDefinition']);
+        }
+        else {
+            scopeNode = variableParentNode;
+        }
+        estraverse.replace(scopeNode, {
+            enter: (node, parentNode) => {
                 this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableName);
             }
         });

+ 20 - 6
src/node-obfuscators/VariableDeclarationObfuscator.ts

@@ -1,5 +1,6 @@
 import { NodeObfuscator } from './NodeObfuscator';
 import { Utils } from '../Utils';
+import {NodeUtils} from "../NodeUtils";
 
 let estraverse = require('estraverse');
 
@@ -29,7 +30,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
         }
 
         this.replaceVariableName(variableDeclarationNode);
-        this.replaceVariableCalls(parentNode);
+        this.replaceVariableCalls(variableDeclarationNode, parentNode);
     }
 
     /**
@@ -38,13 +39,13 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
     private replaceVariableName (variableDeclarationNode: any): void {
         variableDeclarationNode.declarations.forEach((declarationNode) => {
             estraverse.replace(declarationNode, {
-                leave: (node) => {
+                enter: (node) => {
                     if (node.type !== 'VariableDeclarator') {
                         return;
                     }
 
                     estraverse.replace(node.id, {
-                        leave: (node) => {
+                        enter: (node) => {
                             this.variableName.set(node.name, Utils.getRandomVariableName());
                             node.name = this.variableName.get(node.name);
                         }
@@ -55,11 +56,24 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
     }
 
     /**
+     * @param variableDeclarationNode
      * @param variableParentNode
      */
-    private replaceVariableCalls (variableParentNode: any): void {
-        estraverse.replace(variableParentNode, {
-            leave: (node, parentNode) => {
+    private replaceVariableCalls (variableDeclarationNode: any, variableParentNode: any): void {
+        let scopeNode: any,
+            statementNode: any;
+
+        if (variableDeclarationNode.kind === 'var') {
+            scopeNode = NodeUtils.getParentNodeWithType(
+                variableDeclarationNode,
+                ['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'MethodDefinition']
+            );
+        } else {
+            scopeNode = variableParentNode;
+        }
+
+        estraverse.replace(scopeNode, {
+            enter: (node, parentNode) => {
                 this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableName);
             }
         });

+ 4 - 1
tests/dev-test.js

@@ -1,18 +1,21 @@
 var JavaScriptObfuscator = require('../index.js');
 var obfuscatedCode = JavaScriptObfuscator.obfuscate(`
-    (function(){
+   (function(){
         var result = 1,
             term1 = 0,
             term2 = 1,
             i = 1;
         while(i < 10)
         {
+            var test = 10;
             result = term1 + term2;
             console.log(result);
             term1 = term2;
             term2 = result;
             i++;
         }
+
+        console.log(test);
     })();
     `, {
     rotateUnicodeArray: false

+ 4 - 1
tests/dev-test.ts

@@ -2,19 +2,22 @@ var JavaScriptObfuscator = require('../index.js');
 
 var obfuscatedCode = JavaScriptObfuscator.obfuscate(
     `
-    (function(){
+   (function(){
         var result = 1,
             term1 = 0,
             term2 = 1,
             i = 1;
         while(i < 10)
         {
+            var test = 10;
             result = term1 + term2;
             console.log(result);
             term1 = term2;
             term2 = result;
             i++;
         }
+
+        console.log(test);
     })();
     `,
     {

+ 1 - 6
tsconfig.json

@@ -7,10 +7,5 @@
     "experimentalDecorators": true,
     "removeComments": true,
     "noImplicitAny": false
-  },
-  "files": [
-    "index.ts",
-    "typings/tsd.d.ts",
-    "tests/dev-test.ts"
-  ]
+  }
 }