Selaa lähdekoodia

Updated self defending template #3

sanex 3 vuotta sitten
vanhempi
commit
8a872dca9f

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.browser.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.cli.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.js


+ 2 - 0
package.json

@@ -52,6 +52,7 @@
     "@types/eslint-scope": "3.7.1",
     "@types/estraverse": "5.1.1",
     "@types/estree": "0.0.50",
+    "@types/js-beautify": "1.13.2",
     "@types/js-string-escape": "1.0.0",
     "@types/md5": "2.3.1",
     "@types/mkdirp": "1.0.2",
@@ -75,6 +76,7 @@
     "eslint-plugin-unicorn": "34.0.1",
     "fork-ts-checker-notifier-webpack-plugin": "4.0.0",
     "fork-ts-checker-webpack-plugin": "6.2.12",
+    "js-beautify": "1.14.0",
     "mocha": "9.0.2",
     "nyc": "15.1.0",
     "pjson": "1.0.9",

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

@@ -9,7 +9,7 @@ export function SelfDefendingNoEvalTemplate (): string {
             {globalVariableTemplate}
         
             return that
-                .RegExp('([\\\\S]+([\\\\s]+[\\\\S]+)+)+[\\\\S]}')
+                .RegExp('(.*)+\\\\S}')
                 .test({selfDefendingFunctionName});
         });
         

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

@@ -9,7 +9,7 @@ export function SelfDefendingTemplate (): string {
             return {selfDefendingFunctionName}
                 .constructor('return /" + this + "/')
                 .call()
-                .constructor('([\\\\S]+([\\\\s]+[\\\\S]+)+)+[\\\\S]}')
+                .constructor('(.*)+\\\\S}')
                 .test({selfDefendingFunctionName});
         });
         

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

@@ -7,16 +7,16 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/N
 import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
 
 import { evaluateInWorker } from '../../../../helpers/evaluateInWorker';
-import { readFileAsString } from '../../../../helpers/readFileAsString';
 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 = 3500;
+    const redosEvaluationTimeout: number = 10000;
 
-    this.timeout(10000);
+    this.timeout(30000);
 
     describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => {
         const expectedEvaluationResult: number = 1;
@@ -190,4 +190,82 @@ describe('SelfDefendingNoEvalTemplate', function () {
             });
         });
     });
+
+    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);
+            });
+        });
+    });
 });

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

@@ -13,9 +13,9 @@ import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFac
 
 describe('SelfDefendingTemplate', function () {
     const correctEvaluationTimeout: number = 100;
-    const redosEvaluationTimeout: number = 3500;
+    const redosEvaluationTimeout: number = 10000;
 
-    this.timeout(10000);
+    this.timeout(30000);
 
     describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => {
         const expectedEvaluationResult: number = 1;
@@ -184,4 +184,80 @@ describe('SelfDefendingTemplate', function () {
             });
         });
     });
+
+    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
+                    }
+                ).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
+                    }
+                ).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);
+            });
+        });
+    });
 });

+ 7 - 7
test/helpers/beautifyCode.ts

