Browse Source

refactoring

sanex3339 9 years ago
parent
commit
47c560ed7a

+ 1 - 0
src/NodeUtils.js

@@ -22,6 +22,7 @@ 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);
         }

+ 2 - 0
src/NodeUtils.ts

@@ -65,6 +65,8 @@ 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);
         }

+ 18 - 257
src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js

@@ -1,9 +1,8 @@
 "use strict";
+const esprima = require('esprima');
 const AppendState_1 = require("../../enums/AppendState");
-const NodeType_1 = require("../../enums/NodeType");
 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();
@@ -23,263 +22,25 @@ class UnicodeArrayRotateFunctionNode extends Node_1.Node {
         return super.getNode();
     }
     getNodeStructure() {
-        return {
-            "type": NodeType_1.NodeType.ExpressionStatement,
-            "expression": {
-                "type": NodeType_1.NodeType.CallExpression,
-                "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'
+        return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(esprima.parse(`
+                    (function (array, times, reverse) {
+                        if (times < 0) {
+                            return;
                         }
-                    ],
-                    'defaults': [],
-                    'body': {
-                        'type': NodeType_1.NodeType.BlockStatement,
-                        'body': [
-                            {
-                                'type': NodeType_1.NodeType.IfStatement,
-                                'test': {
-                                    'type': NodeType_1.NodeType.BinaryExpression,
-                                    'operator': '<',
-                                    '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'
-                                                                }
-                                                            ]
-                                                        }
-                                                    }
-                                                ]
-                                            }
-                                        }
-                                    ]
-                                }
+                    
+                        var temp;
+                    
+                        while (times--) {
+                            if (!reverse) {
+                                temp = array.pop();
+                                array.unshift(temp);
+                            } else {
+                                temp = array.shift();
+                                array.push(temp);
                             }
-                        ]
-                    },
-                    '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'
-                    }
-                ]
-            }
-        };
+                        }
+                    })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
+                `));
     }
 }
 exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode;

+ 49 - 257
src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts

@@ -1,13 +1,15 @@
+import * as esprima from 'esprima';
+
 import { INode } from "../../interfaces/nodes/INode";
 
 import { BlockScopeNode } from "../../types/BlockScopeNode";
 
 import { AppendState } from "../../enums/AppendState";
-import { NodeType } from "../../enums/NodeType";
 
 import { Node } from '../Node';
 import { NodeUtils } from "../../NodeUtils";
-import { Utils } from '../../Utils';
+import {Utils} from "../../Utils";
+import {Obfuscator} from "../../Obfuscator";
 
 export class UnicodeArrayRotateFunctionNode extends Node {
     /**
@@ -71,262 +73,52 @@ export class UnicodeArrayRotateFunctionNode extends Node {
      * @returns any
      */
     protected getNodeStructure (): any {
-        return {
-            "type": NodeType.ExpressionStatement,
-            "expression": {
-                "type": NodeType.CallExpression,
-                "callee": {
-                    'type': NodeType.FunctionExpression,
-                    'id': null,
-                    'params': [
-                        {
-                            'type': NodeType.Identifier,
-                            'name': 'array'
-                        },
-                        {
-                            'type': NodeType.Identifier,
-                            'name': 'times'
-                        },
-                        {
-                            'type': NodeType.Identifier,
-                            'name': 'reverse'
+        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);
+                            }
                         }
-                    ],
-                    'defaults': [],
-                    'body': {
-                        'type': NodeType.BlockStatement,
-                        'body': [
-                            {
-                                'type': NodeType.IfStatement,
-                                'test': {
-                                    'type': NodeType.BinaryExpression,
-                                    'operator': '<',
-                                    '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);
+                `)
+        );
+
+        /*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);
                             }
-                        ]
-                    },
-                    'generator': false,
-                    'expression': false
-                },
-                "arguments": [
-                    {
-                        'type': NodeType.Identifier,
-                        'name': this.unicodeArrayName
-                    },
-                    {
-                        'type': NodeType.Literal,
-                        'value': this.unicodeArrayRotateValue,
-                        'raw': `'${this.unicodeArrayRotateValue}'`
-                    },
-                    {
-                        'type': NodeType.Literal,
-                        'value': true,
-                        'raw': 'true'
-                    }
-                ]
-            }
-        };
+                        }
+                    })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
+                `)
+            )
+        );*/
     }
 }

+ 0 - 1
tests/dev-test.js

@@ -48,7 +48,6 @@ 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
     }
 );