Browse Source

convert shorthand object expression to non-shorthand

sanex3339 8 years ago
parent
commit
05732c4fcc

+ 6 - 18
dist/index.js

@@ -892,7 +892,6 @@ exports.NO_CUSTOM_NODES_PRESET = Object.freeze({
     disableConsoleOutput: false,
     domainLock: [],
     encodeUnicodeLiterals: false,
-    optimize: false,
     reservedNames: [],
     rotateUnicodeArray: false,
     selfDefending: false,
@@ -1069,7 +1068,6 @@ exports.DEFAULT_PRESET = Object.freeze({
     disableConsoleOutput: true,
     domainLock: [],
     encodeUnicodeLiterals: false,
-    optimize: false,
     reservedNames: [],
     rotateUnicodeArray: true,
     selfDefending: true,
@@ -1113,12 +1111,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
 
 var esprima = __webpack_require__(21);
 var escodegen = __webpack_require__(11);
-var SourceMapMode_1 = __webpack_require__(12);
 var ObfuscationResult_1 = __webpack_require__(17);
 var Obfuscator_1 = __webpack_require__(25);
 var Options_1 = __webpack_require__(54);
 var SourceMapCorrector_1 = __webpack_require__(26);
-var optimizeJs = __webpack_require__(75);
 
 var JavaScriptObfuscatorInternal = function () {
     function JavaScriptObfuscatorInternal(sourceCode) {
@@ -1156,11 +1152,6 @@ var JavaScriptObfuscatorInternal = function () {
                 compact: options.compact
             };
             var generatorOutput = escodegen.generate(astTree, escodegenParams);
-            if (options.optimize) {
-                generatorOutput.code = optimizeJs(generatorOutput.code, {
-                    sourceMap: options.sourceMap && options.sourceMapMode === SourceMapMode_1.SourceMapMode.Inline
-                });
-            }
             generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
             return generatorOutput;
         }
@@ -1544,7 +1535,7 @@ var JavaScriptObfuscatorCLI = function () {
     }, {
         key: 'configureCommands',
         value: function configureCommands() {
-            this.commands = new commander.Command().version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage('<inputPath> [options]').option('-o, --output <path>', 'Output path for obfuscated code').option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--optimize <boolean>', 'Enables optimizations for obfuscated code with help of `optimize-js`', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) {
+            this.commands = new commander.Command().version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage('<inputPath> [options]').option('-o, --output <path>', 'Output path for obfuscated code').option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) {
                 return val.split(',');
             }).option('--rotateUnicodeArray <boolean>', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMap <boolean>', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMapBaseUrl <string>', 'Sets base url to the source map import url when `--sourceMapMode=separate`').option('--sourceMapFileName <string>', 'Sets file name for output source map when `--sourceMapMode=separate`').option('--sourceMapMode <string> [inline, separate]', 'Specify source map output mode', JavaScriptObfuscatorCLI.parseSourceMapMode).option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).option('--domainLock <list>', 'Blocks the execution of the code in domains that do not match the passed RegExp patterns (comma separated)', function (val) {
                 return val.split(',');
@@ -2974,8 +2965,11 @@ var ObjectExpressionObfuscator = function (_AbstractNodeObfuscat) {
             var _this2 = this;
 
             objectExpressionNode.properties.forEach(function (property) {
+                if (property.shorthand) {
+                    property.shorthand = false;
+                }
                 estraverse.replace(property.key, {
-                    leave: function leave(node, parentNode) {
+                    enter: function enter(node, parentNode) {
                         if (Nodes_1.Nodes.isLiteralNode(node)) {
                             _this2.obfuscateLiteralPropertyKey(node);
                             return;
@@ -3187,7 +3181,6 @@ __decorate([class_validator_1.ArrayUnique(), class_validator_1.IsString({
     each: true
 }), __metadata('design:type', Array)], Options.prototype, "domainLock", void 0);
 __decorate([class_validator_1.IsBoolean(), __metadata('design:type', Boolean)], Options.prototype, "encodeUnicodeLiterals", void 0);
-__decorate([class_validator_1.IsBoolean(), __metadata('design:type', Boolean)], Options.prototype, "optimize", void 0);
 __decorate([class_validator_1.ArrayUnique(), class_validator_1.IsString({
     each: true
 }), __metadata('design:type', Array)], Options.prototype, "reservedNames", void 0);
@@ -3634,12 +3627,7 @@ module.exports = require("fs");
 module.exports = require("mkdirp");
 
 /***/ },
-/* 75 */
-/***/ function(module, exports) {
-
-module.exports = require("optimize-js");
-
-/***/ },
+/* 75 */,
 /* 76 */
 /***/ function(module, exports, __webpack_require__) {
 

+ 5 - 1
src/node-obfuscators/ObjectExpressionObfuscator.ts

@@ -25,8 +25,12 @@ export class ObjectExpressionObfuscator extends AbstractNodeObfuscator {
     public obfuscateNode (objectExpressionNode: ESTree.ObjectExpression): void {
         objectExpressionNode.properties
             .forEach((property: ESTree.Property) => {
+                if (property.shorthand) {
+                    property.shorthand = false;
+                }
+
                 estraverse.replace(property.key, {
-                    leave: (node: ESTree.Node, parentNode: ESTree.Node): any => {
+                    enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                         if (Nodes.isLiteralNode(node)) {
                             this.obfuscateLiteralPropertyKey(node);
 

+ 2 - 2
test/functional-tests/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.spec.ts

@@ -14,7 +14,7 @@ describe('ConsoleOutputDisableExpressionNode', () => {
             }
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(), /for *\(_0x([a-z0-9]){5,6} in _0x([a-z0-9]){5,6} *= *_0x([a-z0-9]){4}\('0x.*'\)\)/);
+        assert.match(obfuscationResult.getObfuscatedCode(), /for *\(_0x([a-z0-9]){4,6} in _0x([a-z0-9]){4,6} *= *_0x([a-z0-9]){4}\('0x.*'\)\)/);
     });
 
     it('should\'t append `ConsoleOutputDisableExpressionNode` custom node into the obfuscated code if `disableConsoleOutput` option is not set', () => {
@@ -26,6 +26,6 @@ describe('ConsoleOutputDisableExpressionNode', () => {
             }
         );
 
-        assert.notMatch(obfuscationResult.getObfuscatedCode(), /for *\(_0x([a-z0-9]){5,6} in _0x([a-z0-9]){5,6} *= *_0x([a-z0-9]){4}\('0x.*'\)\)/);
+        assert.notMatch(obfuscationResult.getObfuscatedCode(), /for *\(_0x([a-z0-9]){4,6} in _0x([a-z0-9]){4,6} *= *_0x([a-z0-9]){4}\('0x.*'\)\)/);
     });
 });

+ 2 - 2
test/functional-tests/custom-nodes/domain-lock-nodes/DomainLockNode.spec.ts

@@ -13,7 +13,7 @@ describe('DomainLockNode', () => {
             }
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){5,6} *= *new RegExp/);
+        assert.match(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){4,6} *= *new RegExp/);
     });
 
     it('should\'t append `DomainLockNode` custom node into the obfuscated code if `domainLock` option is not set', () => {
@@ -24,6 +24,6 @@ describe('DomainLockNode', () => {
             }
         );
 
-        assert.notMatch(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){5,6} *= *new RegExp/);
+        assert.notMatch(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){4,6} *= *new RegExp/);
     });
 });

+ 2 - 2
test/functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayCallsWrapper.spec.ts

@@ -13,7 +13,7 @@ describe('UnicodeArrayCallsWrapper', () => {
             }
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(), /return _0x([a-z0-9]){4}\[parseInt\(_0x([a-z0-9]){5,6}, *0x010\)\];/);
+        assert.match(obfuscationResult.getObfuscatedCode(), /return _0x([a-z0-9]){4}\[parseInt\(_0x([a-z0-9]){4,6}, *0x010\)\];/);
     });
 
     it('should\'t append `UnicodeArrayDecodeNode` custom node into the obfuscated code if `wrapUnicodeArrayCalls` option is not set', () => {
@@ -24,6 +24,6 @@ describe('UnicodeArrayCallsWrapper', () => {
             }
         );
 
-        assert.notMatch(obfuscationResult.getObfuscatedCode(), /return _0x([a-z0-9]){4}\[parseInt\(_0x([a-z0-9]){5,6}, *0x010\)\];/);
+        assert.notMatch(obfuscationResult.getObfuscatedCode(), /return _0x([a-z0-9]){4}\[parseInt\(_0x([a-z0-9]){4,6}, *0x010\)\];/);
     });
 });

+ 2 - 2
test/functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayDecodeNode.spec.ts

@@ -13,7 +13,7 @@ describe('UnicodeArrayDecodeNode', () => {
             }
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(), /decodeURI\(atob\(_0x([a-z0-9]){4}\[_0x([a-z0-9]){5,6}\]\)\)/);
+        assert.match(obfuscationResult.getObfuscatedCode(), /decodeURI\(atob\(_0x([a-z0-9]){4}\[_0x([a-z0-9]){4,6}\]\)\)/);
     });
 
     it('should\'t append `UnicodeArrayDecodeNode` custom node into the obfuscated code if `encodeUnicodeLiterals` option is not set', () => {
@@ -24,6 +24,6 @@ describe('UnicodeArrayDecodeNode', () => {
             }
         );
 
-        assert.notMatch(obfuscationResult.getObfuscatedCode(), /decodeURI\(atob\(_0x([a-z0-9]){4}\[_0x([a-z0-9]){5,6}\]\)\)/);
+        assert.notMatch(obfuscationResult.getObfuscatedCode(), /decodeURI\(atob\(_0x([a-z0-9]){4}\[_0x([a-z0-9]){4,6}\]\)\)/);
     });
 });

+ 2 - 2
test/functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.spec.ts

@@ -13,7 +13,7 @@ describe('UnicodeArrayRotateFunctionNode', () => {
             }
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(), /while *\(-- *_0x([a-z0-9]){5,6}\) *\{/);
+        assert.match(obfuscationResult.getObfuscatedCode(), /while *\(-- *_0x([a-z0-9]){4,6}\) *\{/);
     });
 
     it('should\'t append `UnicodeArrayRotateFunctionNode` custom node into the obfuscated code if `rotateUnicodeArray` option is not set', () => {
@@ -24,6 +24,6 @@ describe('UnicodeArrayRotateFunctionNode', () => {
             }
         );
 
-        assert.notMatch(obfuscationResult.getObfuscatedCode(), /while *\(-- *_0x([a-z0-9]){5,6}\) *\{/);
+        assert.notMatch(obfuscationResult.getObfuscatedCode(), /while *\(-- *_0x([a-z0-9]){4,6}\) *\{/);
     });
 });

+ 2 - 2
test/functional-tests/node-obfuscators/FunctionObfuscator.spec.ts

@@ -29,9 +29,9 @@ describe('FunctionObfuscator', () => {
             const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
 
             const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/var _0x[a-z0-9]{5,6} *= *function *\((_0x[a-z0-9]{5,6})\) *\{/);
+                .match(/var _0x[a-z0-9]{4,6} *= *function *\((_0x[a-z0-9]{4,6})\) *\{/);
             const functionBodyIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{5,6})\)/);
+                .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{4,6})\)/);
 
             const functionParamIdentifierName: string = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
             const functionBodyIdentifierName: string = (<RegExpMatchArray>functionBodyIdentifierMatch)[1];

