mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Mixed string array encoding prototype (#727)
`stringArrayEncoding` option now accepts an array of encodings. Each string will be randomly encoded with passed encoding.
This commit is contained in:
@@ -5,6 +5,7 @@ import { TCustomCodeHelperFactory } from '../../../types/container/custom-code-h
|
||||
import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
|
||||
import { TInitialData } from '../../../types/TInitialData';
|
||||
import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
|
||||
import { TStringArrayEncoding } from '../../../types/options/TStringArrayEncoding';
|
||||
|
||||
import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
|
||||
import { ICustomCodeHelper } from '../../../interfaces/custom-code-helpers/ICustomCodeHelper';
|
||||
@@ -16,15 +17,25 @@ import { initializable } from '../../../decorators/Initializable';
|
||||
|
||||
import { CustomCodeHelper } from '../../../enums/custom-code-helpers/CustomCodeHelper';
|
||||
import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
|
||||
import { StringArrayEncoding } from '../../../enums/StringArrayEncoding';
|
||||
|
||||
import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGroup';
|
||||
import { NodeAppender } from '../../../node/NodeAppender';
|
||||
import { StringArrayCodeHelper } from '../StringArrayCodeHelper';
|
||||
import { StringArrayCallsWrapperCodeHelper } from '../StringArrayCallsWrapperCodeHelper';
|
||||
import { StringArrayCodeHelper } from '../StringArrayCodeHelper';
|
||||
import { StringArrayRotateFunctionCodeHelper } from '../StringArrayRotateFunctionCodeHelper';
|
||||
|
||||
@injectable()
|
||||
export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
|
||||
/**
|
||||
* @type {Map<TStringArrayEncoding, CustomCodeHelper>}
|
||||
*/
|
||||
private static readonly stringArrayCallsWrapperCodeHelperMap: Map<TStringArrayEncoding, CustomCodeHelper> = new Map([
|
||||
[StringArrayEncoding.None, CustomCodeHelper.StringArrayCallsWrapper],
|
||||
[StringArrayEncoding.Base64, CustomCodeHelper.StringArrayCallsWrapperBase64],
|
||||
[StringArrayEncoding.Rc4, CustomCodeHelper.StringArrayCallsWrapperRc4]
|
||||
]);
|
||||
|
||||
/**
|
||||
* @type {Map<CustomCodeHelper, ICustomCodeHelper>}
|
||||
*/
|
||||
@@ -85,12 +96,16 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
|
||||
);
|
||||
|
||||
// stringArrayCallsWrapper helper nodes append
|
||||
this.appendCustomNodeIfExist(
|
||||
CustomCodeHelper.StringArrayCallsWrapper,
|
||||
(customCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>>) => {
|
||||
NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), 1);
|
||||
}
|
||||
);
|
||||
for (const stringArrayEncoding of this.options.stringArrayEncoding) {
|
||||
const stringArrayCallsWrapperCodeHelperName: CustomCodeHelper = this.getStringArrayCallsWrapperCodeHelperName(stringArrayEncoding);
|
||||
|
||||
this.appendCustomNodeIfExist(
|
||||
stringArrayCallsWrapperCodeHelperName,
|
||||
(customCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>>) => {
|
||||
NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), 1);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// stringArrayRotateFunction helper nodes append
|
||||
this.appendCustomNodeIfExist(
|
||||
@@ -108,27 +123,48 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
|
||||
return;
|
||||
}
|
||||
|
||||
// stringArray helper initialize
|
||||
const stringArrayCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCodeHelper>> =
|
||||
this.customCodeHelperFactory(CustomCodeHelper.StringArray);
|
||||
const stringArrayCallsWrapperCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>> =
|
||||
this.customCodeHelperFactory(CustomCodeHelper.StringArrayCallsWrapper);
|
||||
const stringArrayRotateFunctionCodeHelper: ICustomCodeHelper<TInitialData<StringArrayRotateFunctionCodeHelper>> =
|
||||
this.customCodeHelperFactory(CustomCodeHelper.StringArrayRotateFunction);
|
||||
|
||||
const stringArrayName: string = this.stringArrayStorage.getStorageName();
|
||||
const stringArrayCallsWrapperName: string = this.stringArrayStorage.getStorageCallsWrapperName();
|
||||
const stringArrayRotationAmount: number = this.stringArrayStorage.getRotationAmount();
|
||||
const atobFunctionName: string = this.randomGenerator.getRandomString(6);
|
||||
|
||||
stringArrayCodeHelper.initialize(this.stringArrayStorage, stringArrayName);
|
||||
stringArrayCallsWrapperCodeHelper.initialize(stringArrayName, stringArrayCallsWrapperName, atobFunctionName);
|
||||
stringArrayRotateFunctionCodeHelper.initialize(stringArrayName, stringArrayRotationAmount);
|
||||
|
||||
this.customCodeHelpers.set(CustomCodeHelper.StringArray, stringArrayCodeHelper);
|
||||
this.customCodeHelpers.set(CustomCodeHelper.StringArrayCallsWrapper, stringArrayCallsWrapperCodeHelper);
|
||||
|
||||
// stringArrayCallsWrapper helper initialize
|
||||
for (const stringArrayEncoding of this.options.stringArrayEncoding) {
|
||||
const stringArrayCallsWrapperCodeHelperName: CustomCodeHelper = this.getStringArrayCallsWrapperCodeHelperName(stringArrayEncoding);
|
||||
const stringArrayCallsWrapperCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>> =
|
||||
this.customCodeHelperFactory(stringArrayCallsWrapperCodeHelperName);
|
||||
const stringArrayCallsWrapperName: string = this.stringArrayStorage.getStorageCallsWrapperName(stringArrayEncoding);
|
||||
|
||||
stringArrayCallsWrapperCodeHelper.initialize(
|
||||
stringArrayName,
|
||||
stringArrayCallsWrapperName
|
||||
);
|
||||
|
||||
this.customCodeHelpers.set(stringArrayCallsWrapperCodeHelperName, stringArrayCallsWrapperCodeHelper);
|
||||
}
|
||||
|
||||
// stringArrayRotateFunction helper initialize
|
||||
const stringArrayRotateFunctionCodeHelper: ICustomCodeHelper<TInitialData<StringArrayRotateFunctionCodeHelper>> =
|
||||
this.customCodeHelperFactory(CustomCodeHelper.StringArrayRotateFunction);
|
||||
const stringArrayRotationAmount: number = this.stringArrayStorage.getRotationAmount();
|
||||
|
||||
stringArrayRotateFunctionCodeHelper.initialize(stringArrayName, stringArrayRotationAmount);
|
||||
|
||||
if (this.options.rotateStringArray) {
|
||||
this.customCodeHelpers.set(CustomCodeHelper.StringArrayRotateFunction, stringArrayRotateFunctionCodeHelper);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TStringArrayEncoding} stringArrayEncoding
|
||||
* @returns {CustomCodeHelper}
|
||||
*/
|
||||
private getStringArrayCallsWrapperCodeHelperName (stringArrayEncoding: TStringArrayEncoding): CustomCodeHelper {
|
||||
return StringArrayCodeHelperGroup
|
||||
.stringArrayCallsWrapperCodeHelperMap.get(stringArrayEncoding)
|
||||
?? CustomCodeHelper.StringArrayCallsWrapper;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user