Ver código fonte

Updated self defending helper. Removed `no-eval` self-defending helper

sanex 3 anos atrás
pai
commit
7920538a51

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
dist/index.browser.js


Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
dist/index.cli.js


Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
dist/index.js


+ 3 - 3
src/container/modules/custom-code-helpers/CustomCodeHelpersModule.ts

@@ -24,7 +24,7 @@ import { DebugProtectionFunctionIntervalCodeHelper } from '../../../custom-code-
 import { DebugProtectionFunctionCodeHelper } from '../../../custom-code-helpers/debug-protection/DebugProtectionFunctionCodeHelper';
 import { DomainLockCodeHelper } from '../../../custom-code-helpers/domain-lock/DomainLockCodeHelper';
 import { CallsControllerFunctionCodeHelper } from '../../../custom-code-helpers/calls-controller/CallsControllerFunctionCodeHelper';
-import { SelfDefendingUnicodeCodeHelper } from '../../../custom-code-helpers/self-defending/SelfDefendingUnicodeCodeHelper';
+import { SelfDefendingCodeHelper } from '../../../custom-code-helpers/self-defending/SelfDefendingCodeHelper';
 import { StringArrayCallsWrapperCodeHelper } from '../../../custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper';
 import { StringArrayCallsWrapperBase64CodeHelper } from '../../../custom-code-helpers/string-array/StringArrayCallsWrapperBase64CodeHelper';
 import { StringArrayCallsWrapperRc4CodeHelper } from '../../../custom-code-helpers/string-array/StringArrayCallsWrapperRc4CodeHelper';
@@ -58,8 +58,8 @@ export const customCodeHelpersModule: interfaces.ContainerModule = new Container
         .whenTargetNamed(CustomCodeHelper.CallsControllerFunction);
 
     bind<ICustomCodeHelper>(ServiceIdentifiers.ICustomCodeHelper)
-        .to(SelfDefendingUnicodeCodeHelper)
-        .whenTargetNamed(CustomCodeHelper.SelfDefendingUnicode);
+        .to(SelfDefendingCodeHelper)
+        .whenTargetNamed(CustomCodeHelper.SelfDefending);
 
     bind<ICustomCodeHelper>(ServiceIdentifiers.ICustomCodeHelper)
         .to(StringArrayCallsWrapperCodeHelper)

+ 3 - 15
src/custom-code-helpers/self-defending/SelfDefendingUnicodeCodeHelper.ts → src/custom-code-helpers/self-defending/SelfDefendingCodeHelper.ts

