-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply Augmented Array Replacement to Func2Arr cases, Unwrappers and S…
…implifications (#56) * Fix issue where a number below zero would be replaced with a string * Improve failure messages * Improve context collection to include missing member expression objects and call expressions' callees * Create simplifyCalls to remove unnecessary usage of '.call(this' or '.apply(this' when calling a function * Create unwrappers for IIFEs and simple operations * Create resolveFunctionToArray module * Add tests for new modules * Adjust test results * Remove redundant code * Apply augmented array replacement for function2array cases * Add augmented function2array replacement test * Use assert.equal() instead of assert() * Add test for resolveFunctionToArray * 1.5.0
- Loading branch information
Showing
20 changed files
with
365 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Remove unnecessary usage of '.call(this' or '.apply(this' when calling a function | ||
* @param {Arborist} arb | ||
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list | ||
* @return {Arborist} | ||
*/ | ||
function simplifyCalls(arb, candidateFilter = () => true) { | ||
const candidates = arb.ast.filter(n => | ||
n.type === 'CallExpression' && | ||
n.arguments.length && | ||
n.arguments[0].type === 'ThisExpression' && | ||
n.callee.type === 'MemberExpression' && | ||
['apply', 'call'].includes(n.callee.property?.name || n.callee.property?.value) && | ||
(n.callee.object?.name || n.callee?.value) !== 'Function' && | ||
!/function/i.test(n.callee.object.type) && | ||
candidateFilter(n)); | ||
|
||
for (const c of candidates) { | ||
arb.markNode(c, { | ||
type: 'CallExpression', | ||
callee: c.callee.object, | ||
arguments: (c.callee.property?.name || c.callee.property?.value) === 'apply' ? c.arguments[1].elements : c.arguments.slice(1), | ||
}); | ||
} | ||
return arb; | ||
} | ||
|
||
module.exports = simplifyCalls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Replace IIFEs that are unwrapping a function with the unwraped function. | ||
* @param {Arborist} arb | ||
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list | ||
* @return {Arborist} | ||
*/ | ||
function unwrapIIFEs(arb, candidateFilter = () => true) { | ||
const candidates = arb.ast.filter(n => | ||
n.type === 'CallExpression' && | ||
!n.arguments.length && | ||
['ArrowFunctionExpression', 'FunctionExpression'].includes(n.callee.type) && | ||
!n.callee.id && | ||
( | ||
n.callee.body.type !== 'BlockStatement' || | ||
( | ||
n.callee.body.body.length === 1 && | ||
n.callee.body.body[0].type === 'ReturnStatement') | ||
) && | ||
n.parentKey === 'init' && | ||
candidateFilter(n)); | ||
|
||
for (const c of candidates) { | ||
const replacementNode = c.callee.body.type !== 'BlockStatement' ? c.callee.body : c.callee.body.body[0].argument; | ||
arb.markNode(c, replacementNode); | ||
} | ||
return arb; | ||
} | ||
|
||
module.exports = unwrapIIFEs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const operators = ['+', '-', '*', '/', '%', '&', '|', '&&', '||', '**', '^']; | ||
const fixes = ['!', '~', '-', '+', '--', '++']; | ||
|
||
/** | ||
* | ||
* @param {ASTNode} n | ||
* @return {boolean} | ||
*/ | ||
function matchBinaryOrLogical(n) { | ||
// noinspection JSUnresolvedVariable | ||
return ['LogicalExpression', 'BinaryExpression'].includes(n.type) && | ||
operators.includes(n.operator) && | ||
n.parentNode.type === 'ReturnStatement' && | ||
n.parentNode.parentNode?.body?.length === 1 && | ||
n.left?.declNode?.parentKey === 'params' && | ||
n.right?.declNode?.parentKey === 'params'; | ||
} | ||
|
||
/** | ||
* | ||
* @param {ASTNode} c | ||
* @param {Arborist} arb | ||
*/ | ||
function handleBinaryOrLogical(c, arb) { | ||
// noinspection JSUnresolvedVariable | ||
const refs = (c.scope.block?.id?.references || []).map(r => r.parentNode); | ||
for (const ref of refs) { | ||
if (ref.arguments.length === 2) arb.markNode(ref, { | ||
type: c.type, | ||
operator: c.operator, | ||
left: ref.arguments[0], | ||
right: ref.arguments[1], | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {ASTNode} n | ||
* @return {boolean} | ||
*/ | ||
function matchUnary(n) { | ||
// noinspection JSUnresolvedVariable | ||
return n.type === 'UnaryExpression' && | ||
fixes.includes(n.operator) && | ||
n.parentNode.type === 'ReturnStatement' && | ||
n.parentNode.parentNode?.body?.length === 1 && | ||
n.argument?.declNode?.parentKey === 'params'; | ||
} | ||
|
||
/** | ||
* | ||
* @param {ASTNode} c | ||
* @param {Arborist} arb | ||
*/ | ||
function handleUnary(c, arb) { | ||
// noinspection JSUnresolvedVariable | ||
const refs = (c.scope.block?.id?.references || []).map(r => r.parentNode); | ||
for (const ref of refs) { | ||
if (ref.arguments.length === 1) arb.markNode(ref, { | ||
type: c.type, | ||
operator: c.operator, | ||
prefix: c.prefix, | ||
argument: ref.arguments[0], | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Replace calls to functions that wrap simple operations with the actual operations | ||
* @param {Arborist} arb | ||
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list | ||
* @return {Arborist} | ||
*/ | ||
function unwrapSimpleOperations(arb, candidateFilter = () => true) { | ||
const candidates = arb.ast.filter(n => | ||
(matchBinaryOrLogical(n) || matchUnary(n)) && | ||
candidateFilter(n)); | ||
|
||
for (const c of candidates) { | ||
switch (c.type) { | ||
case 'BinaryExpression': | ||
case 'LogicalExpression': | ||
handleBinaryOrLogical(c, arb); | ||
break; | ||
case 'UnaryExpression': | ||
handleUnary(c, arb); | ||
break; | ||
} | ||
} | ||
return arb; | ||
} | ||
|
||
module.exports = unwrapSimpleOperations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Function To Array Replacements | ||
* The obfuscated script dynamically generates an array which is referenced throughout the script. | ||
*/ | ||
const evalInVm = require(__dirname + '/evalInVm'); | ||
const { | ||
createOrderedSrc, | ||
getDeclarationWithContext, | ||
} = require(__dirname + '/../utils'); | ||
const {badValue} = require(__dirname + '/../config'); | ||
|
||
/** | ||
* Run the generating function and replace it with the actual array. | ||
* Candidates are variables which are assigned a call expression, and every reference to them is a member expression. | ||
* E.g. | ||
* function getArr() {return ['One', 'Two', 'Three']}; | ||
* const a = getArr(); | ||
* console.log(`${a[0]} + ${a[1]} = ${a[2]}`); | ||
* @param {Arborist} arb | ||
* @return {Arborist} | ||
*/ | ||
function resolveFunctionToArray(arb) { | ||
// noinspection DuplicatedCode | ||
const candidates = arb.ast.filter(n => | ||
n.type === 'VariableDeclarator' && | ||
n.init?.type === 'CallExpression' && | ||
n.id?.references && | ||
!n.id.references.find(r => r.parentNode.type !== 'MemberExpression')); | ||
|
||
for (const c of candidates) { | ||
const targetNode = c.init.callee?.declNode?.parentNode || c.init; | ||
const src = createOrderedSrc(getDeclarationWithContext(targetNode).concat(c.init)); | ||
const newNode = evalInVm(src); | ||
if (newNode !== badValue) { | ||
arb.markNode(c.init, newNode); | ||
} | ||
} | ||
return arb; | ||
} | ||
|
||
module.exports = resolveFunctionToArray; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.