All files / src Obfuscator.ts

96.97% Statements 32/33
83.33% Branches 5/6
100% Functions 4/4
96.97% Lines 32/33
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 1581x 1x   1x                           1x 1x 1x 1x   1x 1x   1x 1x       1x               1x                                                                                                 2622x 2622x 2622x 2622x                 2622x 3x     2619x     2619x 2619x     3618x     2619x     2619x                 2619x           2619x   2619x                         2619x   495400x   495400x 163732x            
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from './container/ServiceIdentifiers';
 
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
 
import { TNodeTransformersFactory } from './types/container/TNodeTransformersFactory';
import { TVisitorDirection } from './types/TVisitorDirection';
 
import { ICustomNode } from './interfaces/custom-nodes/ICustomNode';
import { IObfuscationEventEmitter } from './interfaces/event-emitters/IObfuscationEventEmitter';
import { IObfuscator } from './interfaces/IObfuscator';
import { IOptions } from './interfaces/options/IOptions';
import { INodeTransformer } from './interfaces/node-transformers/INodeTransformer';
import { IStackTraceAnalyzer } from './interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
import { IStorage } from './interfaces/storages/IStorage';
 
import { NodeTransformers } from './enums/container/NodeTransformers';
import { NodeType } from './enums/NodeType';
import { ObfuscationEvents } from './enums/ObfuscationEvents';
import { VisitorDirection } from './enums/VisitorDirection';
 
import { Node } from './node/Node';
import { NodeUtils } from './node/NodeUtils';
 
@injectable()
export class Obfuscator implements IObfuscator {
    /**
     * @type {Map<string, NodeTransformers[]>}
     */
    private static readonly nodeControlFlowTransformersMap: Map <string, NodeTransformers[]> = new Map <string, NodeTransformers[]> ([
        [NodeType.FunctionDeclaration, [NodeTransformers.FunctionControlFlowTransformer]],
        [NodeType.FunctionExpression, [NodeTransformers.FunctionControlFlowTransformer]]
    ]);
 
    /**
     * @type {Map<string, NodeTransformers[]>}
     */
    private static readonly nodeObfuscatorsMap: Map <string, NodeTransformers[]> = new Map <string, NodeTransformers[]> ([
        [NodeType.ArrowFunctionExpression, [NodeTransformers.FunctionObfuscator]],
        [NodeType.ClassDeclaration, [NodeTransformers.FunctionDeclarationObfuscator]],
        [NodeType.CatchClause, [NodeTransformers.CatchClauseObfuscator]],
        [NodeType.FunctionDeclaration, [
            NodeTransformers.FunctionDeclarationObfuscator,
            NodeTransformers.FunctionObfuscator
        ]],
        [NodeType.FunctionExpression, [NodeTransformers.FunctionObfuscator]],
        [NodeType.MemberExpression, [NodeTransformers.MemberExpressionObfuscator]],
        [NodeType.MethodDefinition, [NodeTransformers.MethodDefinitionObfuscator]],
        [NodeType.ObjectExpression, [NodeTransformers.ObjectExpressionObfuscator]],
        [NodeType.VariableDeclaration, [NodeTransformers.VariableDeclarationObfuscator]],
        [NodeType.LabeledStatement, [NodeTransformers.LabeledStatementObfuscator]],
        [NodeType.Literal, [NodeTransformers.LiteralObfuscator]]
    ]);
 
    /**
     * @type {TNodeTransformersFactory}
     */
    private readonly nodeTransformersFactory: TNodeTransformersFactory;
 
    /**
     * @type {IObfuscationEventEmitter}
     */
    private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
 
    /**
     * @type {IOptions}
     */
    private readonly options: IOptions;
 
    /**
     * @type {IStackTraceAnalyzer}
     */
    private readonly stackTraceAnalyzer: IStackTraceAnalyzer;
 
    /**
     * @param stackTraceAnalyzer
     * @param obfuscationEventEmitter
     * @param nodeTransformersFactory
     * @param options
     */
    constructor (
        @inject(ServiceIdentifiers.IStackTraceAnalyzer) stackTraceAnalyzer: IStackTraceAnalyzer,
        @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
        @inject(ServiceIdentifiers['Factory<INodeTransformer[]>']) nodeTransformersFactory: TNodeTransformersFactory,
        @inject(ServiceIdentifiers.IOptions) options: IOptions
    ) {
        this.stackTraceAnalyzer = stackTraceAnalyzer;
        this.obfuscationEventEmitter = obfuscationEventEmitter;
        this.nodeTransformersFactory = nodeTransformersFactory;
        this.options = options;
    }
 
    /**
     * @param astTree
     * @param customNodesStorage
     * @returns {ESTree.Program}
     */
    public obfuscateAstTree (astTree: ESTree.Program, customNodesStorage: IStorage<ICustomNode>): ESTree.Program {
        if (Node.isProgramNode(astTree) && !astTree.body.length) {
            return astTree;
        }
 
        NodeUtils.parentize(astTree);
 
        // prepare custom nodes
        customNodesStorage.initialize(this.stackTraceAnalyzer.analyze(astTree.body));
        customNodesStorage
            .getStorage()
            .forEach((customNode: ICustomNode) => {
                this.obfuscationEventEmitter.once(customNode.getAppendEvent(), customNode.appendNode.bind(customNode));
            });
 
        this.obfuscationEventEmitter.emit(ObfuscationEvents.BeforeObfuscation, astTree);
 
        // first pass: control flow flattening
        Iif (this.options.controlFlowFlattening) {
            this.transformAstTree(
                astTree,
                VisitorDirection.leave,
                this.nodeTransformersFactory(Obfuscator.nodeControlFlowTransformersMap)
            );
        }
 
        // second pass: nodes obfuscation
        this.transformAstTree(
            astTree,
            VisitorDirection.enter,
            this.nodeTransformersFactory(Obfuscator.nodeObfuscatorsMap)
        );
 
        this.obfuscationEventEmitter.emit(ObfuscationEvents.AfterObfuscation, astTree);
 
        return astTree;
    }
 
    /**
     * @param astTree
     * @param direction
     * @param nodeTransformersConcreteFactory
     */
    private transformAstTree (
        astTree: ESTree.Program,
        direction: TVisitorDirection,
        nodeTransformersConcreteFactory: (nodeType: string) => INodeTransformer[]
    ): void {
        estraverse.traverse(astTree, {
            [direction]: (node: ESTree.Node, parentNode: ESTree.Node): void => {
                const nodeTransformers: INodeTransformer[] = nodeTransformersConcreteFactory(node.type);
 
                nodeTransformers.forEach((nodeTransformer: INodeTransformer) => {
                    nodeTransformer.transformNode(node, parentNode);
                });
            }
        });
    }
}