Browse Source

refactoring

sanex3339 9 years ago
parent
commit
6212afecbb

+ 12 - 12
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;

+ 17 - 14
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

+ 2 - 5
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']) {

+ 2 - 2
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) => {

+ 4 - 2
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)}`;
     }
 
     /**

+ 1 - 1
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); };
  *
  */

+ 1 - 1
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);

+ 2 - 2
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
         );
 

+ 1 - 1
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; };
  *
  */

+ 2 - 2
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

+ 1 - 1
src/node-obfuscators/MethodDefinitionObfuscator.ts

@@ -11,7 +11,7 @@ import { NodeUtils } from "../NodeUtils";
  *     function foo () { //... };
  *     foo();
  *
- * by:
+ * on:
  *     function _0x12d45f () { //... };
  *     _0x12d45f();
  */

+ 1 - 1
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 {

+ 1 - 1
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) => {

+ 2 - 2
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;
 

+ 0 - 1
typings/custom.d.ts

@@ -1 +0,0 @@
-/// <reference path="custom/estraverse/estraverse.d.ts" />

+ 0 - 25
typings/custom/estraverse/estraverse.d.ts

@@ -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;
-}

+ 1 - 0
typings/main.d.ts

@@ -1,4 +1,5 @@
 /// <reference path="main/ambient/escodegen/index.d.ts" />
+/// <reference path="main/ambient/estraverse/index.d.ts" />
 /// <reference path="main/ambient/esprima/index.d.ts" />
 /// <reference path="main/ambient/estree/index.d.ts" />
 /// <reference path="main/ambient/node/index.d.ts" />

+ 22 - 0
typings/main/ambient/estraverse/index.d.ts

@@ -0,0 +1,22 @@
+// Type definitions for estraverse
+// Project: https://github.com/estools/estraverse
+// Definitions by: Sanex3339 <https://github.com/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;
+}