@@ -1,16 +1,16 @@
+const beautify = require('js-beautify').js;
+
 /**
- * Adds some spaces between some language constructions
+ * Beautifies code
  *
  * @param {string} code
  * @param {" " | "  "} character
  * @returns {string}
  */
 export function beautifyCode (code: string, character: 'space' | 'tab'): string {
-    const spaceCharacter: string = character === 'space' ? '\x20' : '\x09';
+    const indentCharacter: string = character === 'space' ? '\x20' : '\x09';
 
-    return code
-        .replace(/function\(\){/g, 'function () {')
-        .replace(/(!?=+)/g, ' $1 ')
-        .replace(/,/g, `,${spaceCharacter}`)
-        .replace(/;/g, `;\n${spaceCharacter}`);
+    return beautify(code, {
+        indent_char: indentCharacter
+    });
 }

+ 63 - 3
yarn.lock

@@ -644,6 +644,11 @@
     "@types/minimatch" "*"
     "@types/node" "*"
 
+"@types/js-beautify@^1.13.2":
+  version "1.13.2"
+  resolved "https://registry.yarnpkg.com/@types/js-beautify/-/js-beautify-1.13.2.tgz#49783f6c6c68558738139e612b64b4f1a275383e"
+  integrity sha512-crV/441NhrynLIclg94i1wV6nX/6rU9ByUyn4muCrsL0HPd3nBzrt6kpQ9MQOB+HeYgLcRARteNJcbnYkp5OwA==
+
 "@types/[email protected]":
   version "1.0.0"
   resolved "https://registry.npmjs.org/@types/js-string-escape/-/js-string-escape-1.0.0.tgz"
@@ -982,6 +987,11 @@
   resolved "https://registry.npmjs.org/@xtuc/long/-/long-4.2.2.tgz"
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
 
+abbrev@1:
+  version "1.1.1"
+  resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
+  integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==
+
 acorn-jsx@^5.2.0:
   version "5.2.0"
   resolved "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.2.0.tgz"
@@ -1512,7 +1522,7 @@ [email protected]:
   resolved "https://registry.yarnpkg.com/commander/-/commander-8.0.0.tgz#1da2139548caef59bd23e66d18908dfb54b02258"
   integrity sha512-Xvf85aAtu6v22+E5hfVoLHqyul/jyxh91zvqk/ioJTQuJR7Z78n7H558vMPKanPSRgIEeZemT92I2g9Y8LPbSQ==
 
-commander@^2.20.0:
+commander@^2.19.0, commander@^2.20.0:
   version "2.20.3"
   resolved "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz"
   integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
@@ -1547,6 +1557,14 @@ concat-stream@^1.4.7:
     readable-stream "^2.2.2"
     typedarray "^0.0.6"
 
+config-chain@^1.1.12:
+  version "1.1.13"
+  resolved "https://registry.yarnpkg.com/config-chain/-/config-chain-1.1.13.tgz#fad0795aa6a6cdaff9ed1b68e9dff94372c232f4"
+  integrity sha512-qj+f8APARXHrM0hraqXYb2/bOVSV4PvJQlNZ/DVj0QrmNM2q2euizkeuVckQ57J+W0mRH6Hvi+k50M4Jul2VRQ==
+  dependencies:
+    ini "^1.3.4"
+    proto-list "~1.2.1"
+
 consola@^2.15.0:
   version "2.15.0"
   resolved "https://registry.npmjs.org/consola/-/consola-2.15.0.tgz"
@@ -1740,6 +1758,16 @@ doctrine@^3.0.0:
   dependencies:
     esutils "^2.0.2"
 
+editorconfig@^0.15.3:
+  version "0.15.3"
+  resolved "https://registry.yarnpkg.com/editorconfig/-/editorconfig-0.15.3.tgz#bef84c4e75fb8dcb0ce5cee8efd51c15999befc5"
+  integrity sha512-M9wIMFx96vq0R4F+gRpY3o2exzb8hEj/n9S8unZtHSvYjibBp/iMufSzvmOcV/laG0ZtuTVGtiJggPOSW2r93g==
+  dependencies:
+    commander "^2.19.0"
+    lru-cache "^4.1.5"
+    semver "^5.6.0"
+    sigmund "^1.0.1"
+
 electron-to-chromium@^1.3.723:
   version "1.3.736"
   resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.736.tgz#f632d900a1f788dab22fec9c62ec5c9c8f0c4052"
@@ -2593,6 +2621,11 @@ inherits@2, inherits@^2.0.3, inherits@~2.0.3:
   resolved "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz"
   integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
 
+ini@^1.3.4:
+  version "1.3.8"
+  resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
+  integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==
+
 interpret@^2.2.0:
   version "2.2.0"
   resolved "https://registry.npmjs.org/interpret/-/interpret-2.2.0.tgz"
@@ -2916,6 +2949,16 @@ jest-worker@^27.0.2:
     merge-stream "^2.0.0"
     supports-color "^8.0.0"
 
+js-beautify@^1.14.0:
+  version "1.14.0"
+  resolved "https://registry.yarnpkg.com/js-beautify/-/js-beautify-1.14.0.tgz#2ce790c555d53ce1e3d7363227acf5dc69024c2d"
+  integrity sha512-yuck9KirNSCAwyNJbqW+BxJqJ0NLJ4PwBUzQQACl5O3qHMBXVkXb/rD0ilh/Lat/tn88zSZ+CAHOlk0DsY7GuQ==
+  dependencies:
+    config-chain "^1.1.12"
+    editorconfig "^0.15.3"
+    glob "^7.1.3"
+    nopt "^5.0.0"
+
 [email protected]:
   version "1.0.1"
   resolved "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz"
@@ -3105,7 +3148,7 @@ [email protected]:
     chalk "^4.1.0"
     is-unicode-supported "^0.1.0"
 
-lru-cache@^4.0.1:
+lru-cache@^4.0.1, lru-cache@^4.1.5:
   version "4.1.5"
   resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz"
   integrity sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==
@@ -3327,6 +3370,13 @@ node-releases@^1.1.71:
   resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.72.tgz#14802ab6b1039a79a0c7d662b610a5bbd76eacbe"
   integrity sha512-LLUo+PpH3dU6XizX3iVoubUNheF/owjXCZZ5yACDxNnPtgFuludV1ZL3ayK1kVep42Rmm0+R9/Y60NQbZ2bifw==
 
+nopt@^5.0.0:
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/nopt/-/nopt-5.0.0.tgz#530942bb58a512fccafe53fe210f13a25355dc88"
+  integrity sha512-Tbj67rffqceeLpcRXrT7vKAN8CwfPeIBgM7E6iBkmKLV7bEMwpGgYLGv0jACUsECaa/vuxP0IjEont6umdMgtQ==
+  dependencies:
+    abbrev "1"
+
 normalize-package-data@^2.3.2, normalize-package-data@^2.5.0:
   version "2.5.0"
   resolved "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz"
@@ -3730,6 +3780,11 @@ progress@^2.0.0:
   resolved "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz"
   integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==
 
+proto-list@~1.2.1:
+  version "1.2.4"
+  resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
+  integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=
+
 pseudomap@^1.0.2:
   version "1.0.2"
   resolved "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz"
@@ -3976,7 +4031,7 @@ schema-utils@^3.1.0:
     ajv "^6.12.5"
     ajv-keywords "^3.5.2"
 
-"semver@2 || 3 || 4 || 5", semver@^5.4.1:
+"semver@2 || 3 || 4 || 5", semver@^5.4.1, semver@^5.6.0:
   version "5.7.1"
   resolved "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz"
   integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
@@ -4060,6 +4115,11 @@ shellwords@^0.1.1:
   resolved "https://registry.npmjs.org/shellwords/-/shellwords-0.1.1.tgz"
   integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==
 
+sigmund@^1.0.1:
+  version "1.0.1"
+  resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"
+  integrity sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=
+
 signal-exit@^3.0.2:
   version "3.0.2"
   resolved "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz"

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä