Browse Source

Fix for incorrect obfuscation of function names

sanex3339 9 years ago
parent
commit
d125f8d4ea

+ 26 - 22
dist/index.js

@@ -1862,12 +1862,12 @@ module.exports =
 	    _createClass(CatchClauseObfuscator, [{
 	        key: 'obfuscateNode',
 	        value: function obfuscateNode(catchClauseNode) {
+	            this.storeCatchClauseParam(catchClauseNode);
 	            this.replaceCatchClauseParam(catchClauseNode);
-	            this.replaceCatchClauseParamInBlockStatement(catchClauseNode);
 	        }
 	    }, {
-	        key: 'replaceCatchClauseParam',
-	        value: function replaceCatchClauseParam(catchClauseNode) {
+	        key: 'storeCatchClauseParam',
+	        value: function storeCatchClauseParam(catchClauseNode) {
 	            var _this2 = this;
 	
 	            estraverse.traverse(catchClauseNode.param, {
@@ -1877,8 +1877,8 @@ module.exports =
 	            });
 	        }
 	    }, {
-	        key: 'replaceCatchClauseParamInBlockStatement',
-	        value: function replaceCatchClauseParamInBlockStatement(catchClauseNode) {
+	        key: 'replaceCatchClauseParam',
+	        value: function replaceCatchClauseParam(catchClauseNode) {
 	            var _this3 = this;
 	
 	            estraverse.replace(catchClauseNode, {
@@ -1938,12 +1938,12 @@ module.exports =
 	            if (parentNode.type === NodeType_1.NodeType.Program) {
 	                return;
 	            }
+	            this.storeFunctionName(functionDeclarationNode);
 	            this.replaceFunctionName(functionDeclarationNode);
-	            this.replaceFunctionCalls(functionDeclarationNode);
 	        }
 	    }, {
-	        key: "replaceFunctionName",
-	        value: function replaceFunctionName(functionDeclarationNode) {
+	        key: "storeFunctionName",
+	        value: function storeFunctionName(functionDeclarationNode) {
 	            var _this2 = this;
 	
 	            estraverse.traverse(functionDeclarationNode.id, {
@@ -1953,8 +1953,8 @@ module.exports =
 	            });
 	        }
 	    }, {
-	        key: "replaceFunctionCalls",
-	        value: function replaceFunctionCalls(functionDeclarationNode) {
+	        key: "replaceFunctionName",
+	        value: function replaceFunctionName(functionDeclarationNode) {
 	            var _this3 = this;
 	
 	            var scopeNode = NodeUtils_1.NodeUtils.getBlockScopeOfNode(functionDeclarationNode);
@@ -2010,12 +2010,12 @@ module.exports =
 	    _createClass(FunctionObfuscator, [{
 	        key: 'obfuscateNode',
 	        value: function obfuscateNode(functionNode) {
+	            this.storeFunctionParams(functionNode);
 	            this.replaceFunctionParams(functionNode);
-	            this.replaceFunctionParamsInBlockStatement(functionNode);
 	        }
 	    }, {
-	        key: 'replaceFunctionParams',
-	        value: function replaceFunctionParams(functionNode) {
+	        key: 'storeFunctionParams',
+	        value: function storeFunctionParams(functionNode) {
 	            var _this2 = this;
 	
 	            functionNode.params.forEach(function (paramsNode) {
@@ -2027,15 +2027,19 @@ module.exports =
 	            });
 	        }
 	    }, {
-	        key: 'replaceFunctionParamsInBlockStatement',
-	        value: function replaceFunctionParamsInBlockStatement(functionNode) {
+	        key: 'replaceFunctionParams',
+	        value: function replaceFunctionParams(functionNode) {
 	            var _this3 = this;
 	
-	            estraverse.replace(functionNode, {
+	            var replaceVisitor = {
 	                leave: function leave(node, parentNode) {
 	                    _this3.replaceIdentifiersWithRandomNames(node, parentNode, _this3.functionParams);
 	                }
+	            };
+	            functionNode.params.forEach(function (paramsNode) {
+	                estraverse.replace(paramsNode, replaceVisitor);
 	            });
+	            estraverse.replace(functionNode.body, replaceVisitor);
 	        }
 	    }]);
 	
@@ -2405,12 +2409,12 @@ module.exports =
 	            if (parentNode.type === NodeType_1.NodeType.Program) {
 	                return;
 	            }
-	            this.replaceVariableName(variableDeclarationNode);
-	            this.replaceVariableCalls(variableDeclarationNode, parentNode);
+	            this.storeVariableNames(variableDeclarationNode);
+	            this.replaceVariableNames(variableDeclarationNode, parentNode);
 	        }
 	    }, {
-	        key: "replaceVariableName",
-	        value: function replaceVariableName(variableDeclarationNode) {
+	        key: "storeVariableNames",
+	        value: function storeVariableNames(variableDeclarationNode) {
 	            var _this2 = this;
 	
 	            variableDeclarationNode.declarations.forEach(function (declarationNode) {
@@ -2422,8 +2426,8 @@ module.exports =
 	            });
 	        }
 	    }, {
-	        key: "replaceVariableCalls",
-	        value: function replaceVariableCalls(variableDeclarationNode, variableParentNode) {
+	        key: "replaceVariableNames",
+	        value: function replaceVariableNames(variableDeclarationNode, variableParentNode) {
 	            var _this3 = this;
 	
 	            var scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getBlockScopeOfNode(variableDeclarationNode) : variableParentNode,

+ 3 - 3
src/node-obfuscators/CatchClauseObfuscator.ts

@@ -26,14 +26,14 @@ export class CatchClauseObfuscator extends NodeObfuscator {
      * @param catchClauseNode
      */
     public obfuscateNode (catchClauseNode: ICatchClauseNode): void {
+        this.storeCatchClauseParam(catchClauseNode);
         this.replaceCatchClauseParam(catchClauseNode);
-        this.replaceCatchClauseParamInBlockStatement(catchClauseNode);
     }
 
     /**
      * @param catchClauseNode
      */
-    private replaceCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
+    private storeCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
         estraverse.traverse(catchClauseNode.param, {
             leave: (node: INode): any => this.storeIdentifiersNames(node, this.catchClauseParam)
         });
@@ -42,7 +42,7 @@ export class CatchClauseObfuscator extends NodeObfuscator {
     /**
      * @param catchClauseNode
      */
-    private replaceCatchClauseParamInBlockStatement (catchClauseNode: ICatchClauseNode): void {
+    private replaceCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
         estraverse.replace(catchClauseNode, {
             leave: (node: INode, parentNode: INode): any => {
                 this.replaceIdentifiersWithRandomNames(node, parentNode, this.catchClauseParam);

+ 3 - 3
src/node-obfuscators/FunctionDeclarationObfuscator.ts

@@ -32,14 +32,14 @@ export class FunctionDeclarationObfuscator extends NodeObfuscator {
             return;
         }
 
+        this.storeFunctionName(functionDeclarationNode);
         this.replaceFunctionName(functionDeclarationNode);
-        this.replaceFunctionCalls(functionDeclarationNode);
     }
 
     /**
      * @param functionDeclarationNode
      */
-    private replaceFunctionName (functionDeclarationNode: IFunctionDeclarationNode): void {
+    private storeFunctionName (functionDeclarationNode: IFunctionDeclarationNode): void {
         estraverse.traverse(functionDeclarationNode.id, {
             leave: (node: INode): any => this.storeIdentifiersNames(node, this.functionName)
         });
@@ -48,7 +48,7 @@ export class FunctionDeclarationObfuscator extends NodeObfuscator {
     /**
      * @param functionDeclarationNode
      */
-    private replaceFunctionCalls (functionDeclarationNode: IFunctionDeclarationNode): void {
+    private replaceFunctionName (functionDeclarationNode: IFunctionDeclarationNode): void {
         let scopeNode: INode = NodeUtils.getBlockScopeOfNode(
             functionDeclarationNode
         );

+ 17 - 9
src/node-obfuscators/FunctionObfuscator.ts

@@ -26,29 +26,37 @@ export class FunctionObfuscator extends NodeObfuscator {
      * @param functionNode
      */
     public obfuscateNode (functionNode: IFunctionNode): void {
+        this.storeFunctionParams(functionNode);
         this.replaceFunctionParams(functionNode);
-        this.replaceFunctionParamsInBlockStatement(functionNode);
     }
 
     /**
      * @param functionNode
      */
-    private replaceFunctionParams (functionNode: IFunctionNode): void {
-        functionNode.params.forEach((paramsNode: INode) => {
-            estraverse.traverse(paramsNode, {
-                leave: (node: INode): any => this.storeIdentifiersNames(node, this.functionParams)
+    private storeFunctionParams (functionNode: IFunctionNode): void {
+        functionNode.params
+            .forEach((paramsNode: INode) => {
+                estraverse.traverse(paramsNode, {
+                    leave: (node: INode): any => this.storeIdentifiersNames(node, this.functionParams)
+                });
             });
-        });
     }
 
     /**
      * @param functionNode
      */
-    private replaceFunctionParamsInBlockStatement (functionNode: IFunctionNode): void {
-        estraverse.replace(functionNode, {
+    private replaceFunctionParams (functionNode: IFunctionNode): void {
+        let replaceVisitor: estraverse.Visitor = {
             leave: (node: INode, parentNode: INode): any => {
                 this.replaceIdentifiersWithRandomNames(node, parentNode, this.functionParams);
             }
-        });
+        };
+
+        functionNode.params
+            .forEach((paramsNode: INode) => {
+                estraverse.replace(paramsNode, replaceVisitor);
+            });
+
+        estraverse.replace(functionNode.body, replaceVisitor);
     }
 }

+ 4 - 4
src/node-obfuscators/VariableDeclarationObfuscator.ts

@@ -36,14 +36,14 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
             return;
         }
 
-        this.replaceVariableName(variableDeclarationNode);
-        this.replaceVariableCalls(variableDeclarationNode, parentNode);
+        this.storeVariableNames(variableDeclarationNode);
+        this.replaceVariableNames(variableDeclarationNode, parentNode);
     }
 
     /**
      * @param variableDeclarationNode
      */
-    private replaceVariableName (variableDeclarationNode: IVariableDeclarationNode): void {
+    private storeVariableNames (variableDeclarationNode: IVariableDeclarationNode): void {
         variableDeclarationNode.declarations
             .forEach((declarationNode: IVariableDeclaratorNode) => {
                 estraverse.traverse(declarationNode.id, {
@@ -56,7 +56,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
      * @param variableDeclarationNode
      * @param variableParentNode
      */
-    private replaceVariableCalls (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: INode): void {
+    private replaceVariableNames (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: INode): void {
         let scopeNode: INode = variableDeclarationNode.kind === 'var' ? NodeUtils.getBlockScopeOfNode(
                 variableDeclarationNode
             ) : variableParentNode,