Fixed wrong source-map: 'inline' encoding after 1.3.0

This commit is contained in:
sanex3339
2020-07-10 16:31:12 +03:00
parent 514e34bbd5
commit c6887464c4
20 changed files with 154 additions and 37 deletions
+4
View File
@@ -1,5 +1,9 @@
Change Log
v1.5.1
---
* Fixed wrong `source-map: 'inline'` encoding after `1.3.0`
v1.5.0
---
* New `mangled-shuffled` identifier names generator based on `mangled` identifier names generator
+7 -7
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "1.5.0",
"version": "1.5.1",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
+5
View File
@@ -0,0 +1,5 @@
import { alphabetStringUppercase } from './AlphabetStringUppercase';
import { alphabetString } from './AlphabetString';
import { numbersString } from './NumbersString';
export const base64alphabet: string = `${alphabetStringUppercase}${alphabetString}${numbersString}+/=`;
+8
View File
@@ -0,0 +1,8 @@
import { alphabetStringUppercase } from './AlphabetStringUppercase';
import { alphabetString } from './AlphabetString';
import { numbersString } from './NumbersString';
/**
* Swapped lowercase and uppercase groups of alphabet to prevent easy decode
*/
export const base64alphabetSwapped: string = `${alphabetString}${alphabetStringUppercase}${numbersString}+/=`;
+1
View File
@@ -22,6 +22,7 @@ export enum ServiceIdentifiers {
ICodeTransformerNamesGroupsBuilder = 'ICodeTransformerNamesGroupsBuilder',
ICodeTransformersRunner = 'ICodeTransformersRunner',
ICryptUtils = 'ICryptUtils',
ICryptUtilsSwappedAlphabet = 'ICryptUtilsSwappedAlphabet',
ICustomCodeHelper = 'ICustomCodeHelper',
ICustomCodeHelperGroup = 'ICustomCodeHelperGroup',
IControlFlowReplacer = 'IControlFlowReplacer',
@@ -3,12 +3,14 @@ import { ServiceIdentifiers } from '../../ServiceIdentifiers';
import { IArrayUtils } from '../../../interfaces/utils/IArrayUtils';
import { ICryptUtils } from '../../../interfaces/utils/ICryptUtils';
import { ICryptUtilsSwappedAlphabet } from '../../../interfaces/utils/ICryptUtilsSwappedAlphabet';
import { IEscapeSequenceEncoder } from '../../../interfaces/utils/IEscapeSequenceEncoder';
import { ILevelledTopologicalSorter } from '../../../interfaces/utils/ILevelledTopologicalSorter';
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
import { ArrayUtils } from '../../../utils/ArrayUtils';
import { CryptUtils } from '../../../utils/CryptUtils';
import { CryptUtilsSwappedAlphabet } from '../../../utils/CryptUtilsSwappedAlphabet';
import { EscapeSequenceEncoder } from '../../../utils/EscapeSequenceEncoder';
import { LevelledTopologicalSorter } from '../../../utils/LevelledTopologicalSorter';
import { RandomGenerator } from '../../../utils/RandomGenerator';
@@ -29,6 +31,11 @@ export const utilsModule: interfaces.ContainerModule = new ContainerModule((bind
.to(CryptUtils)
.inSingletonScope();
// crypt utils with swapped alphabet
bind<ICryptUtilsSwappedAlphabet>(ServiceIdentifiers.ICryptUtilsSwappedAlphabet)
.to(CryptUtilsSwappedAlphabet)
.inSingletonScope();
// escape sequence encoder
bind<IEscapeSequenceEncoder>(ServiceIdentifiers.IEscapeSequenceEncoder)
.to(EscapeSequenceEncoder)
@@ -1,15 +1,12 @@
import { alphabetStringUppercase } from '../../../../constants/AlphabetStringUppercase';
import { alphabetString } from '../../../../constants/AlphabetString';
import { numbersString } from '../../../../constants/NumbersString';
import { base64alphabetSwapped } from '../../../../constants/Base64AlphabetSwapped';
/**
* @returns {string}
*/
export function AtobTemplate (): string {
// swapped lowercase and uppercase groups of alphabet to prevent easy decode!!!!
return `
var {atobFunctionName} = function (input) {
const chars = '${alphabetString}${alphabetStringUppercase}${numbersString}+/=';
const chars = '${base64alphabetSwapped}';
const str = String(input).replace(/=+$/, '');
let output = '';
+2 -1
View File
@@ -1,6 +1,7 @@
export interface ICryptUtils {
/**
* @param string
* @param {string} string
* @returns {string}
*/
btoa (string: string): string;
@@ -0,0 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
import { ICryptUtils } from './ICryptUtils';
export interface ICryptUtilsSwappedAlphabet extends ICryptUtils {}
@@ -5,7 +5,7 @@ import { TIdentifierNamesGeneratorFactory } from '../../types/container/generato
import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
import { ICryptUtils } from '../../interfaces/utils/ICryptUtils';
import { ICryptUtilsSwappedAlphabet } from '../../interfaces/utils/ICryptUtilsSwappedAlphabet';
import { IEncodedValue } from '../../interfaces/IEncodedValue';
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
import { IOptions } from '../../interfaces/options/IOptions';
@@ -50,9 +50,9 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
private readonly arrayUtils: IArrayUtils;
/**
* @type {ICryptUtils}
* @type {ICryptUtilsSwappedAlphabet}
*/
private readonly cryptUtils: ICryptUtils;
private readonly cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet;
/**
* @type {IEscapeSequenceEncoder}
@@ -94,7 +94,7 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
* @param {IArrayUtils} arrayUtils
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
* @param {ICryptUtils} cryptUtils
* @param {ICryptUtilsSwappedAlphabet} cryptUtilsSwappedAlphabet
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
*/
public constructor (
@@ -103,14 +103,14 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
@inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions,
@inject(ServiceIdentifiers.ICryptUtils) cryptUtils: ICryptUtils,
@inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet,
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
) {
super(randomGenerator, options);
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
this.arrayUtils = arrayUtils;
this.cryptUtils = cryptUtils;
this.cryptUtilsSwappedAlphabet = cryptUtilsSwappedAlphabet;
this.escapeSequenceEncoder = escapeSequenceEncoder;
this.rc4Keys = this.randomGenerator.getRandomGenerator()
@@ -275,7 +275,7 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
*/
case StringArrayEncoding.Rc4: {
const decodeKey: string = this.randomGenerator.getRandomGenerator().pickone(this.rc4Keys);
const encodedValue: string = this.cryptUtils.btoa(this.cryptUtils.rc4(value, decodeKey));
const encodedValue: string = this.cryptUtilsSwappedAlphabet.btoa(this.cryptUtilsSwappedAlphabet.rc4(value, decodeKey));
const encodedValueSources: string[] = this.rc4EncodedValuesSourcesCache.get(encodedValue) ?? [];
let encodedValueSourcesLength: number = encodedValueSources.length;
@@ -298,7 +298,7 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
case StringArrayEncoding.Base64: {
const decodeKey: null = null;
const encodedValue: string = this.cryptUtils.btoa(value);
const encodedValue: string = this.cryptUtilsSwappedAlphabet.btoa(value);
return { encodedValue, decodeKey };
}
+7 -5
View File
@@ -4,15 +4,18 @@ import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import { ICryptUtils } from '../interfaces/utils/ICryptUtils';
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
import { alphabetStringUppercase } from '../constants/AlphabetStringUppercase';
import { alphabetString } from '../constants/AlphabetString';
import { numbersString } from '../constants/NumbersString';
import { base64alphabet } from '../constants/Base64Alphabet';
import { RandomGenerator } from './RandomGenerator';
import { Utils } from './Utils';
@injectable()
export class CryptUtils implements ICryptUtils {
/**
* @type {string}
*/
protected readonly base64Alphabet: string = base64alphabet;
/**
* @type {IRandomGenerator}
*/
@@ -32,8 +35,7 @@ export class CryptUtils implements ICryptUtils {
* @returns {string}
*/
public btoa (string: string): string {
// swapped lowercase and uppercase groups of alphabet to prevent easy decode!!!!
const chars: string = `${alphabetString}${alphabetStringUppercase}${numbersString}+/=`;
const chars: string = this.base64Alphabet;
let output: string = '';
+26
View File
@@ -0,0 +1,26 @@
import { inject, injectable } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import { ICryptUtilsSwappedAlphabet } from '../interfaces/utils/ICryptUtilsSwappedAlphabet';
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
import { base64alphabetSwapped } from '../constants/Base64AlphabetSwapped';
import { CryptUtils } from './CryptUtils';
@injectable()
export class CryptUtilsSwappedAlphabet extends CryptUtils implements ICryptUtilsSwappedAlphabet {
/**
* @type {string}
*/
protected readonly base64Alphabet: string = base64alphabetSwapped;
/**
* @param {IRandomGenerator} randomGenerator
*/
public constructor (
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator
) {
super(randomGenerator);
}
}
@@ -6,7 +6,7 @@ import { assert } from 'chai';
import { ServiceIdentifiers } from '../../../../../../src/container/ServiceIdentifiers';
import { ICryptUtils } from '../../../../../../src/interfaces/utils/ICryptUtils';
import { ICryptUtilsSwappedAlphabet } from '../../../../../../src/interfaces/utils/ICryptUtilsSwappedAlphabet';
import { IInversifyContainerFacade } from '../../../../../../src/interfaces/container/IInversifyContainerFacade';
import { IObfuscatedCode } from '../../../../../../src/interfaces/source-code/IObfuscatedCode';
import { IRandomGenerator } from '../../../../../../src/interfaces/utils/IRandomGenerator';
@@ -28,15 +28,17 @@ describe('StringArrayCallsWrapperTemplate', () => {
const stringArrayCallsWrapperName: string = 'stringArrayCallsWrapperName';
const atobFunctionName: string = 'atob';
let cryptUtils: ICryptUtils,
let cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet,
randomGenerator: IRandomGenerator;
before(() => {
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
inversifyContainerFacade.load('', '', {});
cryptUtils = inversifyContainerFacade.get<ICryptUtils>(ServiceIdentifiers.ICryptUtils);
randomGenerator = inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator);
cryptUtilsSwappedAlphabet = inversifyContainerFacade
.get<ICryptUtilsSwappedAlphabet>(ServiceIdentifiers.ICryptUtilsSwappedAlphabet);
randomGenerator = inversifyContainerFacade
.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator);
});
describe('Variant #1: `base64` encoding', () => {
@@ -65,7 +67,7 @@ describe('StringArrayCallsWrapperTemplate', () => {
});
decodedValue = Function(`
var ${stringArrayName} = ['${cryptUtils.btoa('test1')}'];
var ${stringArrayName} = ['${cryptUtilsSwappedAlphabet.btoa('test1')}'];
${stringArrayCallsWrapperTemplate}
@@ -108,7 +110,7 @@ describe('StringArrayCallsWrapperTemplate', () => {
});
decodedValue = Function(`
var ${stringArrayName} = ['${cryptUtils.btoa(cryptUtils.rc4('test1', key))}'];
var ${stringArrayName} = ['${cryptUtilsSwappedAlphabet.btoa(cryptUtilsSwappedAlphabet.rc4('test1', key))}'];
${stringArrayCallsWrapperTemplate}
+1
View File
@@ -44,6 +44,7 @@ import './unit-tests/storages/MapStorage.spec';
import './unit-tests/storages/string-array/StringArrayStorage.spec';
import './unit-tests/utils/ArrayUtils.spec';
import './unit-tests/utils/CryptUtils.spec';
import './unit-tests/utils/CryptUtilsSwappedAlphabet.spec';
import './unit-tests/utils/EscapeSequenceEncoder.spec';
import './unit-tests/utils/LevelledTopologicalSorter.spec';
import './unit-tests/utils/NumberUtils.spec';
@@ -89,7 +89,7 @@ describe('ObfuscatedCode', () => {
});
describe('source map is set, source map mode is `inline`', () => {
const regExp: RegExp = /data:application\/json;base64/;
const regExp: RegExp = /data:application\/json;base64,dGVzdA==/;
before(() => {
obfuscatedCode = getObfuscatedCode(
+1 -1
View File
@@ -22,7 +22,7 @@ describe('CryptUtils', () => {
});
describe('btoa', () => {
const expectedString: string = swapLettersCase('c3RyaW5n');
const expectedString: string = swapLettersCase('C3rYAw5N');
let string: string;
@@ -0,0 +1,59 @@
import 'reflect-metadata';
import { assert } from 'chai';
import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
import { ICryptUtilsSwappedAlphabet } from '../../../src/interfaces/utils/ICryptUtilsSwappedAlphabet';
import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
import { swapLettersCase } from '../../helpers/swapLettersCase';
describe('CryptUtilsSwappedAlphabet', () => {
let cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet;
before(() => {
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
inversifyContainerFacade.load('', '', {});
cryptUtilsSwappedAlphabet = inversifyContainerFacade
.get<ICryptUtilsSwappedAlphabet>(ServiceIdentifiers.ICryptUtilsSwappedAlphabet);
});
describe('btoa', () => {
const expectedString: string = swapLettersCase('c3RyaW5n');
let string: string;
before(() => {
string = cryptUtilsSwappedAlphabet.btoa('string');
});
it('should create a base-64 encoded string with swapped alphabet from a given string', () => {
assert.equal(string, expectedString);
});
});
describe('rc4', () => {
const string: string = 'test';
const key: string = 'key';
let encodedString: string,
decodedString: string;
before(() => {
encodedString = cryptUtilsSwappedAlphabet.rc4(string, key);
decodedString = cryptUtilsSwappedAlphabet.rc4(encodedString, key);
});
it('should encode string using the rc4 algorithm', () => {
assert.notEqual(encodedString, string);
});
it('should encode and successfully decode string using the rc4 algorithm', () => {
assert.equal(decodedString, string);
});
});
});