Skip to content

Commit addaf18

Browse files
authored
Add support for augmented proxied array function replacements (#3)
* Add detector for augmented proxied array function replacements * Add sample for testing * Refactor imports do be dynamic with less repetition; Add new augmentedProxiedArrayFunctionReplacements detector; * Update dependencies * 1.1.0
1 parent 86f8401 commit addaf18

File tree

5 files changed

+12034
-40
lines changed

5 files changed

+12034
-40
lines changed

package-lock.json

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obfuscation-detector",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "Javascript obfuscation detector",
55
"main": "src/index.js",
66
"directories": {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const obfuscationName = 'augmented_proxied_array_function_replacements';
2+
3+
/**
4+
* Augmented Proxied Array-Function Replacements obfuscation type has the following characteristics:
5+
* - Has at least 3 root nodes - the last one containing the actual obfuscated code and the rest are obfuscation code.
6+
* - Has a function that assigns an array full of strings to itself, and then returns itself.
7+
* - Has an anonymous IIFE that is called with the array function as one of its arguments.
8+
* @param {ASTNode[]} flatTree
9+
* @return {string} The obfuscation name if detected; empty string otherwise.
10+
*/
11+
function detectAugmentedProxiedArrayFunctionReplacements(flatTree) {
12+
const roots = flatTree.filter(n => n.parentNode?.type === 'Program');
13+
if (roots.length > 3) {
14+
const arrFunc = roots.find(n => n.type === 'FunctionDeclaration' &&
15+
n.body?.body?.length &&
16+
n.body.body.slice(-1)[0]?.argument?.callee?.name === n?.id?.name);
17+
if (arrFunc) {
18+
const augFunc = roots.find(n => n.type === 'ExpressionStatement' &&
19+
n.expression.type === 'CallExpression' &&
20+
n.expression.arguments.find(a => a?.name === arrFunc.id.name));
21+
if (augFunc) {
22+
return obfuscationName;
23+
}
24+
}
25+
}
26+
return '';
27+
}
28+
29+
try {
30+
module.exports = detectAugmentedProxiedArrayFunctionReplacements;
31+
} catch {}

src/index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const {generateFlatAST} = require('flast');
2-
const detectCaesarPlus = require(__dirname + '/detectors/caesarp');
3-
const detectObfuscatorIo = require(__dirname + '/detectors/obfuscator-io');
4-
const detectArrayReplacements = require(__dirname + '/detectors/arrayReplacements');
5-
const detectArrayFunctionReplacements = require(__dirname + '/detectors/arrayFunctionReplacements');
6-
const detectAugmentedArrayReplacements = require(__dirname + '/detectors/augmentedArrayReplacements');
7-
const detectFunctionToArrayReplacemets = require(__dirname + '/detectors/functionToArrayReplacements');
8-
const detectAugmentedArrayFunctionReplacements = require(__dirname + '/detectors/augmentedArrayFunctionReplacements');
2+
3+
const availableDetectors = [];
4+
// Dynamically import available detectors
5+
[
6+
'arrayReplacements',
7+
'functionToArrayReplacements',
8+
'augmentedArrayReplacements',
9+
'arrayFunctionReplacements',
10+
'augmentedArrayFunctionReplacements',
11+
'obfuscator-io',
12+
'caesarp',
13+
'augmentedProxiedArrayFunctionReplacements',
14+
].forEach(detName => availableDetectors.push(require(__dirname + `/detectors/${detName}`)));
915

1016
/**
1117
* @param {string} code
@@ -15,18 +21,9 @@ const detectAugmentedArrayFunctionReplacements = require(__dirname + '/detectors
1521
*/
1622
function detectObfuscation(code, stopAfterFirst = true) {
1723
const detectedObfuscations = [];
18-
const detectors = [
19-
detectArrayReplacements,
20-
detectFunctionToArrayReplacemets,
21-
detectAugmentedArrayReplacements,
22-
detectArrayFunctionReplacements,
23-
detectAugmentedArrayFunctionReplacements,
24-
detectObfuscatorIo,
25-
detectCaesarPlus,
26-
];
2724
try {
2825
const tree = generateFlatAST(code);
29-
for (const detection of detectors) {
26+
for (const detection of availableDetectors) {
3027
try {
3128
const detectionType = detection(tree, detectedObfuscations);
3229
if (detectionType) detectedObfuscations.push(detectionType);

0 commit comments

Comments
 (0)