diff --git a/index.js b/index.js index 4fffe9db..03a9f02e 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,7 @@ JavaScriptObfuscator.defaultOptions = { compact: true, debugProtection: false, debugProtectionInterval: false, + disableConsoleOutput: true, rotateUnicodeArray: true }; JavaScriptObfuscator.escodegenParams = { diff --git a/index.ts b/index.ts index 5a9bf217..e774b9c0 100644 --- a/index.ts +++ b/index.ts @@ -15,6 +15,7 @@ export class JavaScriptObfuscator { compact: true, debugProtection: false, debugProtectionInterval: false, + disableConsoleOutput: true, rotateUnicodeArray: true }; diff --git a/readme.md b/readme.md index c44df34d..35e37e3f 100644 --- a/readme.md +++ b/readme.md @@ -86,6 +86,11 @@ Works if `debugProtection` is enabled. Force enable debug mode in some browsers (mainly based on WebKit) when Developers Tools panel was enbaled, even after page was loaded. +####disableConsoleOutput +Type: `boolean` Default: `true` + +Disable `console.log`, `console.info`, `console.error` and `console.warn` messages output into browser console. + ####rotateUnicodeArray Type: `boolean` Default: `true` diff --git a/src/Obfuscator.js b/src/Obfuscator.js index ea718f05..8b5c0bd4 100644 --- a/src/Obfuscator.js +++ b/src/Obfuscator.js @@ -3,6 +3,7 @@ const estraverse = require('estraverse'); const AppendState_1 = require('./enums/AppendState'); const NodeType_1 = require("./enums/NodeType"); const CatchClauseObfuscator_1 = require("./node-obfuscators/CatchClauseObfuscator"); +const ConsoleOutputDisableExpressionNode_1 = require("./nodes/ConsoleOutputDisableExpressionNode"); const DebugProtectionNodesGroup_1 = require("./node-groups/DebugProtectionNodesGroup"); const FunctionDeclarationObfuscator_1 = require('./node-obfuscators/FunctionDeclarationObfuscator'); const FunctionObfuscator_1 = require('./node-obfuscators/FunctionObfuscator'); @@ -76,6 +77,9 @@ class Obfuscator { else { this.setNode('unicodeArrayNode', new UnicodeArrayNode_1.UnicodeArrayNode(astTree, Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH))); } + if (this.options['disableConsoleOutput']) { + this.setNode('consoleOutputDisableExpressionNode', new ConsoleOutputDisableExpressionNode_1.ConsoleOutputDisableExpressionNode(astTree)); + } if (this.options['debugProtection']) { this.setNodesGroup(new DebugProtectionNodesGroup_1.DebugProtectionNodesGroup(astTree, this.options)); } diff --git a/src/Obfuscator.ts b/src/Obfuscator.ts index f86a2a61..2045f453 100644 --- a/src/Obfuscator.ts +++ b/src/Obfuscator.ts @@ -9,6 +9,7 @@ import { AppendState } from './enums/AppendState'; import { NodeType } from "./enums/NodeType"; import { CatchClauseObfuscator } from "./node-obfuscators/CatchClauseObfuscator"; +import { ConsoleOutputDisableExpressionNode } from "./nodes/ConsoleOutputDisableExpressionNode"; import { DebugProtectionNodesGroup } from "./node-groups/DebugProtectionNodesGroup"; import { FunctionDeclarationObfuscator } from './node-obfuscators/FunctionDeclarationObfuscator'; import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator'; @@ -127,6 +128,13 @@ export class Obfuscator { ); } + if (this.options['disableConsoleOutput']) { + this.setNode( + 'consoleOutputDisableExpressionNode', + new ConsoleOutputDisableExpressionNode(astTree) + ); + } + if (this.options['debugProtection']) { this.setNodesGroup(new DebugProtectionNodesGroup(astTree, this.options)); } diff --git a/src/nodes/ConsoleOutputDisableExpressionNode.js b/src/nodes/ConsoleOutputDisableExpressionNode.js new file mode 100644 index 00000000..17ea499b --- /dev/null +++ b/src/nodes/ConsoleOutputDisableExpressionNode.js @@ -0,0 +1,129 @@ +"use strict"; +const estraverse = require('estraverse'); +const NodeType_1 = require("../enums/NodeType"); +const Node_1 = require('./Node'); +class ConsoleOutputDisableExpressionNode extends Node_1.Node { + constructor(astTree) { + super(); + this.astTree = astTree; + this.node = this.getNodeStructure(); + } + appendNode() { + estraverse.replace(this.astTree, { + leave: (node, parent) => { + switch (node.type) { + case NodeType_1.NodeType.Program: + node.body.unshift(this.getNode()); + break; + default: + break; + } + } + }); + } + getNodeStructure() { + return { + "type": NodeType_1.NodeType.ExpressionStatement, + "expression": { + "type": NodeType_1.NodeType.CallExpression, + "callee": { + "type": NodeType_1.NodeType.CallExpression, + "callee": { + "type": NodeType_1.NodeType.MemberExpression, + "computed": true, + "object": { + "type": NodeType_1.NodeType.MemberExpression, + "computed": true, + "object": { + "type": NodeType_1.NodeType.ArrayExpression, + "elements": [] + }, + "property": { + "type": NodeType_1.NodeType.Literal, + "value": "filter", + "raw": "\"filter\"" + } + }, + "property": { + "type": NodeType_1.NodeType.Literal, + "value": "constructor", + "raw": "\"constructor\"" + } + }, + "arguments": [ + { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType_1.NodeType.Literal, + "value": "_='(\u0002var \u0004\u0005[][\"filter\"]", + "raw": "\"_='(\\u0002var \\u0004\\u0005[][\\\"filter\\\"]\"" + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "[\"\u0006tructor\"](\"return t", + "raw": "\"[\\\"\\u0006tructor\\\"](\\\"return t\"" + } + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "his\")()\u0003log\u0001error\u0001info\u0001warn\u0005\u0002};})()\u0001\u0005\u0002}\u0003\u0002function () {\u0003;\u0004.\u0006ole.\u0004_window\u0005 = \u0006cons';for(Y in $='\u0006\u0005\u0004\u0003\u0002\u0001')with", + "raw": "\"his\\\")()\\u0003log\\u0001error\\u0001info\\u0001warn\\u0005\\u0002};})()\\u0001\\u0005\\u0002}\\u0003\\u0002function () {\\u0003;\\u0004.\\u0006ole.\\u0004_window\\u0005 = \\u0006cons';for(Y in $='\\u0006\\u0005\\u0004\\u0003\\u0002\\u0001')with\"" + } + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "(_.", + "raw": "\"(_.\"" + } + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "split($[Y]))_=jo", + "raw": "\"split($[Y]))_=jo\"" + } + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "in(pop", + "raw": "\"in(pop\"" + } + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "());ev", + "raw": "\"());ev\"" + } + }, + "right": { + "type": NodeType_1.NodeType.Literal, + "value": "al(_)", + "raw": "\"al(_)\"" + } + } + ] + }, + "arguments": [] + } + }; + } +} +exports.ConsoleOutputDisableExpressionNode = ConsoleOutputDisableExpressionNode; diff --git a/src/nodes/ConsoleOutputDisableExpressionNode.ts b/src/nodes/ConsoleOutputDisableExpressionNode.ts new file mode 100644 index 00000000..f4c54b07 --- /dev/null +++ b/src/nodes/ConsoleOutputDisableExpressionNode.ts @@ -0,0 +1,156 @@ +/* tslint:disable:max-line-length */ + +import * as estraverse from 'estraverse'; + +import { IProgramNode } from '../interfaces/nodes/IProgramNode'; +import { ITreeNode } from '../interfaces/nodes/ITreeNode'; + +import { NodeType } from "../enums/NodeType"; + +import { Node } from './Node'; + +export class ConsoleOutputDisableExpressionNode extends Node { + /** + * @type {ITreeNode} + */ + protected node: ITreeNode; + + /** + * @type {ITreeNode} + */ + private astTree: ITreeNode; + + /** + * @param astTree + */ + constructor (astTree: ITreeNode) { + super(); + + this.astTree = astTree; + this.node = this.getNodeStructure(); + } + + public appendNode (): void { + estraverse.replace(this.astTree, { + leave: (node: ITreeNode, parent: ITreeNode): any => { + switch (node.type) { + case NodeType.Program: + (node).body.unshift(this.getNode()); + + break; + + default: + break; + } + } + }); + } + + /** + * @returns any + */ + protected getNodeStructure (): any { + return { + "type": NodeType.ExpressionStatement, + "expression": { + "type": NodeType.CallExpression, + "callee": { + "type": NodeType.CallExpression, + "callee": { + "type": NodeType.MemberExpression, + "computed": true, + "object": { + "type": NodeType.MemberExpression, + "computed": true, + "object": { + "type": NodeType.ArrayExpression, + "elements": [] + }, + "property": { + "type": NodeType.Literal, + "value": "filter", + "raw": "\"filter\"" + } + }, + "property": { + "type": NodeType.Literal, + "value": "constructor", + "raw": "\"constructor\"" + } + }, + "arguments": [ + { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.BinaryExpression, + "operator": "+", + "left": { + "type": NodeType.Literal, + "value": "_='(\u0002var \u0004\u0005[][\"filter\"]", + "raw": "\"_='(\\u0002var \\u0004\\u0005[][\\\"filter\\\"]\"" + }, + "right": { + "type": NodeType.Literal, + "value": "[\"\u0006tructor\"](\"return t", + "raw": "\"[\\\"\\u0006tructor\\\"](\\\"return t\"" + } + }, + "right": { + "type": NodeType.Literal, + "value": "his\")()\u0003log\u0001error\u0001info\u0001warn\u0005\u0002};})()\u0001\u0005\u0002}\u0003\u0002function () {\u0003;\u0004.\u0006ole.\u0004_window\u0005 = \u0006cons';for(Y in $='\u0006\u0005\u0004\u0003\u0002\u0001')with", + "raw": "\"his\\\")()\\u0003log\\u0001error\\u0001info\\u0001warn\\u0005\\u0002};})()\\u0001\\u0005\\u0002}\\u0003\\u0002function () {\\u0003;\\u0004.\\u0006ole.\\u0004_window\\u0005 = \\u0006cons';for(Y in $='\\u0006\\u0005\\u0004\\u0003\\u0002\\u0001')with\"" + } + }, + "right": { + "type": NodeType.Literal, + "value": "(_.", + "raw": "\"(_.\"" + } + }, + "right": { + "type": NodeType.Literal, + "value": "split($[Y]))_=jo", + "raw": "\"split($[Y]))_=jo\"" + } + }, + "right": { + "type": NodeType.Literal, + "value": "in(pop", + "raw": "\"in(pop\"" + } + }, + "right": { + "type": NodeType.Literal, + "value": "());ev", + "raw": "\"());ev\"" + } + }, + "right": { + "type": NodeType.Literal, + "value": "al(_)", + "raw": "\"al(_)\"" + } + } + ] + }, + "arguments": [] + } + }; + } +} diff --git a/tests/dev-test.js b/tests/dev-test.js index 1e9c4e95..e867442e 100644 --- a/tests/dev-test.js +++ b/tests/dev-test.js @@ -47,6 +47,7 @@ let obfuscatedCode = index_1.JavaScriptObfuscator.obfuscate(` console.log(true, false); })(); `, { + disableConsoleOutput: false, rotateUnicodeArray: false }); console.log(obfuscatedCode); diff --git a/tests/dev-test.ts b/tests/dev-test.ts index 3ee27909..186b1024 100644 --- a/tests/dev-test.ts +++ b/tests/dev-test.ts @@ -49,6 +49,7 @@ let obfuscatedCode: string = JavaScriptObfuscator.obfuscate( })(); `, { + disableConsoleOutput: false, rotateUnicodeArray: false } );