From d125f8d4ea85da72d0ba6ea703756849eb008a60 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Fri, 17 Jun 2016 21:08:10 +0300 Subject: [PATCH] Fix for incorrect obfuscation of function names --- dist/index.js | 48 ++++++++++--------- src/node-obfuscators/CatchClauseObfuscator.ts | 6 +-- .../FunctionDeclarationObfuscator.ts | 6 +-- src/node-obfuscators/FunctionObfuscator.ts | 36 ++++++++------ .../VariableDeclarationObfuscator.ts | 8 ++-- 5 files changed, 58 insertions(+), 46 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0bde177a..35981bb5 100644 --- a/dist/index.js +++ b/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, diff --git a/src/node-obfuscators/CatchClauseObfuscator.ts b/src/node-obfuscators/CatchClauseObfuscator.ts index f436d499..7a09487a 100644 --- a/src/node-obfuscators/CatchClauseObfuscator.ts +++ b/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); diff --git a/src/node-obfuscators/FunctionDeclarationObfuscator.ts b/src/node-obfuscators/FunctionDeclarationObfuscator.ts index 14c7e750..6cca873a 100644 --- a/src/node-obfuscators/FunctionDeclarationObfuscator.ts +++ b/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 ); diff --git a/src/node-obfuscators/FunctionObfuscator.ts b/src/node-obfuscators/FunctionObfuscator.ts index a49d00a4..eb105a0a 100644 --- a/src/node-obfuscators/FunctionObfuscator.ts +++ b/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 storeFunctionParams (functionNode: IFunctionNode): void { + functionNode.params + .forEach((paramsNode: INode) => { + estraverse.traverse(paramsNode, { + leave: (node: INode): any => this.storeIdentifiersNames(node, this.functionParams) + }); + }); } /** * @param functionNode */ private replaceFunctionParams (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, { + 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); } } diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.ts b/src/node-obfuscators/VariableDeclarationObfuscator.ts index 94878279..5942bf8f 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.ts +++ b/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,