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:
Timofey Kachalov
2020-08-30 21:10:46 +03:00
committed by GitHub
parent 8c91a254be
commit 42ff66849e
39 changed files with 545 additions and 232 deletions
@@ -0,0 +1,32 @@
import { injectable, } from 'inversify';
import { AtobTemplate } from './templates/string-array-calls-wrapper/AtobTemplate';
import { StringArrayCallsWrapperCodeHelper } from './StringArrayCallsWrapperCodeHelper';
import { StringArrayBase64DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate';
@injectable()
export class StringArrayCallsWrapperBase64CodeHelper extends StringArrayCallsWrapperCodeHelper {
/**
* @returns {string}
*/
protected getDecodeStringArrayTemplate (): string {
const atobFunctionName: string = this.randomGenerator.getRandomString(6);
const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), {
atobFunctionName: atobFunctionName
});
const selfDefendingCode: string = this.getSelfDefendingTemplate();
return this.customCodeHelperFormatter.formatTemplate(
StringArrayBase64DecodeTemplate(this.randomGenerator),
{
atobPolyfill,
atobFunctionName,
selfDefendingCode,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
}
);
}
}
@@ -10,16 +10,10 @@ import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEn
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
import { initializable } from '../../decorators/Initializable';
import { AtobTemplate } from './templates/string-array-calls-wrapper/AtobTemplate';
import { Rc4Template } from './templates/string-array-calls-wrapper/Rc4Template';
import { SelfDefendingTemplate } from './templates/string-array-calls-wrapper/SelfDefendingTemplate';
import { StringArrayBase64DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate';
import { StringArrayCallsWrapperTemplate } from './templates/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
import { StringArrayRC4DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayRC4DecodeTemplate';
import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
import { NodeUtils } from '../../node/NodeUtils';
@@ -30,19 +24,13 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
* @type {string}
*/
@initializable()
private atobFunctionName!: string;
protected stringArrayName!: string;
/**
* @type {string}
*/
@initializable()
private stringArrayName!: string;
/**
* @type {string}
*/
@initializable()
private stringArrayCallsWrapperName!: string;
protected stringArrayCallsWrapperName!: string;
/**
* @type {IEscapeSequenceEncoder}
@@ -80,16 +68,13 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
/**
* @param {string} stringArrayName
* @param {string} stringArrayCallsWrapperName
* @param {string} atobFunctionName
*/
public initialize (
stringArrayName: string,
stringArrayCallsWrapperName: string,
atobFunctionName: string
stringArrayCallsWrapperName: string
): void {
this.stringArrayName = stringArrayName;
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
this.atobFunctionName = atobFunctionName;
}
/**
@@ -123,56 +108,27 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
/**
* @returns {string}
*/
private getDecodeStringArrayTemplate (): string {
const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), {
atobFunctionName: this.atobFunctionName
});
const rc4Polyfill: string = this.customCodeHelperFormatter.formatTemplate(Rc4Template(), {
atobFunctionName: this.atobFunctionName
});
protected getDecodeStringArrayTemplate (): string {
return '';
}
let decodeStringArrayTemplate: string = '';
let selfDefendingCode: string = '';
if (this.options.selfDefending) {
selfDefendingCode = this.customCodeHelperFormatter.formatTemplate(
SelfDefendingTemplate(
this.randomGenerator,
this.escapeSequenceEncoder
),
{
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
stringArrayName: this.stringArrayName
}
);
/**
* @returns {string}
*/
protected getSelfDefendingTemplate (): string {
if (!this.options.selfDefending) {
return '';
}
switch (this.options.stringArrayEncoding) {
case StringArrayEncoding.Rc4:
decodeStringArrayTemplate = this.customCodeHelperFormatter.formatTemplate(
StringArrayRC4DecodeTemplate(this.randomGenerator),
{
atobPolyfill,
rc4Polyfill,
selfDefendingCode,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
}
);
break;
case StringArrayEncoding.Base64:
decodeStringArrayTemplate = this.customCodeHelperFormatter.formatTemplate(
StringArrayBase64DecodeTemplate(this.randomGenerator),
{
atobPolyfill,
atobFunctionName: this.atobFunctionName,
selfDefendingCode,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
}
);
}
return decodeStringArrayTemplate;
return this.customCodeHelperFormatter.formatTemplate(
SelfDefendingTemplate(
this.randomGenerator,
this.escapeSequenceEncoder
),
{
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
stringArrayName: this.stringArrayName
}
);
}
}
@@ -0,0 +1,36 @@
import { injectable, } from 'inversify';
import { AtobTemplate } from './templates/string-array-calls-wrapper/AtobTemplate';
import { Rc4Template } from './templates/string-array-calls-wrapper/Rc4Template';
import { StringArrayRC4DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayRC4DecodeTemplate';
import { StringArrayCallsWrapperCodeHelper } from './StringArrayCallsWrapperCodeHelper';
@injectable()
export class StringArrayCallsWrapperRc4CodeHelper extends StringArrayCallsWrapperCodeHelper {
/**
* @returns {string}
*/
protected getDecodeStringArrayTemplate (): string {
const atobFunctionName: string = this.randomGenerator.getRandomString(6);
const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), {
atobFunctionName
});
const rc4Polyfill: string = this.customCodeHelperFormatter.formatTemplate(Rc4Template(), {
atobFunctionName
});
const selfDefendingCode: string = this.getSelfDefendingTemplate();
return this.customCodeHelperFormatter.formatTemplate(
StringArrayRC4DecodeTemplate(this.randomGenerator),
{
atobPolyfill,
rc4Polyfill,
selfDefendingCode,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
}
);
}
}
@@ -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;
}
}