Added node interfaces

This commit is contained in:
kachalov
2016-05-13 17:47:20 +03:00
parent e9c71d1f51
commit 11c7739af7
29 changed files with 312 additions and 145 deletions
+15
View File
@@ -33,8 +33,23 @@ class NodeUtils {
}
return node.parentNode;
}
static isBlockStatementNode(node) {
return node.type === 'BlockStatement';
}
static isIdentifierNode(node) {
return node.type === 'Identifier';
}
static isLiteralNode(node) {
return node.type === 'Literal';
}
static isMemberExpressionNode(node) {
return node.type === 'MemberExpression';
}
static isPropertyNode(node) {
return node.type === 'Property';
}
static isVariableDeclaratorNode(node) {
return node.type === 'VariableDeclarator';
}
}
exports.NodeUtils = NodeUtils;
+48
View File
@@ -1,5 +1,10 @@
import { IBlockStatementNode } from "./interfaces/nodes/IBlockStatementNode";
import { IIdentifierNode } from "./interfaces/nodes/IIdentifierNode";
import { ILiteralNode } from "./interfaces/nodes/ILiteralNode";
import { IMemberExpressionNode } from "./interfaces/nodes/IMemberExpressionNode";
import { IPropertyNode } from "./interfaces/nodes/IPropertyNode";
import { ITreeNode } from './interfaces/nodes/ITreeNode';
import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode";
export class NodeUtils {
/**
@@ -62,6 +67,14 @@ export class NodeUtils {
return node.parentNode;
}
/**
* @param node
* @returns {boolean}
*/
public static isBlockStatementNode (node: ITreeNode): node is IBlockStatementNode {
return node.type === 'BlockStatement';
}
/**
* @param node
* @returns {boolean}
@@ -69,4 +82,39 @@ export class NodeUtils {
public static isIdentifierNode (node: ITreeNode): node is IIdentifierNode {
return node.type === 'Identifier';
}
/**
* @param node
* @returns {boolean}
*/
public static isLiteralNode (node: ITreeNode): node is ILiteralNode {
return node.type === 'Literal';
}
/**
* @param node
* @returns {boolean}
*/
public static isMemberExpressionNode (node: ITreeNode): node is IMemberExpressionNode {
return node.type === 'MemberExpression';
}
/**
*
* @param node
* @returns {boolean}
*/
public static isPropertyNode (node: ITreeNode): node is IPropertyNode {
return node.type === 'Property';
}
/**
*
* @param node
* @returns {boolean}
*/
public static isVariableDeclaratorNode (node: ITreeNode): node is IVariableDeclaratorNode {
return node.type === 'VariableDeclarator';
}
}
@@ -0,0 +1 @@
"use strict";
+1
View File
@@ -0,0 +1 @@
"use strict";
+7
View File
@@ -0,0 +1,7 @@
import { ITreeNode } from "./ITreeNode";
export interface ILiteralNode extends ITreeNode {
value: boolean|number|string;
raw: string;
'x-verbatim-property': any;
}
@@ -0,0 +1 @@
"use strict";
@@ -0,0 +1,9 @@
import { IIdentifierNode } from "./IIdentifierNode";
import { ILiteralNode } from "./ILiteralNode";
import { ITreeNode } from "./ITreeNode";
export interface IMemberExpressionNode extends ITreeNode {
computed: boolean;
object: IIdentifierNode;
property: IIdentifierNode|ILiteralNode;
}
@@ -0,0 +1 @@
"use strict";
@@ -0,0 +1,12 @@
import { IFunctionExpressionNode } from "./IFunctionExpressionNode";
import { IIdentifierNode } from "./IIdentifierNode";
import { ILiteralNode } from "./ILiteralNode";
import { ITreeNode } from "./ITreeNode";
export interface IMethodDefinitionNode extends ITreeNode {
key: IIdentifierNode|ILiteralNode;
computed: boolean;
value: IFunctionExpressionNode;
kind: string;
static: boolean;
}
@@ -0,0 +1 @@
"use strict";
@@ -0,0 +1,6 @@
import { IPropertyNode } from "./IPropertyNode";
import { ITreeNode } from "./ITreeNode";
export interface IObjectExpressionNode extends ITreeNode {
properties: IPropertyNode[];
}
+1
View File
@@ -0,0 +1 @@
"use strict";
+12
View File
@@ -0,0 +1,12 @@
import { IIdentifierNode } from "./IIdentifierNode";
import { ILiteralNode } from "./ILiteralNode";
import { ITreeNode } from "./ITreeNode";
export interface IPropertyNode extends ITreeNode {
key: IIdentifierNode|ILiteralNode;
computed: boolean;
value: IIdentifierNode|ILiteralNode;
kind: string;
method: boolean;
shorthand: boolean;
}
@@ -0,0 +1 @@
"use strict";
@@ -0,0 +1,7 @@
import { ITreeNode } from "./ITreeNode";
import { IVariableDeclaratorNode } from "./IVariableDeclaratorNode";
export interface IVariableDeclarationNode extends ITreeNode {
declarations: IVariableDeclaratorNode[];
kind: string;
}
@@ -0,0 +1 @@
"use strict";
@@ -0,0 +1,7 @@
import { IIdentifierNode } from "./IIdentifierNode";
import { ITreeNode } from "./ITreeNode";
export interface IVariableDeclaratorNode extends ITreeNode {
id: IIdentifierNode;
init: any;
}
+2 -1
View File
@@ -1,9 +1,10 @@
"use strict";
const escodegen = require('escodegen');
const NodeObfuscator_1 = require('./NodeObfuscator');
const NodeUtils_1 = require("../NodeUtils");
class LiteralObfuscator extends NodeObfuscator_1.NodeObfuscator {
obfuscateNode(literalNode, parentNode) {
if (parentNode.type === 'Property' && parentNode.key === literalNode) {
if (NodeUtils_1.NodeUtils.isPropertyNode(parentNode) && parentNode.key) {
return;
}
switch (typeof literalNode.value) {
+7 -3
View File
@@ -1,14 +1,18 @@
import * as escodegen from 'escodegen';
import { ILiteralNode } from "../interfaces/nodes/ILiteralNode";
import { ITreeNode } from "../interfaces/nodes/ITreeNode";
import { NodeObfuscator } from './NodeObfuscator';
import { NodeUtils } from "../NodeUtils";
export class LiteralObfuscator extends NodeObfuscator {
/**
* @param literalNode
* @param parentNode
*/
public obfuscateNode (literalNode: any, parentNode: any): void {
if (parentNode.type === 'Property' && parentNode.key === literalNode) {
public obfuscateNode (literalNode: ILiteralNode, parentNode: ITreeNode): void {
if (NodeUtils.isPropertyNode(parentNode) && parentNode.key) {
return;
}
@@ -19,7 +23,7 @@ export class LiteralObfuscator extends NodeObfuscator {
}
literalNode['x-verbatim-property'] = {
content : this.replaceLiteralStringByArrayElement(literalNode.value),
content : this.replaceLiteralStringByArrayElement(<string>literalNode.value),
precedence: escodegen.Precedence.Primary
};
@@ -2,35 +2,37 @@
const escodegen = require('escodegen');
const estraverse = require('estraverse');
const NodeObfuscator_1 = require('./NodeObfuscator');
const NodeUtils_1 = require("../NodeUtils");
class MemberExpressionObfuscator extends NodeObfuscator_1.NodeObfuscator {
obfuscateNode(memberExpressionNode) {
estraverse.replace(memberExpressionNode.property, {
leave: (node, parentNode) => {
switch (node.type) {
case 'Literal':
this.literalNodeController(node);
break;
case 'Identifier':
if (memberExpressionNode.computed) {
break;
}
memberExpressionNode.computed = true;
this.identifierNodeController(node);
break;
if (NodeUtils_1.NodeUtils.isLiteralNode(node)) {
this.literalNodeController(node);
return;
}
if (NodeUtils_1.NodeUtils.isIdentifierNode(node)) {
if (memberExpressionNode.computed) {
return;
}
memberExpressionNode.computed = true;
this.identifierNodeController(node);
}
}
});
}
identifierNodeController(node) {
let nodeValue = node['name'];
node['type'] = 'Literal';
node['value'] = nodeValue;
node['raw'] = `'${nodeValue}'`;
node['x-verbatim-property'] = {
content: this.replaceLiteralStringByArrayElement(nodeValue),
precedence: escodegen.Precedence.Primary
let nodeValue = node.name, literalNode = {
type: 'Literal',
value: nodeValue,
raw: `'${nodeValue}'`,
'x-verbatim-property': {
content: this.replaceLiteralStringByArrayElement(nodeValue),
precedence: escodegen.Precedence.Primary
}
};
delete node['name'];
delete node.name;
Object.assign(node, literalNode);
}
literalNodeController(node) {
switch (typeof node.value) {
@@ -1,30 +1,34 @@
import * as escodegen from 'escodegen';
import * as estraverse from 'estraverse';
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
import { ILiteralNode } from "../interfaces/nodes/ILiteralNode";
import { IMemberExpressionNode } from "../interfaces/nodes/IMemberExpressionNode";
import { ITreeNode } from "../interfaces/nodes/ITreeNode";
import { NodeObfuscator } from './NodeObfuscator'
import { NodeUtils } from "../NodeUtils";
export class MemberExpressionObfuscator extends NodeObfuscator {
/**
* @param memberExpressionNode
*/
public obfuscateNode (memberExpressionNode: any): void {
public obfuscateNode (memberExpressionNode: IMemberExpressionNode): void {
estraverse.replace(memberExpressionNode.property, {
leave: (node, parentNode) => {
switch (node.type) {
case 'Literal':
this.literalNodeController(node);
leave: (node: ITreeNode, parentNode: ITreeNode) => {
if (NodeUtils.isLiteralNode(node)) {
this.literalNodeController(node);
break;
return;
}
case 'Identifier':
if (memberExpressionNode.computed) {
break;
}
if (NodeUtils.isIdentifierNode(node)) {
if (memberExpressionNode.computed) {
return;
}
memberExpressionNode.computed = true;
this.identifierNodeController(node);
break;
memberExpressionNode.computed = true;
this.identifierNodeController(node);
}
}
});
@@ -42,18 +46,21 @@ export class MemberExpressionObfuscator extends NodeObfuscator {
*
* @param node
*/
private identifierNodeController (node: any): void {
let nodeValue: string = node['name'];
private identifierNodeController (node: IIdentifierNode): void {
let nodeValue: string = node.name,
literalNode: ILiteralNode = {
type: 'Literal',
value: nodeValue,
raw: `'${nodeValue}'`,
'x-verbatim-property': {
content : this.replaceLiteralStringByArrayElement(nodeValue),
precedence: escodegen.Precedence.Primary
}
};
node['type'] = 'Literal';
node['value'] = nodeValue;
node['raw'] = `'${nodeValue}'`;
node['x-verbatim-property'] = {
content : this.replaceLiteralStringByArrayElement(nodeValue),
precedence: escodegen.Precedence.Primary
};
delete node.name;
delete node['name'];
Object.assign(node, literalNode);
}
/**
@@ -65,7 +72,7 @@ export class MemberExpressionObfuscator extends NodeObfuscator {
*
* @param node
*/
private literalNodeController (node: any): void {
private literalNodeController (node: ILiteralNode): void {
switch (typeof node.value) {
case 'string':
if (node['x-verbatim-property']) {
@@ -73,7 +80,7 @@ export class MemberExpressionObfuscator extends NodeObfuscator {
}
node['x-verbatim-property'] = {
content : this.replaceLiteralStringByArrayElement(node.value),
content : this.replaceLiteralStringByArrayElement(<string>node.value),
precedence: escodegen.Precedence.Primary
};
@@ -1,10 +1,10 @@
"use strict";
const estraverse = require('estraverse');
const NodeObfuscator_1 = require('./NodeObfuscator');
const NodeUtils_1 = require("../NodeUtils");
class MethodDefinitionObfuscator extends NodeObfuscator_1.NodeObfuscator {
constructor(...args) {
super(...args);
this.methodName = new Map();
this.ignoredNames = ['constructor'];
}
obfuscateNode(methodDefinitionNode, parentNode) {
@@ -13,13 +13,13 @@ class MethodDefinitionObfuscator extends NodeObfuscator_1.NodeObfuscator {
replaceMethodName(methodDefinitionNode) {
estraverse.replace(methodDefinitionNode.key, {
leave: (node) => {
if (node.type !== 'Identifier' ||
this.ignoredNames.indexOf(node.name) >= 0 ||
methodDefinitionNode.computed === true) {
return estraverse.VisitorOption.Skip;
if (NodeUtils_1.NodeUtils.isIdentifierNode(node) &&
this.ignoredNames.indexOf(node.name) < 0 &&
methodDefinitionNode.computed === false) {
methodDefinitionNode.computed = true;
node.name = this.replaceLiteralStringByArrayElement(node.name);
}
methodDefinitionNode.computed = true;
node.name = this.replaceLiteralStringByArrayElement(node.name);
return estraverse.VisitorOption.Skip;
}
});
}
@@ -1,6 +1,10 @@
import * as estraverse from 'estraverse';
import { IMethodDefinitionNode } from "../interfaces/nodes/IMethodDefinitionNode";
import { ITreeNode } from "../interfaces/nodes/ITreeNode";
import { NodeObfuscator } from './NodeObfuscator';
import { NodeUtils } from "../NodeUtils";
/**
* replaces:
@@ -12,11 +16,6 @@ import { NodeObfuscator } from './NodeObfuscator';
* _0x12d45f();
*/
export class MethodDefinitionObfuscator extends NodeObfuscator {
/**
* @type {Map<string, string>}
*/
private methodName: Map <string, string> = new Map <string, string> ();
/**
* @type {string[]}
*/
@@ -26,26 +25,26 @@ export class MethodDefinitionObfuscator extends NodeObfuscator {
* @param methodDefinitionNode
* @param parentNode
*/
public obfuscateNode (methodDefinitionNode: any, parentNode: any): void {
public obfuscateNode (methodDefinitionNode: IMethodDefinitionNode, parentNode: ITreeNode): void {
this.replaceMethodName(methodDefinitionNode);
}
/**
* @param methodDefinitionNode
*/
private replaceMethodName (methodDefinitionNode: any): void {
private replaceMethodName (methodDefinitionNode: IMethodDefinitionNode): void {
estraverse.replace(methodDefinitionNode.key, {
leave: (node) => {
leave: (node: ITreeNode) => {
if (
node.type !== 'Identifier' ||
this.ignoredNames.indexOf(node.name) >= 0 ||
methodDefinitionNode.computed === true
NodeUtils.isIdentifierNode(node) &&
this.ignoredNames.indexOf(node.name) < 0 &&
methodDefinitionNode.computed === false
) {
return estraverse.VisitorOption.Skip;
methodDefinitionNode.computed = true;
node.name = this.replaceLiteralStringByArrayElement(node.name);
}
methodDefinitionNode.computed = true;
node.name = this.replaceLiteralStringByArrayElement(node.name);
return estraverse.VisitorOption.Skip;
}
});
}
+4 -3
View File
@@ -1,13 +1,14 @@
"use strict";
const NodeUtils_1 = require("../NodeUtils");
const Utils_1 = require('../Utils');
class NodeObfuscator {
constructor(nodes) {
this.nodes = nodes;
}
replaceNodeIdentifierByNewValue(node, parentNode, namesMap) {
if (node.type === 'Identifier' && namesMap.has(node.name)) {
if ((parentNode.type === 'Property' && parentNode.key === node) ||
(parentNode.type === 'MemberExpression' && parentNode.computed === false && parentNode.property === node)) {
if (NodeUtils_1.NodeUtils.isIdentifierNode(node) && namesMap.has(node.name)) {
if ((NodeUtils_1.NodeUtils.isPropertyNode(parentNode) && parentNode.key === node) ||
(NodeUtils_1.NodeUtils.isMemberExpressionNode(parentNode) && parentNode.computed === false && parentNode.property === node)) {
return;
}
node.name = namesMap.get(node.name);
+7 -5
View File
@@ -1,6 +1,8 @@
import { INode } from '../interfaces/INode';
import { INodeObfuscator } from '../interfaces/INodeObfuscator';
import { ITreeNode } from "../interfaces/nodes/ITreeNode";
import { NodeUtils } from "../NodeUtils";
import { Utils } from '../Utils';
export abstract class NodeObfuscator implements INodeObfuscator {
@@ -20,18 +22,18 @@ export abstract class NodeObfuscator implements INodeObfuscator {
* @param node
* @param parentNode
*/
public abstract obfuscateNode (node: any, parentNode?: any): void;
public abstract obfuscateNode (node: ITreeNode, parentNode?: ITreeNode): void;
/**
* @param node
* @param parentNode
* @param namesMap
*/
protected replaceNodeIdentifierByNewValue (node: any, parentNode: any, namesMap: Map <string, string>) {
if (node.type === 'Identifier' && namesMap.has(node.name)) {
protected replaceNodeIdentifierByNewValue (node: ITreeNode, parentNode: ITreeNode, namesMap: Map <string, string>) {
if (NodeUtils.isIdentifierNode(node) && namesMap.has(node.name)) {
if (
(parentNode.type === 'Property' && parentNode.key === node) ||
(parentNode.type === 'MemberExpression' && parentNode.computed === false && parentNode.property === node)
(NodeUtils.isPropertyNode(parentNode) && parentNode.key === node) ||
(NodeUtils.isMemberExpressionNode(parentNode) && parentNode.computed === false && parentNode.property === node )
) {
return;
}
@@ -2,19 +2,19 @@
const escodegen = require('escodegen');
const estraverse = require('estraverse');
const NodeObfuscator_1 = require('./NodeObfuscator');
const NodeUtils_1 = require("../NodeUtils");
const Utils_1 = require('../Utils');
class ObjectExpressionObfuscator extends NodeObfuscator_1.NodeObfuscator {
obfuscateNode(objectExpressionNode) {
objectExpressionNode.properties.forEach((property) => {
estraverse.replace(property.key, {
leave: (node, parentNode) => {
switch (node.type) {
case 'Literal':
this.literalNodeController(node);
break;
case 'Identifier':
this.identifierNodeController(node);
break;
if (NodeUtils_1.NodeUtils.isLiteralNode(node)) {
this.literalNodeController(node);
return;
}
if (NodeUtils_1.NodeUtils.isIdentifierNode(node)) {
this.identifierNodeController(node);
}
}
});
@@ -34,15 +34,17 @@ class ObjectExpressionObfuscator extends NodeObfuscator_1.NodeObfuscator {
}
}
identifierNodeController(node) {
let nodeValue = node['name'];
node['type'] = 'Literal';
node['value'] = nodeValue;
node['raw'] = `'${nodeValue}'`;
node['x-verbatim-property'] = {
content: Utils_1.Utils.stringToUnicode(nodeValue),
precedence: escodegen['Precedence']['Primary']
let nodeValue = node.name, literalNode = {
type: 'Literal',
value: nodeValue,
raw: `'${nodeValue}'`,
'x-verbatim-property': {
content: Utils_1.Utils.stringToUnicode(nodeValue),
precedence: escodegen.Precedence.Primary
}
};
delete node['name'];
delete node.name;
Object.assign(node, literalNode);
}
}
exports.ObjectExpressionObfuscator = ObjectExpressionObfuscator;
@@ -1,7 +1,13 @@
import * as escodegen from 'escodegen';
import * as estraverse from 'estraverse';
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
import { ILiteralNode } from "../interfaces/nodes/ILiteralNode";
import { IObjectExpressionNode } from "../interfaces/nodes/IObjectExpressionNode";
import { ITreeNode } from "../interfaces/nodes/ITreeNode";
import { NodeObfuscator } from './NodeObfuscator'
import { NodeUtils } from "../NodeUtils";
import { Utils } from '../Utils';
/**
@@ -18,20 +24,18 @@ export class ObjectExpressionObfuscator extends NodeObfuscator {
/**
* @param objectExpressionNode
*/
public obfuscateNode (objectExpressionNode: any): void {
public obfuscateNode (objectExpressionNode: IObjectExpressionNode): void {
objectExpressionNode.properties.forEach((property) => {
estraverse.replace(property.key, {
leave: (node, parentNode) => {
switch (node.type) {
case 'Literal':
this.literalNodeController(node);
leave: (node: ITreeNode, parentNode: ITreeNode) => {
if (NodeUtils.isLiteralNode(node)) {
this.literalNodeController(node);
break;
return;
}
case 'Identifier':
this.identifierNodeController(node);
break;
if (NodeUtils.isIdentifierNode(node)) {
this.identifierNodeController(node);
}
}
});
@@ -41,7 +45,7 @@ export class ObjectExpressionObfuscator extends NodeObfuscator {
/**
* @param node
*/
private literalNodeController (node: any): void {
private literalNodeController (node: ILiteralNode): void {
switch (typeof node.value) {
case 'string':
if (node['x-verbatim-property']) {
@@ -49,7 +53,7 @@ export class ObjectExpressionObfuscator extends NodeObfuscator {
}
node['x-verbatim-property'] = {
content : Utils.stringToUnicode(node.value),
content : Utils.stringToUnicode(<string>node.value),
precedence: escodegen.Precedence.Primary
};
@@ -60,17 +64,20 @@ export class ObjectExpressionObfuscator extends NodeObfuscator {
/**
* @param node
*/
private identifierNodeController (node: any): void {
let nodeValue: string = node['name'];
private identifierNodeController (node: IIdentifierNode): void {
let nodeValue: string = node.name,
literalNode: ILiteralNode = {
type: 'Literal',
value: nodeValue,
raw: `'${nodeValue}'`,
'x-verbatim-property': {
content : Utils.stringToUnicode(nodeValue),
precedence: escodegen.Precedence.Primary
}
};
node['type'] = 'Literal';
node['value'] = nodeValue;
node['raw'] = `'${nodeValue}'`;
node['x-verbatim-property'] = {
content : Utils.stringToUnicode(nodeValue),
precedence: escodegen['Precedence']['Primary']
};
delete node.name;
delete node['name'];
Object.assign(node, literalNode);
}
}
@@ -19,15 +19,16 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
variableDeclarationNode.declarations.forEach((declarationNode) => {
estraverse.replace(declarationNode, {
enter: (node) => {
if (node.type !== 'VariableDeclarator') {
return estraverse.VisitorOption.Skip;
if (NodeUtils_1.NodeUtils.isVariableDeclaratorNode(node)) {
estraverse.replace(node.id, {
enter: (node) => {
this.variableNames.set(node.name, Utils_1.Utils.getRandomVariableName());
node.name = this.variableNames.get(node.name);
}
});
return;
}
estraverse.replace(node.id, {
enter: (node) => {
this.variableNames.set(node.name, Utils_1.Utils.getRandomVariableName());
node.name = this.variableNames.get(node.name);
}
});
return estraverse.VisitorOption.Skip;
}
});
});
@@ -47,9 +48,11 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression') {
functionParentScope = NodeUtils_1.NodeUtils.getNodeScope(node);
functionIndex = functionParentScope.body.indexOf(node);
if (functionIndex >= 0) {
functionNextNode = functionParentScope.body[functionIndex + 1];
if (NodeUtils_1.NodeUtils.isBlockStatementNode(functionParentScope)) {
functionIndex = functionParentScope.body.indexOf(node);
if (functionIndex >= 0) {
functionNextNode = functionParentScope.body[functionIndex + 1];
}
}
isNodeAfterVariableDeclaratorFlag = true;
}
@@ -1,5 +1,9 @@
import * as estraverse from 'estraverse';
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
import { ITreeNode } from "../interfaces/nodes/ITreeNode";
import { IVariableDeclarationNode } from "../interfaces/nodes/IVariableDeclarationNode";
import { NodeObfuscator } from './NodeObfuscator';
import { NodeUtils } from "../NodeUtils";
import { Utils } from '../Utils';
@@ -24,7 +28,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
* @param variableDeclarationNode
* @param parentNode
*/
public obfuscateNode (variableDeclarationNode: any, parentNode: any): void {
public obfuscateNode (variableDeclarationNode: IVariableDeclarationNode, parentNode: ITreeNode): void {
if (parentNode.type === 'Program') {
return;
}
@@ -36,20 +40,22 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
/**
* @param variableDeclarationNode
*/
private replaceVariableName (variableDeclarationNode: any): void {
private replaceVariableName (variableDeclarationNode: IVariableDeclarationNode): void {
variableDeclarationNode.declarations.forEach((declarationNode) => {
estraverse.replace(declarationNode, {
enter: (node) => {
if (node.type !== 'VariableDeclarator') {
return estraverse.VisitorOption.Skip;
enter: (node: ITreeNode) => {
if (NodeUtils.isVariableDeclaratorNode(node)) {
estraverse.replace(node.id, {
enter: (node: IIdentifierNode) => {
this.variableNames.set(node.name, Utils.getRandomVariableName());
node.name = this.variableNames.get(node.name);
}
});
return;
}
estraverse.replace(node.id, {
enter: (node) => {
this.variableNames.set(node.name, Utils.getRandomVariableName());
node.name = this.variableNames.get(node.name);
}
});
return estraverse.VisitorOption.Skip;
}
});
});
@@ -59,8 +65,8 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
* @param variableDeclarationNode
* @param variableParentNode
*/
private replaceVariableCalls (variableDeclarationNode: any, variableParentNode: any): void {
let scopeNode: any;
private replaceVariableCalls (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: ITreeNode): void {
let scopeNode: ITreeNode;
if (variableDeclarationNode.kind === 'var') {
scopeNode = NodeUtils.getNodeScope(
@@ -72,12 +78,12 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
let isNodeAfterVariableDeclaratorFlag: boolean = false,
isNodeBeforeVariableDeclaratorFlag: boolean = true,
functionParentScope: any,
functionNextNode: any,
functionParentScope: ITreeNode,
functionNextNode: ITreeNode,
functionIndex: number = -1;
estraverse.replace(scopeNode, {
enter: (node, parentNode) => {
enter: (node: ITreeNode, parentNode: ITreeNode) => {
if (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
@@ -87,10 +93,12 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
node
);
functionIndex = functionParentScope.body.indexOf(node);
if (NodeUtils.isBlockStatementNode(functionParentScope)) {
functionIndex = functionParentScope.body.indexOf(node);
if (functionIndex >= 0) {
functionNextNode = functionParentScope.body[functionIndex + 1];
if (functionIndex >= 0) {
functionNextNode = functionParentScope.body[functionIndex + 1];
}
}
isNodeAfterVariableDeclaratorFlag = true;