From 3809f5445ad2634105e9c4b70a248efd8a27d6d8 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Fri, 23 Sep 2016 15:50:59 +0300 Subject: [PATCH] wip runtime error fix --- dist/index.js | 16 +++++----- src/Obfuscator.ts | 2 +- src/node-obfuscators/CatchClauseObfuscator.ts | 4 +-- .../FunctionDeclarationObfuscator.ts | 2 +- src/node-obfuscators/FunctionObfuscator.ts | 2 +- .../MemberExpressionObfuscator.ts | 2 +- .../MethodDefinitionObfuscator.ts | 2 +- .../VariableDeclarationObfuscator.ts | 2 +- .../FunctionObfuscator.spec.ts | 32 ++++++++++++++++--- .../VariableDeclarationObfuscator.spec.ts | 6 ++-- test/index.spec.ts | 1 + 11 files changed, 47 insertions(+), 24 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2fbe118c..7c54291b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1256,7 +1256,7 @@ var Obfuscator = function () { var _this2 = this; estraverse.replace(node, { - leave: function leave(node, parentNode) { + enter: function enter(node, parentNode) { _this2.initializeNodeObfuscators(node, parentNode); } }); @@ -2536,7 +2536,7 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) { var _this2 = this; NodeUtils_1.NodeUtils.typedReplace(catchClauseNode.param, NodeType_1.NodeType.Identifier, { - leave: function leave(node) { + enter: function enter(node) { return _this2.identifierReplacer.storeNames(node.name); } }); @@ -2547,7 +2547,7 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) { var _this3 = this; estraverse.replace(catchClauseNode, { - leave: function leave(node, parentNode) { + enter: function enter(node, parentNode) { if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode)) { node.name = _this3.identifierReplacer.replace(node.name); } @@ -2610,7 +2610,7 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeObfuscat) { var _this2 = this; NodeUtils_1.NodeUtils.typedReplace(functionDeclarationNode.id, NodeType_1.NodeType.Identifier, { - leave: function leave(node) { + enter: function enter(node) { return _this2.identifierReplacer.storeNames(node.name); } }); @@ -2683,7 +2683,7 @@ var FunctionObfuscator = function (_AbstractNodeObfuscat) { functionNode.params.forEach(function (paramsNode) { NodeUtils_1.NodeUtils.typedReplace(paramsNode, NodeType_1.NodeType.Identifier, { - leave: function leave(node) { + enter: function enter(node) { return _this2.identifierReplacer.storeNames(node.name); } }); @@ -2819,7 +2819,7 @@ var MemberExpressionObfuscator = function (_AbstractNodeObfuscat) { var _this2 = this; estraverse.replace(memberExpressionNode.property, { - leave: function leave(node, parentNode) { + enter: function enter(node, parentNode) { if (Nodes_1.Nodes.isLiteralNode(node)) { _this2.obfuscateLiteralProperty(node); return; @@ -2911,7 +2911,7 @@ var MethodDefinitionObfuscator = function (_AbstractNodeObfuscat) { var _this2 = this; estraverse.replace(methodDefinitionNode.key, { - leave: function leave(node) { + enter: function enter(node) { if (Nodes_1.Nodes.isIdentifierNode(node) && !Utils_1.Utils.arrayContains(_this2.ignoredNames, node.name) && methodDefinitionNode.computed === false) { methodDefinitionNode.computed = true; node.name = new StringLiteralReplacer_1.StringLiteralReplacer(_this2.nodes, _this2.options).replace(node.name); @@ -3061,7 +3061,7 @@ var VariableDeclarationObfuscator = function (_AbstractNodeObfuscat) { variableDeclarationNode.declarations.forEach(function (declarationNode) { NodeUtils_1.NodeUtils.typedReplace(declarationNode.id, NodeType_1.NodeType.Identifier, { - leave: function leave(node) { + enter: function enter(node) { return _this2.identifierReplacer.storeNames(node.name); } }); diff --git a/src/Obfuscator.ts b/src/Obfuscator.ts index 261d2fa2..61fc3317 100644 --- a/src/Obfuscator.ts +++ b/src/Obfuscator.ts @@ -133,7 +133,7 @@ export class Obfuscator implements IObfuscator { */ private obfuscate (node: ESTree.Node): void { estraverse.replace(node, { - leave: (node: ESTree.Node, parentNode: ESTree.Node): any => { + enter: (node: ESTree.Node, parentNode: ESTree.Node): any => { this.initializeNodeObfuscators(node, parentNode); } }); diff --git a/src/node-obfuscators/CatchClauseObfuscator.ts b/src/node-obfuscators/CatchClauseObfuscator.ts index 48241724..b36f8b4e 100644 --- a/src/node-obfuscators/CatchClauseObfuscator.ts +++ b/src/node-obfuscators/CatchClauseObfuscator.ts @@ -48,7 +48,7 @@ export class CatchClauseObfuscator extends AbstractNodeObfuscator { */ private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause): void { NodeUtils.typedReplace(catchClauseNode.param, NodeType.Identifier, { - leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) + enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) }); } @@ -57,7 +57,7 @@ export class CatchClauseObfuscator extends AbstractNodeObfuscator { */ private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause): void { estraverse.replace(catchClauseNode, { - leave: (node: ESTree.Node, parentNode: ESTree.Node): any => { + enter: (node: ESTree.Node, parentNode: ESTree.Node): any => { if (Nodes.isReplaceableIdentifierNode(node, parentNode)) { node.name = this.identifierReplacer.replace(node.name); } diff --git a/src/node-obfuscators/FunctionDeclarationObfuscator.ts b/src/node-obfuscators/FunctionDeclarationObfuscator.ts index 2d4905f5..05b1ac1e 100644 --- a/src/node-obfuscators/FunctionDeclarationObfuscator.ts +++ b/src/node-obfuscators/FunctionDeclarationObfuscator.ts @@ -54,7 +54,7 @@ export class FunctionDeclarationObfuscator extends AbstractNodeObfuscator { */ private storeFunctionName (functionDeclarationNode: ESTree.FunctionDeclaration): void { NodeUtils.typedReplace(functionDeclarationNode.id, NodeType.Identifier, { - leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) + enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) }); } diff --git a/src/node-obfuscators/FunctionObfuscator.ts b/src/node-obfuscators/FunctionObfuscator.ts index 3f046e2f..dcc50576 100644 --- a/src/node-obfuscators/FunctionObfuscator.ts +++ b/src/node-obfuscators/FunctionObfuscator.ts @@ -50,7 +50,7 @@ export class FunctionObfuscator extends AbstractNodeObfuscator { functionNode.params .forEach((paramsNode: ESTree.Node) => { NodeUtils.typedReplace(paramsNode, NodeType.Identifier, { - leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) + enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) }); }); } diff --git a/src/node-obfuscators/MemberExpressionObfuscator.ts b/src/node-obfuscators/MemberExpressionObfuscator.ts index 856c1e49..ed5e6669 100644 --- a/src/node-obfuscators/MemberExpressionObfuscator.ts +++ b/src/node-obfuscators/MemberExpressionObfuscator.ts @@ -14,7 +14,7 @@ export class MemberExpressionObfuscator extends AbstractNodeObfuscator { */ public obfuscateNode (memberExpressionNode: ESTree.MemberExpression): void { estraverse.replace(memberExpressionNode.property, { - leave: (node: ESTree.Node, parentNode: ESTree.Node): any => { + enter: (node: ESTree.Node, parentNode: ESTree.Node): any => { if (Nodes.isLiteralNode(node)) { this.obfuscateLiteralProperty(node); diff --git a/src/node-obfuscators/MethodDefinitionObfuscator.ts b/src/node-obfuscators/MethodDefinitionObfuscator.ts index d11d0c82..2902d9c9 100644 --- a/src/node-obfuscators/MethodDefinitionObfuscator.ts +++ b/src/node-obfuscators/MethodDefinitionObfuscator.ts @@ -32,7 +32,7 @@ export class MethodDefinitionObfuscator extends AbstractNodeObfuscator { */ private replaceMethodName (methodDefinitionNode: ESTree.MethodDefinition): void { estraverse.replace(methodDefinitionNode.key, { - leave: (node: ESTree.Node): any => { + enter: (node: ESTree.Node): any => { if ( Nodes.isIdentifierNode(node) && !Utils.arrayContains(this.ignoredNames, node.name) && diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.ts b/src/node-obfuscators/VariableDeclarationObfuscator.ts index 7540b477..e583ed12 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.ts +++ b/src/node-obfuscators/VariableDeclarationObfuscator.ts @@ -57,7 +57,7 @@ export class VariableDeclarationObfuscator extends AbstractNodeObfuscator { variableDeclarationNode.declarations .forEach((declarationNode: ESTree.VariableDeclarator) => { NodeUtils.typedReplace(declarationNode.id, NodeType.Identifier, { - leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) + enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name) }); }); } diff --git a/test/functional-tests/node-obfuscators/FunctionObfuscator.spec.ts b/test/functional-tests/node-obfuscators/FunctionObfuscator.spec.ts index fa05728d..368dcccd 100644 --- a/test/functional-tests/node-obfuscators/FunctionObfuscator.spec.ts +++ b/test/functional-tests/node-obfuscators/FunctionObfuscator.spec.ts @@ -7,14 +7,36 @@ import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator"; const assert: Chai.AssertStatic = require('chai').assert; describe('FunctionObfuscator', () => { - describe('obfuscation of identifiers of FunctionDeclaration and FunctionExpression node body', () => { - it('should replace identifier name with obfuscated one', () => { - let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate( - `var test = 'test';`, + describe('identifiers obfuscation inside `FunctionDeclaration` and `FunctionExpression` node body', () => { + it('should correct obfuscate both function parameter identifier and function body identifier with same name', () => { + const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate( + ` + (function () { + var test = function (test) { + console.log(test); + + if (true) { + var test = 5 + } + + return test; + } + })(); + `, Object.assign({}, NO_CUSTOM_NODES_PRESET) ); - assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *'\\x74\\x65\\x73\\x74';$/); + const obfuscatedCode: string = obfuscationResult.getObfuscatedCode(); + + const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode + .match(/var _0x[a-z0-9]{5,6} *= *function *\((_0x[a-z0-9]{5,6})\) *\{/); + const functionBodyIdentifierMatch: RegExpMatchArray|null = obfuscatedCode + .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{5,6})\)/); + + const functionParamIdentifierName: string = (functionParamIdentifierMatch)[1]; + const functionBodyIdentifierName: string = (functionBodyIdentifierMatch)[1]; + + assert.equal(functionParamIdentifierName, functionBodyIdentifierName); }); }); }); diff --git a/test/functional-tests/node-obfuscators/VariableDeclarationObfuscator.spec.ts b/test/functional-tests/node-obfuscators/VariableDeclarationObfuscator.spec.ts index 2a850310..794c2a42 100644 --- a/test/functional-tests/node-obfuscators/VariableDeclarationObfuscator.spec.ts +++ b/test/functional-tests/node-obfuscators/VariableDeclarationObfuscator.spec.ts @@ -114,7 +114,7 @@ describe('VariableDeclarationObfuscator', () => { ); }); - it('blablabla', () => { + it('should correct obfuscate variables inside function body', () => { const obfuscatedCode: string = obfuscationResult.getObfuscatedCode(); const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode .match(/function *_0x[a-z0-9]{5,6} *\((_0x[a-z0-9]{5,6})\,(_0x[a-z0-9]{5,6})\) *\{/); @@ -136,8 +136,8 @@ describe('VariableDeclarationObfuscator', () => { assert.notEqual(functionParamIdentifierName, constructorIdentifierName); assert.notEqual(functionParamIdentifierName, innerFunctionParamIdentifierName); - assert.notEqual(functionParamIdentifierName, objectIdentifierName); - assert.notEqual(functionParamIdentifierName, variableDeclarationIdentifierName); + assert.equal(functionParamIdentifierName, objectIdentifierName); + assert.equal(functionParamIdentifierName, variableDeclarationIdentifierName); assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName); assert.equal(variableDeclarationIdentifierName, objectIdentifierName); diff --git a/test/index.spec.ts b/test/index.spec.ts index f81b4eb3..5136a0d4 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -29,6 +29,7 @@ import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayCallsWra import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayDecodeNode.spec'; import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.spec'; import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayNode.spec'; +import './functional-tests/node-obfuscators/FunctionObfuscator.spec'; import './functional-tests/node-obfuscators/LiteralObfuscator.spec'; import './functional-tests/node-obfuscators/MemberExpressionObfuscator.spec'; import './functional-tests/node-obfuscators/MethodDefinitionObfuscator.spec';