summaryrefslogtreecommitdiff
path: root/tests/analysis/scan/grammar.py
blob: 3a8196aa5eddc8363e13dafcd3a12d0034df2f44 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

import json

from common import RostTestClass
from pychrysalide.analysis.contents import MemoryContent


class TestRostGrammar(RostTestClass):
    """TestCases for the ROST grammar."""

    def testRelationalExpressions(self):
        """Build expressions with relational comparisons."""

        cases = [

            # Regular
            [ '-1', '<=', '2', True ],
            [ '-1', '<=', '2', True ],
            [ '"aaa"', '==', '"aaa"', True ],
            [ '"aaa"', '<', '"aaaa"', True ],
            [ '""', '<', '"aaaa"', True ],

            # Cast
            [ 'false', '==', '0', True ],
            [ 'false', '==', '1', False ],
            [ 'true', '!=', '0', True ],
            [ '1', '==', 'true', True ],
            [ 'false', '==', '()', True ],
            [ 'true', '==', '(0,)', True ],

        ]

        for op1, kwd, op2, expected in cases:

            rule = '''
rule test {

   condition:
      %s %s %s

}
''' % (op1, kwd, op2)

            if expected:
                self.check_rule_success(rule)
            else:
                self.check_rule_failure(rule)


    def testLogicalOperations(self):
        """Evaluate some logical operations."""

        cases = [
            [ 'true and false', False ],
            [ 'false or false', False ],
            [ 'true and true or false', True ],
            [ 'false or true and false', False ],
            [ '1 or false', True ],
        ]

        for cond, expected in cases:

            rule = '''
rule test {

   condition:
      %s

}
''' % (cond)

            if expected:
                self.check_rule_success(rule)
            else:
                self.check_rule_failure(rule)


    def testArithmeticOperations(self):
        """Evaluate some arithmetic operations."""

        cases = [

            # Clever
            '1 + 2 == 3',
            '10 + -3 == 7',
            '-3 + 10 == 7',
            '-10 - 1 < 0',
            '-10 - 1 == -11',
            '(-10 - 1) == -11',
            '(-1 - -10) == 9',
            '-2 * -3 == 6',
            '-2 * 3 == -6',

            # Legacy
            '1 + 4 * 3 + 2 == 15',
            '(1 + 4) * 3 + 2 == 17',
            '1 + 4 * (3 + 2) == 21',
            '(1 + 4) * (3 + 2) == 25',

        ]

        for c in cases:

            rule = '''
rule test {

   condition:
      %s

}
''' % (c)

            self.check_rule_success(rule)


    def testBasicStringsOperations(self):
        """Build expressions with basic strings operations."""

        cases = [

            # Clever
            [ '123---456', 'contains', '---', True ],
            [ '123---456', 'contains', 'xxx', False ],
            [ '---123---456', 'startswith', '---', True ],
            [ '---123---456', 'startswith', 'xxx', False ],
            [ '123---456---', 'endswith', '---', True ],
            [ '123---456---', 'endswith', 'xxx', False ],
            [ 'AAA---BBB', 'icontains', 'aaa', True ],
            [ 'AAA---BBB', 'icontains', 'xxx', False ],
            [ 'AAA---BBB', 'istartswith', 'aAa', True ],
            [ 'AAA---BBB', 'istartswith', 'xxx', False ],
            [ 'AAA---BBB', 'iendswith', 'bBb', True ],
            [ 'AAA---BBB', 'iendswith', 'xxx', False ],
            [ 'AzertY', 'iequals', 'AZERTY', True ],
            [ 'AzertY', 'iequals', 'AZERTY-', False ],

            # Legacy
            [ '123\t456', 'contains', '\t', True ],
            [ '123-456', 'startswith', '1', True ],
            [ '123-456', 'startswith', '1234', False ],
            [ '123-456', 'endswith', '6', True ],
            [ '123-456', 'endswith', '3456', False ],

        ]

        for op1, kwd, op2, expected in cases:

            rule = '''
rule test {

   condition:
      "%s" %s "%s"

}
''' % (op1, kwd, op2)

            if expected:
                self.check_rule_success(rule)
            else:
                self.check_rule_failure(rule)


    def testSizeUnits(self):
        """Evaluate size units."""

        cases = [
            '1KB == 1024',
            '2MB == 2 * 1024 * 1024',
            '4Kb == (4 * 1024)',
            '1KB <= 1024 and 1024 < 1MB',
        ]

        for c in cases:

            rule = '''
rule test {

   condition:
      %s

}
''' % (c)

            self.check_rule_success(rule)


    def testPrivateRules(self):
        """Ensure private rules remain silent."""

        for private in [ True, False ]:
            for state in [ True, False ]:

                rule = '''
%srule silent {

   condition:
      %s

}

rule test {

   condition:
      silent

}
''' % ('private ' if private else '', 'true' if state else 'false')

                scanner, ctx = self._validate_rule_result(rule, self._empty_content, state)

                data = scanner.convert_to_json(ctx)
                jdata = json.loads(data)

                # Exemple :
                #
                # [{'bytes_patterns': [], 'matched': True, 'name': 'test'},
                #  {'bytes_patterns': [], 'matched': True, 'name': 'silent'}]

                found = len([ j['name'] for j in jdata if j['name'] == 'silent' ]) > 0

                self.assertTrue(private ^ found)


    def testGlobalRules(self):
        """Take global rules into account."""

        for glob_state in [ True, False ]:
            for state in [ True, False ]:

                rule = '''
%srule silent {

   condition:
      %s

}

rule test {

   condition:
      true

}
''' % ('global ' if glob_state else '', 'true' if state else 'false')

                expected = not(glob_state) or state

                if expected:
                    self.check_rule_success(rule)
                else:
                    self.check_rule_failure(rule)


    def testMatchCount(self):
        """Ensure match count provides expected values."""

        cnt = MemoryContent(b'\x01\x02\x02\x03\x03\x03')

        rule = '''
rule test {

    bytes:
        $int_01 = "\x01"
        $int_02 = "\x02"
        $int_03 = "\x03"

    condition:
        #int_01 == count($int_01) and #int_01 == 1
        and #int_02 == count($int_02) and #int_02 == 2
        and #int_03 == count($int_03) and #int_03 == 3
        and #int_0* == count($int_0*) and #int_0* == 6

}
'''

        self.check_rule_success(rule, cnt)


# TODO : test     <haystack> matches <regex>