Add prefix only for variables with global declaration

This commit is contained in:
sanex3339
2018-01-22 23:15:43 +03:00
parent e4f9c100f8
commit cf94be2285
27 changed files with 134 additions and 93 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -63,7 +63,7 @@
"mocha": "5.0.0",
"pre-commit": "1.2.2",
"rimraf": "2.6.2",
"sinon": "4.1.6",
"sinon": "4.2.0",
"threads": "0.10.1",
"ts-node": "4.1.0",
"tslint": "5.9.1",
@@ -64,7 +64,7 @@ export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
: GlobalVariableNoEvalTemplate();
return format(ConsoleOutputDisableExpressionTemplate(), {
consoleLogDisableFunctionName: this.identifierNamesGenerator.generate(6),
consoleLogDisableFunctionName: this.identifierNamesGenerator.generate(),
globalVariableTemplate,
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
});
@@ -92,7 +92,7 @@ export class ConsoleOutputCustomNodeGroup extends AbstractCustomNodeGroup {
return;
}
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate(6);
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate();
const consoleOutputDisableExpressionNode: ICustomNode = this.customNodeFactory(CustomNode.ConsoleOutputDisableExpressionNode);
const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
@@ -105,8 +105,8 @@ export class DebugProtectionCustomNodeGroup extends AbstractCustomNodeGroup {
return;
}
const debugProtectionFunctionName: string = this.identifierNamesGenerator.generate(6);
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate(6);
const debugProtectionFunctionName: string = this.identifierNamesGenerator.generate();
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate();
const debugProtectionFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.DebugProtectionFunctionNode);
const debugProtectionFunctionCallNode: ICustomNode = this.customNodeFactory(CustomNode.DebugProtectionFunctionCallNode);
@@ -79,7 +79,7 @@ export class DomainLockNode extends AbstractCustomNode {
: GlobalVariableNoEvalTemplate();
return format(DomainLockNodeTemplate(), {
domainLockFunctionName: this.identifierNamesGenerator.generate(6),
domainLockFunctionName: this.identifierNamesGenerator.generate(),
diff: diff,
domains: hiddenDomainsString,
globalVariableTemplate,
@@ -92,7 +92,7 @@ export class DomainLockCustomNodeGroup extends AbstractCustomNodeGroup {
return;
}
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate(6);
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate();
const domainLockNode: ICustomNode = this.customNodeFactory(CustomNode.DomainLockNode);
const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
@@ -71,7 +71,7 @@ export class SelfDefendingUnicodeNode extends AbstractCustomNode {
protected getTemplate (): string {
return JavaScriptObfuscator.obfuscate(
format(SelfDefendingTemplate(this.escapeSequenceEncoder), {
selfDefendingFunctionName: this.identifierNamesGenerator.generate(6),
selfDefendingFunctionName: this.identifierNamesGenerator.generate(),
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
}),
{
@@ -92,7 +92,7 @@ export class SelfDefendingCustomNodeGroup extends AbstractCustomNodeGroup {
return;
}
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate(6);
const callsControllerFunctionName: string = this.identifierNamesGenerator.generate();
const selfDefendingUnicodeNode: ICustomNode = this.customNodeFactory(CustomNode.SelfDefendingUnicodeNode);
const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
@@ -82,8 +82,8 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
* @returns {string}
*/
protected getTemplate (): string {
const timesName: string = this.identifierNamesGenerator.generate(6);
const whileFunctionName: string = this.identifierNamesGenerator.generate(6);
const timesName: string = this.identifierNamesGenerator.generate();
const whileFunctionName: string = this.identifierNamesGenerator.generate();
let code: string = '';
@@ -39,8 +39,12 @@ export abstract class AbstractIdentifierNamesGenerator implements IIdentifierNam
}
/**
* @param {number} length
* @returns {string}
*/
public abstract generate (length: number): string;
public abstract generate (): string;
/**
* @returns {string}
*/
public abstract generateWithPrefix (): string;
}
@@ -9,6 +9,11 @@ import { Utils } from '../../utils/Utils';
@injectable()
export class HexadecimalIdentifierNamesGenerator extends AbstractIdentifierNamesGenerator {
/**
* @type {number}
*/
private static baseIdentifierNameLength: number = 6;
/**
* @type {Set<string>}
*/
@@ -26,23 +31,31 @@ export class HexadecimalIdentifierNamesGenerator extends AbstractIdentifierNames
}
/**
* @param {number} length
* @returns {string}
*/
public generate (length: number): string {
const prefix: string = `${this.identifiersPrefix}_${Utils.hexadecimalPrefix}`;
public generate (): string {
const rangeMinInteger: number = 10000;
const rangeMaxInteger: number = 99999999;
const randomInteger: number = this.randomGenerator.getRandomInteger(rangeMinInteger, rangeMaxInteger);
const hexadecimalNumber: string = Utils.decToHex(randomInteger);
const randomVariableName: string = `${prefix}${hexadecimalNumber.substr(0, length)}`;
const baseIdentifierName: string = hexadecimalNumber.substr(0, HexadecimalIdentifierNamesGenerator.baseIdentifierNameLength);
const identifierName: string = `_${Utils.hexadecimalPrefix}${baseIdentifierName}`;
if (this.randomVariableNameSet.has(randomVariableName)) {
return this.generate(length);
if (this.randomVariableNameSet.has(identifierName)) {
return this.generate();
}
this.randomVariableNameSet.add(randomVariableName);
this.randomVariableNameSet.add(identifierName);
return randomVariableName;
return identifierName;
}
/**
* @returns {string}
*/
public generateWithPrefix (): string {
const identifierName: string = this.generate();
return `${this.identifiersPrefix}${identifierName}`.replace('__', '_');
}
}
@@ -98,17 +98,25 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
}
/**
* @param {number} length
* @returns {string}
*/
public generate (length: number): string {
public generate (): string {
const identifierName: string = MangledIdentifierNamesGenerator.generateNewMangledName(this.previousMangledName);
this.previousMangledName = identifierName;
return identifierName;
}
/**
* @returns {string}
*/
public generateWithPrefix (): string {
const prefix: string = this.options.identifiersPrefix ?
`${this.identifiersPrefix}_`
: '';
const newName: string = MangledIdentifierNamesGenerator.generateNewMangledName(this.previousMangledName);
const identifierName: string = this.generate();
this.previousMangledName = newName;
return `${prefix}${newName}`;
return `${prefix}${identifierName}`;
}
}
@@ -1,7 +1,11 @@
export interface IIdentifierNamesGenerator {
/**
* @param {number} length
* @returns {string}
*/
generate (length: number): string;
generate (): string;
/**
* @returns {string}
*/
generateWithPrefix (): string;
}
@@ -4,8 +4,9 @@ import { IObfuscatingReplacer } from './IObfuscatingReplacer';
export interface IIdentifierObfuscatingReplacer extends IObfuscatingReplacer <ESTree.Identifier> {
/**
* @param nodeValue
* @param {string} nodeValue
* @param isGlobalDeclaration
* @param {number} nodeIdentifier
*/
storeNames (nodeValue: any, nodeIdentifier: number): void;
storeNames (nodeValue: string, isGlobalDeclaration: boolean, nodeIdentifier: number): void;
}
@@ -90,7 +90,7 @@ export class CatchClauseTransformer extends AbstractNodeTransformer {
*/
private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: number): void {
if (NodeGuards.isIdentifierNode(catchClauseNode.param)) {
this.identifierObfuscatingReplacer.storeNames(catchClauseNode.param.name, nodeIdentifier);
this.identifierObfuscatingReplacer.storeNames(catchClauseNode.param.name, false, nodeIdentifier);
}
}
@@ -93,7 +93,7 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
return classDeclarationNode;
}
this.storeClassName(classDeclarationNode, nodeIdentifier);
this.storeClassName(classDeclarationNode, isGlobalDeclaration, nodeIdentifier);
// check for cached identifiers for current scope node. If exist - loop through them.
if (this.replaceableIdentifiers.has(blockScopeNode)) {
@@ -107,10 +107,15 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
/**
* @param {ClassDeclaration} classDeclarationNode
* @param {boolean} isGlobalDeclaration
* @param {number} nodeIdentifier
*/
private storeClassName (classDeclarationNode: ESTree.ClassDeclaration, nodeIdentifier: number): void {
this.identifierObfuscatingReplacer.storeNames(classDeclarationNode.id.name, nodeIdentifier);
private storeClassName (
classDeclarationNode: ESTree.ClassDeclaration,
isGlobalDeclaration: boolean,
nodeIdentifier: number
): void {
this.identifierObfuscatingReplacer.storeNames(classDeclarationNode.id.name, isGlobalDeclaration, nodeIdentifier);
}
/**
@@ -95,7 +95,7 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
return functionDeclarationNode;
}
this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
this.storeFunctionName(functionDeclarationNode, isGlobalDeclaration, nodeIdentifier);
// check for cached identifiers for current scope node. If exist - loop through them.
if (this.replaceableIdentifiers.has(blockScopeNode)) {
@@ -109,10 +109,15 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
/**
* @param {FunctionDeclaration} functionDeclarationNode
* @param {boolean} isGlobalDeclaration
* @param {number} nodeIdentifier
*/
private storeFunctionName (functionDeclarationNode: ESTree.FunctionDeclaration, nodeIdentifier: number): void {
this.identifierObfuscatingReplacer.storeNames(functionDeclarationNode.id.name, nodeIdentifier);
private storeFunctionName (
functionDeclarationNode: ESTree.FunctionDeclaration,
isGlobalDeclaration: boolean,
nodeIdentifier: number
): void {
this.identifierObfuscatingReplacer.storeNames(functionDeclarationNode.id.name, isGlobalDeclaration, nodeIdentifier);
}
/**
@@ -104,13 +104,13 @@ export class FunctionTransformer extends AbstractNodeTransformer {
estraverse.traverse(paramsNode, {
enter: (node: ESTree.Node): any => {
if (NodeGuards.isAssignmentPatternNode(node) && NodeGuards.isIdentifierNode(node.left)) {
this.identifierObfuscatingReplacer.storeNames(node.left.name, nodeIdentifier);
this.identifierObfuscatingReplacer.storeNames(node.left.name, false, nodeIdentifier);
return estraverse.VisitorOption.Skip;
}
if (NodeGuards.isIdentifierNode(node)) {
this.identifierObfuscatingReplacer.storeNames(node.name, nodeIdentifier);
this.identifierObfuscatingReplacer.storeNames(node.name, false, nodeIdentifier);
}
}
});
@@ -98,7 +98,7 @@ export class LabeledStatementTransformer extends AbstractNodeTransformer {
*/
private storeLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement, nodeIdentifier: number): void {
this.identifierObfuscatingReplacer
.storeNames(labeledStatementNode.label.name, nodeIdentifier);
.storeNames(labeledStatementNode.label.name, false, nodeIdentifier);
}
/**
@@ -100,7 +100,7 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
? blockScopeNode
: parentNode;
this.storeVariableNames(variableDeclarationNode, nodeIdentifier);
this.storeVariableNames(variableDeclarationNode, isGlobalDeclaration, nodeIdentifier);
// check for cached identifiers for current scope node. If exist - loop through them.
if (this.replaceableIdentifiers.has(scopeNode)) {
@@ -114,11 +114,16 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
/**
* @param {VariableDeclaration} variableDeclarationNode
* @param {boolean} isGlobalDeclaration
* @param {number} nodeIdentifier
*/
private storeVariableNames (variableDeclarationNode: ESTree.VariableDeclaration, nodeIdentifier: number): void {
private storeVariableNames (
variableDeclarationNode: ESTree.VariableDeclaration,
isGlobalDeclaration: boolean,
nodeIdentifier: number
): void {
this.traverseDeclarationIdentifiers(variableDeclarationNode, (identifierNode: ESTree.Identifier) => {
this.identifierObfuscatingReplacer.storeNames(identifierNode.name, nodeIdentifier);
this.identifierObfuscatingReplacer.storeNames(identifierNode.name, isGlobalDeclaration, nodeIdentifier);
});
}
@@ -58,14 +58,19 @@ export class BaseIdentifierObfuscatingReplacer extends AbstractObfuscatingReplac
* Reserved names will be ignored.
*
* @param {string} nodeName
* @param {boolean} isGlobalDeclaration
* @param {number} nodeIdentifier
*/
public storeNames (nodeName: string, nodeIdentifier: number): void {
public storeNames (nodeName: string, isGlobalDeclaration: boolean, nodeIdentifier: number): void {
if (this.isReservedName(nodeName)) {
return;
}
this.namesMap.set(`${nodeName}-${String(nodeIdentifier)}`, this.identifierNamesGenerator.generate(6));
const identifierName: string = isGlobalDeclaration
? this.identifierNamesGenerator.generateWithPrefix()
: this.identifierNamesGenerator.generate();
this.namesMap.set(`${nodeName}-${String(nodeIdentifier)}`, identifierName);
}
/**
@@ -12,6 +12,11 @@ import { ArrayStorage } from '../ArrayStorage';
@injectable()
export class StringArrayStorage extends ArrayStorage <string> {
/**
* @type {number}
*/
private static readonly stringArrayNameLength: number = 7;
/**
* @type {IArrayUtils}
*/
@@ -45,8 +50,10 @@ export class StringArrayStorage extends ArrayStorage <string> {
public initialize (): void {
super.initialize();
const stringArrayName: string = this.identifierNamesGenerator.generate(4);
const stringArrayCallsWrapperName: string = this.identifierNamesGenerator.generate(4);
const stringArrayName: string = this.identifierNamesGenerator
.generateWithPrefix().slice(0, StringArrayStorage.stringArrayNameLength);
const stringArrayCallsWrapperName: string = this.identifierNamesGenerator
.generateWithPrefix().slice(0, StringArrayStorage.stringArrayNameLength);
this.storageId = `${stringArrayName}|${stringArrayCallsWrapperName}`;
}
+5 -6
View File
@@ -6,19 +6,18 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
var bar = 1;
var baz = 2;
(function(){
function foo () {
eval('var s = 1;');
}
foo();
var bark = bar + baz;
})();
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
identifiersPrefix: 'foo',
identifierNamesGenerator: 'mangled'
identifierNamesGenerator: 'mangled',
renameGlobals: true
}
).getObfuscatedCode();
@@ -12,7 +12,7 @@ import { IdentifierNamesGenerator } from '../../../../src/enums/generators/ident
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
describe('HexadecimalIdentifierNamesGenerator', () => {
describe('generate (length: number): string', () => {
describe('generate (length: number, attachPrefix: boolean): string', () => {
describe('Hexadecimal name without prefix', () => {
let identifierNamesGenerator: IIdentifierNamesGenerator,
hexadecimalIdentifierName: string,
@@ -25,30 +25,15 @@ describe('HexadecimalIdentifierNamesGenerator', () => {
identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
ServiceIdentifiers.IIdentifierNamesGenerator,
IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
)
);
hexadecimalIdentifierName = identifierNamesGenerator.generate();
regExp = /^_0x(\w){4,6}$/;
});
describe('variant #1: hexadecimal name with length `4`', () => {
before(() => {
hexadecimalIdentifierName = identifierNamesGenerator.generate(4);
regExp = /^_0x(\w){4}$/;
});
it('should return hexadecimal name', () => {
assert.match(hexadecimalIdentifierName, regExp);
})
});
describe('variant #2: hexadecimal name with length `6`', () => {
before(() => {
hexadecimalIdentifierName = identifierNamesGenerator.generate(6);
regExp = /^_0x(\w){4,6}$/;
});
it('should return hexadecimal name', () => {
assert.match(hexadecimalIdentifierName, regExp);
})
});
it('should return hexadecimal name', () => {
assert.match(hexadecimalIdentifierName, regExp);
})
});
describe('Hexadecimal name with prefix', () => {
@@ -68,7 +53,7 @@ describe('HexadecimalIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
);
hexadecimalIdentifierName = identifierNamesGenerator.generate(6);
hexadecimalIdentifierName = identifierNamesGenerator.generateWithPrefix();
});
it('should return hexadecimal name with prefix', () => {
@@ -93,7 +78,7 @@ describe('HexadecimalIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
);
hexadecimalIdentifierName = identifierNamesGenerator.generate(6);
hexadecimalIdentifierName = identifierNamesGenerator.generateWithPrefix();
});
it('should return hexadecimal name with prefix', () => {
@@ -31,7 +31,7 @@ describe('MangledIdentifierNamesGenerator', () => {
const expectedMangledIdentifierName: string = 'a';
beforeEach(() => {
mangledIdentifierName = identifierNamesGenerator.generate(4);
mangledIdentifierName = identifierNamesGenerator.generate();
});
it('should return mangled name', () => {
@@ -45,7 +45,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate(6);
mangledIdentifierName = identifierNamesGenerator.generate();
}
});
@@ -60,7 +60,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate(6);
mangledIdentifierName = identifierNamesGenerator.generate();
}
});
@@ -75,7 +75,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate(6);
mangledIdentifierName = identifierNamesGenerator.generate();
}
});
@@ -90,7 +90,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate(6);
mangledIdentifierName = identifierNamesGenerator.generate();
}
});
@@ -110,7 +110,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition2; i++) {
mangledIdentifierName = identifierNamesGenerator.generate(6);
mangledIdentifierName = identifierNamesGenerator.generate();
if (i === expectedMangledIdentifierPosition1) {
mangledIdentifierName1 = mangledIdentifierName;
@@ -150,7 +150,7 @@ describe('MangledIdentifierNamesGenerator', () => {
const expectedMangledIdentifierName: string = 'foo_a';
beforeEach(() => {
mangledIdentifierName = identifierNamesGenerator.generate(4);
mangledIdentifierName = identifierNamesGenerator.generateWithPrefix();
});
it('should return mangled name with prefix', () => {
@@ -162,7 +162,7 @@ describe('MangledIdentifierNamesGenerator', () => {
const expectedMangledIdentifierName: string = 'foo_b';
beforeEach(() => {
mangledIdentifierName = identifierNamesGenerator.generate(4);
mangledIdentifierName = identifierNamesGenerator.generateWithPrefix();
});
it('should return mangled name with prefix', () => {
@@ -191,7 +191,7 @@ describe('MangledIdentifierNamesGenerator', () => {
const expectedMangledIdentifierNameRegExp: RegExp = /(\w){6}_a/;
beforeEach(() => {
mangledIdentifierName = identifierNamesGenerator.generate(4);
mangledIdentifierName = identifierNamesGenerator.generateWithPrefix();
});
it('should return mangled name with prefix', () => {
@@ -203,7 +203,7 @@ describe('MangledIdentifierNamesGenerator', () => {
const expectedMangledIdentifierNameRegExp: RegExp = /(\w){6}_b/;
beforeEach(() => {
mangledIdentifierName = identifierNamesGenerator.generate(4);
mangledIdentifierName = identifierNamesGenerator.generateWithPrefix();
});
it('should return mangled name with prefix', () => {
+3 -3
View File
@@ -3329,9 +3329,9 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
sinon@4.1.6:
version "4.1.6"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.1.6.tgz#9cb346bddb180d68a804429ffe14978d7fafd629"
sinon@4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.2.0.tgz#3b7c0d2289a80d2d84c4685f2ec130b7ab9af621"
dependencies:
diff "^3.1.0"
formatio "1.2.0"