From 446a544aa87055a889ee01dcebe4f60be66a67e6 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sat, 4 Jun 2016 18:13:09 +0300 Subject: [PATCH] selfDefending test implementation --- dist/src/OptionsNormalizer.js | 6 +++++ dist/src/node-obfuscators/NodeObfuscator.js | 6 ++--- dist/src/preset-options/DefaultPreset.js | 1 + .../src/preset-options/NoCustomNodesPreset.js | 1 + readme.md | 10 +++++++++ src/OptionsNormalizer.ts | 22 ++++++++++++++++++- src/interfaces/IOptions.d.ts | 1 + src/node-obfuscators/NodeObfuscator.ts | 9 ++++---- src/preset-options/DefaultPreset.ts | 1 + src/preset-options/NoCustomNodesPreset.ts | 1 + 10 files changed, 50 insertions(+), 8 deletions(-) diff --git a/dist/src/OptionsNormalizer.js b/dist/src/OptionsNormalizer.js index 9913447e..b5b90dc9 100644 --- a/dist/src/OptionsNormalizer.js +++ b/dist/src/OptionsNormalizer.js @@ -3,6 +3,7 @@ class OptionsNormalizer { static normalize(options) { let normalizedOptions = Object.assign({}, options); normalizedOptions = OptionsNormalizer.unicodeArrayRule(normalizedOptions); + normalizedOptions = OptionsNormalizer.unicodeArrayThresholdRule(normalizedOptions); normalizedOptions = OptionsNormalizer.selfDefendingRule(normalizedOptions); return normalizedOptions; } @@ -18,6 +19,11 @@ class OptionsNormalizer { } return options; } + static unicodeArrayThresholdRule(options) { + const minValue = 0, maxValue = 1; + options['unicodeArrayThreshold'] = Math.min(Math.max(options['unicodeArrayThreshold'], minValue), maxValue); + return options; + } } OptionsNormalizer.DISABLED_UNICODE_ARRAY_OPTIONS = { encodeUnicodeLiterals: false, diff --git a/dist/src/node-obfuscators/NodeObfuscator.js b/dist/src/node-obfuscators/NodeObfuscator.js index a84e94ba..efa3892a 100644 --- a/dist/src/node-obfuscators/NodeObfuscator.js +++ b/dist/src/node-obfuscators/NodeObfuscator.js @@ -35,12 +35,12 @@ class NodeObfuscator { return `${prefix}${Utils_1.Utils.decToHex(nodeValue)}`; } replaceLiteralValueByUnicodeValue(nodeValue) { - let value = nodeValue; - if (this.options['encodeUnicodeLiterals']) { + let value = nodeValue, replaceByUnicodeArrayFlag = Math.random() > this.options['unicodeArrayThreshold']; + if (this.options['encodeUnicodeLiterals'] && replaceByUnicodeArrayFlag) { value = new Buffer(encodeURI(value)).toString('base64'); } value = Utils_1.Utils.stringToUnicode(value); - if (!this.options['unicodeArray']) { + if (!this.options['unicodeArray'] || !replaceByUnicodeArrayFlag) { return value; } return this.replaceLiteralValueByUnicodeArrayCall(value); diff --git a/dist/src/preset-options/DefaultPreset.js b/dist/src/preset-options/DefaultPreset.js index 8b4241c3..c655e7ab 100644 --- a/dist/src/preset-options/DefaultPreset.js +++ b/dist/src/preset-options/DefaultPreset.js @@ -9,5 +9,6 @@ exports.DEFAULT_PRESET = Object.freeze({ rotateUnicodeArray: true, selfDefending: true, unicodeArray: true, + unicodeArrayThreshold: 0.8, wrapUnicodeArrayCalls: true }); diff --git a/dist/src/preset-options/NoCustomNodesPreset.js b/dist/src/preset-options/NoCustomNodesPreset.js index b0e32eaa..1e2e3510 100644 --- a/dist/src/preset-options/NoCustomNodesPreset.js +++ b/dist/src/preset-options/NoCustomNodesPreset.js @@ -9,5 +9,6 @@ exports.NO_CUSTOM_NODES_PRESET = Object.freeze({ rotateUnicodeArray: false, selfDefending: false, unicodeArray: false, + unicodeArrayThreshold: 0, wrapUnicodeArrayCalls: false }); diff --git a/readme.md b/readme.md index bd2b9249..ad0c26d2 100644 --- a/readme.md +++ b/readme.md @@ -123,6 +123,16 @@ Type: `boolean` Default: `true` Put all literal strings into array and replace every literal string by array call. +####unicodeArrayThreshold +Type: `number` Default: `0.8` Min: `0` Max: `1` + +#####`unicodeArray` option must be enabled + +Probability that the literal string will inserted into `unicodeArray`. +Use this option for huge source code size, because many calls to `unicodeArray` will slowdown code performance. + +Value `0` is equals `unicodeArray: false`. + ####wrapUnicodeArrayCalls Type: `boolean` Default: `true` diff --git a/src/OptionsNormalizer.ts b/src/OptionsNormalizer.ts index 34b9b58d..b46cdf9e 100644 --- a/src/OptionsNormalizer.ts +++ b/src/OptionsNormalizer.ts @@ -27,6 +27,7 @@ export class OptionsNormalizer { let normalizedOptions: IOptions = Object.assign({}, options); normalizedOptions = OptionsNormalizer.unicodeArrayRule(normalizedOptions); + normalizedOptions = OptionsNormalizer.unicodeArrayThresholdRule(normalizedOptions); normalizedOptions = OptionsNormalizer.selfDefendingRule(normalizedOptions); return normalizedOptions; @@ -55,4 +56,23 @@ export class OptionsNormalizer { return options; } -} \ No newline at end of file + + /** + * @param options + * @returns {IOptions} + */ + private static unicodeArrayThresholdRule (options: IOptions): IOptions { + const minValue: number = 0, + maxValue: number = 1; + + options['unicodeArrayThreshold'] = Math.min( + Math.max( + options['unicodeArrayThreshold'], + minValue + ), + maxValue + ); + + return options; + } +} diff --git a/src/interfaces/IOptions.d.ts b/src/interfaces/IOptions.d.ts index 3278b86c..e47984bd 100644 --- a/src/interfaces/IOptions.d.ts +++ b/src/interfaces/IOptions.d.ts @@ -8,5 +8,6 @@ export interface IOptions { rotateUnicodeArray?: boolean; selfDefending?: boolean; unicodeArray?: boolean; + unicodeArrayThreshold?: number; wrapUnicodeArrayCalls?: boolean; } diff --git a/src/node-obfuscators/NodeObfuscator.ts b/src/node-obfuscators/NodeObfuscator.ts index 08368c62..d2e06a73 100644 --- a/src/node-obfuscators/NodeObfuscator.ts +++ b/src/node-obfuscators/NodeObfuscator.ts @@ -97,19 +97,20 @@ export abstract class NodeObfuscator implements INodeObfuscator { * @returns {string} */ protected replaceLiteralValueByUnicodeValue (nodeValue: string): string { - let value: string = nodeValue; + let value: string = nodeValue, + replaceByUnicodeArrayFlag: boolean = Math.random() > this.options['unicodeArrayThreshold']; - if (this.options['encodeUnicodeLiterals']) { + if (this.options['encodeUnicodeLiterals'] && replaceByUnicodeArrayFlag) { value = new Buffer(encodeURI(value)).toString('base64'); } value = Utils.stringToUnicode(value); - if (!this.options['unicodeArray']) { + if (!this.options['unicodeArray'] || !replaceByUnicodeArrayFlag) { return value; } - return this.replaceLiteralValueByUnicodeArrayCall(value) + return this.replaceLiteralValueByUnicodeArrayCall(value); } /** diff --git a/src/preset-options/DefaultPreset.ts b/src/preset-options/DefaultPreset.ts index 9d907761..1f48140b 100644 --- a/src/preset-options/DefaultPreset.ts +++ b/src/preset-options/DefaultPreset.ts @@ -10,5 +10,6 @@ export const DEFAULT_PRESET: IOptions = Object.freeze({ rotateUnicodeArray: true, selfDefending: true, unicodeArray: true, + unicodeArrayThreshold: 0.8, wrapUnicodeArrayCalls: true }); diff --git a/src/preset-options/NoCustomNodesPreset.ts b/src/preset-options/NoCustomNodesPreset.ts index f207c282..06b114df 100644 --- a/src/preset-options/NoCustomNodesPreset.ts +++ b/src/preset-options/NoCustomNodesPreset.ts @@ -10,5 +10,6 @@ export const NO_CUSTOM_NODES_PRESET: IOptions = Object.freeze({ rotateUnicodeArray: false, selfDefending: false, unicodeArray: false, + unicodeArrayThreshold: 0, wrapUnicodeArrayCalls: false });