+ 20 - 2
test/functional-tests/node-obfuscators/ObjectExpressionObfuscator.spec.ts

@@ -13,7 +13,7 @@ describe('ObjectExpressionObfuscator', () => {
             Object.assign({}, NO_CUSTOM_NODES_PRESET)
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *\{'\\x66\\x6f\\x6f':0x0\};$/);
+        assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *\{'\\x66\\x6f\\x6f':0x0\};/);
     });
 
     it('should replace object expression node `key` property with identifier value by unicode value', () => {
@@ -22,6 +22,24 @@ describe('ObjectExpressionObfuscator', () => {
             Object.assign({}, NO_CUSTOM_NODES_PRESET)
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *\{'\\x66\\x6f\\x6f':0x0\};$/);
+        assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *\{'\\x66\\x6f\\x6f':0x0\};/);
+    });
+
+    it('should correct convert shorthand ES6 object expression to non-shorthand object expression', () => {
+        let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+            `
+                (function () {
+                    let a = 0;
+                    let b = 0;
+                    var test = {a, b};
+                })();
+            `,
+            Object.assign({}, NO_CUSTOM_NODES_PRESET)
+        );
+
+        assert.match(
+            obfuscationResult.getObfuscatedCode(),
+            /var *_0x[a-z0-9]{4,6} *= *\{'\\x61': *_0x[a-z0-9]{4,6}\, *'\\x62': *_0x[a-z0-9]{4,6}\};/
+        );
     });
 });

