浏览代码

added UnicodeArray translator node

sanex3339 9 年之前
父节点
当前提交
74f0ec4644

+ 1 - 8
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;

+ 1 - 8
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));
     }
 }

+ 93 - 0
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);
+    }
+}

+ 9 - 3
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;

+ 30 - 3
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
                 )
-            ]
-        ]);
+            );
+        }
     }
 }

+ 1 - 1
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;

+ 1 - 1
src/node-obfuscators/LiteralObfuscator.ts

@@ -35,7 +35,7 @@ export class LiteralObfuscator extends NodeObfuscator {
 
 
             case 'string':
-                content = this.replaceLiteralStringByArrayElement(<string>literalNode.value);
+                content = this.replaceLiteralStringByUnicodeArrayTranslatorCall(<string>literalNode.value);
 
                 break;
 

+ 2 - 2
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;

+ 2 - 2
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(<string>node.value),
+                    content : this.replaceLiteralStringByUnicodeArrayTranslatorCall(<string>node.value),
                     precedence: escodegen.Precedence.Primary
                 };
 

+ 1 - 1
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;

+ 1 - 1
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;
                 }

+ 2 - 2
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;

+ 2 - 2
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}')`;
     }
 }

+ 1 - 2
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));

+ 1 - 2
tests/dev-test.ts

@@ -49,8 +49,7 @@ let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
     })();
     `,
     {
-        disableConsoleOutput: false,
-        rotateUnicodeArray: false
+        disableConsoleOutput: false
     }
 );