sanex3339 há 9 anos atrás
pai
commit
15eeaa8b41

+ 9 - 1
src/NodeUtils.js

@@ -3,6 +3,15 @@ const estraverse = require('estraverse');
 const NodeType_1 = require("./enums/NodeType");
 const Utils_1 = require("./Utils");
 class NodeUtils {
+    static addXVerbatimPropertyToLiterals(node) {
+        estraverse.replace(node, {
+            enter: (node, parentNode) => {
+                if (NodeUtils.isLiteralNode(node)) {
+                    node['x-verbatim-property'] = node.raw;
+                }
+            }
+        });
+    }
     static appendNode(blockScopeBody, node) {
         if (!NodeUtils.validateNode(node)) {
             return;
@@ -22,7 +31,6 @@ class NodeUtils {
         if (node.parentNode.type === NodeType_1.NodeType.Program) {
             return node.parentNode;
         }
-        console.log(node.type, node.parentNode.type);
         if (!Utils_1.Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
             return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
         }

+ 13 - 2
src/NodeUtils.ts

@@ -26,6 +26,19 @@ export class NodeUtils {
         NodeType.MethodDefinition
     ];
 
+    /**
+     * @param node
+     */
+    public static addXVerbatimPropertyToLiterals (node: INode): void {
+        estraverse.replace(node, {
+            enter: (node: INode, parentNode: INode): any => {
+                if (NodeUtils.isLiteralNode(node)) {
+                   node['x-verbatim-property'] = node.raw;
+                }
+            }
+        });
+    }
+
     /**
      * @param blockScopeBody
      * @param node
@@ -65,8 +78,6 @@ export class NodeUtils {
             return <BlockScopeNode> node.parentNode;
         }
 
-        console.log(node.type, node.parentNode.type);
-
         if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
             return NodeUtils.getBlockScopeOfNode(node.parentNode, depth);
         }

+ 17 - 19
src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js

@@ -3,6 +3,7 @@ const esprima = require('esprima');
 const AppendState_1 = require("../../enums/AppendState");
 const Node_1 = require('../Node');
 const NodeUtils_1 = require("../../NodeUtils");
+const Utils_1 = require("../../Utils");
 class UnicodeArrayRotateFunctionNode extends Node_1.Node {
     constructor(unicodeArrayName, unicodeArray, unicodeArrayRotateValue) {
         super();
@@ -22,25 +23,22 @@ class UnicodeArrayRotateFunctionNode extends Node_1.Node {
         return super.getNode();
     }
     getNodeStructure() {
-        return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(esprima.parse(`
-                    (function (array, times, reverse) {
-                        if (times < 0) {
-                            return;
-                        }
-                    
-                        var temp;
-                    
-                        while (times--) {
-                            if (!reverse) {
-                                temp = array.pop();
-                                array.unshift(temp);
-                            } else {
-                                temp = array.shift();
-                                array.push(temp);
-                            }
-                        }
-                    })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
-                `));
+        let arrayName = Utils_1.Utils.getRandomVariableName(), timesName = Utils_1.Utils.getRandomVariableName(), tempArrayName = Utils_1.Utils.getRandomVariableName(), node = esprima.parse(`
+                (function (${arrayName}, ${timesName}) {
+                    if (${timesName} < 0x${Utils_1.Utils.decToHex(0)}) {
+                        return;
+                    }
+
+                    var ${tempArrayName};
+
+                    while (${timesName}--) {
+                        ${tempArrayName} = ${arrayName}[${Utils_1.Utils.stringToUnicode('shift')}]();
+                        ${arrayName}[${Utils_1.Utils.stringToUnicode('push')}](${tempArrayName});
+                    }
+                })(${this.unicodeArrayName}, 0x${Utils_1.Utils.decToHex(this.unicodeArrayRotateValue)});
+            `);
+        NodeUtils_1.NodeUtils.addXVerbatimPropertyToLiterals(node);
+        return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(node);
     }
 }
 exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode;

+ 22 - 48
src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts

@@ -40,7 +40,7 @@ export class UnicodeArrayRotateFunctionNode extends Node {
     constructor (
         unicodeArrayName: string,
         unicodeArray: string[],
-        unicodeArrayRotateValue
+        unicodeArrayRotateValue: number
     ) {
         super();
 
@@ -73,52 +73,26 @@ export class UnicodeArrayRotateFunctionNode extends Node {
      * @returns any
      */
     protected getNodeStructure (): any {
-        return NodeUtils.getBlockScopeNodeByIndex(
-            esprima.parse(`
-                    (function (array, times, reverse) {
-                        if (times < 0) {
-                            return;
-                        }
-                    
-                        var temp;
-                    
-                        while (times--) {
-                            if (!reverse) {
-                                temp = array.pop();
-                                array.unshift(temp);
-                            } else {
-                                temp = array.shift();
-                                array.push(temp);
-                            }
-                        }
-                    })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
-                `)
-        );
-
-        /*return new Obfuscator({
-            rotateUnicodeArray: false
-        }).obfuscateNode(
-            NodeUtils.getBlockScopeNodeByIndex(
-                esprima.parse(`
-                    (function (array, times, reverse) {
-                        if (times < 0) {
-                            return;
-                        }
-                    
-                        var temp;
-                    
-                        while (times--) {
-                            if (!reverse) {
-                                temp = array.pop();
-                                array.unshift(temp);
-                            } else {
-                                temp = array.shift();
-                                array.push(temp);
-                            }
-                        }
-                    })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
-                `)
-            )
-        );*/
+        let arrayName: string = Utils.getRandomVariableName(),
+            timesName: string = Utils.getRandomVariableName(),
+            tempArrayName: string = Utils.getRandomVariableName(),
+            node: INode = esprima.parse(`
+                (function (${arrayName}, ${timesName}) {
+                    if (${timesName} < 0x${Utils.decToHex(0)}) {
+                        return;
+                    }
+
+                    var ${tempArrayName};
+
+                    while (${timesName}--) {
+                        ${tempArrayName} = ${arrayName}[${Utils.stringToUnicode('shift')}]();
+                        ${arrayName}[${Utils.stringToUnicode('push')}](${tempArrayName});
+                    }
+                })(${this.unicodeArrayName}, 0x${Utils.decToHex(this.unicodeArrayRotateValue)});
+            `);
+
+        NodeUtils.addXVerbatimPropertyToLiterals(node);
+
+        return NodeUtils.getBlockScopeNodeByIndex(node);
     }
 }

+ 1 - 0
tests/dev-test.js

@@ -48,6 +48,7 @@ let obfuscatedCode = index_1.JavaScriptObfuscator.obfuscate(`
     })();
     `, {
     disableConsoleOutput: false,
+    rotateUnicodeArray: false
 });
 console.log(obfuscatedCode);
 console.log(eval(obfuscatedCode));

+ 1 - 1
tests/dev-test.ts

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