Code coverage report for src/node-transformers/obfuscating-transformers/replacers/StringLiteralReplacer.ts

# All files / src/node-transformers/obfuscating-transformers/replacers StringLiteralReplacer.ts

100% Statements 55/55
100% Branches 16/16
100% Functions 2/2
100% Lines 50/50

    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
    158
    159
    1601x
    1x

    1x

    1x
    1x
    1x
    1x

    1x

    1x

    5514x

    5514x

    5514x

    5514x
    5514x

    5514x
    275700x

    60532x

    60532x

    60532x
    36363x

    24169x

    24169x
    10252x

    13917x

    24169x

    24169x

    10252x
    1x

    10251x
    10251x

    10251x
    10251x

    10251x

    10252x
    10252x

    134x
    134x

    134x

    1x

    1x

    10117x

    10252x

    10252x

    10252x
    10252x
    10252x
    10252x

    10252x
    134x

    10118x

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

    import { ICustomNodeGroup } from '../../../interfaces/custom-nodes/ICustomNodeGroup';
    import { IEncodedValue } from '../../../interfaces/node-transformers/IEncodedValue';
    import { IOptions } from '../../../interfaces/options/IOptions';
    import { IStorage } from '../../../interfaces/storages/IStorage';

    import { StringArrayEncoding } from '../../../enums/StringArrayEncoding';

    import { AbstractReplacer } from './AbstractReplacer';
    import { CryptUtils } from '../../../utils/CryptUtils';
    import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
    import { Utils } from '../../../utils/Utils';

    @injectable()
    export class StringLiteralReplacer extends AbstractReplacer {
        /**
         * @type {number}
         */
        private static readonly minimumLengthForStringArray: number = 3;

        /**
         * @type {IStorage<ICustomNodeGroup>}
         */
        private readonly customNodeGroupStorage: IStorage<ICustomNodeGroup>;

        /**
         * @type {string[]}
         */
        private readonly rc4Keys: string[];

        /**
         * @type {Map<string, string>}
         */
        private readonly stringLiteralCache: Map <string, string> = new Map();

        /**
         * @type {Map<string, string>}
         */
        private readonly stringLiteralHexadecimalIndexCache: Map <string, string> = new Map();

        /**
         * @type {IStorage<string>}
         */
        private readonly stringArrayStorage: IStorage<string>;

        /**
         * @param customNodeGroupStorage
         * @param stringArrayStorage
         * @param options
         */
        constructor (
            @inject(ServiceIdentifiers.TCustomNodeGroupStorage) customNodeGroupStorage: IStorage<ICustomNodeGroup>,
            @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStorage<string>,
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(options);

            this.customNodeGroupStorage = customNodeGroupStorage;
            this.stringArrayStorage = stringArrayStorage;

            this.rc4Keys = RandomGeneratorUtils.getRandomGenerator()
                .n(() => RandomGeneratorUtils.getRandomGenerator().string({length: 4}), 50);
        }

        /**
         * @param nodeValue
         * @returns {string}
         */
        public replace (nodeValue: string): string {
            const usingStringArray: boolean = (
                this.options.stringArray &&
                nodeValue.length >= StringLiteralReplacer.minimumLengthForStringArray &&
                RandomGeneratorUtils.getMathRandom() <= this.options.stringArrayThreshold
            );
            const cacheKey: string = `${nodeValue}-${String(usingStringArray)}`;

            if (this.stringLiteralCache.has(cacheKey) && this.options.stringArrayEncoding !== StringArrayEncoding.rc4) {
                return <string>this.stringLiteralCache.get(cacheKey);
            }

            let result: string;

            if (usingStringArray) {
                result = this.replaceStringLiteralWithStringArrayCall(nodeValue);
            } else {
                result = `'${Utils.stringToUnicodeEscapeSequence(nodeValue, !this.options.unicodeEscapeSequence)}'`;
            }

            this.stringLiteralCache.set(cacheKey, result);

            return result;
        }

        /**
         * @param value
         * @return {string}
         */
        private getArrayHexadecimalIndex (value: string): string {
            if (this.stringLiteralHexadecimalIndexCache.has(value)) {
                return <string>this.stringLiteralHexadecimalIndexCache.get(value);
            }

            const stringArrayStorageLength: number = this.stringArrayStorage.getLength();
            const hexadecimalIndex: string = `${Utils.hexadecimalPrefix}${Utils.decToHex(stringArrayStorageLength)}`;

            this.stringArrayStorage.set(stringArrayStorageLength, value);
            this.stringLiteralHexadecimalIndexCache.set(value, hexadecimalIndex);

            return hexadecimalIndex;
        }

        /**
         * @param value
         * @returns {IEncodedValue}
         */
        private getEncodedValue (value: string): IEncodedValue {
            let encodedValue: string,
                key: string | undefined;

            switch (this.options.stringArrayEncoding) {
                case StringArrayEncoding.rc4:
                    key = RandomGeneratorUtils.getRandomGenerator().pickone(this.rc4Keys);
                    encodedValue = CryptUtils.btoa(CryptUtils.rc4(value, key));

                    break;

                case StringArrayEncoding.base64:
                    encodedValue = CryptUtils.btoa(value);

                    break;

                default:
                    encodedValue = value;
            }

            encodedValue = Utils.stringToUnicodeEscapeSequence(encodedValue, !this.options.unicodeEscapeSequence);

            return { encodedValue, key };
        }

        /**
         * @param value
         * @returns {string}
         */
        private replaceStringLiteralWithStringArrayCall (value: string): string {
            const { encodedValue, key }: IEncodedValue = this.getEncodedValue(value);
            const hexadecimalIndex: string = this.getArrayHexadecimalIndex(encodedValue);
            const rotatedStringArrayStorageId: string = Utils.stringRotate(this.stringArrayStorage.getStorageId(), 1);
            const stringArrayStorageCallsWrapperName: string = `_${Utils.hexadecimalPrefix}${rotatedStringArrayStorageId}`;

            if (key) {
                return `${stringArrayStorageCallsWrapperName}('${hexadecimalIndex}', '${Utils.stringToUnicodeEscapeSequence(key, !this.options.unicodeEscapeSequence)}')`;
            }

            return `${stringArrayStorageCallsWrapperName}('${hexadecimalIndex}')`;
        }
    }