refactoring

This commit is contained in:
sanex3339
2016-05-24 19:03:21 +03:00
parent e78256b382
commit 47c560ed7a
6 changed files with 71 additions and 516 deletions
+1
View File
@@ -22,6 +22,7 @@ class NodeUtils {
if (node.parentNode.type === NodeType_1.NodeType.Program) { if (node.parentNode.type === NodeType_1.NodeType.Program) {
return node.parentNode; return node.parentNode;
} }
console.log(node.type, node.parentNode.type);
if (!Utils_1.Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) { if (!Utils_1.Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
return NodeUtils.getBlockScopeOfNode(node.parentNode, depth); return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
} }
+2
View File
@@ -65,6 +65,8 @@ export class NodeUtils {
return <BlockScopeNode> node.parentNode; return <BlockScopeNode> node.parentNode;
} }
console.log(node.type, node.parentNode.type);
if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) { if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
return NodeUtils.getBlockScopeOfNode(node.parentNode, depth); return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
} }
@@ -1,9 +1,8 @@
"use strict"; "use strict";
const esprima = require('esprima');
const AppendState_1 = require("../../enums/AppendState"); const AppendState_1 = require("../../enums/AppendState");
const NodeType_1 = require("../../enums/NodeType");
const Node_1 = require('../Node'); const Node_1 = require('../Node');
const NodeUtils_1 = require("../../NodeUtils"); const NodeUtils_1 = require("../../NodeUtils");
const Utils_1 = require('../../Utils');
class UnicodeArrayRotateFunctionNode extends Node_1.Node { class UnicodeArrayRotateFunctionNode extends Node_1.Node {
constructor(unicodeArrayName, unicodeArray, unicodeArrayRotateValue) { constructor(unicodeArrayName, unicodeArray, unicodeArrayRotateValue) {
super(); super();
@@ -23,263 +22,25 @@ class UnicodeArrayRotateFunctionNode extends Node_1.Node {
return super.getNode(); return super.getNode();
} }
getNodeStructure() { getNodeStructure() {
return { return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(esprima.parse(`
"type": NodeType_1.NodeType.ExpressionStatement, (function (array, times, reverse) {
"expression": { if (times < 0) {
"type": NodeType_1.NodeType.CallExpression, return;
"callee": {
'type': NodeType_1.NodeType.FunctionExpression,
'id': null,
'params': [
{
'type': NodeType_1.NodeType.Identifier,
'name': 'array'
},
{
'type': NodeType_1.NodeType.Identifier,
'name': 'times'
},
{
'type': NodeType_1.NodeType.Identifier,
'name': 'reverse'
} }
],
'defaults': [], var temp;
'body': {
'type': NodeType_1.NodeType.BlockStatement, while (times--) {
'body': [ if (!reverse) {
{ temp = array.pop();
'type': NodeType_1.NodeType.IfStatement, array.unshift(temp);
'test': { } else {
'type': NodeType_1.NodeType.BinaryExpression, temp = array.shift();
'operator': '<', array.push(temp);
'left': {
'type': NodeType_1.NodeType.Identifier,
'name': 'times'
},
'right': {
'type': NodeType_1.NodeType.Literal,
'value': 0,
'raw': '0'
}
},
'consequent': {
'type': NodeType_1.NodeType.BlockStatement,
'body': [
{
'type': NodeType_1.NodeType.ReturnStatement,
'argument': null
}
]
},
'alternate': null
},
{
'type': NodeType_1.NodeType.ExpressionStatement,
'expression': {
'type': NodeType_1.NodeType.AssignmentExpression,
'operator': '=',
'left': {
'type': NodeType_1.NodeType.Identifier,
'name': 'reverse'
},
'right': {
'type': NodeType_1.NodeType.LogicalExpression,
'operator': '||',
'left': {
'type': NodeType_1.NodeType.Identifier,
'name': 'reverse'
},
'right': {
'type': NodeType_1.NodeType.Literal,
'value': false,
'raw': 'false'
}
}
}
},
{
'type': NodeType_1.NodeType.VariableDeclaration,
'declarations': [
{
'type': NodeType_1.NodeType.VariableDeclarator,
'id': {
'type': NodeType_1.NodeType.Identifier,
'name': 'temp'
},
'init': null
}
],
'kind': 'var'
},
{
'type': NodeType_1.NodeType.WhileStatement,
'test': {
'type': NodeType_1.NodeType.UpdateExpression,
'operator': '--',
'argument': {
'type': NodeType_1.NodeType.Identifier,
'name': 'times'
},
'prefix': false
},
'body': {
'type': NodeType_1.NodeType.BlockStatement,
'body': [
{
'type': NodeType_1.NodeType.IfStatement,
'test': {
'type': NodeType_1.NodeType.UnaryExpression,
'operator': '!',
'argument': {
'type': NodeType_1.NodeType.Identifier,
'name': 'reverse'
},
'prefix': true
},
'consequent': {
'type': NodeType_1.NodeType.BlockStatement,
'body': [
{
'type': NodeType_1.NodeType.ExpressionStatement,
'expression': {
'type': NodeType_1.NodeType.AssignmentExpression,
'operator': '=',
'left': {
'type': NodeType_1.NodeType.Identifier,
'name': 'temp'
},
'right': {
'type': NodeType_1.NodeType.CallExpression,
'callee': {
'type': NodeType_1.NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType_1.NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType_1.NodeType.Literal,
'name': 'pop',
'x-verbatim-property': Utils_1.Utils.stringToUnicode('pop')
}
},
'arguments': []
}
}
},
{
'type': NodeType_1.NodeType.ExpressionStatement,
'expression': {
'type': NodeType_1.NodeType.CallExpression,
'callee': {
'type': NodeType_1.NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType_1.NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType_1.NodeType.Literal,
'name': 'unshift',
'x-verbatim-property': Utils_1.Utils.stringToUnicode('unshift')
}
},
'arguments': [
{
'type': NodeType_1.NodeType.Identifier,
'name': 'temp'
}
]
}
}
]
},
'alternate': {
'type': NodeType_1.NodeType.BlockStatement,
'body': [
{
'type': NodeType_1.NodeType.ExpressionStatement,
'expression': {
'type': NodeType_1.NodeType.AssignmentExpression,
'operator': '=',
'left': {
'type': NodeType_1.NodeType.Identifier,
'name': 'temp'
},
'right': {
'type': NodeType_1.NodeType.CallExpression,
'callee': {
'type': NodeType_1.NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType_1.NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType_1.NodeType.Literal,
'name': 'shift',
'x-verbatim-property': Utils_1.Utils.stringToUnicode('shift')
}
},
'arguments': []
}
}
},
{
'type': NodeType_1.NodeType.ExpressionStatement,
'expression': {
'type': NodeType_1.NodeType.CallExpression,
'callee': {
'type': NodeType_1.NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType_1.NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType_1.NodeType.Literal,
'name': 'push',
'x-verbatim-property': Utils_1.Utils.stringToUnicode('push')
}
},
'arguments': [
{
'type': NodeType_1.NodeType.Identifier,
'name': 'temp'
}
]
}
}
]
}
}
]
}
} }
] }
}, })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
'generator': false, `));
'expression': false
},
"arguments": [
{
'type': NodeType_1.NodeType.Identifier,
'name': this.unicodeArrayName
},
{
'type': NodeType_1.NodeType.Literal,
'value': this.unicodeArrayRotateValue,
'raw': `'${this.unicodeArrayRotateValue}'`
},
{
'type': NodeType_1.NodeType.Literal,
'value': true,
'raw': 'true'
}
]
}
};
} }
} }
exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode; exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode;
@@ -1,13 +1,15 @@
import * as esprima from 'esprima';
import { INode } from "../../interfaces/nodes/INode"; import { INode } from "../../interfaces/nodes/INode";
import { BlockScopeNode } from "../../types/BlockScopeNode"; import { BlockScopeNode } from "../../types/BlockScopeNode";
import { AppendState } from "../../enums/AppendState"; import { AppendState } from "../../enums/AppendState";
import { NodeType } from "../../enums/NodeType";
import { Node } from '../Node'; import { Node } from '../Node';
import { NodeUtils } from "../../NodeUtils"; import { NodeUtils } from "../../NodeUtils";
import { Utils } from '../../Utils'; import {Utils} from "../../Utils";
import {Obfuscator} from "../../Obfuscator";
export class UnicodeArrayRotateFunctionNode extends Node { export class UnicodeArrayRotateFunctionNode extends Node {
/** /**
@@ -71,262 +73,52 @@ export class UnicodeArrayRotateFunctionNode extends Node {
* @returns any * @returns any
*/ */
protected getNodeStructure (): any { protected getNodeStructure (): any {
return { return NodeUtils.getBlockScopeNodeByIndex(
"type": NodeType.ExpressionStatement, esprima.parse(`
"expression": { (function (array, times, reverse) {
"type": NodeType.CallExpression, if (times < 0) {
"callee": { return;
'type': NodeType.FunctionExpression,
'id': null,
'params': [
{
'type': NodeType.Identifier,
'name': 'array'
},
{
'type': NodeType.Identifier,
'name': 'times'
},
{
'type': NodeType.Identifier,
'name': 'reverse'
} }
],
'defaults': [], var temp;
'body': {
'type': NodeType.BlockStatement, while (times--) {
'body': [ if (!reverse) {
{ temp = array.pop();
'type': NodeType.IfStatement, array.unshift(temp);
'test': { } else {
'type': NodeType.BinaryExpression, temp = array.shift();
'operator': '<', array.push(temp);
'left': {
'type': NodeType.Identifier,
'name': 'times'
},
'right': {
'type': NodeType.Literal,
'value': 0,
'raw': '0'
}
},
'consequent': {
'type': NodeType.BlockStatement,
'body': [
{
'type': NodeType.ReturnStatement,
'argument': null
}
]
},
'alternate': null
},
{
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.AssignmentExpression,
'operator': '=',
'left': {
'type': NodeType.Identifier,
'name': 'reverse'
},
'right': {
'type': NodeType.LogicalExpression,
'operator': '||',
'left': {
'type': NodeType.Identifier,
'name': 'reverse'
},
'right': {
'type': NodeType.Literal,
'value': false,
'raw': 'false'
}
}
}
},
{
'type': NodeType.VariableDeclaration,
'declarations': [
{
'type': NodeType.VariableDeclarator,
'id': {
'type': NodeType.Identifier,
'name': 'temp'
},
'init': null
}
],
'kind': 'var'
},
{
'type': NodeType.WhileStatement,
'test': {
'type': NodeType.UpdateExpression,
'operator': '--',
'argument': {
'type': NodeType.Identifier,
'name': 'times'
},
'prefix': false
},
'body': {
'type': NodeType.BlockStatement,
'body': [
{
'type': NodeType.IfStatement,
'test': {
'type': NodeType.UnaryExpression,
'operator': '!',
'argument': {
'type': NodeType.Identifier,
'name': 'reverse'
},
'prefix': true
},
'consequent': {
'type': NodeType.BlockStatement,
'body': [
{
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.AssignmentExpression,
'operator': '=',
'left': {
'type': NodeType.Identifier,
'name': 'temp'
},
'right': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType.Literal,
'name': 'pop',
'x-verbatim-property': Utils.stringToUnicode('pop')
}
},
'arguments': []
}
}
},
{
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType.Literal,
'name': 'unshift',
'x-verbatim-property': Utils.stringToUnicode('unshift')
}
},
'arguments': [
{
'type': NodeType.Identifier,
'name': 'temp'
}
]
}
}
]
},
'alternate': {
'type': NodeType.BlockStatement,
'body': [
{
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.AssignmentExpression,
'operator': '=',
'left': {
'type': NodeType.Identifier,
'name': 'temp'
},
'right': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType.Literal,
'name': 'shift',
'x-verbatim-property': Utils.stringToUnicode('shift')
}
},
'arguments': []
}
}
},
{
'type': NodeType.ExpressionStatement,
'expression': {
'type': NodeType.CallExpression,
'callee': {
'type': NodeType.MemberExpression,
'computed': true,
'object': {
'type': NodeType.Identifier,
'name': 'array'
},
'property': {
'type': NodeType.Literal,
'name': 'push',
'x-verbatim-property': Utils.stringToUnicode('push')
}
},
'arguments': [
{
'type': NodeType.Identifier,
'name': 'temp'
}
]
}
}
]
}
}
]
}
} }
] }
}, })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
'generator': false, `)
'expression': false );
},
"arguments": [ /*return new Obfuscator({
{ rotateUnicodeArray: false
'type': NodeType.Identifier, }).obfuscateNode(
'name': this.unicodeArrayName NodeUtils.getBlockScopeNodeByIndex(
}, esprima.parse(`
{ (function (array, times, reverse) {
'type': NodeType.Literal, if (times < 0) {
'value': this.unicodeArrayRotateValue, return;
'raw': `'${this.unicodeArrayRotateValue}'` }
},
{ var temp;
'type': NodeType.Literal,
'value': true, while (times--) {
'raw': 'true' if (!reverse) {
} temp = array.pop();
] array.unshift(temp);
} } else {
}; temp = array.shift();
array.push(temp);
}
}
})(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
`)
)
);*/
} }
} }
-1
View File
@@ -48,7 +48,6 @@ let obfuscatedCode = index_1.JavaScriptObfuscator.obfuscate(`
})(); })();
`, { `, {
disableConsoleOutput: false, disableConsoleOutput: false,
rotateUnicodeArray: false
}); });
console.log(obfuscatedCode); console.log(obfuscatedCode);
console.log(eval(obfuscatedCode)); console.log(eval(obfuscatedCode));
+1 -1
View File
@@ -50,7 +50,7 @@ let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`, `,
{ {
disableConsoleOutput: false, disableConsoleOutput: false,
rotateUnicodeArray: false //rotateUnicodeArray: false
} }
); );