diff --git a/src/NodeUtils.js b/src/NodeUtils.js index 507ab14f..ba4a695f 100644 --- a/src/NodeUtils.js +++ b/src/NodeUtils.js @@ -1,4 +1,5 @@ "use strict"; +const estraverse = require('estraverse'); const NodeType_1 = require("./enums/NodeType"); const Utils_1 = require("./Utils"); class NodeUtils { @@ -14,18 +15,18 @@ class NodeUtils { } return node; } - static getScopeOfNode(node, depth = 0) { + static getBlockScopeOfNode(node, depth = 0) { if (node.parentNode.type === NodeType_1.NodeType.Program) { return node.parentNode; } if (!Utils_1.Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) { - return NodeUtils.getScopeOfNode(node.parentNode, depth); + return NodeUtils.getBlockScopeOfNode(node.parentNode, depth); } if (depth > 0) { - return NodeUtils.getScopeOfNode(node.parentNode, --depth); + return NodeUtils.getBlockScopeOfNode(node.parentNode, --depth); } if (node.type !== NodeType_1.NodeType.BlockStatement) { - return NodeUtils.getScopeOfNode(node.parentNode); + return NodeUtils.getBlockScopeOfNode(node.parentNode); } return node; } @@ -71,6 +72,18 @@ class NodeUtils { static isVariableDeclaratorNode(node) { return node.type === NodeType_1.NodeType.VariableDeclarator; } + static parentize(node) { + estraverse.replace(node, { + enter: (node, parentNode) => { + Object.defineProperty(node, 'parentNode', { + configurable: true, + enumerable: true, + value: parentNode || node, + writable: true + }); + } + }); + } static prependNode(blockScopeBody, node) { if (!NodeUtils.validateNode(node)) { return; diff --git a/src/NodeUtils.ts b/src/NodeUtils.ts index 62f648e4..24c41bcf 100644 --- a/src/NodeUtils.ts +++ b/src/NodeUtils.ts @@ -1,6 +1,6 @@ +import * as estraverse from 'estraverse'; + import { IBlockStatementNode } from "./interfaces/nodes/IBlockStatementNode"; -import { ICatchClauseNode } from "./interfaces/nodes/ICatchClauseNode"; -import { IFunctionNode } from "./interfaces/nodes/IFunctionNode"; import { IIdentifierNode } from "./interfaces/nodes/IIdentifierNode"; import { ILiteralNode } from "./interfaces/nodes/ILiteralNode"; import { IMemberExpressionNode } from "./interfaces/nodes/IMemberExpressionNode"; @@ -9,6 +9,8 @@ import { IPropertyNode } from "./interfaces/nodes/IPropertyNode"; import { INode } from './interfaces/nodes/INode'; import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode"; +import { BlockScopeNode } from "./types/BlockScopeNode"; + import { NodeType } from "./enums/NodeType"; import { Utils } from "./Utils"; @@ -54,24 +56,24 @@ export class NodeUtils { * @param depth * @returns {INode} */ - public static getScopeOfNode (node: INode, depth: number = 0): INode { + public static getBlockScopeOfNode (node: INode, depth: number = 0): BlockScopeNode { if (node.parentNode.type === NodeType.Program) { - return node.parentNode; + return node.parentNode; } if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) { - return NodeUtils.getScopeOfNode(node.parentNode, depth); + return NodeUtils.getBlockScopeOfNode(node.parentNode, depth); } if (depth > 0) { - return NodeUtils.getScopeOfNode(node.parentNode, --depth); + return NodeUtils.getBlockScopeOfNode(node.parentNode, --depth); } if (node.type !== NodeType.BlockStatement) { - return NodeUtils.getScopeOfNode(node.parentNode); + return NodeUtils.getBlockScopeOfNode(node.parentNode); } - return node; // blocks statement of scopeNodes + return node; // blocks statement of scopeNodes } /** @@ -151,9 +153,7 @@ export class NodeUtils { * @param node * @returns {boolean} */ - public static isNodeHasBlockScope ( - node: INode - ): node is IBlockStatementNode|ICatchClauseNode|IFunctionNode|IProgramNode { + public static isNodeHasBlockScope (node: INode): node is BlockScopeNode { return node.hasOwnProperty('body'); } @@ -184,6 +184,22 @@ export class NodeUtils { return node.type === NodeType.VariableDeclarator; } + /** + * @param node + */ + public static parentize (node: INode): void { + estraverse.replace(node, { + enter: (node: INode, parentNode: INode): any => { + Object.defineProperty(node, 'parentNode', { + configurable: true, + enumerable: true, + value: parentNode || node, + writable: true + }); + } + }); + } + /** * @param blockScopeBody * @param node diff --git a/src/Obfuscator.js b/src/Obfuscator.js index 4243cc34..ff555f9d 100644 --- a/src/Obfuscator.js +++ b/src/Obfuscator.js @@ -10,6 +10,7 @@ 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 NodeUtils_1 = require("./NodeUtils"); const ObjectExpressionObfuscator_1 = require('./node-obfuscators/ObjectExpressionObfuscator'); const UnicodeArrayNode_1 = require('./custom-nodes/unicode-array-nodes/UnicodeArrayNode'); const UnicodeArrayNodesGroup_1 = require('./node-groups/UnicodeArrayNodesGroup'); @@ -37,13 +38,9 @@ class Obfuscator { } obfuscateNode(node) { this.setNewNodes(); + NodeUtils_1.NodeUtils.parentize(node); this.beforeObfuscation(node); - estraverse.replace(node, { - enter: (node, parent) => this.nodeControllerFirstPass(node, parent) - }); - estraverse.replace(node, { - leave: (node, parent) => this.nodeControllerSecondPass(node, parent) - }); + this.obfuscate(node); this.afterObfuscation(node); } setNode(nodeName, node) { @@ -58,18 +55,33 @@ class Obfuscator { afterObfuscation(astTree) { this.nodes.forEach((node) => { if (node.getAppendState() === AppendState_1.AppendState.AfterObfuscation) { - node.appendNode(astTree); + node.appendNode(NodeUtils_1.NodeUtils.getBlockScopeOfNode(astTree)); } }); } beforeObfuscation(astTree) { this.nodes.forEach((node) => { if (node.getAppendState() === AppendState_1.AppendState.BeforeObfuscation) { - node.appendNode(astTree); + node.appendNode(NodeUtils_1.NodeUtils.getBlockScopeOfNode(astTree)); } }); } ; + initializeNodeObfuscators(node, parentNode) { + if (!this.nodeObfuscators.has(node.type)) { + return; + } + this.nodeObfuscators.get(node.type).forEach((obfuscator) => { + new obfuscator(this.nodes).obfuscateNode(node, parentNode); + }); + } + obfuscate(node) { + estraverse.replace(node, { + leave: (node, parentNode) => { + this.initializeNodeObfuscators(node, parentNode); + } + }); + } setNewNodes() { if (this.options['disableConsoleOutput']) { this.setNode('consoleOutputDisableExpressionNode', new ConsoleOutputDisableExpressionNode_1.ConsoleOutputDisableExpressionNode()); @@ -84,27 +96,5 @@ class Obfuscator { this.setNode('unicodeArrayNode', new UnicodeArrayNode_1.UnicodeArrayNode(Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH))); } } - nodeControllerFirstPass(node, parent) { - Object.defineProperty(node, 'parentNode', { - configurable: true, - enumerable: true, - value: parent || node, - writable: true - }); - } - nodeControllerSecondPass(node, parent) { - switch (node.type) { - default: - this.initializeNodeObfuscators(node, parent); - } - } - initializeNodeObfuscators(node, parent) { - if (!this.nodeObfuscators.has(node.type)) { - return; - } - this.nodeObfuscators.get(node.type).forEach((obfuscator) => { - new obfuscator(this.nodes).obfuscateNode(node, parent); - }); - } } exports.Obfuscator = Obfuscator; diff --git a/src/Obfuscator.ts b/src/Obfuscator.ts index 9725b324..e343579d 100644 --- a/src/Obfuscator.ts +++ b/src/Obfuscator.ts @@ -16,6 +16,7 @@ import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator'; import { LiteralObfuscator } from './node-obfuscators/LiteralObfuscator'; import { MemberExpressionObfuscator } from './node-obfuscators/MemberExpressionObfuscator'; import { MethodDefinitionObfuscator } from './node-obfuscators/MethodDefinitionObfuscator'; +import { NodeUtils } from "./NodeUtils"; import { ObjectExpressionObfuscator } from './node-obfuscators/ObjectExpressionObfuscator'; import { UnicodeArrayNode } from './custom-nodes/unicode-array-nodes/UnicodeArrayNode'; import { UnicodeArrayNodesGroup } from './node-groups/UnicodeArrayNodesGroup'; @@ -64,16 +65,11 @@ export class Obfuscator { */ public obfuscateNode (node: INode): void { this.setNewNodes(); + + NodeUtils.parentize(node); + this.beforeObfuscation(node); - - estraverse.replace(node, { - enter: (node: INode, parent: INode): any => this.nodeControllerFirstPass(node, parent) - }); - - estraverse.replace(node, { - leave: (node: INode, parent: INode): any => this.nodeControllerSecondPass(node, parent) - }); - + this.obfuscate(node); this.afterObfuscation(node); } @@ -102,7 +98,7 @@ export class Obfuscator { private afterObfuscation (astTree: INode): void { this.nodes.forEach((node: ICustomNode) => { if (node.getAppendState() === AppendState.AfterObfuscation) { - node.appendNode(astTree); + node.appendNode(NodeUtils.getBlockScopeOfNode(astTree)); } }); } @@ -113,11 +109,37 @@ export class Obfuscator { private beforeObfuscation (astTree: INode): void { this.nodes.forEach((node: ICustomNode) => { if (node.getAppendState() === AppendState.BeforeObfuscation) { - node.appendNode(astTree); + node.appendNode(NodeUtils.getBlockScopeOfNode(astTree)); } }); }; + + /** + * @param node + * @param parentNode + */ + private initializeNodeObfuscators (node: INode, parentNode: INode): void { + if (!this.nodeObfuscators.has(node.type)) { + return; + } + + this.nodeObfuscators.get(node.type).forEach((obfuscator: Function) => { + new ( obfuscator(this.nodes)).obfuscateNode(node, parentNode); + }); + } + + /** + * @param node + */ + private obfuscate (node: INode): void { + estraverse.replace(node, { + leave: (node: INode, parentNode: INode): any => { + this.initializeNodeObfuscators(node, parentNode); + } + }); + } + private setNewNodes (): void { if (this.options['disableConsoleOutput']) { this.setNode( @@ -142,42 +164,4 @@ export class Obfuscator { ); } } - - /** - * @param node - * @param parent - */ - private nodeControllerFirstPass (node: INode, parent: INode): void { - Object.defineProperty(node, 'parentNode', { - configurable: true, - enumerable: true, - value: parent || node, - writable: true - }); - } - - /** - * @param node - * @param parent - */ - private nodeControllerSecondPass (node: INode, parent: INode): void { - switch (node.type) { - default: - this.initializeNodeObfuscators(node, parent); - } - } - - /** - * @param node - * @param parent - */ - private initializeNodeObfuscators (node: INode, parent: INode): void { - if (!this.nodeObfuscators.has(node.type)) { - return; - } - - this.nodeObfuscators.get(node.type).forEach((obfuscator: Function) => { - new ( obfuscator(this.nodes)).obfuscateNode(node, parent); - }); - } } diff --git a/src/custom-nodes/Node.js b/src/custom-nodes/Node.js index 21b87f0d..48eeae59 100644 --- a/src/custom-nodes/Node.js +++ b/src/custom-nodes/Node.js @@ -1,5 +1,6 @@ "use strict"; const AppendState_1 = require('../enums/AppendState'); +const NodeUtils_1 = require("../NodeUtils"); class Node { constructor() { this.appendState = AppendState_1.AppendState.BeforeObfuscation; @@ -8,6 +9,7 @@ class Node { return this.appendState; } getNode() { + NodeUtils_1.NodeUtils.parentize(this.node); return this.node; } setNode(node) { diff --git a/src/custom-nodes/Node.ts b/src/custom-nodes/Node.ts index 489f1575..a6e4a8b5 100644 --- a/src/custom-nodes/Node.ts +++ b/src/custom-nodes/Node.ts @@ -3,6 +3,8 @@ import { INode } from '../interfaces/nodes/INode'; import { AppendState } from '../enums/AppendState'; +import { NodeUtils } from "../NodeUtils"; + export abstract class Node implements ICustomNode { /** * @type {AppendState} @@ -29,6 +31,8 @@ export abstract class Node implements ICustomNode { * @returns any */ public getNode (): INode { + NodeUtils.parentize(this.node); + return this.node; } diff --git a/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.js b/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.js index 1ce5847b..b2012341 100644 --- a/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.js +++ b/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.js @@ -1,6 +1,5 @@ "use strict"; const esprima = require('esprima'); -const estraverse = require('estraverse'); const Node_1 = require('../Node'); const NodeUtils_1 = require("../../NodeUtils"); class ConsoleOutputDisableExpressionNode extends Node_1.Node { @@ -8,16 +7,8 @@ class ConsoleOutputDisableExpressionNode extends Node_1.Node { super(); this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - NodeUtils_1.NodeUtils.prependNode(node.body, this.getNode()); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + NodeUtils_1.NodeUtils.prependNode(blockScopeNode.body, this.getNode()); } getNodeStructure() { return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(esprima.parse(` diff --git a/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.ts b/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.ts index 08fd3e92..0c6268b9 100644 --- a/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.ts +++ b/src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.ts @@ -1,7 +1,6 @@ import * as esprima from 'esprima'; -import * as estraverse from 'estraverse'; -import { INode } from '../../interfaces/nodes/INode'; +import { BlockScopeNode } from "../../types/BlockScopeNode"; import { Node } from '../Node'; import { NodeUtils } from "../../NodeUtils"; @@ -14,20 +13,10 @@ export class ConsoleOutputDisableExpressionNode extends Node { } /** - * @param astTree + * @param blockScopeNode */ - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - NodeUtils.prependNode(node.body, this.getNode()); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.prependNode(blockScopeNode.body, this.getNode()); } /** diff --git a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.js b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.js index 57022c7a..6e24c47a 100644 --- a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.js +++ b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.js @@ -1,5 +1,4 @@ "use strict"; -const estraverse = require('estraverse'); const NodeType_1 = require("../../enums/NodeType"); const Node_1 = require('../Node'); const NodeUtils_1 = require("../../NodeUtils"); @@ -9,16 +8,8 @@ class DebugProtectionFunctionCallNode extends Node_1.Node { this.debugProtectionFunctionName = debugProtectionFunctionName; this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - NodeUtils_1.NodeUtils.appendNode(node.body, this.getNode()); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + NodeUtils_1.NodeUtils.appendNode(blockScopeNode.body, this.getNode()); } getNodeStructure() { return { diff --git a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.ts b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.ts index 0e56ace8..e3e13851 100644 --- a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.ts +++ b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.ts @@ -1,6 +1,4 @@ -import * as estraverse from 'estraverse'; - -import { INode } from '../../interfaces/nodes/INode'; +import { BlockScopeNode } from "../../types/BlockScopeNode"; import { NodeType } from "../../enums/NodeType"; @@ -25,20 +23,10 @@ export class DebugProtectionFunctionCallNode extends Node { } /** - * @param astTree + * @param blockScopeNode */ - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - NodeUtils.appendNode(node.body, this.getNode()); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.appendNode(blockScopeNode.body, this.getNode()); } /** diff --git a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.js b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.js index 61e4f56a..cf656e36 100644 --- a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.js +++ b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.js @@ -1,5 +1,4 @@ "use strict"; -const estraverse = require('estraverse'); const NodeType_1 = require('../../enums/NodeType'); const Node_1 = require('../Node'); const NodeUtils_1 = require('../../NodeUtils'); @@ -9,16 +8,8 @@ class DebugProtectionFunctionIntervalNode extends Node_1.Node { this.debugProtectionFunctionName = debugProtectionFunctionName; this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - NodeUtils_1.NodeUtils.appendNode(node.body, this.getNode()); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + NodeUtils_1.NodeUtils.appendNode(blockScopeNode.body, this.getNode()); } getNodeStructure() { return { diff --git a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.ts b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.ts index 161b78e6..589c3392 100644 --- a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.ts +++ b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.ts @@ -1,6 +1,4 @@ -import * as estraverse from 'estraverse'; - -import { INode } from '../../interfaces/nodes/INode'; +import { BlockScopeNode } from "../../types/BlockScopeNode"; import { NodeType } from '../../enums/NodeType'; @@ -25,20 +23,10 @@ export class DebugProtectionFunctionIntervalNode extends Node { } /** - * @param astTree + * @param blockScopeNode */ - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - NodeUtils.appendNode(node.body, this.getNode()); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.appendNode(blockScopeNode.body, this.getNode()); } /** diff --git a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.js b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.js index f1deae27..0db39ca5 100644 --- a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.js +++ b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.js @@ -1,6 +1,5 @@ "use strict"; const esprima = require('esprima'); -const estraverse = require('estraverse'); const Node_1 = require('../Node'); const NodeUtils_1 = require('../../NodeUtils'); const Utils_1 = require("../../Utils"); @@ -10,17 +9,9 @@ class DebugProtectionFunctionNode extends Node_1.Node { this.debugProtectionFunctionName = debugProtectionFunctionName; this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - let programBodyLength = node.body.length, randomIndex = Utils_1.Utils.getRandomInteger(0, programBodyLength); - NodeUtils_1.NodeUtils.insertNodeAtIndex(node.body, this.getNode(), randomIndex); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + let programBodyLength = blockScopeNode.body.length, randomIndex = Utils_1.Utils.getRandomInteger(0, programBodyLength); + NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex); } getNodeIdentifier() { return this.debugProtectionFunctionName; diff --git a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts index a96de225..7c2fe8fa 100644 --- a/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts +++ b/src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts @@ -1,7 +1,6 @@ import * as esprima from 'esprima'; -import * as estraverse from 'estraverse'; -import { INode } from '../../interfaces/nodes/INode'; +import { BlockScopeNode } from "../../types/BlockScopeNode"; import { Node } from '../Node'; import { NodeUtils } from '../../NodeUtils'; @@ -27,23 +26,13 @@ export class DebugProtectionFunctionNode extends Node { } /** - * @param astTree + * @param blockScopeNode */ - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - let programBodyLength: number = node.body.length, - randomIndex = Utils.getRandomInteger(0, programBodyLength); + public appendNode (blockScopeNode: BlockScopeNode): void { + let programBodyLength: number = blockScopeNode.body.length, + randomIndex = Utils.getRandomInteger(0, programBodyLength); - NodeUtils.insertNodeAtIndex(node.body, this.getNode(), randomIndex); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex); } /** diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.js b/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.js index 93dc5416..ccfa8aba 100644 --- a/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.js +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.js @@ -1,6 +1,5 @@ "use strict"; const escodegen = require('escodegen'); -const estraverse = require('estraverse'); const AppendState_1 = require('../../enums/AppendState'); const NodeType_1 = require("../../enums/NodeType"); const Node_1 = require('../Node'); @@ -15,16 +14,8 @@ class UnicodeArrayNode extends Node_1.Node { this.unicodeArrayRotateValue = unicodeArrayRotateValue; this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - NodeUtils_1.NodeUtils.prependNode(node.body, this.getNode()); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + NodeUtils_1.NodeUtils.prependNode(blockScopeNode.body, this.getNode()); } getNodeIdentifier() { return this.unicodeArrayName; diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.ts b/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.ts index f5b51981..3c4d67dd 100644 --- a/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.ts +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.ts @@ -1,8 +1,9 @@ import * as escodegen from 'escodegen'; -import * as estraverse from 'estraverse'; import { INode } from '../../interfaces/nodes/INode'; +import { BlockScopeNode } from "../../types/BlockScopeNode"; + import { AppendState } from '../../enums/AppendState'; import { NodeType } from "../../enums/NodeType"; @@ -50,20 +51,10 @@ export class UnicodeArrayNode extends Node { } /** - * @param astTree + * @param blockScopeNode */ - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - NodeUtils.prependNode(node.body, this.getNode()); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.prependNode(blockScopeNode.body, this.getNode()); } /** diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.js b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.js index ce0ab000..132450b1 100644 --- a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.js +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.js @@ -1,5 +1,4 @@ "use strict"; -const estraverse = require('estraverse'); const NodeType_1 = require("../../enums/NodeType"); const Node_1 = require('../Node'); const NodeUtils_1 = require("../../NodeUtils"); @@ -11,16 +10,8 @@ class UnicodeArrayRotateFunctionCallNode extends Node_1.Node { this.unicodeArrayRotateValue = unicodeArrayRotateValue; this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - NodeUtils_1.NodeUtils.prependNode(node.body, this.getNode()); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + NodeUtils_1.NodeUtils.prependNode(blockScopeNode.body, this.getNode()); } getNodeStructure() { return { diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.ts b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.ts index cf5e83e0..0be952a4 100644 --- a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.ts +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionCallNode.ts @@ -1,6 +1,4 @@ -import * as estraverse from 'estraverse'; - -import { INode } from "../../interfaces/nodes/INode"; +import { BlockScopeNode } from "../../types/BlockScopeNode"; import { NodeType } from "../../enums/NodeType"; @@ -42,18 +40,11 @@ export class UnicodeArrayRotateFunctionCallNode extends Node { this.node = this.getNodeStructure(); } - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - NodeUtils.prependNode(node.body, this.getNode()); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + /** + * @param blockScopeNode + */ + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.prependNode(blockScopeNode.body, this.getNode()); } /** diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js index 4f935aae..45149d8e 100644 --- a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js @@ -1,5 +1,4 @@ "use strict"; -const estraverse = require('estraverse'); const NodeType_1 = require("../../enums/NodeType"); const Node_1 = require('../Node'); const NodeUtils_1 = require("../../NodeUtils"); @@ -11,16 +10,8 @@ class UnicodeArrayRotateFunctionNode extends Node_1.Node { this.unicodeArrayName = unicodeArrayName; this.node = this.getNodeStructure(); } - appendNode(astTree) { - estraverse.replace(astTree, { - leave: (node, parent) => { - if (NodeUtils_1.NodeUtils.isProgramNode(node)) { - NodeUtils_1.NodeUtils.appendNode(node.body, this.getNode()); - return estraverse.VisitorOption.Break; - } - return estraverse.VisitorOption.Skip; - } - }); + appendNode(blockScopeNode) { + NodeUtils_1.NodeUtils.appendNode(blockScopeNode.body, this.getNode()); } getNodeIdentifier() { return this.unicodeArrayRotateFunctionName; diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts index ddc4912d..f4b99f8e 100644 --- a/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts @@ -1,6 +1,4 @@ -import * as estraverse from 'estraverse'; - -import { INode } from '../../interfaces/nodes/INode'; +import { BlockScopeNode } from "../../types/BlockScopeNode"; import { NodeType } from "../../enums/NodeType"; @@ -36,20 +34,10 @@ export class UnicodeArrayRotateFunctionNode extends Node { } /** - * @param astTree + * @param blockScopeNode */ - public appendNode (astTree: INode): void { - estraverse.replace(astTree, { - leave: (node: INode, parent: INode): any => { - if (NodeUtils.isProgramNode(node)) { - NodeUtils.appendNode(node.body, this.getNode()); - - return estraverse.VisitorOption.Break; - } - - return estraverse.VisitorOption.Skip; - } - }); + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.appendNode(blockScopeNode.body, this.getNode()); } /** diff --git a/src/node-obfuscators/FunctionDeclarationObfuscator.js b/src/node-obfuscators/FunctionDeclarationObfuscator.js index 692b3357..147a7666 100644 --- a/src/node-obfuscators/FunctionDeclarationObfuscator.js +++ b/src/node-obfuscators/FunctionDeclarationObfuscator.js @@ -29,7 +29,7 @@ class FunctionDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator { }); } replaceFunctionCalls(functionDeclarationNode) { - let scopeNode = NodeUtils_1.NodeUtils.getScopeOfNode(functionDeclarationNode); + let scopeNode = NodeUtils_1.NodeUtils.getBlockScopeOfNode(functionDeclarationNode); estraverse.replace(scopeNode, { enter: (node, parentNode) => { this.replaceNodeIdentifierByNewValue(node, parentNode, this.functionName); diff --git a/src/node-obfuscators/FunctionDeclarationObfuscator.ts b/src/node-obfuscators/FunctionDeclarationObfuscator.ts index ccc47357..880c981e 100644 --- a/src/node-obfuscators/FunctionDeclarationObfuscator.ts +++ b/src/node-obfuscators/FunctionDeclarationObfuscator.ts @@ -59,7 +59,7 @@ export class FunctionDeclarationObfuscator extends NodeObfuscator { * @param functionDeclarationNode */ private replaceFunctionCalls (functionDeclarationNode: IFunctionDeclarationNode): void { - let scopeNode: INode = NodeUtils.getScopeOfNode( + let scopeNode: INode = NodeUtils.getBlockScopeOfNode( functionDeclarationNode ); diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.js b/src/node-obfuscators/VariableDeclarationObfuscator.js index bbce3586..74d44942 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.js +++ b/src/node-obfuscators/VariableDeclarationObfuscator.js @@ -32,7 +32,7 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator { } replaceVariableCalls(variableDeclarationNode, variableParentNode) { let scopeNode; - scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getScopeOfNode(variableDeclarationNode) : variableParentNode; + scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getBlockScopeOfNode(variableDeclarationNode) : variableParentNode; let isNodeAfterVariableDeclaratorFlag = false; estraverse.replace(scopeNode, { enter: (node, parentNode) => { diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.ts b/src/node-obfuscators/VariableDeclarationObfuscator.ts index 66dca16d..79caf367 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.ts +++ b/src/node-obfuscators/VariableDeclarationObfuscator.ts @@ -66,7 +66,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator { private replaceVariableCalls (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: INode): void { let scopeNode: INode; - scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils.getScopeOfNode( + scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils.getBlockScopeOfNode( variableDeclarationNode ) : variableParentNode; diff --git a/src/types/BlockScopeNode.d.ts b/src/types/BlockScopeNode.d.ts new file mode 100644 index 00000000..b463d6a2 --- /dev/null +++ b/src/types/BlockScopeNode.d.ts @@ -0,0 +1,6 @@ +import { IBlockStatementNode } from "../interfaces/nodes/IBlockStatementNode"; +import { ICatchClauseNode } from "../interfaces/nodes/ICatchClauseNode"; +import { IFunctionNode } from "../interfaces/nodes/IFunctionNode"; +import { IProgramNode } from "../interfaces/nodes/IProgramNode"; + +export type BlockScopeNode = IBlockStatementNode|ICatchClauseNode|IFunctionNode|IProgramNode; \ No newline at end of file