blob: 4a29ef83db686f02ff8034a8b824a3988e5cf98f (
plain)
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
|
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-
import locale
from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import MemoryContent
from pychrysalide.analysis.scan import ContentScanner
from pychrysalide.analysis.scan import ScanOptions
from pychrysalide.analysis.scan.patterns.backends import AcismBackend
from pychrysalide import core
from pychrysalide.plugins.kaitai.parsers import KaitaiStruct
from pychrysalide.plugins.kaitai.rost import KaitaiTrigger
class TestScansWithKaitai(ChrysalideTestCase):
"""TestCase for ROST scan with the KaitaiStruct parsing."""
@classmethod
def setUpClass(cls):
super(TestScansWithKaitai, cls).setUpClass()
cls._options = ScanOptions()
cls._options.backend_for_data = AcismBackend
def testSimpleKaitaiDefinitionForScanning(self):
"""Rely on basic Kaitai simple definition for scanning."""
definitions = '''
meta:
id: basic_test
seq:
- id: field0
type: u1
'''
kstruct = KaitaiStruct(definitions)
trigger = KaitaiTrigger(kstruct)
root_ns = core.get_rost_root_namespace()
ns = root_ns.resolve('kaitai')
ns.register_item(trigger)
ns = ns.resolve('basic_test')
self.assertEqual(ns, trigger)
cnt = MemoryContent(b'\x01\x02\x03')
rule = '''
rule testing {
condition:
kaitai.basic_test.field0 == 1
}
'''
scanner = ContentScanner(rule)
ctx = scanner.analyze(self._options, cnt)
self.assertIsNotNone(ctx)
self.assertFalse(ctx.has_match_for_rule('no_such_rule'))
self.assertTrue(ctx.has_match_for_rule('testing'))
definitions = '''
meta:
id: other_basic_test
seq:
- id: field0
type: u1
- id: field1
type: u1
'''
kstruct = KaitaiStruct(definitions)
trigger = KaitaiTrigger(kstruct)
root_ns = core.get_rost_root_namespace()
ns = root_ns.resolve('kaitai')
ns.register_item(trigger)
ns = ns.resolve('other_basic_test')
self.assertEqual(ns, trigger)
cnt = MemoryContent(b'\x01\x02\x03')
rule = '''
rule testing {
condition:
kaitai.other_basic_test.field0 == 1 and kaitai.other_basic_test.field1 == 2
}
'''
scanner = ContentScanner(rule)
ctx = scanner.analyze(self._options, cnt)
self.assertIsNotNone(ctx)
self.assertTrue(ctx.has_match_for_rule('testing'))
rule = '''
rule testing {
condition:
kaitai.other_basic_test.field0 == 1 and kaitai.other_basic_testXXXX.field1 == 2
}
'''
scanner = ContentScanner(rule)
ctx = scanner.analyze(self._options, cnt)
self.assertIsNotNone(ctx)
self.assertFalse(ctx.has_match_for_rule('testing'))
def testKaitaiDefinitionWithListForScanning(self):
"""Access list items from Kaitai definition when scanning with ROST."""
definitions = '''
meta:
id: test_with_list
seq:
- id: field0
type: u1
repeat: eos
'''
kstruct = KaitaiStruct(definitions)
trigger = KaitaiTrigger(kstruct)
root_ns = core.get_rost_root_namespace()
ns = root_ns.resolve('kaitai')
ns.register_item(trigger)
ns = ns.resolve('test_with_list')
self.assertEqual(ns, trigger)
cnt = MemoryContent(b'\x01\x02\x03')
rule = '''
rule testing {
condition:
kaitai.test_with_list.field0[0] == 1 and kaitai.test_with_list.field0[1] == 2 and kaitai.test_with_list.field0[2] == 3
}
'''
scanner = ContentScanner(rule)
ctx = scanner.analyze(self._options, cnt)
self.assertIsNotNone(ctx)
self.assertTrue(ctx.has_match_for_rule('testing'))
|