-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_core.py
180 lines (156 loc) · 6.71 KB
/
test_core.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from unittest import TestCase
from slimit.parser import Parser
from jsard.core import deobfuscate, scan_obfuscation_array
# noinspection PyPep8Naming
class Test_deobfuscate(TestCase):
def test_expression(self):
"""Obfuscation with parent node `ExprStatement`."""
obfuscated = 'var _0x01ff=["foo"];_0x01ff[0];'
expected = (
'var _0x01ff = ["foo"];\n'
'"foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_var_declaration(self):
"""Obfuscation with parent node `VarDecl`."""
obfuscated = 'var _0x01ff=["foo"];var a=_0x01ff[0];'
expected = (
'var _0x01ff = ["foo"];\n'
'var a = "foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_assignment(self):
"""Obfuscation with parent node `Assign`."""
obfuscated = 'var _0x01ff=["foo"];var a;a=_0x01ff[0];'
expected = (
'var _0x01ff = ["foo"];\n'
'var a;\n'
'a = "foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_array(self):
"""Obfuscation with parent node `Array`."""
obfuscated = 'var _0x01ff=["foo"];var a=[9,_0x01ff[0],7];'
expected = (
'var _0x01ff = ["foo"];\n'
'var a = [9,"foo",7];')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_array_access(self):
"""Obfuscation with parent node `BracketAccessor`."""
obfuscated = 'var _0x01ff=["foo", 0, 1];var a=_0x01ff[_0x01ff[_0x01ff[2]]];'
expected = (
'var _0x01ff = ["foo",0,1];\n'
'var a = "foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_conditional(self):
"""Obfuscation with parent node `Conditional`."""
obfuscated = 'var _0x01ff=["foo"];_0x01ff[0]?true:false;'
expected = (
'var _0x01ff = ["foo"];\n'
'"foo" ? true : false;')
self.assertEqual(expected, deobfuscate(obfuscated))
obfuscated = 'var _0x01ff=["foo"];true?_0x01ff[0]:false;'
expected = (
'var _0x01ff = ["foo"];\n'
'true ? "foo" : false;')
self.assertEqual(expected, deobfuscate(obfuscated))
obfuscated = 'var _0x01ff=["foo"];true?true:_0x01ff[0];'
expected = (
'var _0x01ff = ["foo"];\n'
'true ? true : "foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_return(self):
"""Obfuscation with parent node `Return`."""
obfuscated = 'var _0x01ff=["foo"];function f(){return _0x01ff[0]}'
expected = (
'var _0x01ff = ["foo"];\n'
'function f() {\n'
' return "foo";\n'
'}')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_binary_operation(self):
"""Obfuscation with parent node `BinOp`."""
obfuscated = 'var _0x01ff=["foo"];_0x01ff[0]+_0x01ff[0];'
expected = (
'var _0x01ff = ["foo"];\n'
'"foo" + "foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_binary_operation_multiple(self):
"""Obfuscation with parent node `BinOp`."""
obfuscated = 'var _0x01ff=["foo"];_0x01ff[0]+_0x01ff[0]+_0x01ff[0]+_0x01ff[0];'
expected = (
'var _0x01ff = ["foo"];\n'
'"foo" + "foo" + "foo" + "foo";')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_switch(self):
"""Obfuscation with parent node `Switch`."""
obfuscated = 'var _0x01ff=["foo"];switch(_0x01ff[0]){case 1:1;break;default:3;break;}'
expected = (
'var _0x01ff = ["foo"];\n'
'switch ("foo") {\n'
' case 1:\n'
' 1;\n'
' break;\n'
' default:\n'
' 3;\n'
' break;\n'
'}')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_case(self):
"""Obfuscation with parent node `Case`."""
obfuscated = 'var _0x01ff=["foo"];switch(0){case _0x01ff[0]:1;break;default:3;break;}'
expected = (
'var _0x01ff = ["foo"];\n'
'switch (0) {\n'
' case "foo":\n'
' 1;\n'
' break;\n'
' default:\n'
' 3;\n'
' break;\n'
'}')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_function_call(self):
"""Obfuscation with parent node `FunctionCall`."""
obfuscated = 'var _0x01ff=["foo"];function f(a){return a}f(_0x01ff[0]);'
expected = (
'var _0x01ff = ["foo"];\n'
'function f(a) {\n'
' return a;\n'
'}\n'
'f("foo");')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_new_expression(self):
"""Obfuscation with parent node `NewExpr`."""
obfuscated = 'var _0x01ff=["foo"];new C(_0x01ff[0]);'
expected = (
'var _0x01ff = ["foo"];\n'
'new C("foo");')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_list(self):
"""Obfuscation with parent node `list`,e.g., list of parameters."""
obfuscated = 'var _0x01ff=["foo"];new C(9,8,_0x01ff[0]);'
expected = (
'var _0x01ff = ["foo"];\n'
'new C(9, 8, "foo");')
self.assertEqual(expected, deobfuscate(obfuscated))
def test_context_awareness(self):
"""Test for different contexts."""
obfuscated = 'var _0x01ff=[9,"context aware \\"_0x01ff[0]\\""];var a=_0x01ff[0];a=_0x01ff[1];'
expected = (
'var _0x01ff = [9,"context aware \\"_0x01ff[0]\\""];\n' +
'var a = 9;\n' +
'a = "context aware \\"_0x01ff[0]\\"";')
self.assertEqual(expected, deobfuscate(obfuscated))
# noinspection PyPep8Naming
class Test_scan_obfuscation_array(TestCase):
def test_single_array(self):
tree = Parser().parse('var _0x01ff=["text"];')
obfuscation_array_name, obfuscation_array = scan_obfuscation_array(tree)
self.assertEqual('_0x01ff', obfuscation_array_name)
def test_multiple_arrays(self):
tree = Parser().parse('var _a=["text a"];var _b=["text b"];var _c=["text c"];')
obfuscation_array_name, obfuscation_array = scan_obfuscation_array(tree)
self.assertEqual('_a', obfuscation_array_name)
def test_mixed_w_vars(self):
"""Obfuscation array mixed with other global variables."""
tree = Parser().parse('var x=1;var _y={"z":2};var _a=["text a"];var _b=["text b"];var _c=["text c"];')
obfuscation_array_name, obfuscation_array = scan_obfuscation_array(tree)
self.assertEqual('_a', obfuscation_array_name)