+ 12 - 12
test/functional-tests/node-obfuscators/VariableDeclarationObfuscator.spec.ts

@@ -18,8 +18,8 @@ describe('VariableDeclarationObfuscator', () => {
             Object.assign({}, NO_CUSTOM_NODES_PRESET)
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(),  /var *_0x([a-z0-9]){5,6} *= *'\\x61\\x62\\x63';/);
-        assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){5,6}\);/);
+        assert.match(obfuscationResult.getObfuscatedCode(),  /var *_0x([a-z0-9]){4,6} *= *'\\x61\\x62\\x63';/);
+        assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\);/);
     });
 
     it('should obfuscate variable call (`identifier` node) outside of block scope of node in which this variable was declared with `var` kind', () => {
@@ -35,7 +35,7 @@ describe('VariableDeclarationObfuscator', () => {
             Object.assign({}, NO_CUSTOM_NODES_PRESET)
         );
 
-        assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){5,6}\);/);
+        assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\);/);
     });
 
     it('should not obfuscate variable call (`identifier` node) outside of block scope of node in which this variable was declared with `let` kind', () => {
@@ -78,11 +78,11 @@ describe('VariableDeclarationObfuscator', () => {
         });
 
         it('should obfuscate variable call (`identifier` node) before variable declaration if this call is inside function body', () => {
-            assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){5,6}\['\\x69\\x74\\x65\\x6d'\]\);/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\['\\x69\\x74\\x65\\x6d'\]\);/);
         });
 
         it('should not obfuscate variable call (`identifier` node) before variable declaration', () => {
-            assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){5,6}\);/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\);/);
         });
     });
 
