-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrayObfuscation.js
80 lines (73 loc) · 2.33 KB
/
arrayObfuscation.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
const estraverse = require('estraverse')
const getArrayDeclaration = require('./util/getArrayDeclaration')
const getArrayResolvingFunction = require('./util/getArrayResolvingFunction')
const getRotateCount = require('./util/getRotateCount')
const Literal = require('./util/Literal')
module.exports = parsed => {
const array = getArrayDeclaration(parsed.body)
if (!array) return parsed
const func = getArrayResolvingFunction(parsed.body, array.name)
if (func) {
const rotateCount = getRotateCount(parsed.body, array.name)
rotate(array.array, rotateCount)
parsed = estraverse.replace(parsed, {
enter: node => {
// resolve all the calls to the function to literals
if (node.type === 'CallExpression' && node.callee.name === func.name) {
const index = node.arguments[0].value - 0
return Literal(array.array[index])
}
// remove the resolving function
if (node.type === 'VariableDeclaration') {
const { name } = node.declarations[0].id
if (name === func.name) {
return estraverse.VisitorOption.Remove
}
}
// remove the array rotation
if (node.type === 'ExpressionStatement') {
const expr = node.expression
if (
expr.type === 'CallExpression' &&
expr.arguments.length === 2 &&
expr.arguments[0].type === 'Identifier' &&
expr.arguments[0].name === array.name &&
expr.arguments[1].type === 'Literal'
) {
return estraverse.VisitorOption.Remove
}
}
}
})
} else {
// resolve all the references to the array
parsed = estraverse.replace(parsed, {
enter: node => {
if (
node.type === 'MemberExpression' &&
node.computed &&
node.object.name === array.name
) {
return Literal(array.array[node.property.value])
}
}
})
}
// remove the array
parsed = estraverse.replace(parsed, {
enter: node => {
if (node.type === 'VariableDeclaration') {
const { name } = node.declarations[0].id
if (name === array.name) {
return estraverse.VisitorOption.Remove
}
}
}
})
return parsed
}
function rotate (array, count) {
while (count--) {
array.push(array.shift())
}
}