Skip to content

Function obfuscation bugs #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Change Log
v0.18.0
---
* **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/313
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/309
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/307

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.

Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,27 @@ export class FunctionTransformer extends AbstractNodeTransformer {
* @param {TNodeWithLexicalScope} lexicalScopeNode
*/
private storeFunctionParams (functionNode: ESTree.Function, lexicalScopeNode: TNodeWithLexicalScope): void {
functionNode.params
.forEach((paramsNode: ESTree.Node) => {
estraverse.traverse(paramsNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): estraverse.VisitorOption | void => {
// Should check with identifier as first argument,
// because prohibited identifier can be easily ignored
if (FunctionTransformer.isProhibitedIdentifierOfPropertyNode(node, parentNode)) {
return;
}
const visitor: estraverse.Visitor = {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): estraverse.VisitorOption | void => {
// Should check with identifier as first argument,
// because prohibited identifier can be easily ignored
if (FunctionTransformer.isProhibitedIdentifierOfPropertyNode(node, parentNode)) {
return;
}

if (NodeGuards.isAssignmentPatternNode(node) && NodeGuards.isIdentifierNode(node.left)) {
this.identifierObfuscatingReplacer.storeLocalName(node.left.name, lexicalScopeNode);
if (NodeGuards.isAssignmentPatternNode(node) && NodeGuards.isIdentifierNode(node.left)) {
this.identifierObfuscatingReplacer.storeLocalName(node.left.name, lexicalScopeNode);

return estraverse.VisitorOption.Skip;
}
return estraverse.VisitorOption.Skip;
}

if (NodeGuards.isIdentifierNode(node)) {
this.identifierObfuscatingReplacer.storeLocalName(node.name, lexicalScopeNode);
}
}
});
});
if (NodeGuards.isIdentifierNode(node)) {
this.identifierObfuscatingReplacer.storeLocalName(node.name, lexicalScopeNode);
}
}
};

functionNode.params.forEach((paramsNode: ESTree.Node) => estraverse.traverse(paramsNode, visitor));
}

/**
Expand All @@ -157,19 +156,12 @@ export class FunctionTransformer extends AbstractNodeTransformer {
lexicalScopeNode: TNodeWithLexicalScope,
ignoredIdentifierNamesSet: Set <string> = new Set()
): void {
const replaceVisitor: estraverse.Visitor = {
const visitor: estraverse.Visitor = {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
/**
* Should skip function node itself
*/
if (node === functionNode) {
return;
}

/**
* Should process nested functions in different traverse loop to avoid wrong code generation
*/
if (NodeGuards.isFunctionNode(node)) {
if (NodeGuards.isFunctionNode(node) && node !== functionNode) {
this.replaceFunctionParams(node, lexicalScopeNode, new Set(ignoredIdentifierNamesSet));

return estraverse.VisitorOption.Skip;
Expand All @@ -187,6 +179,7 @@ export class FunctionTransformer extends AbstractNodeTransformer {
if (
parentNode
&& NodeGuards.isReplaceableIdentifierNode(node, parentNode)
&& !NodeMetadata.isRenamedIdentifier(node)
&& !ignoredIdentifierNamesSet.has(node.name)
) {
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
Expand All @@ -201,6 +194,6 @@ export class FunctionTransformer extends AbstractNodeTransformer {
}
};

estraverse.replace(functionNode, replaceVisitor)
estraverse.replace(functionNode, visitor)
}
}
8 changes: 5 additions & 3 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
`
(function(foo){
function foo () {

}

return new foo();
})();

`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
transformObjectKeys: true
transformObjectKeys: true,
seed: 1
}
).getObfuscatedCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,59 +46,107 @@ describe('FunctionTransformer', () => {
});

describe('function id name obfuscation', () => {
const functionExpressionParamIdentifierRegExp: RegExp = /\(function *\((_0x[a-f0-9]{4,6})\) *\{/;
const functionParamIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
const functionObjectIdentifierRegExp: RegExp = /return new (_0x[a-f0-9]{4,6}) *\(\);/;
describe('Variant #1', () => {
const functionExpressionParamIdentifierRegExp: RegExp = /\(function *\((_0x[a-f0-9]{4,6})\) *\{/;
const functionParamIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
const functionObjectIdentifierRegExp: RegExp = /return new (_0x[a-f0-9]{4,6}) *\(\);/;

let obfuscatedCode: string,
functionExpressionParamIdentifierName: string,
functionParamIdentifierName: string,
functionObjectIdentifierName: string;
let obfuscatedCode: string,
functionExpressionParamIdentifierName: string,
functionParamIdentifierName: string,
functionObjectIdentifierName: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name.js');
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name-1.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET
}
).getObfuscatedCode();
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET
}
).getObfuscatedCode();

const functionExpressionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionExpressionParamIdentifierRegExp);
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionParamIdentifierRegExp);
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionObjectIdentifierRegExp);
const functionExpressionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionExpressionParamIdentifierRegExp);
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionParamIdentifierRegExp);
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionObjectIdentifierRegExp);

functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
functionExpressionParamIdentifierName = (<RegExpMatchArray>functionExpressionParamIdentifierMatch)[1];
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
});
functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
functionExpressionParamIdentifierName = (<RegExpMatchArray>functionExpressionParamIdentifierMatch)[1];
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
});

it('should correctly transform function expression parameter identifier', () => {
assert.match(obfuscatedCode, functionExpressionParamIdentifierRegExp);
});
it('should correctly transform function expression parameter identifier', () => {
assert.match(obfuscatedCode, functionExpressionParamIdentifierRegExp);
});

it('should correctly transform function parameter identifier', () => {
assert.match(obfuscatedCode, functionParamIdentifierRegExp);
});
it('should correctly transform function parameter identifier', () => {
assert.match(obfuscatedCode, functionParamIdentifierRegExp);
});

it('should correctly transform function object parameter identifier', () => {
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
});
it('should correctly transform function object parameter identifier', () => {
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
});

it('should generate same names for function parameter and function object identifiers', () => {
assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
});
it('should generate same names for function parameter and function object identifiers', () => {
assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
});

it('should generate same names for function parameter identifiers', () => {
assert.equal(functionExpressionParamIdentifierName, functionParamIdentifierName);
});

it('should generate same names for function parameter identifiers', () => {
assert.equal(functionExpressionParamIdentifierName, functionParamIdentifierName);
it('should generate same names for function expression parameter and function object identifiers', () => {
assert.equal(functionExpressionParamIdentifierName, functionObjectIdentifierName);
});
});

it('should generate same names for function expression parameter and function object identifiers', () => {
assert.equal(functionExpressionParamIdentifierName, functionObjectIdentifierName);
describe('Variant #2', () => {
const functionIdentifiersRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\((_0x[a-f0-9]{4,6})\) *\{/;
const functionObjectIdentifierRegExp: RegExp = /return new (_0x[a-f0-9]{4,6}) *\(\);/;

let obfuscatedCode: string,
functionIdentifierName: string,
functionParamIdentifierName: string,
functionObjectIdentifierName: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name-2.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET
}
).getObfuscatedCode();

const functionIdentifiersMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionIdentifiersRegExp);
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionObjectIdentifierRegExp);

functionIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[1];
functionParamIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[2];
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
});

it('should correctly transform function identifiers', () => {
assert.match(obfuscatedCode, functionIdentifiersRegExp);
});

it('should correctly transform function object parameter identifier', () => {
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
});

it('should generate same names for function parameter and function object identifiers', () => {
assert.equal(functionIdentifierName, functionObjectIdentifierName);
});

it('should generate same names for function id and parameter identifiers', () => {
assert.equal(functionIdentifierName, functionParamIdentifierName);
});
});
});

Expand Down Expand Up @@ -356,27 +404,27 @@ describe('FunctionTransformer', () => {
assert.match(obfuscatedCode, functionBodyRegExp);
});

it('equal #1: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.notEqual(variableDeclarationIdentifierName, functionParameterIdentifierName);
it('equal #1: should keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.equal(variableDeclarationIdentifierName, functionParameterIdentifierName);
});

it('equal #2: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.notEqual(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName1);
});

it('equal #3: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.notEqual(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName2);
it('equal #3: should keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.equal(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName2);
});

it('should keep same names for identifier in first function parameter and default value identifier of second function parameter', () => {
it('equal #4: should keep same names for identifier in first function parameter and default value identifier of second function parameter', () => {
assert.equal(functionParameterIdentifierName, functionDefaultParameterIdentifierName2);
});

it('equal #1: should keep same names for identifiers in function params and function body', () => {
it('equal #5: should keep same names for identifiers in function params and function body', () => {
assert.equal(functionParameterIdentifierName, functionBodyIdentifierName1);
});

it('equal #2: should keep same names for identifiers in function params and function body', () => {
it('equal #6: should keep same names for identifiers in function params and function body', () => {
assert.equal(functionDefaultParameterIdentifierName1, functionBodyIdentifierName2);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function () {
function foo (foo) {}

return new foo();
})();
Loading