diff --git a/src/NodeUtils.js b/src/NodeUtils.js index 18625af5..76c9766c 100644 --- a/src/NodeUtils.js +++ b/src/NodeUtils.js @@ -1,23 +1,17 @@ "use strict"; class NodeUtils { - static getNodeScope(node, deep = 0) { - let scopeNodes = [ - 'FunctionDeclaration', - 'FunctionExpression', - 'ArrowFunctionExpression', - 'MethodDefinition' - ]; + static getScopeOfNode(node, depth = 0) { if (node.parentNode.type === 'Program') { return node.parentNode; } - if (scopeNodes.indexOf(node.parentNode.type) < 0) { - return NodeUtils.getNodeScope(node.parentNode, deep); + if (NodeUtils.scopeNodes.indexOf(node.parentNode.type) < 0) { + return NodeUtils.getScopeOfNode(node.parentNode, depth); } - if (deep > 0) { - return NodeUtils.getNodeScope(node.parentNode, --deep); + if (depth > 0) { + return NodeUtils.getScopeOfNode(node.parentNode, --depth); } if (node.type !== 'BlockStatement') { - return NodeUtils.getNodeScope(node.parentNode); + return NodeUtils.getScopeOfNode(node.parentNode); } return node; } @@ -52,4 +46,10 @@ class NodeUtils { return node.type === 'VariableDeclarator'; } } +NodeUtils.scopeNodes = [ + 'ArrowFunctionExpression', + 'FunctionDeclaration', + 'FunctionExpression', + 'MethodDefinition' +]; exports.NodeUtils = NodeUtils; diff --git a/src/NodeUtils.ts b/src/NodeUtils.ts index 0e0f8cb4..0cab70c2 100644 --- a/src/NodeUtils.ts +++ b/src/NodeUtils.ts @@ -7,33 +7,36 @@ import { ITreeNode } from './interfaces/nodes/ITreeNode'; import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode"; export class NodeUtils { + /** + * @type {string[]} + */ + private static scopeNodes: string[] = [ + 'ArrowFunctionExpression', + 'FunctionDeclaration', + 'FunctionExpression', + 'MethodDefinition' + ]; + /** * @param node - * @param deep + * @param depth * @returns {ITreeNode} */ - public static getNodeScope (node: ITreeNode, deep: number = 0): ITreeNode { - let scopeNodes: string[] = [ - 'FunctionDeclaration', - 'FunctionExpression', - 'ArrowFunctionExpression', - 'MethodDefinition' - ]; - + public static getScopeOfNode (node: ITreeNode, depth: number = 0): ITreeNode { if (node.parentNode.type === 'Program') { return node.parentNode; } - if (scopeNodes.indexOf(node.parentNode.type) < 0) { - return NodeUtils.getNodeScope(node.parentNode, deep); + if (NodeUtils.scopeNodes.indexOf(node.parentNode.type) < 0) { + return NodeUtils.getScopeOfNode(node.parentNode, depth); } - if (deep > 0) { - return NodeUtils.getNodeScope(node.parentNode, --deep); + if (depth > 0) { + return NodeUtils.getScopeOfNode(node.parentNode, --depth); } if (node.type !== 'BlockStatement') { - return NodeUtils.getNodeScope(node.parentNode); + return NodeUtils.getScopeOfNode(node.parentNode); } return node; // BlockStatement of scopeNodes diff --git a/src/Obfuscator.js b/src/Obfuscator.js index 4c2472d4..532e41d1 100644 --- a/src/Obfuscator.js +++ b/src/Obfuscator.js @@ -13,7 +13,7 @@ const UnicodeArrayNodesGroup_1 = require('./node-groups/UnicodeArrayNodesGroup') const Utils_1 = require('./Utils'); const VariableDeclarationObfuscator_1 = require('./node-obfuscators/VariableDeclarationObfuscator'); class Obfuscator { - constructor(options) { + constructor(options = {}) { this.nodes = new Map(); this.nodeObfuscators = new Map([ ['ClassDeclaration', [FunctionDeclarationObfuscator_1.FunctionDeclarationObfuscator]], @@ -30,10 +30,7 @@ class Obfuscator { ['MemberExpression', [MemberExpressionObfuscator_1.MemberExpressionObfuscator]], ['Literal', [LiteralObfuscator_1.LiteralObfuscator]] ]); - this.options = { - rotateUnicodeArray: true - }; - Object.assign(this.options, options); + this.options = options; } obfuscateNode(node) { if (this.options['rotateUnicodeArray']) { diff --git a/src/Utils.js b/src/Utils.js index e3440ea9..7da523f1 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -24,8 +24,8 @@ class Utils { return Math.floor(Math.random() * (max - min + 1)) + min; } static getRandomVariableName(length = 6) { - const prefix = '_0x'; - return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 99999999))).substr(0, length)}`; + const rangeMinInteger = 10000, rangeMaxInteger = 99999999, prefix = '_0x'; + return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(rangeMinInteger, rangeMaxInteger))).substr(0, length)}`; } static stringToUnicode(string) { return `'${string.replace(/[\s\S]/g, (escape) => { diff --git a/src/Utils.ts b/src/Utils.ts index e07a700b..8ffb94e6 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -48,9 +48,11 @@ export class Utils { * @returns any */ public static getRandomVariableName (length: number = 6): string { - const prefix = '_0x'; + const rangeMinInteger: number = 10000, + rangeMaxInteger: number = 99999999, + prefix: string = '_0x'; - return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 99999999))).substr(0, length)}`; + return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(rangeMinInteger, rangeMaxInteger))).substr(0, length)}`; } /** diff --git a/src/node-obfuscators/CatchClauseObfuscator.ts b/src/node-obfuscators/CatchClauseObfuscator.ts index f560e38c..38a03244 100644 --- a/src/node-obfuscators/CatchClauseObfuscator.ts +++ b/src/node-obfuscators/CatchClauseObfuscator.ts @@ -11,7 +11,7 @@ import { Utils } from '../Utils'; * replaces: * try {} catch (e) { console.log(e); }; * - * by: + * on: * try {} catch (_0x12d45f) { console.log(_0x12d45f); }; * */ diff --git a/src/node-obfuscators/FunctionDeclarationObfuscator.js b/src/node-obfuscators/FunctionDeclarationObfuscator.js index 3034542e..f84f8ff2 100644 --- a/src/node-obfuscators/FunctionDeclarationObfuscator.js +++ b/src/node-obfuscators/FunctionDeclarationObfuscator.js @@ -28,7 +28,7 @@ class FunctionDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator { }); } replaceFunctionCalls(functionDeclarationNode) { - let scopeNode = NodeUtils_1.NodeUtils.getNodeScope(functionDeclarationNode); + let scopeNode = NodeUtils_1.NodeUtils.getScopeOfNode(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 3aa54d64..b7570c63 100644 --- a/src/node-obfuscators/FunctionDeclarationObfuscator.ts +++ b/src/node-obfuscators/FunctionDeclarationObfuscator.ts @@ -12,7 +12,7 @@ import { Utils } from '../Utils'; * function foo () { //... }; * foo(); * - * by: + * on: * function _0x12d45f () { //... }; * _0x12d45f(); */ @@ -57,7 +57,7 @@ export class FunctionDeclarationObfuscator extends NodeObfuscator { * @param functionDeclarationNode */ private replaceFunctionCalls (functionDeclarationNode: IFunctionDeclarationNode): void { - let scopeNode: ITreeNode = NodeUtils.getNodeScope( + let scopeNode: ITreeNode = NodeUtils.getScopeOfNode( functionDeclarationNode ); diff --git a/src/node-obfuscators/FunctionObfuscator.ts b/src/node-obfuscators/FunctionObfuscator.ts index 19d7216f..92c77cf5 100644 --- a/src/node-obfuscators/FunctionObfuscator.ts +++ b/src/node-obfuscators/FunctionObfuscator.ts @@ -11,7 +11,7 @@ import { Utils } from '../Utils'; * replaces: * function foo (argument1) { return argument1; }; * - * by: + * on: * function foo (_0x12d45f) { return _0x12d45f; }; * */ diff --git a/src/node-obfuscators/MemberExpressionObfuscator.ts b/src/node-obfuscators/MemberExpressionObfuscator.ts index ab1fc145..d6f2d372 100644 --- a/src/node-obfuscators/MemberExpressionObfuscator.ts +++ b/src/node-obfuscators/MemberExpressionObfuscator.ts @@ -38,7 +38,7 @@ export class MemberExpressionObfuscator extends NodeObfuscator { * replaces: * object.identifier = 1; * - * by: + * on: * object[_0x23d45[25]] = 1; * * and skip: @@ -67,7 +67,7 @@ export class MemberExpressionObfuscator extends NodeObfuscator { * replaces: * object['literal'] = 1; * - * by: + * on: * object[_0x23d45[25]] = 1; * * @param node diff --git a/src/node-obfuscators/MethodDefinitionObfuscator.ts b/src/node-obfuscators/MethodDefinitionObfuscator.ts index d1d9cc7b..db287f3b 100644 --- a/src/node-obfuscators/MethodDefinitionObfuscator.ts +++ b/src/node-obfuscators/MethodDefinitionObfuscator.ts @@ -11,7 +11,7 @@ import { NodeUtils } from "../NodeUtils"; * function foo () { //... }; * foo(); * - * by: + * on: * function _0x12d45f () { //... }; * _0x12d45f(); */ diff --git a/src/node-obfuscators/ObjectExpressionObfuscator.ts b/src/node-obfuscators/ObjectExpressionObfuscator.ts index bb260545..52d9c1de 100644 --- a/src/node-obfuscators/ObjectExpressionObfuscator.ts +++ b/src/node-obfuscators/ObjectExpressionObfuscator.ts @@ -17,7 +17,7 @@ import { Utils } from '../Utils'; * or: * var object = { PSEUDO: 1 }; * - * by: + * on: * var object = { '\u0050\u0053\u0045\u0055\u0044\u004f': 1 }; */ export class ObjectExpressionObfuscator extends NodeObfuscator { diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.js b/src/node-obfuscators/VariableDeclarationObfuscator.js index 5fba76a9..5b0d7461 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.js +++ b/src/node-obfuscators/VariableDeclarationObfuscator.js @@ -35,7 +35,7 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator { } replaceVariableCalls(variableDeclarationNode, variableParentNode) { let scopeNode; - scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getNodeScope(variableDeclarationNode) : variableParentNode; + scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getScopeOfNode(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 bdaccc36..36e3d98b 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.ts +++ b/src/node-obfuscators/VariableDeclarationObfuscator.ts @@ -13,7 +13,7 @@ import { Utils } from '../Utils'; * var variable = 1; * variable++; * - * by: + * on: * var _0x12d45f = 1; * _0x12d45f++; * @@ -68,7 +68,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator { private replaceVariableCalls (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: ITreeNode): void { let scopeNode: ITreeNode; - scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils.getNodeScope( + scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils.getScopeOfNode( variableDeclarationNode ) : variableParentNode; diff --git a/typings/custom.d.ts b/typings/custom.d.ts deleted file mode 100644 index 92a1d861..00000000 --- a/typings/custom.d.ts +++ /dev/null @@ -1 +0,0 @@ -/// \ No newline at end of file diff --git a/typings/custom/estraverse/estraverse.d.ts b/typings/custom/estraverse/estraverse.d.ts deleted file mode 100644 index 64a59511..00000000 --- a/typings/custom/estraverse/estraverse.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -declare namespace estraverse { - interface Callbacks { - enter?: (node: any, parent: any) => any; - leave?: (node: any, parent: any) => any; - - fallback?: string; - - // Methods provided for you, don't override. - break?: () => void; - remove?: () => void; - skip?: () => void; - keys?: {}; - } - - enum VisitorOption { - Skip, Break, Remove - } - - function traverse (astTree: any, callbacks: Callbacks): any; - function replace (astTree: any, callbacks: Callbacks): any; -} - -declare module "estraverse" { - export = estraverse; -} \ No newline at end of file diff --git a/typings/main.d.ts b/typings/main.d.ts index e89af714..c2cb8d0f 100644 --- a/typings/main.d.ts +++ b/typings/main.d.ts @@ -1,4 +1,5 @@ /// +/// /// /// /// diff --git a/typings/main/ambient/estraverse/index.d.ts b/typings/main/ambient/estraverse/index.d.ts new file mode 100644 index 00000000..69b4d5d3 --- /dev/null +++ b/typings/main/ambient/estraverse/index.d.ts @@ -0,0 +1,22 @@ +// Type definitions for estraverse +// Project: https://github.com/estools/estraverse +// Definitions by: Sanex3339 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module 'estraverse' { + export interface Visitor { + enter?: (node: any, parentNode: any) => any; + leave?: (node: any, parentNode: any) => any; + + fallback?: string; + + keys?: {}; + } + + export enum VisitorOption { + Skip, Break, Remove + } + + export function traverse (ast: any, visitor: Visitor): any; + export function replace (ast: any, visitor: Visitor): any; +} \ No newline at end of file