Skip to content

Commit

Permalink
Merge 5e8c164 into 44ac6c3
Browse files Browse the repository at this point in the history
  • Loading branch information
sanex3339 committed Nov 14, 2020
2 parents 44ac6c3 + 5e8c164 commit 06957f3
Show file tree
Hide file tree
Showing 28 changed files with 528 additions and 108 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
Change Log

v2.9.0
---
* New option: `stringArrayIndexShift` enables additional index shift for all string array calls

v2.8.1
---
* Fixed incorrect rename of the identifiers of the added helpers in some rare cases. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/804
Expand Down
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -365,6 +365,7 @@ Following options are available for the JS Obfuscator:
splitStringsChunkLength: 10,
stringArray: true,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersType: 'variable',
Expand Down Expand Up @@ -418,6 +419,7 @@ Following options are available for the JS Obfuscator:
--split-strings-chunk-length <number>
--string-array <boolean>
--string-array-encoding '<list>' (comma separated) [none, base64, rc4]
--string-array-index-shift <boolean>
--string-array-wrappers-count <number>
--string-array-wrappers-chained-calls <boolean>
--string-array-wrappers-type <string> [variable, function]
Expand Down Expand Up @@ -987,6 +989,13 @@ stringArrayEncoding: [
]
```

### `stringArrayIndexShift`
Type: `boolean` Default: `true`

##### :warning: `stringArray` option must be enabled

Enables additional index shift for all string array calls

### `stringArrayWrappersCount`
Type: `number` Default: `1`

Expand Down Expand Up @@ -1236,6 +1245,7 @@ Performance will 50-100% slower than without obfuscation
splitStringsChunkLength: 5,
stringArray: true,
stringArrayEncoding: ['rc4'],
stringArrayIndexShift: true,
stringArrayWrappersCount: 5,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersType: 'function',
Expand Down Expand Up @@ -1271,6 +1281,7 @@ Performance will 30-35% slower than without obfuscation
splitStringsChunkLength: 10,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayIndexShift: true,
stringArrayWrappersCount: 2,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersType: 'variable',
Expand Down Expand Up @@ -1303,6 +1314,7 @@ Performance will slightly slower than without obfuscation
splitStrings: false,
stringArray: true,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersType: 'variable',
Expand Down Expand Up @@ -1332,6 +1344,7 @@ Performance will slightly slower than without obfuscation
splitStrings: false,
stringArray: true,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersType: 'variable',
Expand Down
2 changes: 1 addition & 1 deletion dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.8.1",
"version": "2.9.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down Expand Up @@ -56,7 +56,7 @@
"@types/js-string-escape": "1.0.0",
"@types/md5": "2.2.1",
"@types/mkdirp": "1.0.1",
"@types/mocha": "8.0.3",
"@types/mocha": "8.0.4",
"@types/multimatch": "4.0.0",
"@types/node": "14.14.7",
"@types/rimraf": "3.0.0",
Expand All @@ -71,7 +71,7 @@
"cross-env": "7.0.2",
"eslint": "7.13.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsdoc": "30.7.7",
"eslint-plugin-jsdoc": "30.7.8",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.2",
"eslint-plugin-unicorn": "23.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/cli/JavaScriptObfuscatorCLI.ts
Expand Up @@ -350,6 +350,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
`Default: ${StringArrayEncoding.None}`,
ArraySanitizer
)
.option(
'--string-array-index-shift <boolean>',
'Enables additional index shift for all string array calls',
BooleanSanitizer
)
.option(
'--string-array-wrappers-count <number>',
'Sets the count of wrappers for the string array inside each root or function scope',
Expand Down
Expand Up @@ -20,6 +20,12 @@ import { NodeUtils } from '../../node/NodeUtils';

@injectable()
export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper {
/**
* @type {number}
*/
@initializable()
protected indexShiftAmount!: number;

/**
* @type {string}
*/
Expand Down Expand Up @@ -68,13 +74,16 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
/**
* @param {string} stringArrayName
* @param {string} stringArrayCallsWrapperName
* @param {number} indexShiftAmount
*/
public initialize (
stringArrayName: string,
stringArrayCallsWrapperName: string
stringArrayCallsWrapperName: string,
indexShiftAmount: number
): void {
this.stringArrayName = stringArrayName;
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
this.indexShiftAmount = indexShiftAmount;
}

/**
Expand All @@ -97,7 +106,8 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
this.customCodeHelperFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
stringArrayName: this.stringArrayName
stringArrayName: this.stringArrayName,
indexShiftAmount: this.indexShiftAmount
}),
{
reservedNames: preservedNames
Expand Down
Expand Up @@ -142,7 +142,8 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {

stringArrayCallsWrapperCodeHelper.initialize(
stringArrayName,
stringArrayCallsWrapperName
stringArrayCallsWrapperName,
this.stringArrayStorage.getIndexShiftAmount()
);

this.customCodeHelpers.set(stringArrayCallsWrapperCodeHelperName, stringArrayCallsWrapperCodeHelper);
Expand All @@ -151,9 +152,11 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
// stringArrayRotateFunction helper initialize
const stringArrayRotateFunctionCodeHelper: ICustomCodeHelper<TInitialData<StringArrayRotateFunctionCodeHelper>> =
this.customCodeHelperFactory(CustomCodeHelper.StringArrayRotateFunction);
const stringArrayRotationAmount: number = this.stringArrayStorage.getRotationAmount();

stringArrayRotateFunctionCodeHelper.initialize(stringArrayName, stringArrayRotationAmount);
stringArrayRotateFunctionCodeHelper.initialize(
stringArrayName,
this.stringArrayStorage.getRotationAmount()
);

if (this.options.rotateStringArray) {
this.customCodeHelpers.set(CustomCodeHelper.StringArrayRotateFunction, stringArrayRotateFunctionCodeHelper);
Expand Down
Expand Up @@ -4,7 +4,7 @@
export function StringArrayCallsWrapperTemplate (): string {
return `
const {stringArrayCallsWrapperName} = function (index, key) {
index = index - 0;
index = index - {indexShiftAmount};
let value = {stringArrayName}[index];
Expand Down
11 changes: 10 additions & 1 deletion src/custom-nodes/string-array-nodes/StringArrayCallNode.ts
Expand Up @@ -30,6 +30,12 @@ export class StringArrayCallNode extends AbstractStringArrayCallNode {
@initializable()
private index!: number;

/**
* @type {number}
*/
@initializable()
private indexShiftAmount!: number;

/**
* @type {string}
*/
Expand Down Expand Up @@ -60,15 +66,18 @@ export class StringArrayCallNode extends AbstractStringArrayCallNode {
/**
* @param {string} stringArrayCallsWrapperName
* @param {number} index
* @param {number} indexShiftAmount
* @param {string | null} decodeKey
*/
public initialize (
stringArrayCallsWrapperName: string,
index: number,
indexShiftAmount: number,
decodeKey: string | null
): void {
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
this.index = index;
this.indexShiftAmount = indexShiftAmount;
this.decodeKey = decodeKey;
}

Expand All @@ -77,7 +86,7 @@ export class StringArrayCallNode extends AbstractStringArrayCallNode {
*/
protected getNodeStructure (): TStatement[] {
const callExpressionArgs: ESTree.Expression[] = [
this.getHexadecimalNode(this.index)
this.getHexadecimalNode(this.indexShiftAmount + this.index)
];

if (this.decodeKey) {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/options/IOptions.ts
Expand Up @@ -43,6 +43,7 @@ export interface IOptions {
readonly splitStringsChunkLength: number;
readonly stringArray: boolean;
readonly stringArrayEncoding: TStringArrayEncoding[];
readonly stringArrayIndexShift: boolean;
readonly stringArrayWrappersChainedCalls: boolean;
readonly stringArrayWrappersCount: number;
readonly stringArrayWrappersType: TStringArrayWrappersType;
Expand Down
Expand Up @@ -4,6 +4,11 @@ import { IMapStorage } from '../IMapStorage';
import { IStringArrayStorageItemData } from './IStringArrayStorageItem';

export interface IStringArrayStorage extends IMapStorage <string, IStringArrayStorageItemData> {
/**
* @returns {number}
*/
getIndexShiftAmount (): number;

/**
* @returns {number}
*/
Expand Down
Expand Up @@ -204,16 +204,21 @@ export class StringArrayTransformer extends AbstractNodeTransformer {

/**
* @param {IStringArrayStorageItemData} stringArrayStorageItemData
* @returns {Node}
* @returns {Expression}
*/
private getStringArrayCallNode (stringArrayStorageItemData: IStringArrayStorageItemData): ESTree.Node {
private getStringArrayCallNode (stringArrayStorageItemData: IStringArrayStorageItemData): ESTree.Expression {
const [stringArrayCallsWrapperName, index] = this.getStringArrayCallsWrapperData(stringArrayStorageItemData);
const {decodeKey } = stringArrayStorageItemData;

const stringArrayCallCustomNode: ICustomNode<TInitialData<StringArrayCallNode>> =
this.stringArrayTransformerCustomNodeFactory(StringArrayCustomNode.StringArrayCallNode);

stringArrayCallCustomNode.initialize(stringArrayCallsWrapperName, index, decodeKey);
stringArrayCallCustomNode.initialize(
stringArrayCallsWrapperName,
index,
this.stringArrayStorage.getIndexShiftAmount(),
decodeKey
);

const statementNode: TStatement = stringArrayCallCustomNode.getNode()[0];

Expand Down
6 changes: 6 additions & 0 deletions src/options/Options.ts
Expand Up @@ -314,6 +314,12 @@ export class Options implements IOptions {
@IsIn([StringArrayEncoding.None, StringArrayEncoding.Base64, StringArrayEncoding.Rc4], { each: true })
public readonly stringArrayEncoding!: TStringArrayEncoding[];

/**
* @type {boolean}
*/
@IsBoolean()
public readonly stringArrayIndexShift!: boolean;

/**
* @type {boolean}
*/
Expand Down
1 change: 1 addition & 0 deletions src/options/normalizer-rules/StringArrayRule.ts
Expand Up @@ -18,6 +18,7 @@ export const StringArrayRule: TOptionsNormalizerRule = (options: IOptions): IOpt
stringArrayEncoding: [
StringArrayEncoding.None
],
stringArrayIndexShift: false,
stringArrayWrappersChainedCalls: false,
stringArrayWrappersCount: 0,
stringArrayThreshold: 0
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/Default.ts
Expand Up @@ -47,6 +47,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
stringArrayEncoding: [
StringArrayEncoding.None
],
stringArrayIndexShift: true,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersCount: 1,
stringArrayWrappersType: StringArrayWrappersType.Variable,
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/NoCustomNodes.ts
Expand Up @@ -44,6 +44,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
stringArrayEncoding: [
StringArrayEncoding.None
],
stringArrayIndexShift: false,
stringArrayWrappersChainedCalls: false,
stringArrayWrappersCount: 0,
stringArrayWrappersType: StringArrayWrappersType.Variable,
Expand Down
28 changes: 28 additions & 0 deletions src/storages/string-array-transformers/StringArrayStorage.ts
Expand Up @@ -29,6 +29,16 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
*/
private static readonly maximumRotationAmount: number = 500;

/**
* @type {number}
*/
private static readonly minimumIndexShiftAmount: number = 100;

/**
* @type {number}
*/
private static readonly maximumIndexShiftAmount: number = 500;

/**
* @type {number}
*/
Expand Down Expand Up @@ -69,6 +79,11 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
*/
private readonly rc4EncodedValuesSourcesCache: Map<string, string[]> = new Map();

/**
* @type {number}
*/
private indexShiftAmount: number = 0;

/**
* @type {number}
*/
Expand Down Expand Up @@ -118,6 +133,12 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
public initialize (): void {
super.initialize();

this.indexShiftAmount = this.options.stringArrayIndexShift
? this.randomGenerator.getRandomInteger(
StringArrayStorage.minimumIndexShiftAmount,
StringArrayStorage.maximumIndexShiftAmount
)
: 0;
this.rotationAmount = this.options.rotateStringArray
? this.randomGenerator.getRandomInteger(
StringArrayStorage.minimumRotationAmount,
Expand All @@ -133,6 +154,13 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
return this.getOrSetIfDoesNotExist(value);
}

/**
* @returns {number}
*/
public getIndexShiftAmount (): number {
return this.indexShiftAmount;
}

/**
* @returns {number}
*/
Expand Down

0 comments on commit 06957f3

Please sign in to comment.