@@ -117,15 +117,15 @@ describe('VariableDeclarationObfuscator', () => {
         it('should correct obfuscate variables inside function body', () => {
             const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
             const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/function *_0x[a-z0-9]{5,6} *\((_0x[a-z0-9]{5,6})\,(_0x[a-z0-9]{5,6})\) *\{/);
+                .match(/function *_0x[a-z0-9]{4,6} *\((_0x[a-z0-9]{4,6})\,(_0x[a-z0-9]{4,6})\) *\{/);
             const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/function _0x[a-z0-9]{5,6} *\((_0x[a-z0-9]{5,6})\) *\{/);
+                .match(/function _0x[a-z0-9]{4,6} *\((_0x[a-z0-9]{4,6})\) *\{/);
             const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{5,6})\)/);
+                .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{4,6})\)/);
             const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/return\{'\\x74':(_0x[a-z0-9]{5,6})\}/);
+                .match(/return\{'\\x74':(_0x[a-z0-9]{4,6})\}/);
             const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
-                .match(/var *(_0x[a-z0-9]{5,6});/);
+                .match(/var *(_0x[a-z0-9]{4,6});/);
 
             functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
             innerFunctionParamIdentifierName = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
@@ -161,7 +161,7 @@ describe('VariableDeclarationObfuscator', () => {
                 Object.assign({}, NO_CUSTOM_NODES_PRESET)
             );
 
-            assert.match(obfuscationResult.getObfuscatedCode(),  /var _0x([a-z0-9]){5,6} *= *\{'\\x74\\x65\\x73\\x74/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var _0x([a-z0-9]){4,6} *= *\{'\\x74\\x65\\x73\\x74/);
         });
 
         it('shouldn\'t replace computed member expression identifier', () => {
@@ -181,7 +181,7 @@ describe('VariableDeclarationObfuscator', () => {
                 Object.assign({}, NO_CUSTOM_NODES_PRESET)
             );
 
-            assert.match(obfuscationResult.getObfuscatedCode(),  /_0x([a-z0-9]){5,6}\['\\x74\\x65\\x73\\x74'\]/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /_0x([a-z0-9]){4,6}\['\\x74\\x65\\x73\\x74'\]/);
         });
     });
 });