# All files / src/node-transformers/node-obfuscators FunctionObfuscator.ts

100% Statements 29/29
100% Branches 4/4
100% Functions 3/3
100% Lines 26/26

    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
    931x
    1x

    1x

    1x
    1x

    1x
    1x
    1x
    1x

    1x

    1778x

    1778x

    9333x

    9333x
    9333x

    9333x

    6414x
    6414x

    9333x

    455590x
    132354x

    132354x
    20082x
    20082x

    6414x

    9333x

     import { injectable, inject } from 'inversify';
    import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';

    import * as estraverse from 'estraverse';
    import * as ESTree from 'estree';

    import { IOptions } from '../../interfaces/options/IOptions';
    import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
    import { IObfuscatorReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';

    import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
    import { NodeType } from '../../enums/NodeType';

    import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
    import { Node } from '../../node/Node';
    import { NodeUtils } from '../../node/NodeUtils';
    import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';

    /**
     * replaces:
     *     function foo (argument1) { return argument1; };
     *
     * on:
     *     function foo (_0x12d45f) { return _0x12d45f; };
     *
     */
    @injectable()
    export class FunctionObfuscator extends AbstractNodeTransformer {
        /**
         * @type {IObfuscatorReplacerWithStorage}
         */
        private readonly identifierReplacer: IObfuscatorReplacerWithStorage;

        /**
         * @param nodeObfuscatorsReplacersFactory
         * @param options
         */
        constructor(
            @inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(options);

            this.identifierReplacer = <IObfuscatorReplacerWithStorage>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
        }

        /**
         * @param functionNode
         */
        public transformNode (functionNode: ESTree.Function): void {
            const nodeIdentifier: string = RandomGeneratorUtils.getRandomString(7);

            this.storeFunctionParams(functionNode, nodeIdentifier);
            this.replaceFunctionParams(functionNode, nodeIdentifier);
        }

        /**
         * @param functionNode
         * @param nodeIdentifier
         */
        private storeFunctionParams (functionNode: ESTree.Function, nodeIdentifier: string): void {
            functionNode.params
                .forEach((paramsNode: ESTree.Node) => {
                    NodeUtils.typedTraverse(paramsNode, NodeType.Identifier, {
                        enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
                    });
                });
        }

        /**
         * @param functionNode
         * @param nodeIdentifier
         */
        private replaceFunctionParams (functionNode: ESTree.Function, nodeIdentifier: string): void {
            let traverseVisitor: estraverse.Visitor = {
                enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                    if (Node.isReplaceableIdentifierNode(node, parentNode)) {
                        const newNodeName: string = this.identifierReplacer.replace(node.name, nodeIdentifier);

                        if (node.name !== newNodeName) {
                            node.name = newNodeName;
                            node.obfuscated = true;
                        }
                    }
                }
            };

            functionNode.params.forEach((paramsNode: ESTree.Node) => estraverse.replace(paramsNode, traverseVisitor));

            estraverse.replace(functionNode.body, traverseVisitor);
        }
    }