From 74f0ec4644fdfbf54757f9079facf2fc3d90f775 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Wed, 25 May 2016 16:26:07 +0300 Subject: [PATCH] added UnicodeArray translator node --- src/Obfuscator.js | 9 +- src/Obfuscator.ts | 9 +- .../UnicodeArrayTranslator.ts | 93 +++++++++++++++++++ src/node-groups/UnicodeArrayNodesGroup.js | 12 ++- src/node-groups/UnicodeArrayNodesGroup.ts | 33 ++++++- src/node-obfuscators/LiteralObfuscator.js | 2 +- src/node-obfuscators/LiteralObfuscator.ts | 2 +- .../MemberExpressionObfuscator.js | 4 +- .../MemberExpressionObfuscator.ts | 4 +- .../MethodDefinitionObfuscator.js | 2 +- .../MethodDefinitionObfuscator.ts | 2 +- src/node-obfuscators/NodeObfuscator.js | 4 +- src/node-obfuscators/NodeObfuscator.ts | 4 +- tests/dev-test.js | 3 +- tests/dev-test.ts | 3 +- 15 files changed, 148 insertions(+), 38 deletions(-) create mode 100644 src/custom-nodes/unicode-array-nodes/UnicodeArrayTranslator.ts diff --git a/src/Obfuscator.js b/src/Obfuscator.js index ff555f9d..22054375 100644 --- a/src/Obfuscator.js +++ b/src/Obfuscator.js @@ -12,9 +12,7 @@ const MemberExpressionObfuscator_1 = require('./node-obfuscators/MemberExpressio const MethodDefinitionObfuscator_1 = require('./node-obfuscators/MethodDefinitionObfuscator'); const NodeUtils_1 = require("./NodeUtils"); const ObjectExpressionObfuscator_1 = require('./node-obfuscators/ObjectExpressionObfuscator'); -const UnicodeArrayNode_1 = require('./custom-nodes/unicode-array-nodes/UnicodeArrayNode'); const UnicodeArrayNodesGroup_1 = require('./node-groups/UnicodeArrayNodesGroup'); -const Utils_1 = require('./Utils'); const VariableDeclarationObfuscator_1 = require('./node-obfuscators/VariableDeclarationObfuscator'); class Obfuscator { constructor(options = {}) { @@ -89,12 +87,7 @@ class Obfuscator { if (this.options['debugProtection']) { this.setNodesGroup(new DebugProtectionNodesGroup_1.DebugProtectionNodesGroup(this.options)); } - if (this.options['rotateUnicodeArray']) { - this.setNodesGroup(new UnicodeArrayNodesGroup_1.UnicodeArrayNodesGroup()); - } - else { - this.setNode('unicodeArrayNode', new UnicodeArrayNode_1.UnicodeArrayNode(Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH))); - } + this.setNodesGroup(new UnicodeArrayNodesGroup_1.UnicodeArrayNodesGroup(this.options)); } } exports.Obfuscator = Obfuscator; diff --git a/src/Obfuscator.ts b/src/Obfuscator.ts index e343579d..cc654dae 100644 --- a/src/Obfuscator.ts +++ b/src/Obfuscator.ts @@ -155,13 +155,6 @@ export class Obfuscator { /** * Important to set this nodes latest to prevent runtime errors cause by `rotateUnicodeArray` option */ - if (this.options['rotateUnicodeArray']) { - this.setNodesGroup(new UnicodeArrayNodesGroup()); - } else { - this.setNode( - 'unicodeArrayNode', - new UnicodeArrayNode(Utils.getRandomVariableName(UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH)) - ); - } + this.setNodesGroup(new UnicodeArrayNodesGroup(this.options)); } } diff --git a/src/custom-nodes/unicode-array-nodes/UnicodeArrayTranslator.ts b/src/custom-nodes/unicode-array-nodes/UnicodeArrayTranslator.ts new file mode 100644 index 00000000..ae3677db --- /dev/null +++ b/src/custom-nodes/unicode-array-nodes/UnicodeArrayTranslator.ts @@ -0,0 +1,93 @@ +import * as esprima from 'esprima'; + +import { INode } from "../../interfaces/nodes/INode"; + +import { BlockScopeNode } from "../../types/BlockScopeNode"; + +import { AppendState } from "../../enums/AppendState"; + +import { Node } from '../Node'; +import { NodeUtils } from "../../NodeUtils"; +import { Utils } from "../../Utils"; + +export class UnicodeArrayTranslator extends Node { + /** + * @type {AppendState} + */ + protected appendState: AppendState = AppendState.AfterObfuscation; + + /** + * @type {string[]} + */ + private unicodeArray: string[]; + + /** + * @type {string} + */ + private unicodeArrayName: string; + + /** + * @type {string} + */ + private unicodeArrayTranslatorName: string; + + /** + * @param unicodeArrayTranslatorName + * @param unicodeArrayName + * @param unicodeArray + */ + constructor ( + unicodeArrayTranslatorName: string, + unicodeArrayName: string, + unicodeArray: string[] + ) { + super(); + + this.unicodeArrayTranslatorName = unicodeArrayTranslatorName; + this.unicodeArrayName = unicodeArrayName; + this.unicodeArray = unicodeArray; + + this.node = this.getNodeStructure(); + } + + /** + * @param blockScopeNode + */ + public appendNode (blockScopeNode: BlockScopeNode): void { + NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1); + } + + /** + * @returns {string} + */ + public getNodeIdentifier (): string { + return this.unicodeArrayTranslatorName; + }; + + /** + * @returns {INode} + */ + public getNode (): INode { + if (!this.unicodeArray.length) { + return; + } + + return super.getNode(); + } + + /** + * @returns any + */ + protected getNodeStructure (): any { + let keyName: string = Utils.getRandomVariableName(), + node: INode = esprima.parse(` + var ${this.unicodeArrayTranslatorName} = function (${keyName}) { + return ${this.unicodeArrayName}[parseInt(${keyName})] + }; + `); + + NodeUtils.addXVerbatimPropertyToLiterals(node); + + return NodeUtils.getBlockScopeNodeByIndex(node); + } +} diff --git a/src/node-groups/UnicodeArrayNodesGroup.js b/src/node-groups/UnicodeArrayNodesGroup.js index 088172bb..1e05faa3 100644 --- a/src/node-groups/UnicodeArrayNodesGroup.js +++ b/src/node-groups/UnicodeArrayNodesGroup.js @@ -3,11 +3,14 @@ const NodesGroup_1 = require('./NodesGroup'); const UnicodeArrayNode_1 = require('../custom-nodes/unicode-array-nodes/UnicodeArrayNode'); const UnicodeArrayRotateFunctionNode_1 = require('../custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode'); const Utils_1 = require('../Utils'); +const UnicodeArrayTranslator_1 = require("../custom-nodes/unicode-array-nodes/UnicodeArrayTranslator"); class UnicodeArrayNodesGroup extends NodesGroup_1.NodesGroup { - constructor() { + constructor(options) { super(); this.unicodeArrayName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH); this.unicodeArrayRotateValue = Utils_1.Utils.getRandomInteger(100, 500); + this.unicodeArrayTranslatorName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH); + this.options = options; let unicodeArrayNode = new UnicodeArrayNode_1.UnicodeArrayNode(this.unicodeArrayName, this.unicodeArrayRotateValue), unicodeArray = unicodeArrayNode.getNodeData(); this.nodes = new Map([ [ @@ -15,10 +18,13 @@ class UnicodeArrayNodesGroup extends NodesGroup_1.NodesGroup { unicodeArrayNode ], [ - 'unicodeArrayRotateFunctionNode', - new UnicodeArrayRotateFunctionNode_1.UnicodeArrayRotateFunctionNode(this.unicodeArrayName, unicodeArray, this.unicodeArrayRotateValue) + 'unicodeArrayTranslator', + new UnicodeArrayTranslator_1.UnicodeArrayTranslator(this.unicodeArrayTranslatorName, this.unicodeArrayName, unicodeArray) ] ]); + if (this.options['rotateUnicodeArray']) { + this.nodes.set('unicodeArrayRotateFunctionNode', new UnicodeArrayRotateFunctionNode_1.UnicodeArrayRotateFunctionNode(this.unicodeArrayName, unicodeArray, this.unicodeArrayRotateValue)); + } } } exports.UnicodeArrayNodesGroup = UnicodeArrayNodesGroup; diff --git a/src/node-groups/UnicodeArrayNodesGroup.ts b/src/node-groups/UnicodeArrayNodesGroup.ts index 6ed13358..78473a3a 100644 --- a/src/node-groups/UnicodeArrayNodesGroup.ts +++ b/src/node-groups/UnicodeArrayNodesGroup.ts @@ -4,8 +4,14 @@ import { NodesGroup } from './NodesGroup'; import { UnicodeArrayNode } from '../custom-nodes/unicode-array-nodes/UnicodeArrayNode'; import { UnicodeArrayRotateFunctionNode } from '../custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode'; import { Utils } from '../Utils'; +import {UnicodeArrayTranslator} from "../custom-nodes/unicode-array-nodes/UnicodeArrayTranslator"; export class UnicodeArrayNodesGroup extends NodesGroup { + /** + * @type {any} + */ + private options: any; + /** * @type {string} */ @@ -16,9 +22,19 @@ export class UnicodeArrayNodesGroup extends NodesGroup { */ private unicodeArrayRotateValue: number = Utils.getRandomInteger(100, 500); - constructor () { + /** + * @type {string} + */ + private unicodeArrayTranslatorName: string = Utils.getRandomVariableName(UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH); + + /** + * @param options + */ + constructor (options: any) { super(); + this.options = options; + let unicodeArrayNode: UnicodeArrayNode = new UnicodeArrayNode( this.unicodeArrayName, this.unicodeArrayRotateValue @@ -31,13 +47,24 @@ export class UnicodeArrayNodesGroup extends NodesGroup { unicodeArrayNode ], [ + 'unicodeArrayTranslator', + new UnicodeArrayTranslator( + this.unicodeArrayTranslatorName, + this.unicodeArrayName, + unicodeArray + ) + ] + ]); + + if (this.options['rotateUnicodeArray']) { + this.nodes.set( 'unicodeArrayRotateFunctionNode', new UnicodeArrayRotateFunctionNode( this.unicodeArrayName, unicodeArray, this.unicodeArrayRotateValue ) - ] - ]); + ); + } } } diff --git a/src/node-obfuscators/LiteralObfuscator.js b/src/node-obfuscators/LiteralObfuscator.js index 8635dc15..38a0c172 100644 --- a/src/node-obfuscators/LiteralObfuscator.js +++ b/src/node-obfuscators/LiteralObfuscator.js @@ -19,7 +19,7 @@ class LiteralObfuscator extends NodeObfuscator_1.NodeObfuscator { content = this.replaceLiteralNumberByHexadecimalValue(literalNode.value); break; case 'string': - content = this.replaceLiteralStringByArrayElement(literalNode.value); + content = this.replaceLiteralStringByUnicodeArrayTranslatorCall(literalNode.value); break; default: return; diff --git a/src/node-obfuscators/LiteralObfuscator.ts b/src/node-obfuscators/LiteralObfuscator.ts index 6120b9ec..bc0d2988 100644 --- a/src/node-obfuscators/LiteralObfuscator.ts +++ b/src/node-obfuscators/LiteralObfuscator.ts @@ -35,7 +35,7 @@ export class LiteralObfuscator extends NodeObfuscator { case 'string': - content = this.replaceLiteralStringByArrayElement(literalNode.value); + content = this.replaceLiteralStringByUnicodeArrayTranslatorCall(literalNode.value); break; diff --git a/src/node-obfuscators/MemberExpressionObfuscator.js b/src/node-obfuscators/MemberExpressionObfuscator.js index ccd9e560..3a55c17c 100644 --- a/src/node-obfuscators/MemberExpressionObfuscator.js +++ b/src/node-obfuscators/MemberExpressionObfuscator.js @@ -26,7 +26,7 @@ class MemberExpressionObfuscator extends NodeObfuscator_1.NodeObfuscator { let nodeValue = node.name, literalNode = { raw: `'${nodeValue}'`, 'x-verbatim-property': { - content: this.replaceLiteralStringByArrayElement(nodeValue), + content: this.replaceLiteralStringByUnicodeArrayTranslatorCall(nodeValue), precedence: escodegen.Precedence.Primary }, type: NodeType_1.NodeType.Literal, @@ -42,7 +42,7 @@ class MemberExpressionObfuscator extends NodeObfuscator_1.NodeObfuscator { break; } node['x-verbatim-property'] = { - content: this.replaceLiteralStringByArrayElement(node.value), + content: this.replaceLiteralStringByUnicodeArrayTranslatorCall(node.value), precedence: escodegen.Precedence.Primary }; break; diff --git a/src/node-obfuscators/MemberExpressionObfuscator.ts b/src/node-obfuscators/MemberExpressionObfuscator.ts index a66f914c..b56627d7 100644 --- a/src/node-obfuscators/MemberExpressionObfuscator.ts +++ b/src/node-obfuscators/MemberExpressionObfuscator.ts @@ -53,7 +53,7 @@ export class MemberExpressionObfuscator extends NodeObfuscator { literalNode: ILiteralNode = { raw: `'${nodeValue}'`, 'x-verbatim-property': { - content : this.replaceLiteralStringByArrayElement(nodeValue), + content : this.replaceLiteralStringByUnicodeArrayTranslatorCall(nodeValue), precedence: escodegen.Precedence.Primary }, type: NodeType.Literal, @@ -82,7 +82,7 @@ export class MemberExpressionObfuscator extends NodeObfuscator { } node['x-verbatim-property'] = { - content : this.replaceLiteralStringByArrayElement(node.value), + content : this.replaceLiteralStringByUnicodeArrayTranslatorCall(node.value), precedence: escodegen.Precedence.Primary }; diff --git a/src/node-obfuscators/MethodDefinitionObfuscator.js b/src/node-obfuscators/MethodDefinitionObfuscator.js index 4fd7a597..fe6ba14d 100644 --- a/src/node-obfuscators/MethodDefinitionObfuscator.js +++ b/src/node-obfuscators/MethodDefinitionObfuscator.js @@ -18,7 +18,7 @@ class MethodDefinitionObfuscator extends NodeObfuscator_1.NodeObfuscator { !Utils_1.Utils.arrayContains(this.ignoredNames, node.name) && methodDefinitionNode.computed === false) { methodDefinitionNode.computed = true; - node.name = this.replaceLiteralStringByArrayElement(node.name); + node.name = this.replaceLiteralStringByUnicodeArrayTranslatorCall(node.name); return; } return estraverse.VisitorOption.Skip; diff --git a/src/node-obfuscators/MethodDefinitionObfuscator.ts b/src/node-obfuscators/MethodDefinitionObfuscator.ts index e79afb29..3fd7ea7c 100644 --- a/src/node-obfuscators/MethodDefinitionObfuscator.ts +++ b/src/node-obfuscators/MethodDefinitionObfuscator.ts @@ -42,7 +42,7 @@ export class MethodDefinitionObfuscator extends NodeObfuscator { methodDefinitionNode.computed === false ) { methodDefinitionNode.computed = true; - node.name = this.replaceLiteralStringByArrayElement(node.name); + node.name = this.replaceLiteralStringByUnicodeArrayTranslatorCall(node.name); return; } diff --git a/src/node-obfuscators/NodeObfuscator.js b/src/node-obfuscators/NodeObfuscator.js index fea047d9..1d30913a 100644 --- a/src/node-obfuscators/NodeObfuscator.js +++ b/src/node-obfuscators/NodeObfuscator.js @@ -25,7 +25,7 @@ class NodeObfuscator { } return `${prefix}${Utils_1.Utils.decToHex(nodeValue)}`; } - replaceLiteralStringByArrayElement(nodeValue) { + replaceLiteralStringByUnicodeArrayTranslatorCall(nodeValue) { let value = Utils_1.Utils.stringToUnicode(nodeValue), unicodeArray = this.nodes.get('unicodeArrayNode').getNodeData(), sameIndex = unicodeArray.indexOf(value), index, hexadecimalIndex; if (sameIndex < 0) { index = unicodeArray.length; @@ -35,7 +35,7 @@ class NodeObfuscator { index = sameIndex; } hexadecimalIndex = this.replaceLiteralNumberByHexadecimalValue(index); - return `${this.nodes.get('unicodeArrayNode').getNodeIdentifier()}[${hexadecimalIndex}]`; + return `${this.nodes.get('unicodeArrayTranslator').getNodeIdentifier()}('${hexadecimalIndex}')`; } } exports.NodeObfuscator = NodeObfuscator; diff --git a/src/node-obfuscators/NodeObfuscator.ts b/src/node-obfuscators/NodeObfuscator.ts index af2f8af5..193edc1d 100644 --- a/src/node-obfuscators/NodeObfuscator.ts +++ b/src/node-obfuscators/NodeObfuscator.ts @@ -70,7 +70,7 @@ export abstract class NodeObfuscator implements INodeObfuscator { * @param nodeValue * @returns {string} */ - protected replaceLiteralStringByArrayElement (nodeValue: string): string { + protected replaceLiteralStringByUnicodeArrayTranslatorCall (nodeValue: string): string { let value: string = Utils.stringToUnicode(nodeValue), unicodeArray: string[] = this.nodes.get('unicodeArrayNode').getNodeData(), sameIndex: number = unicodeArray.indexOf(value), @@ -86,6 +86,6 @@ export abstract class NodeObfuscator implements INodeObfuscator { hexadecimalIndex = this.replaceLiteralNumberByHexadecimalValue(index); - return `${this.nodes.get('unicodeArrayNode').getNodeIdentifier()}[${hexadecimalIndex}]`; + return `${this.nodes.get('unicodeArrayTranslator').getNodeIdentifier()}('${hexadecimalIndex}')`; } } diff --git a/tests/dev-test.js b/tests/dev-test.js index e867442e..b94c201a 100644 --- a/tests/dev-test.js +++ b/tests/dev-test.js @@ -47,8 +47,7 @@ let obfuscatedCode = index_1.JavaScriptObfuscator.obfuscate(` console.log(true, false); })(); `, { - disableConsoleOutput: false, - rotateUnicodeArray: false + disableConsoleOutput: false }); console.log(obfuscatedCode); console.log(eval(obfuscatedCode)); diff --git a/tests/dev-test.ts b/tests/dev-test.ts index 186b1024..22456520 100644 --- a/tests/dev-test.ts +++ b/tests/dev-test.ts @@ -49,8 +49,7 @@ let obfuscatedCode: string = JavaScriptObfuscator.obfuscate( })(); `, { - disableConsoleOutput: false, - rotateUnicodeArray: false + disableConsoleOutput: false } );