Jelajahi Sumber

Merge pull request #167 from javascript-obfuscator/wrong-string-array-calls-wrapper-name

Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/…
Timofey Kachalov 7 tahun lalu
induk
melakukan
12a03d52e9

+ 2 - 0
CHANGELOG.md

@@ -3,6 +3,8 @@ Change Log
 v0.14.0
 ---
 * **Breaking change:** Now CLI obfuscating directory recursively. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/157
+* Fixed runtime errors when `deadCodeInjection` is enabled and `identifierNamesGenerator` is set to `mangled`
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/166
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/156
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/159
 

File diff ditekan karena terlalu besar
+ 0 - 0
dist/index.js


+ 2 - 2
src/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.ts

@@ -223,8 +223,8 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
         const random2: boolean = this.randomGenerator.getMathRandom() > 0.5;
 
         const operator: ESTree.BinaryOperator = random1 ? '===' : '!==';
-        const leftString: string = this.randomGenerator.getRandomString(3);
-        const rightString: string = random2 ? leftString : this.randomGenerator.getRandomString(3);
+        const leftString: string = this.randomGenerator.getRandomString(5);
+        const rightString: string = random2 ? leftString : this.randomGenerator.getRandomString(5);
 
         const [consequent, alternate]: [ESTree.BlockStatement, ESTree.BlockStatement] = random1 === random2
             ? [blockStatementNode, randomBlockStatementNode]

+ 6 - 1
src/node-transformers/obfuscating-transformers/obfuscating-replacers/literal-obfuscating-replacers/StringLiteralObfuscatingReplacer.ts

@@ -250,8 +250,13 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
             ));
         }
 
+        const stringArrayIdentifierNode: ESTree.Identifier = Nodes.getIdentifierNode(stringArrayStorageCallsWrapperName);
+
+        // prevent obfuscation of this identifier
+        stringArrayIdentifierNode.obfuscatedNode = true;
+
         return Nodes.getCallExpressionNode(
-            Nodes.getIdentifierNode(stringArrayStorageCallsWrapperName),
+            stringArrayIdentifierNode,
             callExpressionArgs
         );
     }

+ 2 - 1
src/node/NodeGuards.ts

@@ -238,11 +238,12 @@ export class NodeGuards {
         );
         const parentNodeIsMethodDefinitionNode: boolean = NodeGuards.isMethodDefinitionNode(parentNode) &&
             !parentNode.computed;
+        const isLabelIdentifierNode: boolean = NodeGuards.isLabelIdentifierNode(node, parentNode);
 
         return !parentNodeIsPropertyNode &&
             !parentNodeIsMemberExpressionNode &&
             !parentNodeIsMethodDefinitionNode &&
-            !NodeGuards.isLabelIdentifierNode(node, parentNode);
+            !isLabelIdentifierNode;
     }
 
     /**

+ 26 - 0
test/functional-tests/node-transformers/obfuscating-transformers/literal-transformer/LiteralTransformer.spec.ts

@@ -2,6 +2,7 @@ import { assert } from 'chai';
 
 import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
 
+import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
 import { StringArrayEncoding } from '../../../../../src/enums/StringArrayEncoding';
 
 import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
@@ -289,6 +290,31 @@ describe('LiteralTransformer', () => {
                 assert.closeTo(noStringArrayProbability, stringArrayThreshold, delta);
             });
         });
+
+        describe('variant #11: string array calls wrapper name', () => {
+            const regExp: RegExp = /console\[b\('0x0'\)]\('a'\);/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/string-array-calls-wrapper-name.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_CUSTOM_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                    }
+                );
+
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
+
+            it('match #1: should keep identifier with string array calls wrapper name untouched after obfuscation', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
     });
 
     describe('transformation of literal node with boolean value', () => {

+ 7 - 0
test/functional-tests/node-transformers/obfuscating-transformers/literal-transformer/fixtures/string-array-calls-wrapper-name.js

@@ -0,0 +1,7 @@
+(function(){
+    function foo () {
+        console.log('a');
+        var b;
+        function b () {}
+    }
+})();

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini