Browse Source

Added RandomGenerator clearing after obfuscation

sanex3339 8 years ago
parent
commit
2b1f78e587

+ 22 - 15
dist/index.js

@@ -406,6 +406,20 @@ var RandomGeneratorUtils = function () {
     }
     }
 
 
     (0, _createClass3.default)(RandomGeneratorUtils, null, [{
     (0, _createClass3.default)(RandomGeneratorUtils, null, [{
+        key: "initializeRandomGenerator",
+        value: function initializeRandomGenerator(seed) {
+            if (seed !== 0) {
+                RandomGeneratorUtils.randomGenerator = new chance_1.Chance(seed);
+            } else {
+                RandomGeneratorUtils.randomGenerator = new chance_1.Chance();
+            }
+        }
+    }, {
+        key: "clearRandomGenerator",
+        value: function clearRandomGenerator() {
+            RandomGeneratorUtils.randomVariableNameSet.clear();
+        }
+    }, {
         key: "getRandomFloat",
         key: "getRandomFloat",
         value: function getRandomFloat(min, max) {
         value: function getRandomFloat(min, max) {
             return RandomGeneratorUtils.getRandomGenerator().floating({
             return RandomGeneratorUtils.getRandomGenerator().floating({
@@ -451,11 +465,6 @@ var RandomGeneratorUtils = function () {
             RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName);
             RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName);
             return randomVariableName;
             return randomVariableName;
         }
         }
-    }, {
-        key: "setRandomGenerator",
-        value: function setRandomGenerator(randomGenerator) {
-            RandomGeneratorUtils.randomGenerator = randomGenerator;
-        }
     }]);
     }]);
     return RandomGeneratorUtils;
     return RandomGeneratorUtils;
 }();
 }();
@@ -463,7 +472,6 @@ var RandomGeneratorUtils = function () {
 RandomGeneratorUtils.randomGeneratorPool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 RandomGeneratorUtils.randomGeneratorPool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 RandomGeneratorUtils.randomGeneratorPoolWithNumbers = RandomGeneratorUtils.randomGeneratorPool + "0123456789";
 RandomGeneratorUtils.randomGeneratorPoolWithNumbers = RandomGeneratorUtils.randomGeneratorPool + "0123456789";
 RandomGeneratorUtils.randomVariableNameSet = new _set2.default();
 RandomGeneratorUtils.randomVariableNameSet = new _set2.default();
-RandomGeneratorUtils.randomGenerator = new chance_1.Chance();
 exports.RandomGeneratorUtils = RandomGeneratorUtils;
 exports.RandomGeneratorUtils = RandomGeneratorUtils;
 
 
 /***/ },
 /***/ },
@@ -1429,7 +1437,6 @@ if (!global._babelPolyfill && parseInt(process.version.split('.')[0], 10) < 4) {
     __webpack_require__(135);
     __webpack_require__(135);
 }
 }
 var ServiceIdentifiers_1 = __webpack_require__(4);
 var ServiceIdentifiers_1 = __webpack_require__(4);
-var chance_1 = __webpack_require__(49);
 var InversifyContainerFacade_1 = __webpack_require__(58);
 var InversifyContainerFacade_1 = __webpack_require__(58);
 var JavaScriptObfuscatorCLI_1 = __webpack_require__(57);
 var JavaScriptObfuscatorCLI_1 = __webpack_require__(57);
 var RandomGeneratorUtils_1 = __webpack_require__(9);
 var RandomGeneratorUtils_1 = __webpack_require__(9);
@@ -1446,11 +1453,11 @@ var JavaScriptObfuscator = function () {
 
 
             var inversifyContainerFacade = new InversifyContainerFacade_1.InversifyContainerFacade(inputOptions);
             var inversifyContainerFacade = new InversifyContainerFacade_1.InversifyContainerFacade(inputOptions);
             var options = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IOptions);
             var options = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IOptions);
-            if (options.seed !== 0) {
-                RandomGeneratorUtils_1.RandomGeneratorUtils.setRandomGenerator(new chance_1.Chance(options.seed));
-            }
+            RandomGeneratorUtils_1.RandomGeneratorUtils.initializeRandomGenerator(options.seed);
             var javaScriptObfuscator = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IJavaScriptObfuscator);
             var javaScriptObfuscator = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IJavaScriptObfuscator);
-            return javaScriptObfuscator.obfuscate(sourceCode);
+            var obfuscationResult = javaScriptObfuscator.obfuscate(sourceCode);
+            RandomGeneratorUtils_1.RandomGeneratorUtils.clearRandomGenerator();
+            return obfuscationResult;
         }
         }
     }, {
     }, {
         key: "runCLI",
         key: "runCLI",
@@ -6752,6 +6759,9 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace
         _this.stringLiteralHexadecimalIndexCache = new _map2.default();
         _this.stringLiteralHexadecimalIndexCache = new _map2.default();
         _this.customNodeGroupStorage = customNodeGroupStorage;
         _this.customNodeGroupStorage = customNodeGroupStorage;
         _this.stringArrayStorage = stringArrayStorage;
         _this.stringArrayStorage = stringArrayStorage;
+        _this.rc4Keys = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().n(function () {
+            return RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({ length: 4 });
+        }, 50);
         return _this;
         return _this;
     }
     }
 
 
@@ -6791,7 +6801,7 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace
                 key = void 0;
                 key = void 0;
             switch (this.options.stringArrayEncoding) {
             switch (this.options.stringArrayEncoding) {
                 case StringArrayEncoding_1.StringArrayEncoding.rc4:
                 case StringArrayEncoding_1.StringArrayEncoding.rc4:
-                    key = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().pickone(StringLiteralReplacer_1.rc4Keys);
+                    key = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().pickone(this.rc4Keys);
                     encodedValue = CryptUtils_1.CryptUtils.btoa(CryptUtils_1.CryptUtils.rc4(value, key));
                     encodedValue = CryptUtils_1.CryptUtils.btoa(CryptUtils_1.CryptUtils.rc4(value, key));
                     break;
                     break;
                 case StringArrayEncoding_1.StringArrayEncoding.base64:
                 case StringArrayEncoding_1.StringArrayEncoding.base64:
@@ -6822,9 +6832,6 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace
     return StringLiteralReplacer;
     return StringLiteralReplacer;
 }(AbstractReplacer_1.AbstractReplacer);
 }(AbstractReplacer_1.AbstractReplacer);
 StringLiteralReplacer.minimumLengthForStringArray = 3;
 StringLiteralReplacer.minimumLengthForStringArray = 3;
-StringLiteralReplacer.rc4Keys = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().n(function () {
-    return RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({ length: 4 });
-}, 50);
 StringLiteralReplacer = StringLiteralReplacer_1 = tslib_1.__decorate([inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.TCustomNodeGroupStorage)), tslib_1.__param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.TStringArrayStorage)), tslib_1.__param(2, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), tslib_1.__metadata("design:paramtypes", [Object, Object, Object])], StringLiteralReplacer);
 StringLiteralReplacer = StringLiteralReplacer_1 = tslib_1.__decorate([inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.TCustomNodeGroupStorage)), tslib_1.__param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.TStringArrayStorage)), tslib_1.__param(2, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), tslib_1.__metadata("design:paramtypes", [Object, Object, Object])], StringLiteralReplacer);
 exports.StringLiteralReplacer = StringLiteralReplacer;
 exports.StringLiteralReplacer = StringLiteralReplacer;
 var StringLiteralReplacer_1;
 var StringLiteralReplacer_1;

+ 5 - 6
src/JavaScriptObfuscator.ts

@@ -6,8 +6,6 @@ if (!(<any>global)._babelPolyfill && parseInt(process.version.split('.')[0], 10)
 
 
 import { ServiceIdentifiers } from './container/ServiceIdentifiers';
 import { ServiceIdentifiers } from './container/ServiceIdentifiers';
 
 
-import { Chance } from 'chance';
-
 import { TInputOptions } from './types/options/TInputOptions';
 import { TInputOptions } from './types/options/TInputOptions';
 
 
 import { IInversifyContainerFacade } from './interfaces/container/IInversifyContainerFacade';
 import { IInversifyContainerFacade } from './interfaces/container/IInversifyContainerFacade';
@@ -29,14 +27,15 @@ export class JavaScriptObfuscator {
         const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade(inputOptions);
         const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade(inputOptions);
         const options: IOptions = inversifyContainerFacade.get<IOptions>(ServiceIdentifiers.IOptions);
         const options: IOptions = inversifyContainerFacade.get<IOptions>(ServiceIdentifiers.IOptions);
 
 
-        if (options.seed !== 0) {
-            RandomGeneratorUtils.setRandomGenerator(new Chance(options.seed));
-        }
+        RandomGeneratorUtils.initializeRandomGenerator(options.seed);
 
 
         const javaScriptObfuscator: IJavaScriptObfuscator = inversifyContainerFacade
         const javaScriptObfuscator: IJavaScriptObfuscator = inversifyContainerFacade
             .get<IJavaScriptObfuscator>(ServiceIdentifiers.IJavaScriptObfuscator);
             .get<IJavaScriptObfuscator>(ServiceIdentifiers.IJavaScriptObfuscator);
+        const obfuscationResult: IObfuscationResult = javaScriptObfuscator.obfuscate(sourceCode);
+
+        RandomGeneratorUtils.clearRandomGenerator();
 
 
-        return javaScriptObfuscator.obfuscate(sourceCode);
+        return obfuscationResult;
     }
     }
 
 
     /**
     /**

+ 10 - 8
src/node-transformers/obfuscation-transformers/replacers/StringLiteralReplacer.ts

@@ -20,11 +20,15 @@ export class StringLiteralReplacer extends AbstractReplacer {
      */
      */
     private static readonly minimumLengthForStringArray: number = 3;
     private static readonly minimumLengthForStringArray: number = 3;
 
 
+    /**
+     * @type {IStorage<ICustomNodeGroup>}
+     */
+    private readonly customNodeGroupStorage: IStorage<ICustomNodeGroup>;
+
     /**
     /**
      * @type {string[]}
      * @type {string[]}
      */
      */
-    private static readonly rc4Keys: string[] = RandomGeneratorUtils.getRandomGenerator()
-        .n(() => RandomGeneratorUtils.getRandomGenerator().string({length: 4}), 50);
+    private readonly rc4Keys: string[];
 
 
     /**
     /**
      * @type {Map<string, string>}
      * @type {Map<string, string>}
@@ -36,11 +40,6 @@ export class StringLiteralReplacer extends AbstractReplacer {
      */
      */
     private readonly stringLiteralHexadecimalIndexCache: Map <string, string> = new Map();
     private readonly stringLiteralHexadecimalIndexCache: Map <string, string> = new Map();
 
 
-    /**
-     * @type {IStorage<ICustomNodeGroup>}
-     */
-    private readonly customNodeGroupStorage: IStorage<ICustomNodeGroup>;
-
     /**
     /**
      * @type {IStorage<string>}
      * @type {IStorage<string>}
      */
      */
@@ -60,6 +59,9 @@ export class StringLiteralReplacer extends AbstractReplacer {
 
 
         this.customNodeGroupStorage = customNodeGroupStorage;
         this.customNodeGroupStorage = customNodeGroupStorage;
         this.stringArrayStorage = stringArrayStorage;
         this.stringArrayStorage = stringArrayStorage;
+
+        this.rc4Keys = RandomGeneratorUtils.getRandomGenerator()
+            .n(() => RandomGeneratorUtils.getRandomGenerator().string({length: 4}), 50);
     }
     }
 
 
     /**
     /**
@@ -119,7 +121,7 @@ export class StringLiteralReplacer extends AbstractReplacer {
 
 
         switch (this.options.stringArrayEncoding) {
         switch (this.options.stringArrayEncoding) {
             case StringArrayEncoding.rc4:
             case StringArrayEncoding.rc4:
-                key = RandomGeneratorUtils.getRandomGenerator().pickone(StringLiteralReplacer.rc4Keys);
+                key = RandomGeneratorUtils.getRandomGenerator().pickone(this.rc4Keys);
                 encodedValue = CryptUtils.btoa(CryptUtils.rc4(value, key));
                 encodedValue = CryptUtils.btoa(CryptUtils.rc4(value, key));
 
 
                 break;
                 break;

+ 16 - 8
src/utils/RandomGeneratorUtils.ts

@@ -21,7 +21,22 @@ export class RandomGeneratorUtils {
     /**
     /**
      * @type {Chance.Chance | Chance.SeededChance}
      * @type {Chance.Chance | Chance.SeededChance}
      */
      */
-    private static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance();
+    private static randomGenerator: Chance.Chance | Chance.SeededChance;
+
+    /**
+     * @param seed
+     */
+    public static initializeRandomGenerator (seed: number): void {
+        if (seed !== 0) {
+            RandomGeneratorUtils.randomGenerator = new Chance(seed);
+        } else {
+            RandomGeneratorUtils.randomGenerator = new Chance();
+        }
+    }
+
+    public static clearRandomGenerator (): void {
+        RandomGeneratorUtils.randomVariableNameSet.clear();
+    }
 
 
     /**
     /**
      * @param min
      * @param min
@@ -94,11 +109,4 @@ export class RandomGeneratorUtils {
 
 
         return randomVariableName;
         return randomVariableName;
     }
     }
-
-    /**
-     * @param randomGenerator
-     */
-    public static setRandomGenerator (randomGenerator: Chance.Chance | Chance.SeededChance): void {
-        RandomGeneratorUtils.randomGenerator = randomGenerator;
-    }
 }
 }

+ 2 - 13
test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts

@@ -1,7 +1,5 @@
 import { assert } from 'chai';
 import { assert } from 'chai';
 
 
-import { Chance } from 'chance';
-
 import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
 import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
 
 
 import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
 import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
@@ -15,7 +13,7 @@ import { RandomGeneratorUtils } from '../../../src/utils/RandomGeneratorUtils';
 describe('JavaScriptObfuscator', () => {
 describe('JavaScriptObfuscator', () => {
     describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => {
     describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => {
         beforeEach(() => {
         beforeEach(() => {
-            RandomGeneratorUtils.setRandomGenerator(new Chance());
+            RandomGeneratorUtils.initializeRandomGenerator(0);
         });
         });
 
 
         describe('if `sourceMap` option is `false`', () => {
         describe('if `sourceMap` option is `false`', () => {
@@ -155,9 +153,6 @@ describe('JavaScriptObfuscator', () => {
             let seed: number = 12345,
             let seed: number = 12345,
                 equalsCount: number = 0;
                 equalsCount: number = 0;
 
 
-            //initial clear before testing
-            RandomGeneratorUtils.randomVariableNameSet.clear();
-
             for (let i: number = 0; i < samples; i++) {
             for (let i: number = 0; i < samples; i++) {
                 if (i % 20 === 0) {
                 if (i % 20 === 0) {
                     seed++;
                     seed++;
@@ -166,21 +161,15 @@ describe('JavaScriptObfuscator', () => {
                 const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
                 const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
                     code, { seed: seed }
                     code, { seed: seed }
                 );
                 );
-                RandomGeneratorUtils.randomVariableNameSet.clear();
-
                 const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
                 const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
                     code, { seed: seed }
                     code, { seed: seed }
                 );
                 );
-                RandomGeneratorUtils.randomVariableNameSet.clear();
 
 
                 if (obfuscationResult1.getObfuscatedCode() === obfuscationResult2.getObfuscatedCode()) {
                 if (obfuscationResult1.getObfuscatedCode() === obfuscationResult2.getObfuscatedCode()) {
                     equalsCount++;
                     equalsCount++;
                 }
                 }
             }
             }
 
 
-            //clear after testing
-            RandomGeneratorUtils.randomVariableNameSet.clear();
-
             assert.equal(equalsCount, samples);
             assert.equal(equalsCount, samples);
         });
         });
 
 
@@ -206,7 +195,7 @@ describe('JavaScriptObfuscator', () => {
         });
         });
 
 
         afterEach(() => {
         afterEach(() => {
-            RandomGeneratorUtils.setRandomGenerator(new Chance());
+            RandomGeneratorUtils.initializeRandomGenerator(0);
         });
         });
     });
     });
 });
 });