refactoring

This commit is contained in:
sanex3339
2016-07-06 22:19:25 +03:00
parent 6abd041ff0
commit 8ca260fde2
15 changed files with 285 additions and 478 deletions
+18 -1
View File
@@ -1,3 +1,5 @@
import * as escodegen from 'escodegen';
import * as esprima from 'esprima';
import * as estraverse from 'estraverse';
import { INode } from './interfaces/nodes/INode';
@@ -29,7 +31,10 @@ export class NodeUtils {
estraverse.replace(node, {
enter: (node: INode, parentNode: INode): any => {
if (Nodes.isLiteralNode(node)) {
node['x-verbatim-property'] = node.raw;
node['x-verbatim-property'] = {
content : node.raw,
precedence: escodegen.Precedence.Primary
};
}
}
});
@@ -47,6 +52,18 @@ export class NodeUtils {
blockScopeBody.push(node);
}
/**
* @param code
* @returns {INode}
*/
public static convertCodeToStructure (code: string): INode {
let structure: INode = esprima.parse(code);
NodeUtils.addXVerbatimPropertyToLiterals(structure);
return NodeUtils.getBlockStatementNodeByIndex(structure);
}
/**
* @param node
* @param index
+7 -19
View File
@@ -8,14 +8,11 @@ import { NodeUtils } from "../NodeUtils";
export abstract class Node implements ICustomNode {
/**
* TODO: add `abstract` modifier
*
* @type {AppendState}
*/
protected appendState: AppendState = AppendState.BeforeObfuscation;
/**
* @type {INode}
*/
protected node: INode;
protected appendState: AppendState;
/**
* @type {IOptions}
@@ -39,23 +36,14 @@ export abstract class Node implements ICustomNode {
}
/**
* @returns any
* @returns {INode}
*/
public getNode (): INode {
NodeUtils.parentize(this.node);
let node: INode = this.getNodeStructure();
return this.node;
}
NodeUtils.parentize(node);
/**
* @param node
*/
public setNode (node: INode): void {
this.node = node;
}
public updateNode (): void {
this.node = this.getNodeStructure();
return node;
}
/**
@@ -1,22 +1,19 @@
import * as esprima from 'esprima';
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
import { AppendState } from "../../enums/AppendState";
import { Node } from '../Node';
import { NodeUtils } from "../../NodeUtils";
export class ConsoleOutputDisableExpressionNode extends Node {
/**
* @param options
* @type {AppendState}
*/
constructor (options: IOptions) {
super(options);
this.node = this.getNodeStructure();
}
protected appendState: AppendState = AppendState.BeforeObfuscation;
/**
* @param blockScopeNode
@@ -42,21 +39,19 @@ export class ConsoleOutputDisableExpressionNode extends Node {
* @returns {INode}
*/
protected getNodeStructure (): INode {
return NodeUtils.getBlockStatementNodeByIndex(
esprima.parse(`
(function () {
var _ = '(\u0004\u0006\u0003\u0005[]' + '["filter"]["\u0007tructor"]' + '("return this")()' + '.' + '\u0003;\u0006\u0002\u0005\u0004};' + '_\u0003.log\u0001.in' + 'fo\u0001.' + 'war' + 'n\u0001.er' + 'r' + 'or\u0001})();' + '\u0001\u0005_\u0002;' + '_\u0003\u0002function' + '\u0003\u0007ole\u0004\u0002 ()' + '{\u0005 = \u0006var ' + '_\u0007cons',
Y,
$;
for (Y in $ = "\u0007\u0006\u0005\u0004\u0003\u0002\u0001") {
var arr = _.split($[Y]);
_ = arr.join(arr.pop());
}
[]["filter"]["constructor"](_)();
})()
`)
);
return NodeUtils.convertCodeToStructure(`
(function () {
var _ = '(\u0004\u0006\u0003\u0005[]' + '["filter"]["\u0007tructor"]' + '("return this")()' + '.' + '\u0003;\u0006\u0002\u0005\u0004};' + '_\u0003.log\u0001.in' + 'fo\u0001.' + 'war' + 'n\u0001.er' + 'r' + 'or\u0001})();' + '\u0001\u0005_\u0002;' + '_\u0003\u0002function' + '\u0003\u0007ole\u0004\u0002 ()' + '{\u0005 = \u0006var ' + '_\u0007cons',
Y,
$;
for (Y in $ = "\u0007\u0006\u0005\u0004\u0003\u0002\u0001") {
var arr = _.split($[Y]);
_ = arr.join(arr.pop());
}
[]["filter"]["constructor"](_)();
})()
`);
}
}
@@ -1,14 +1,19 @@
import { IExpressionStatementNode } from "../../interfaces/nodes/IExpressionStatementNode";
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
import { NodeType } from "../../enums/NodeType";
import { AppendState } from "../../enums/AppendState";
import { Node } from '../Node';
import { NodeUtils } from "../../NodeUtils";
export class DebugProtectionFunctionCallNode extends Node {
/**
* @type {AppendState}
*/
protected appendState: AppendState = AppendState.BeforeObfuscation;
/**
* @type {string}
*/
@@ -22,8 +27,6 @@ export class DebugProtectionFunctionCallNode extends Node {
super(options);
this.debugProtectionFunctionName = debugProtectionFunctionName;
this.node = this.getNodeStructure();
}
/**
@@ -34,19 +37,11 @@ export class DebugProtectionFunctionCallNode extends Node {
}
/**
* @returns {IExpressionStatementNode}
* @returns {INode}
*/
protected getNodeStructure (): IExpressionStatementNode {
return {
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.Identifier,
'name': this.debugProtectionFunctionName
},
'arguments': []
}
};
protected getNodeStructure (): INode {
return NodeUtils.convertCodeToStructure(`
${this.debugProtectionFunctionName}();
`);
}
}
@@ -1,14 +1,19 @@
import { IExpressionStatementNode } from "../../interfaces/nodes/IExpressionStatementNode";
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
import { NodeType } from '../../enums/NodeType';
import { AppendState } from "../../enums/AppendState";
import { Node } from '../Node';
import { NodeUtils } from '../../NodeUtils';
export class DebugProtectionFunctionIntervalNode extends Node {
/**
* @type {AppendState}
*/
protected appendState: AppendState = AppendState.BeforeObfuscation;
/**
* @type {string}
*/
@@ -22,8 +27,6 @@ export class DebugProtectionFunctionIntervalNode extends Node {
super(options);
this.debugProtectionFunctionName = debugProtectionFunctionName;
this.node = this.getNodeStructure();
}
/**
@@ -34,49 +37,13 @@ export class DebugProtectionFunctionIntervalNode extends Node {
}
/**
* @returns {IExpressionStatementNode}
* @returns {INode}
*/
protected getNodeStructure (): IExpressionStatementNode {
return {
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.Identifier,
'name': 'setInterval'
},
'arguments': [
{
'type': NodeType.FunctionExpression,
'id': null,
'params': [],
'defaults': [],
'body': {
'type': NodeType.BlockStatement,
'body': [
{
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.Identifier,
'name': this.debugProtectionFunctionName
},
'arguments': []
}
}
]
},
'generator': false,
'expression': false
},
{
'type': NodeType.Literal,
'value': 4000,
'raw': '4000'
}
]
}
};
protected getNodeStructure (): INode {
return NodeUtils.convertCodeToStructure(`
setInterval(function () {
${this.debugProtectionFunctionName}();
}, 4000);
`);
}
}
@@ -1,15 +1,20 @@
import * as esprima from 'esprima';
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
import { AppendState } from "../../enums/AppendState";
import { Node } from '../Node';
import { NodeUtils } from '../../NodeUtils';
import { Utils } from "../../Utils";
export class DebugProtectionFunctionNode extends Node {
/**
* @type {AppendState}
*/
protected appendState: AppendState = AppendState.BeforeObfuscation;
/**
* @type {string}
*/
@@ -23,8 +28,6 @@ export class DebugProtectionFunctionNode extends Node {
super(options);
this.debugProtectionFunctionName = debugProtectionFunctionName;
this.node = this.getNodeStructure();
}
/**
@@ -53,24 +56,22 @@ export class DebugProtectionFunctionNode extends Node {
* @returns {INode}
*/
protected getNodeStructure (): INode {
return NodeUtils.getBlockStatementNodeByIndex(
esprima.parse(`
var ${this.debugProtectionFunctionName} = function () {
function debuggerProtection (counter) {
if (('' + counter / counter)['length'] !== 1 || counter % 20 === 0) {
(function () {}.constructor('debugger')());
} else {
[].filter.constructor(${Utils.stringToJSFuck('debugger')})();
}
debuggerProtection(++counter);
return NodeUtils.convertCodeToStructure(`
var ${this.debugProtectionFunctionName} = function () {
function debuggerProtection (counter) {
if (('' + counter / counter)['length'] !== 1 || counter % 20 === 0) {
(function () {}.constructor('debugger')());
} else {
[].filter.constructor(${Utils.stringToJSFuck('debugger')})();
}
try {
debuggerProtection(0);
} catch (y) {}
};
`)
);
debuggerProtection(++counter);
}
try {
debuggerProtection(0);
} catch (y) {}
};
`);
}
}
@@ -1,7 +1,4 @@
import * as esprima from 'esprima';
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
@@ -21,15 +18,6 @@ export class SelfDefendingUnicodeNode extends Node {
*/
protected appendState: AppendState = AppendState.AfterObfuscation;
/**
* @param options
*/
constructor (options: IOptions) {
super(options);
this.node = this.getNodeStructure();
}
/**
* @param blockScopeNode
*/
@@ -51,7 +39,7 @@ export class SelfDefendingUnicodeNode extends Node {
* @returns {INode}
*/
protected getNodeStructure (): INode {
let node: INode = esprima.parse(
return NodeUtils.convertCodeToStructure(
JavaScriptObfuscator.obfuscate(`
(function () {
var func = function(){return ${Utils.stringToUnicode('dev')};},
@@ -63,9 +51,5 @@ export class SelfDefendingUnicodeNode extends Node {
})();
`, NO_CUSTOM_NODES_PRESET)
);
NodeUtils.addXVerbatimPropertyToLiterals(node);
return NodeUtils.getBlockStatementNodeByIndex(node);
}
}
@@ -1,5 +1,3 @@
import * as esprima from 'esprima';
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
@@ -49,8 +47,6 @@ export class UnicodeArrayCallsWrapper extends Node {
this.unicodeArrayCallsWrapperName = unicodeArrayCallsWrapperName;
this.unicodeArrayName = unicodeArrayName;
this.unicodeArray = unicodeArray;
this.node = this.getNodeStructure();
}
/**
@@ -75,8 +71,6 @@ export class UnicodeArrayCallsWrapper extends Node {
return;
}
this.updateNode();
return super.getNode();
}
@@ -84,17 +78,12 @@ export class UnicodeArrayCallsWrapper extends Node {
* @returns {INode}
*/
protected getNodeStructure (): INode {
let keyName: string = Utils.getRandomVariableName(),
node: INode;
let keyName: string = Utils.getRandomVariableName();
node = esprima.parse(`
return NodeUtils.convertCodeToStructure(`
var ${this.unicodeArrayCallsWrapperName} = function (${keyName}) {
return ${this.unicodeArrayName}[parseInt(${keyName}, 0x010)];
};
`);
NodeUtils.addXVerbatimPropertyToLiterals(node);
return NodeUtils.getBlockStatementNodeByIndex(node);
}
}
@@ -1,4 +1,3 @@
import * as esprima from 'esprima';
import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
import { INode } from "../../interfaces/nodes/INode";
@@ -45,8 +44,6 @@ export class UnicodeArrayDecodeNode extends Node {
this.unicodeArrayName = unicodeArrayName;
this.unicodeArray = unicodeArray;
this.node = this.getNodeStructure();
}
/**
@@ -64,8 +61,6 @@ export class UnicodeArrayDecodeNode extends Node {
return;
}
this.updateNode();
return super.getNode();
}
@@ -78,8 +73,7 @@ export class UnicodeArrayDecodeNode extends Node {
indexVariableName: string = Utils.getRandomVariableName(),
tempArrayName: string = Utils.getRandomVariableName();
let code: string = '',
node: INode;
let code: string = '';
if (this.options.get('selfDefending')) {
code = `
@@ -91,7 +85,7 @@ export class UnicodeArrayDecodeNode extends Node {
code = `${forLoopFunctionName}();`;
}
node = esprima.parse(`
return NodeUtils.convertCodeToStructure(`
(function () {
${JavaScriptObfuscator.obfuscate(`
(function () {
@@ -127,9 +121,5 @@ export class UnicodeArrayDecodeNode extends Node {
${code}
})();
`);
NodeUtils.addXVerbatimPropertyToLiterals(node);
return NodeUtils.getBlockStatementNodeByIndex(node);
}
}
@@ -1,13 +1,9 @@
import * as escodegen from 'escodegen';
import { INode } from '../../interfaces/nodes/INode';
import { IOptions } from "../../interfaces/IOptions";
import { IVariableDeclarationNode } from "../../interfaces/nodes/IVariableDeclarationNode";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
import { AppendState } from '../../enums/AppendState';
import { NodeType } from "../../enums/NodeType";
import { Node } from '../Node';
import { NodeUtils } from "../../NodeUtils";
@@ -53,8 +49,6 @@ export class UnicodeArrayNode extends Node {
this.unicodeArrayName = unicodeArrayName;
this.unicodeArrayRotateValue = unicodeArrayRotateValue;
this.node = this.getNodeStructure();
}
/**
@@ -88,8 +82,6 @@ export class UnicodeArrayNode extends Node {
Utils.arrayRotate <string> (this.unicodeArray, this.unicodeArrayRotateValue);
this.updateNode();
return super.getNode();
}
@@ -103,33 +95,9 @@ export class UnicodeArrayNode extends Node {
/**
* @returns {INode}
*/
protected getNodeStructure (): IVariableDeclarationNode {
return {
'type': NodeType.VariableDeclaration,
'declarations': [
{
'type': NodeType.VariableDeclarator,
'id': {
'type': NodeType.Identifier,
'name': this.unicodeArrayName
},
'init': {
'type': NodeType.ArrayExpression,
'elements': this.unicodeArray.map((value: string) => {
return {
'type': NodeType.Literal,
'value': value,
'raw': `'${value}'`,
'x-verbatim-property': {
'content' : value,
precedence: escodegen.Precedence.Primary
}
};
})
}
}
],
'kind': 'var'
};
protected getNodeStructure (): INode {
return NodeUtils.convertCodeToStructure(`
var ${this.unicodeArrayName} = [${this.unicodeArray.join(',')}];
`);
}
}
@@ -1,5 +1,3 @@
import * as esprima from 'esprima';
import { INode } from "../../interfaces/nodes/INode";
import { IOptions } from "../../interfaces/IOptions";
@@ -53,8 +51,6 @@ export class UnicodeArrayRotateFunctionNode extends Node {
this.unicodeArrayName = unicodeArrayName;
this.unicodeArray = unicodeArray;
this.unicodeArrayRotateValue = unicodeArrayRotateValue;
this.node = this.getNodeStructure();
}
/**
@@ -83,8 +79,7 @@ export class UnicodeArrayRotateFunctionNode extends Node {
code: string = '',
timesName: string = Utils.getRandomVariableName(),
timesArgumentName: string = Utils.getRandomVariableName(),
whileFunctionName: string = Utils.getRandomVariableName(),
node: INode;
whileFunctionName: string = Utils.getRandomVariableName();
if (this.options.get('selfDefending')) {
code = JavaScriptObfuscator.obfuscate(`
@@ -98,7 +93,7 @@ export class UnicodeArrayRotateFunctionNode extends Node {
code = `${whileFunctionName}(++${timesName})`;
}
node = esprima.parse(`
return NodeUtils.convertCodeToStructure(`
(function (${arrayName}, ${timesName}) {
var ${whileFunctionName} = function (${timesArgumentName}) {
while (--${timesArgumentName}) {
@@ -109,9 +104,5 @@ export class UnicodeArrayRotateFunctionNode extends Node {
${code}
})(${this.unicodeArrayName}, 0x${Utils.decToHex(this.unicodeArrayRotateValue)});
`);
NodeUtils.addXVerbatimPropertyToLiterals(node);
return NodeUtils.getBlockStatementNodeByIndex(node);
}
}
-7
View File
@@ -28,13 +28,6 @@ export interface ICustomNode {
*/
getNodeData ? (): any;
/**
* @param node
*/
setNode (node: INode): void;
updateNode (): void;
/**
* @param data
*/