All files / src/custom-nodes/unicode-array-nodes UnicodeArrayDecodeNode.js

29.03% Statements 9/31
20% Branches 1/5
0% Functions 0/1
33.33% Lines 9/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                               1x  
"use strict";
const esprima = require('esprima');
const JavaScriptObfuscator_1 = require('../../JavaScriptObfuscator');
const AppendState_1 = require("../../enums/AppendState");
const JSFuck_1 = require("../../enums/JSFuck");
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, options) {
        super(options);
        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;
        }
        this.updateNode();
        return super.getNode();
    }
    getNodeStructure() {
        const environmentName = Utils_1.Utils.getRandomVariableName(), forLoopFunctionName = Utils_1.Utils.getRandomVariableName(), indexVariableName = Utils_1.Utils.getRandomVariableName(), tempArrayName = Utils_1.Utils.getRandomVariableName();
        let code = '', node;
        if (this.options.get('selfDefending')) {
            code = `
                var ${environmentName} = function(){return ${Utils_1.Utils.stringToUnicode('dev')};};
                   
                Function(${Utils_1.Utils.stringToUnicode(`return/\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/`)})()[${Utils_1.Utils.stringToUnicode('test')}](${environmentName}[${Utils_1.Utils.stringToUnicode('toString')}]()) !== ${JSFuck_1.JSFuck.True} && !${this.unicodeArrayName}++ ? []['filter']['constructor'](${Utils_1.Utils.stringToJSFuck('while')} + '(${JSFuck_1.JSFuck.True}){}')() : Function(${Utils_1.Utils.stringToUnicode('a')}, atob(${Utils_1.Utils.stringToUnicode(Utils_1.Utils.btoa('a.call()'))}))(${forLoopFunctionName}) ? []['filter']['constructor'](${Utils_1.Utils.stringToJSFuck('while')} + '(${JSFuck_1.JSFuck.False}){}')() : []['filter']['constructor'](${Utils_1.Utils.stringToJSFuck('while')} + '(${JSFuck_1.JSFuck.False}){}')();
            `;
        }
        else {
            code = `${forLoopFunctionName}();`;
        }
        node = esprima.parse(`
            (function () {
                ${JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(`
                    (function () {
                        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)}
              
                var ${forLoopFunctionName} = function () {
                    var ${tempArrayName} = [];
                    
                    for (var ${indexVariableName} in ${this.unicodeArrayName}) {
                        ${tempArrayName}[${Utils_1.Utils.stringToUnicode('push')}](decodeURI(atob(${this.unicodeArrayName}[${indexVariableName}])));
                    }
                    
                    ${this.unicodeArrayName} = ${tempArrayName};
                };
                
                ${code}
            })();
        `);
        NodeUtils_1.NodeUtils.addXVerbatimPropertyToLiterals(node);
        return NodeUtils_1.NodeUtils.getBlockStatementNodeByIndex(node);
    }
}
exports.UnicodeArrayDecodeNode = UnicodeArrayDecodeNode;
//# sourceMappingURL=UnicodeArrayDecodeNode.js.map