refactoring

This commit is contained in:
sanex3339
2016-05-20 13:49:00 +03:00
parent 5730fc5fe6
commit 6212afecbb
18 changed files with 72 additions and 73 deletions
+12 -12
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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)}`;
}
/**
@@ -11,7 +11,7 @@ import { Utils } from '../Utils';
* replaces:
* try {} catch (e) { console.log(e); };
*
* by:
* on:
* try {} catch (_0x12d45f) { console.log(_0x12d45f); };
*
*/
@@ -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);
@@ -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
View File
@@ -11,7 +11,7 @@ import { Utils } from '../Utils';
* replaces:
* function foo (argument1) { return argument1; };
*
* by:
* on:
* function foo (_0x12d45f) { return _0x12d45f; };
*
*/
@@ -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
@@ -11,7 +11,7 @@ import { NodeUtils } from "../NodeUtils";
* function foo () { //... };
* foo();
*
* by:
* on:
* function _0x12d45f () { //... };
* _0x12d45f();
*/
@@ -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 {
@@ -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) => {
@@ -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;
-1
View File
@@ -1 +0,0 @@
/// <reference path="custom/estraverse/estraverse.d.ts" />
-25
View File
@@ -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
View File
@@ -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
View File
@@ -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;
}