Selaa lähdekoodia

refactoring, file moving, wip

sanex3339 9 vuotta sitten
vanhempi
commit
bdb3bf5013

+ 2 - 31
dist/index.js

@@ -1,32 +1,3 @@
 "use strict";
-const esprima = require('esprima');
-const escodegen = require('escodegen');
-const Obfuscator_1 = require('./src/Obfuscator');
-class JavaScriptObfuscator {
-    static obfuscate(sourceCode, customOptions) {
-        let astTree = esprima.parse(sourceCode), options = Object.assign(JavaScriptObfuscator.defaultOptions, customOptions), obfuscator = new Obfuscator_1.Obfuscator(options);
-        obfuscator.obfuscateNode(astTree);
-        return JavaScriptObfuscator.generateCode(astTree, options);
-    }
-    static generateCode(astTree, options) {
-        let escodegenParams = Object.assign({}, JavaScriptObfuscator.escodegenParams);
-        if (options.hasOwnProperty('compact')) {
-            escodegenParams.format = {};
-            escodegenParams.format.compact = options.compact;
-        }
-        return escodegen.generate(astTree, escodegenParams);
-    }
-}
-JavaScriptObfuscator.defaultOptions = {
-    compact: true,
-    debugProtection: false,
-    debugProtectionInterval: false,
-    disableConsoleOutput: true,
-    encodeUnicodeArray: false,
-    rotateUnicodeArray: true,
-    wrapUnicodeArrayCalls: true
-};
-JavaScriptObfuscator.escodegenParams = {
-    verbatim: 'x-verbatim-property'
-};
-module.exports = JavaScriptObfuscator;
+const JavaScriptObfuscator_1 = require('./src/JavaScriptObfuscator');
+module.exports = JavaScriptObfuscator_1.JavaScriptObfuscator;

+ 24 - 0
dist/src/JavaScriptObfuscator.js

@@ -0,0 +1,24 @@
+"use strict";
+const esprima = require('esprima');
+const escodegen = require('escodegen');
+const DefaultPreset_1 = require('./preset-options/DefaultPreset');
+const Obfuscator_1 = require('./Obfuscator');
+class JavaScriptObfuscator {
+    static obfuscate(sourceCode, customOptions) {
+        let astTree = esprima.parse(sourceCode), options = Object.assign({}, DefaultPreset_1.DEFAULT_PRESET, customOptions), obfuscator = new Obfuscator_1.Obfuscator(options);
+        astTree = obfuscator.obfuscateNode(astTree);
+        return JavaScriptObfuscator.generateCode(astTree, options);
+    }
+    static generateCode(astTree, options) {
+        let escodegenParams = Object.assign({}, JavaScriptObfuscator.escodegenParams);
+        if (options.hasOwnProperty('compact')) {
+            escodegenParams.format = {};
+            escodegenParams.format.compact = options.compact;
+        }
+        return escodegen.generate(astTree, escodegenParams);
+    }
+}
+JavaScriptObfuscator.escodegenParams = {
+    verbatim: 'x-verbatim-property'
+};
+exports.JavaScriptObfuscator = JavaScriptObfuscator;

+ 6 - 0
dist/src/NodeUtils.js

@@ -42,6 +42,12 @@ class NodeUtils {
         }
         return node;
     }
