-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve wrapped functions handling (#73)
* Add replaceNewFuncCallsWithLiteralContent with tests * Expand unwrapIIFEs to include unwrapping of IIFEs with multiple statements or expressions. Add a relevant test case. * 1.7.0 * Skip cases where IIFE is used to initialize or set a value. Add TN tests * Add obfuscated sample for new Function and IIFEs unwrapping
- Loading branch information
1 parent
f503dd1
commit 2515f3e
Showing
10 changed files
with
312 additions
and
9 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,63 @@ | ||
const {generateFlatAST} = require('flast'); | ||
const logger = require(__dirname + '/../utils/logger'); | ||
const getCache = require(__dirname + '/../utils/getCache'); | ||
const generateHash = require(__dirname + '/../utils/generateHash'); | ||
|
||
/** | ||
* Extract string values of eval call expressions, and replace calls with the actual code, without running it through eval. | ||
* E.g. | ||
* new Function('!function() {console.log("hello world")}()')(); | ||
* will be replaced with | ||
* !function () {console.log("hello world")}(); | ||
* @param {Arborist} arb | ||
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list | ||
* @return {Arborist} | ||
*/ | ||
function replaceNewFuncCallsWithLiteralContent(arb, candidateFilter = () => true) { | ||
const cache = getCache(arb.ast[0].scriptHash); | ||
const candidates = arb.ast.filter(n => | ||
n.type === 'NewExpression' && | ||
n.parentKey === 'callee' && | ||
n.parentNode?.arguments?.length === 0 && | ||
n.callee?.name === 'Function' && | ||
n.arguments?.length === 1 && | ||
n.arguments[0].type === 'Literal' && | ||
candidateFilter(n)); | ||
|
||
for (const c of candidates) { | ||
const targetCodeStr = c.arguments[0].value; | ||
const cacheName = `replaceEval-${generateHash(targetCodeStr)}`; | ||
try { | ||
if (!cache[cacheName]) { | ||
let body; | ||
if (targetCodeStr) { | ||
body = generateFlatAST(targetCodeStr, {detailed: false})[0].body; | ||
if (body.length > 1) { | ||
body = { | ||
type: 'BlockStatement', | ||
body, | ||
}; | ||
} else { | ||
body = body[0]; | ||
if (body.type === 'ExpressionStatement') body = body.expression; | ||
} | ||
} else body = { | ||
type: 'Literal', | ||
value: targetCodeStr, | ||
}; | ||
cache[cacheName] = body; | ||
} | ||
let replacementNode = cache[cacheName]; | ||
let targetNode = c.parentNode; | ||
if (targetNode.parentNode.type === 'ExpressionStatement' && replacementNode.type === 'BlockStatement') { | ||
targetNode = targetNode.parentNode; | ||
} | ||
arb.markNode(targetNode, replacementNode); | ||
} catch (e) { | ||
logger.debug(`[-] Unable to replace new function's body with call expression: ${e}`); | ||
} | ||
} | ||
return arb; | ||
} | ||
|
||
module.exports = replaceNewFuncCallsWithLiteralContent; |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.