-
Notifications
You must be signed in to change notification settings - Fork 39
/
resolveAugmentedFunctionWrappedArrayReplacements.js
82 lines (81 loc) · 3.23 KB
/
resolveAugmentedFunctionWrappedArrayReplacements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {badValue} from '../config.js';
import {Sandbox} from '../utils/sandbox.js';
import {evalInVm} from '../utils/evalInVm.js';
import {getDescendants} from '../utils/getDescendants.js';
import {doesDescendantMatchCondition} from '../utils/doesDescendantMatchCondition.js';
/**
* A special case of function array replacement where the function is wrapped in another function, the array is
* sometimes wrapped in its own function, and is also augmented.
* TODO: Add example code
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
export default function resolveAugmentedFunctionWrappedArrayReplacements(arb, candidateFilter = () => true) {
const relevantNodes = [
...(arb.ast[0].typeMap.FunctionDeclaration || []),
];
for (let i = 0; i < relevantNodes.length; i++) {
const n = relevantNodes[i];
if (n.id &&
doesDescendantMatchCondition(n, d =>
d.type === 'AssignmentExpression' &&
d.left?.name === n.id?.name) &&
candidateFilter(n)) {
const descendants = getDescendants(n);
const arrDecryptor = n;
const arrCandidates = [];
for (let q = 0; q < descendants.length; q++) {
const c = descendants[q];
if (c.type === 'MemberExpression' && c.object.type === 'Identifier') arrCandidates.push(c.object);
}
for (let j = 0; j < arrCandidates.length; j++) {
const ac = arrCandidates[j];
// If a direct reference to a global variable pointing at an array
let arrRef;
if (!ac.declNode) continue;
if (ac.declNode.scope.type === 'global') {
if (ac.declNode.parentNode?.init?.type === 'ArrayExpression') {
arrRef = ac.declNode.parentNode?.parentNode || ac.declNode.parentNode;
}
} else if (ac.declNode.parentNode?.init?.type === 'CallExpression') {
arrRef = ac.declNode.parentNode.init.callee?.declNode?.parentNode;
}
if (arrRef) {
const expressionStatements = arb.ast[0].typeMap.ExpressionStatement || [];
for (let k = 0; k < expressionStatements.length; k++) {
const exp = expressionStatements[k];
if (exp.expression.type === 'CallExpression' &&
exp.expression.callee.type === 'FunctionExpression' &&
exp.expression.arguments.length &&
exp.expression.arguments[0].type === 'Identifier' &&
exp.expression.arguments[0].declNode === ac.declNode) {
const context = [arrRef.src, arrDecryptor.src, exp.src].join('\n');
const skipScopes = [arrRef.scope, arrDecryptor.scope, exp.expression.callee.scope];
const callExpressions = arb.ast[0].typeMap.CallExpression || [];
const replacementCandidates = [];
for (let r = 0; r < callExpressions.length; r++) {
const c = callExpressions[r];
if (c.callee?.name === arrDecryptor.id.name &&
!skipScopes.includes(c.scope)) {
replacementCandidates.push(c);
}
}
const sb = new Sandbox();
sb.run(context);
for (let p = 0; p < replacementCandidates.length; p++) {
const rc = replacementCandidates[p];
const replacementNode = evalInVm(`\n${rc.src}`, sb);
if (replacementNode !== badValue) {
arb.markNode(rc, replacementNode);
}
}
break;
}
}
}
}
}
}
return arb;
}