+    static getProgramNode(bodyNode) {
+        return {
+            'type': NodeType_1.NodeType.Program,
+            'body': bodyNode
+        };
+    }
     static insertNodeAtIndex(blockScopeBody, node, index) {
         if (!NodeUtils.validateNode(node)) {
             return;

+ 1 - 0
dist/src/Obfuscator.js

@@ -40,6 +40,7 @@ class Obfuscator {
         this.beforeObfuscation(node);
         this.obfuscate(node);
         this.afterObfuscation(node);
+        return node;
     }
     setNode(nodeName, node) {
         this.nodes.set(nodeName, node);

+ 62 - 0
dist/src/custom-nodes/unicode-array-nodes/UnicodeArrayDecodeNode.js

@@ -0,0 +1,62 @@
+"use strict";
+const esprima = require('esprima');
+const JavaScriptObfuscator_1 = require('../../JavaScriptObfuscator');
+const AppendState_1 = require("../../enums/AppendState");
+const NoCustomNodesPreset_1 = require("../../preset-options/NoCustomNodesPreset");
+const Node_1 = require('../Node');
+const NodeUtils_1 = require("../../NodeUtils");
+const Utils_1 = require("../../Utils");
+class UnicodeArrayDecodeNode extends Node_1.Node {
+    constructor(unicodeArrayName, unicodeArray) {
+        super();
+        this.appendState = AppendState_1.AppendState.AfterObfuscation;
+        this.unicodeArrayName = unicodeArrayName;
+        this.unicodeArray = unicodeArray;
+        this.node = this.getNodeStructure();
+    }
+    appendNode(blockScopeNode) {
+        NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
+    }
+    getNode() {
+        if (!this.unicodeArray.length) {
+            return;
+        }
+        return super.getNode();
+    }
+    getNodeStructure() {
+        const atobPolyfill = JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(`
+                var object = []['filter']['constructor']('return this')();
+                var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
+    
+                object.atob || (
+                    object.atob = function(input) {
+                        var str = String(input).replace(/=+$/, '');
+                        for (
+                            var bc = 0, bs, buffer, idx = 0, output = '';
+                            buffer = str.charAt(idx++);
+                            ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
+                                bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
+                        ) {
+                            buffer = chars.indexOf(buffer);
+                        }
+                    return output;
+                });
+            `, NoCustomNodesPreset_1.NO_CUSTOM_NODES_PRESET), indexVariableName = Utils_1.Utils.getRandomVariableName(), tempArrayName = Utils_1.Utils.getRandomVariableName();
+        let node = esprima.parse(`
+            (function () {
+                ${atobPolyfill}
+              
+                var ${tempArrayName} = [];
+                
+                for (var ${indexVariableName} in ${this.unicodeArrayName}) {
+                    ${tempArrayName}[${Utils_1.Utils.stringToUnicode('push')}](atob(${this.unicodeArrayName}[${indexVariableName}]));
+                }
+                
+                ${this.unicodeArrayName} = ${tempArrayName};
+            })();
+        `);
+        NodeUtils_1.NodeUtils.addXVerbatimPropertyToLiterals(node);
+        return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(node);
+    }
+}
+exports.UnicodeArrayDecodeNode = UnicodeArrayDecodeNode;

+ 9 - 0
dist/src/preset-nodes/ProgramNode.js

@@ -0,0 +1,9 @@
+"use strict";
+const NodeType_1 = require("../enums/NodeType");
+function getProgramNode(bodyNode) {
+    return {
+        'type': NodeType_1.NodeType.Program,
+        'body': bodyNode
+    };
+}
+exports.getProgramNode = getProgramNode;

+ 10 - 0
dist/src/preset-options/DefaultOptions.js

@@ -0,0 +1,10 @@
+"use strict";
+exports.DEFAULT_OPTIONS = {
+    compact: true,
+    debugProtection: false,
+    debugProtectionInterval: false,
+    disableConsoleOutput: true,
+    encodeUnicodeArray: false,
+    rotateUnicodeArray: true,
+    wrapUnicodeArrayCalls: true
+};

+ 10 - 0
dist/src/preset-options/DefaultPreset.js

@@ -0,0 +1,10 @@
+"use strict";
+exports.DEFAULT_PRESET = {
+    compact: true,
+    debugProtection: false,
+    debugProtectionInterval: false,
+    disableConsoleOutput: true,
+    encodeUnicodeArray: false,
+    rotateUnicodeArray: true,
+    wrapUnicodeArrayCalls: true
+};

+ 10 - 0
dist/src/preset-options/NoCustomNodesPreset.js

@@ -0,0 +1,10 @@
+"use strict";
+exports.NO_CUSTOM_NODES_PRESET = {
+    compact: true,
+    debugProtection: false,
+    debugProtectionInterval: false,
+    disableConsoleOutput: false,
+    encodeUnicodeArray: false,
+    rotateUnicodeArray: false,
+    wrapUnicodeArrayCalls: false
+};

+ 3 - 1
dist/tests/dev-test.js

@@ -1,4 +1,6 @@
-let JavaScriptObfuscator = require('../index'), obfuscatedCode = JavaScriptObfuscator.obfuscate(`
+"use strict";
+const JavaScriptObfuscator_1 = require('../src/JavaScriptObfuscator');
+let obfuscatedCode = JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(`
     (function(){
         var result = 1,
             term1 = 0,

+ 1 - 58
index.ts

@@ -1,62 +1,5 @@
 "use strict";
 
-import * as esprima from 'esprima';
-import * as escodegen from 'escodegen';
-
-import { IOptions } from "./src/interfaces/IOptions";
-import { IProgramNode } from './src/interfaces/nodes/IProgramNode';
-
-import { Obfuscator } from './src/Obfuscator';
-
-class JavaScriptObfuscator {
-    /**
-     * @type {IOptions}
-     */
-    private static defaultOptions: IOptions = {
-        compact: true,
-        debugProtection: false,
-        debugProtectionInterval: false,
-        disableConsoleOutput: true,
-        encodeUnicodeArray: false,
-        rotateUnicodeArray: true,
-        wrapUnicodeArrayCalls: true
-    };
-
-    /**
-     * @type any
-     */
-    private static escodegenParams: any = {
-        verbatim: 'x-verbatim-property'
-    };
-
-    /**
-     * @param sourceCode
-     * @param customOptions
-     */
-    public static obfuscate (sourceCode: string, customOptions?: IOptions): string {
-        let astTree: IProgramNode = esprima.parse(sourceCode),
-            options: any = Object.assign(JavaScriptObfuscator.defaultOptions, customOptions),
-            obfuscator: Obfuscator = new Obfuscator(options);
-
-        obfuscator.obfuscateNode(astTree);
-
-        return JavaScriptObfuscator.generateCode(astTree, options);
-    }
-
-    /**
-     * @param astTree
-     * @param options
-     */
-    private static generateCode (astTree: IProgramNode, options: IOptions): string {
-        let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams);
-
-        if (options.hasOwnProperty('compact')) {
-            escodegenParams.format = {};
-            escodegenParams.format.compact = options.compact;
-        }
-
-        return escodegen.generate(astTree, escodegenParams);
-    }
-}
+import { JavaScriptObfuscator } from './src/JavaScriptObfuscator';
 
 module.exports = JavaScriptObfuscator;

+ 49 - 0
src/JavaScriptObfuscator.ts

@@ -0,0 +1,49 @@
+"use strict";
+
+import * as esprima from 'esprima';
+import * as escodegen from 'escodegen';
+
+import { INode } from './interfaces/nodes/INode';
+import { IOptions } from './interfaces/IOptions';
+
+import { DEFAULT_PRESET } from './preset-options/DefaultPreset';
+
+import { Obfuscator } from './Obfuscator';
+
+export class JavaScriptObfuscator {
+    /**
+     * @type any
+     */
+    private static escodegenParams: any = {
+        verbatim: 'x-verbatim-property'
+    };
+
+    /**
+     * @param sourceCode
+     * @param customOptions
+     */
+    public static obfuscate (sourceCode: string, customOptions?: IOptions): string {
+        let astTree: INode = esprima.parse(sourceCode),
+            options: any = Object.assign({}, DEFAULT_PRESET, customOptions),
+            obfuscator: Obfuscator = new Obfuscator(options);
+
+        astTree = obfuscator.obfuscateNode(astTree);
+
+        return JavaScriptObfuscator.generateCode(astTree, options);
+    }
+
+    /**
+     * @param astTree
+     * @param options
+     */
+    private static generateCode (astTree: INode, options: IOptions): string {
+        let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams);
+
+        if (options.hasOwnProperty('compact')) {
+            escodegenParams.format = {};
+            escodegenParams.format.compact = options.compact;
+        }
+
+        return escodegen.generate(astTree, escodegenParams);
+    }
+}

+ 11 - 0
src/NodeUtils.ts

@@ -93,6 +93,17 @@ export class NodeUtils {
         return <TBlockScopeNode> node; // blocks statement of scopeNodes
     }
 
+    /**
+     * @param bodyNode
+     * @returns IProgramNode
+     */
+    public static getProgramNode (bodyNode: INode[]): IProgramNode {
+        return {
+            'type': NodeType.Program,
+            'body': bodyNode
+        };
+    }
+
     /**
      * @param blockScopeBody
      * @param node

+ 4 - 1
src/Obfuscator.ts

@@ -62,8 +62,9 @@ export class Obfuscator {
 
     /**
      * @param node
+     * @returns {INode}
      */
-    public obfuscateNode (node: INode): void {
+    public obfuscateNode (node: INode): INode {
         this.setNewNodes();
 
         NodeUtils.parentize(node);
@@ -71,6 +72,8 @@ export class Obfuscator {
         this.beforeObfuscation(node);
         this.obfuscate(node);
         this.afterObfuscation(node);
+
+        return node;
     }
 
     /**

+ 36 - 15
src/custom-nodes/unicode-array-nodes/UnicodeArrayDecodeNode.ts

@@ -1,4 +1,5 @@
 import * as esprima from 'esprima';
+import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
 
 import { INode } from "../../interfaces/nodes/INode";
 
@@ -6,6 +7,8 @@ import { TBlockScopeNode } from "../../types/TBlockScopeNode";
 
 import { AppendState } from "../../enums/AppendState";
 
+import { NO_CUSTOM_NODES_PRESET } from "../../preset-options/NoCustomNodesPreset";
+
 import { Node } from '../Node';
 import { NodeUtils } from "../../NodeUtils";
 import { Utils } from "../../Utils";
@@ -64,22 +67,40 @@ export class UnicodeArrayDecodeNode extends Node {
      * @returns {INode}
      */
     protected getNodeStructure (): INode {
-        let decodedTempArrayName: string = Utils.getRandomVariableName(),
+        const atobPolyfill: string = JavaScriptObfuscator.obfuscate(`
+                var object = []['filter']['constructor']('return this')();
+                var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
+    
+                object.atob || (
+                    object.atob = function(input) {
+                        var str = String(input).replace(/=+$/, '');
+                        for (
+                            var bc = 0, bs, buffer, idx = 0, output = '';
+                            buffer = str.charAt(idx++);
+                            ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
+                                bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
+                        ) {
+                            buffer = chars.indexOf(buffer);
+                        }
+                    return output;
+                });
+            `, NO_CUSTOM_NODES_PRESET),
             indexVariableName: string = Utils.getRandomVariableName(),
-            node: INode = esprima.parse(`
-                (function () {
-                    //atob polyfill
-                    !function(){var r=[]["filter"]["constructor"]("return this")(),e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";r.atob||(r.atob=function(r){var o=String(r).replace(/=+$/,"");for(var n,a,i=0,c=0,d="";a=o.charAt(c++);~a&&(n=i%4?64*n+a:a,i++%4)?d+=String.fromCharCode(255&n>>(-2*i&6)):0)a=e.indexOf(a);return d})}(); 
-                    
-                    var ${decodedTempArrayName} = [];
-                    
-                    for (var ${indexVariableName} in ${this.unicodeArrayName}) {
-                        ${decodedTempArrayName}[${Utils.stringToUnicode('push')}](atob(${this.unicodeArrayName}[${indexVariableName}]));
-                    }
-                    
-                    ${this.unicodeArrayName} = ${decodedTempArrayName};
-                })();
-            `);
+            tempArrayName: string = Utils.getRandomVariableName();
+
+        let node: INode = esprima.parse(`
+            (function () {
+                ${atobPolyfill}
+              
+                var ${tempArrayName} = [];
+                
+                for (var ${indexVariableName} in ${this.unicodeArrayName}) {
+                    ${tempArrayName}[${Utils.stringToUnicode('push')}](atob(${this.unicodeArrayName}[${indexVariableName}]));
+                }
+                
+                ${this.unicodeArrayName} = ${tempArrayName};
+            })();
+        `);
 
         NodeUtils.addXVerbatimPropertyToLiterals(node);
 

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

@@ -89,7 +89,7 @@ export abstract class NodeObfuscator implements INodeObfuscator {
         let value: string = nodeValue;
 
         if (this.options['encodeUnicodeArray']) {
-            value = new Buffer(value).toString('base64')
+            value = new Buffer(value).toString('base64');
         }
 
         value = Utils.stringToUnicode(value);

+ 11 - 0
src/preset-options/DefaultPreset.ts

@@ -0,0 +1,11 @@
+import { IOptions } from "../interfaces/IOptions";
+
+export const DEFAULT_PRESET: IOptions = {
+    compact: true,
+    debugProtection: false,
+    debugProtectionInterval: false,
+    disableConsoleOutput: true,
+    encodeUnicodeArray: false,
+    rotateUnicodeArray: true,
+    wrapUnicodeArrayCalls: true
+};

+ 11 - 0
src/preset-options/NoCustomNodesPreset.ts

@@ -0,0 +1,11 @@
+import { IOptions } from "../interfaces/IOptions";
+
+export const NO_CUSTOM_NODES_PRESET: IOptions = {
+    compact: true,
+    debugProtection: false,
+    debugProtectionInterval: false,
+    disableConsoleOutput: false,
+    encodeUnicodeArray: false,
+    rotateUnicodeArray: false,
+    wrapUnicodeArrayCalls: false
+};

+ 3 - 2
tests/dev-test.ts

@@ -1,5 +1,6 @@
-let JavaScriptObfuscator: any = require('../index'),
-    obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
+import { JavaScriptObfuscator } from '../src/JavaScriptObfuscator';
+
+let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
     `
     (function(){
         var result = 1,

+ 1 - 6
tslint.json

@@ -3,7 +3,6 @@
     "align": [
       true,
       "parameters",
-      "arguments",
       "statements"
     ],
     "ban": [
@@ -128,11 +127,7 @@
       }
     ],
     "use-isnan": true,
-    "use-strict": [
-      true,
-      "check-module",
-      "check-function"
-    ],
+    "use-strict": false,
     "variable-name": false,
     "whitespace": [
       true,