@@ -9,19 +9,15 @@ import { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helper
 import { IOptions } from '../../interfaces/options/IOptions';
 import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
 
-import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
-
 import { initializable } from '../../decorators/Initializable';
 
 import { SelfDefendingTemplate } from './templates/SelfDefendingTemplate';
-import { SelfDefendingNoEvalTemplate } from './templates/SelfDefendingNoEvalTemplate';
 
 import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
 import { NodeUtils } from '../../node/NodeUtils';
-import { GlobalVariableNoEvalTemplate } from '../common/templates/GlobalVariableNoEvalTemplate';
 
 @injectable()
-export class SelfDefendingUnicodeCodeHelper extends AbstractCustomCodeHelper {
+export class SelfDefendingCodeHelper extends AbstractCustomCodeHelper {
     /**
      * @type {string}
      */
@@ -79,17 +75,9 @@ export class SelfDefendingUnicodeCodeHelper extends AbstractCustomCodeHelper {
      * @returns {string}
      */
     protected override getCodeHelperTemplate (): string {
-        const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
-            ? this.getGlobalVariableTemplate()
-            : GlobalVariableNoEvalTemplate();
-        const selfDefendingTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
-            ? SelfDefendingTemplate()
-            : SelfDefendingNoEvalTemplate();
-
-        return this.customCodeHelperFormatter.formatTemplate(selfDefendingTemplate, {
+        return this.customCodeHelperFormatter.formatTemplate(SelfDefendingTemplate(), {
             callControllerFunctionName: this.callsControllerFunctionName,
-            selfDefendingFunctionName: this.selfDefendingFunctionName,
-            globalVariableTemplate
+            selfDefendingFunctionName: this.selfDefendingFunctionName
         });
     }
 }

+ 6 - 6
src/custom-code-helpers/self-defending/group/SelfDefendingCodeHelperGroup.ts

@@ -21,7 +21,7 @@ import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGro
 import { CallsControllerFunctionCodeHelper } from '../../calls-controller/CallsControllerFunctionCodeHelper';
 import { NodeAppender } from '../../../node/NodeAppender';
 import { NodeLexicalScopeUtils } from '../../../node/NodeLexicalScopeUtils';
-import { SelfDefendingUnicodeCodeHelper } from '../SelfDefendingUnicodeCodeHelper';
+import { SelfDefendingCodeHelper } from '../SelfDefendingCodeHelper';
 
 @injectable()
 export class SelfDefendingCodeHelperGroup extends AbstractCustomCodeHelperGroup {
@@ -84,8 +84,8 @@ export class SelfDefendingCodeHelperGroup extends AbstractCustomCodeHelperGroup
 
         // selfDefendingUnicode helper nodes append
         this.appendCustomNodeIfExist(
-            CustomCodeHelper.SelfDefendingUnicode,
-            (customCodeHelper: ICustomCodeHelper<TInitialData<SelfDefendingUnicodeCodeHelper>>) => {
+            CustomCodeHelper.SelfDefending,
+            (customCodeHelper: ICustomCodeHelper<TInitialData<SelfDefendingCodeHelper>>) => {
                 customCodeHelper.initialize(callsControllerFunctionName, selfDefendingFunctionName);
 
                 NodeAppender.prepend(selfDefendingFunctionHostNode, customCodeHelper.getNode());
@@ -110,12 +110,12 @@ export class SelfDefendingCodeHelperGroup extends AbstractCustomCodeHelperGroup
             return;
         }
 
-        const selfDefendingUnicodeCodeHelper: ICustomCodeHelper<TInitialData<SelfDefendingUnicodeCodeHelper>> =
-            this.customCodeHelperFactory(CustomCodeHelper.SelfDefendingUnicode);
+        const selfDefendingCodeHelper: ICustomCodeHelper<TInitialData<SelfDefendingCodeHelper>> =
+            this.customCodeHelperFactory(CustomCodeHelper.SelfDefending);
         const callsControllerFunctionCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>> =
             this.customCodeHelperFactory(CustomCodeHelper.CallsControllerFunction);
 
-        this.customCodeHelpers.set(CustomCodeHelper.SelfDefendingUnicode, selfDefendingUnicodeCodeHelper);
+        this.customCodeHelpers.set(CustomCodeHelper.SelfDefending, selfDefendingCodeHelper);
         this.customCodeHelpers.set(CustomCodeHelper.CallsControllerFunction, callsControllerFunctionCodeHelper);
     }
 }

+ 0 - 18
src/custom-code-helpers/self-defending/templates/SelfDefendingNoEvalTemplate.ts

@@ -1,18 +0,0 @@
-/**
- * SelfDefendingTemplate. Enters code in infinity loop.
- *
- * @returns {string}
- */
-export function SelfDefendingNoEvalTemplate (): string {
-    return `
-        const {selfDefendingFunctionName} = {callControllerFunctionName}(this, function () {
-            {globalVariableTemplate}
-        
-            return that
-                .RegExp('((.*)+)+$')
-                .test({selfDefendingFunctionName});
-        });
-        
-        {selfDefendingFunctionName}();
-    `;
-}

+ 5 - 4
src/custom-code-helpers/self-defending/templates/SelfDefendingTemplate.ts

@@ -7,10 +7,11 @@ export function SelfDefendingTemplate (): string {
     return `
         const {selfDefendingFunctionName} = {callControllerFunctionName}(this, function () {
             return {selfDefendingFunctionName}
-                .constructor('return /" + this + "/')
-                .call()
-                .constructor('((.*)+)+$')
-                .test({selfDefendingFunctionName});
+                .toString()
+                .search('(((.+)+)+)+$')
+                .toString()
+                .constructor({selfDefendingFunctionName})
+                .search('(((.+)+)+)+$');
         });
         
         {selfDefendingFunctionName}();

+ 1 - 1
src/enums/custom-code-helpers/CustomCodeHelper.ts

@@ -5,7 +5,7 @@ export enum CustomCodeHelper {
     DebugProtectionFunctionInterval = 'DebugProtectionFunctionInterval',
     DebugProtectionFunction = 'DebugProtectionFunction',
     DomainLock = 'DomainLock',
-    SelfDefendingUnicode = 'SelfDefendingUnicode',
+    SelfDefending = 'SelfDefending',
     StringArray = 'StringArray',
     StringArrayCallsWrapper = 'StringArrayCallsWrapper',
     StringArrayCallsWrapperBase64 = 'StringArrayCallsWrapperBase64',

+ 1 - 3
test/dev/dev.ts

@@ -30,11 +30,9 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,
-            compact: false,
             stringArray: true,
             stringArrayThreshold: 1,
-            transformObjectKeys: true,
-            renameProperties: true
+            selfDefending: true
         }
     ).getObfuscatedCode();
 

+ 0 - 271
test/functional-tests/custom-code-helpers/self-defending/templates/SelfDefendingNoEvalTemplate.spec.ts

@@ -1,271 +0,0 @@
-import { assert } from 'chai';
-
-import { ObfuscationTarget } from '../../../../../src/enums/ObfuscationTarget';
-
-import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
-
-import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
-
-import { evaluateInWorker } from '../../../../helpers/evaluateInWorker';
-import { beautifyCode } from '../../../../helpers/beautifyCode';
-import { readFileAsString } from '../../../../helpers/readFileAsString';
-
-import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
-
-describe('SelfDefendingNoEvalTemplate', function () {
-    const correctEvaluationTimeout: number = 100;
-    const redosEvaluationTimeout: number = 10000;
-
-    this.timeout(30000);
-
-    describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => {
-        const expectedEvaluationResult: number = 1;
-
-        let obfuscatedCode: string,
-            evaluationResult: number = 0;
-
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/input.js');
-
-            obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_ADDITIONAL_NODES_PRESET,
-                    selfDefending: true,
-                    identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
-                    target: ObfuscationTarget.BrowserNoEval
-                }
-            ).getObfuscatedCode();
-
-            return evaluateInWorker(obfuscatedCode, correctEvaluationTimeout)
-                .then((result: string | null) => {
-                    if (!result) {
-                        return;
-                    }
-
-                    evaluationResult = parseInt(result, 10);
-                });
-        });
-
-        it('should correctly evaluate code with enabled self defending', () => {
-            assert.equal(evaluationResult, expectedEvaluationResult);
-        });
-    });
-
-    describe('Variant #2: correctly obfuscate code with `MangledIdentifierNamesGenerator` option', () => {
-        const expectedEvaluationResult: number = 1;
-
-        let obfuscatedCode: string,
-            evaluationResult: number = 0;
-
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/input.js');
-
-            obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_ADDITIONAL_NODES_PRESET,
-                    selfDefending: true,
-                    identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
-                    target: ObfuscationTarget.BrowserNoEval
-                }
-            ).getObfuscatedCode();
-
-            return evaluateInWorker(obfuscatedCode, correctEvaluationTimeout)
-                .then((result: string | null) => {
-                    if (!result) {
-                        return;
-                    }
-
-                    evaluationResult = parseInt(result, 10);
-                });
-        });
-
-        it('should correctly evaluate code with enabled self defending', () => {
-            assert.equal(evaluationResult, expectedEvaluationResult);
-        });
-    });
-
-    describe('Variant #3: correctly obfuscate code with `DictionaryIdentifierNamesGenerator` option', () => {
-        const expectedEvaluationResult: number = 1;
-
-        let obfuscatedCode: string,
-            evaluationResult: number = 0;
-
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/input.js');
-
-            obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_ADDITIONAL_NODES_PRESET,
-                    selfDefending: true,
-                    identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
-                    identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'eagle'],
-                    target: ObfuscationTarget.BrowserNoEval
-                }
-            ).getObfuscatedCode();
-
-            return evaluateInWorker(obfuscatedCode, correctEvaluationTimeout)
-                .then((result: string | null) => {
-                    if (!result) {
-                        return;
-                    }
-
-                    evaluationResult = parseInt(result, 10);
-                });
-        });
-
-        it('should correctly evaluate code with enabled self defending', () => {
-            assert.equal(evaluationResult, expectedEvaluationResult);
-        });
-    });
-
-    describe('Variant #4: obfuscated code with beautified self defending code', () => {
-        describe('Variant #1: beautify with spaces', () => {
-            const expectedEvaluationResult: number = 0;
-
-            let obfuscatedCode: string,
-                evaluationResult: number = 0;
-
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/input.js');
-
-                obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_ADDITIONAL_NODES_PRESET,
-                        selfDefending: true,
-                        target: ObfuscationTarget.BrowserNoEval
-                    }
-                ).getObfuscatedCode();
-                obfuscatedCode = beautifyCode(obfuscatedCode, 'space');
-
-                return evaluateInWorker(obfuscatedCode, redosEvaluationTimeout)
-                    .then((result: string | null) => {
-                        if (!result) {
-                            return;
-                        }
-
-                        evaluationResult = parseInt(result, 10);
-                    });
-            });
-
-            it('should enter code in infinity loop', () => {
-                assert.equal(evaluationResult, expectedEvaluationResult);
-            });
-        });
-
-        describe('Variant #2: beautify with tabs', () => {
-            const expectedEvaluationResult: number = 0;
-
-            let obfuscatedCode: string,
-                evaluationResult: number = 0;
-
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/input.js');
-
-                obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_ADDITIONAL_NODES_PRESET,
-                        selfDefending: true,
-                        target: ObfuscationTarget.BrowserNoEval
-                    }
-                ).getObfuscatedCode();
-                obfuscatedCode = beautifyCode(obfuscatedCode, 'tab');
-
-                return evaluateInWorker(obfuscatedCode, redosEvaluationTimeout)
-                    .then((result: string | null) => {
-                        if (!result) {
-                            return;
-                        }
-
-                        evaluationResult = parseInt(result, 10);
-                    });
-            });
-
-            it('should enter code in infinity loop', () => {
-                assert.equal(evaluationResult, expectedEvaluationResult);
-            });
-        });
-    });
-
-    describe('Variant #5: JavaScript obfuscator code', () => {
-        describe('Variant #1: correct evaluation', () => {
-            const evaluationTimeout: number = 5000;
-            const expectedEvaluationResult: string = 'var foo=0x1;';
-
-            let obfuscatedCode: string,
-                evaluationResult: string = '';
-
-            before(() => {
-                const code: string = readFileAsString(process.cwd() + '/dist/index.js');
-
-                obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                    `
-                        ${code}
-                        module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
-                    `,
-                    {
-                        disableConsoleOutput: true,
-                        selfDefending: true,
-                        identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
-                        target: ObfuscationTarget.BrowserNoEval
-                    }
-                ).getObfuscatedCode();
-
-                return evaluateInWorker(obfuscatedCode, evaluationTimeout)
-                    .then((result: string | null) => {
-                        if (!result) {
-                            return;
-                        }
-
-                        evaluationResult = result;
-                    });
-            });
-
-            it('should correctly evaluate code with enabled self defending', () => {
-                assert.equal(evaluationResult, expectedEvaluationResult);
-            });
-        });
-
-        describe('Variant #2: beautify with spaces', () => {
-            const expectedEvaluationResult: string = '';
-
-            let obfuscatedCode: string,
-                evaluationResult: string = '';
-
-            before(() => {
-                const code: string = readFileAsString(process.cwd() + '/dist/index.js');
-
-                obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                    `
-                        ${code}
-                        module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
-                    `,
-                    {
-                        disableConsoleOutput: true,
-                        selfDefending: true,
-                        identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
-                        target: ObfuscationTarget.BrowserNoEval
-                    }
-                ).getObfuscatedCode();
-                obfuscatedCode = beautifyCode(obfuscatedCode, 'space');
-
-                return evaluateInWorker(obfuscatedCode, redosEvaluationTimeout)
-                    .then((result: string | null) => {
-                        if (!result) {
-                            return;
-                        }
-
-                        evaluationResult = result;
-                    });
-            });
-
-            it('should enter code in infinity loop', () => {
-                assert.equal(evaluationResult, expectedEvaluationResult);
-            });
-        });
-    });
-});

+ 11 - 9
test/functional-tests/custom-code-helpers/self-defending/templates/SelfDefendingTemplate.spec.ts

@@ -1,7 +1,6 @@
 import { assert } from 'chai';
 
-
-import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
+import { TInputOptions } from '../../../../../src/types/options/TInputOptions';
 
 import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
 
@@ -15,6 +14,11 @@ describe('SelfDefendingTemplate', function () {
     const correctEvaluationTimeout: number = 100;
     const redosEvaluationTimeout: number = 10000;
 
+    const baseOptions: TInputOptions = {
+        optionsPreset: 'high-obfuscation',
+        debugProtection: false
+    };
+
     this.timeout(30000);
 
     describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => {
@@ -29,7 +33,7 @@ describe('SelfDefendingTemplate', function () {
             obfuscatedCode = JavaScriptObfuscator.obfuscate(
                 code,
                 {
-                    ...NO_ADDITIONAL_NODES_PRESET,
+                    ...baseOptions,
                     selfDefending: true,
                     identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
                 }
@@ -62,7 +66,7 @@ describe('SelfDefendingTemplate', function () {
             obfuscatedCode = JavaScriptObfuscator.obfuscate(
                 code,
                 {
-                    ...NO_ADDITIONAL_NODES_PRESET,
+                    baseOptions,
                     selfDefending: true,
                     identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
                 }
@@ -95,7 +99,7 @@ describe('SelfDefendingTemplate', function () {
             obfuscatedCode = JavaScriptObfuscator.obfuscate(
                 code,
                 {
-                    ...NO_ADDITIONAL_NODES_PRESET,
+                    baseOptions,
                     selfDefending: true,
                     identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
                     identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'eagle']
@@ -130,7 +134,7 @@ describe('SelfDefendingTemplate', function () {
                 obfuscatedCode = JavaScriptObfuscator.obfuscate(
                     code,
                     {
-                        ...NO_ADDITIONAL_NODES_PRESET,
+                        ...baseOptions,
                         selfDefending: true
                     }
                 ).getObfuscatedCode();
@@ -163,7 +167,7 @@ describe('SelfDefendingTemplate', function () {
                 obfuscatedCode = JavaScriptObfuscator.obfuscate(
                     code,
                     {
-                        ...NO_ADDITIONAL_NODES_PRESET,
+                        ...baseOptions,
                         selfDefending: true
                     }
                 ).getObfuscatedCode();
@@ -202,7 +206,6 @@ describe('SelfDefendingTemplate', function () {
                         module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
                     `,
                     {
-                        disableConsoleOutput: true,
                         selfDefending: true,
                         identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
                     }
@@ -238,7 +241,6 @@ describe('SelfDefendingTemplate', function () {
                         module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
                     `,
                     {
-                        disableConsoleOutput: true,
                         selfDefending: true,
                         identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
                     }

+ 0 - 1
test/index.spec.ts

@@ -66,7 +66,6 @@ import './functional-tests/custom-code-helpers/debug-protection/templates/DebugP
 import './functional-tests/custom-code-helpers/domain-lock/DomainLockCodeHelper.spec';
 import './functional-tests/custom-code-helpers/domain-lock/templates/DomainLockNodeTemplate.spec';
 import './functional-tests/custom-code-helpers/self-defending/SelfDefendingCodeHelper.spec';
-import './functional-tests/custom-code-helpers/self-defending/templates/SelfDefendingNoEvalTemplate.spec';
 import './functional-tests/custom-code-helpers/self-defending/templates/SelfDefendingTemplate.spec';
 import './functional-tests/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.spec';
 import './functional-tests/custom-code-helpers/string-array/StringArrayCodeHelper.spec';

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff