forked from SigmaHQ/sigma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend_sql.py
More file actions
334 lines (271 loc) · 14.6 KB
/
test_backend_sql.py
File metadata and controls
334 lines (271 loc) · 14.6 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Test output backends for sigmac
# Copyright 2020 Jonas Hagg
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from unittest.mock import patch
from sigma.backends.sql import SQLBackend
from sigma.parser.collection import SigmaCollectionParser
from sigma.config.mapping import FieldMapping
from sigma.configuration import SigmaConfiguration
class TestGenerateQuery(unittest.TestCase):
def setUp(self):
self.basic_rule = {"title": "Test", "level": "testing"}
self.table = "eventlog"
def test_regular_queries(self):
# Test regular queries
detection = {"selection": {"fieldname": "test1"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname = "test1"'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": 4}, "condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname = "4"'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": [
"test1", "test2"]}, "condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname IN ("test1", "test2")'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {
"fieldname": [3, 4]}, "condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname IN ("3", "4")'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname1": "test1", "fieldname2": [
"test2", "test3"]}, "condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE (fieldname1 = "test1" AND fieldname2 IN ("test2", "test3"))'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": "test1"}, "filter": {
"fieldname2": "whatever"}, "condition": "selection and filter"}
expected_result = 'SELECT * FROM {} WHERE (fieldname = "test1" AND fieldname2 = "whatever")'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": "test1"}, "filter": {
"fieldname2": "whatever"}, "condition": "selection or filter"}
expected_result = 'SELECT * FROM {} WHERE (fieldname = "test1" OR fieldname2 = "whatever")'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": "test1"}, "filter": {
"fieldname2": "whatever"}, "condition": "selection and not filter"}
expected_result = 'SELECT * FROM {} WHERE (fieldname = "test1" AND NOT (fieldname2 = "whatever"))'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname1": "test1"}, "filter": {
"fieldname2": "test2"}, "condition": "1 of them"}
expected_result = 'SELECT * FROM {} WHERE (fieldname1 = "test1" OR fieldname2 = "test2")'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname1": "test1"}, "filter": {
"fieldname2": "test2"}, "condition": "all of them"}
expected_result = 'SELECT * FROM {} WHERE (fieldname1 = "test1" AND fieldname2 = "test2")'.format(
self.table)
self.validate(detection, expected_result)
def test_modifiers(self):
# contains
detection = {"selection": {"fieldname|contains": "test"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE "%test%" ESCAPE \'\\\''.format(
self.table)
self.validate(detection, expected_result)
# all
detection = {"selection": {"fieldname|all": [
"test1", "test2"]}, "condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE (fieldname = "test1" AND fieldname = "test2")'.format(
self.table)
self.validate(detection, expected_result)
# endswith
detection = {"selection": {"fieldname|endswith": "test"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE "%test" ESCAPE \'\\\''.format(
self.table)
self.validate(detection, expected_result)
# startswith
detection = {"selection": {"fieldname|startswith": "test"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE "test%" ESCAPE \'\\\''.format(
self.table)
self.validate(detection, expected_result)
def test_aggregations(self):
# count
detection = {"selection": {"fieldname": "test"},
"condition": "selection | count() > 5"}
inner_query = 'SELECT *,count(*) AS agg FROM {} WHERE fieldname = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
# min
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | min(fieldname2) > 5"}
inner_query = 'SELECT *,min(fieldname2) AS agg FROM {} WHERE fieldname1 = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
# max
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | max(fieldname2) > 5"}
inner_query = 'SELECT *,max(fieldname2) AS agg FROM {} WHERE fieldname1 = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
# avg
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | avg(fieldname2) > 5"}
inner_query = 'SELECT *,avg(fieldname2) AS agg FROM {} WHERE fieldname1 = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
# sum
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | sum(fieldname2) > 5"}
inner_query = 'SELECT *,sum(fieldname2) AS agg FROM {} WHERE fieldname1 = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
# <
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | sum(fieldname2) < 5"}
inner_query = 'SELECT *,sum(fieldname2) AS agg FROM {} WHERE fieldname1 = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg < 5'.format(inner_query)
self.validate(detection, expected_result)
# ==
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | sum(fieldname2) == 5"}
inner_query = 'SELECT *,sum(fieldname2) AS agg FROM {} WHERE fieldname1 = "test"'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg == 5'.format(inner_query)
self.validate(detection, expected_result)
# group by
detection = {"selection": {"fieldname1": "test"},
"condition": "selection | sum(fieldname2) by fieldname3 == 5"}
inner_query = 'SELECT *,sum(fieldname2) AS agg FROM {} WHERE fieldname1 = "test" GROUP BY fieldname3'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg == 5'.format(inner_query)
self.validate(detection, expected_result)
# multiple conditions
detection = {"selection": {"fieldname1": "test"}, "filter": {
"fieldname2": "tessst"}, "condition": "selection OR filter | sum(fieldname2) == 5"}
inner_query = 'SELECT *,sum(fieldname2) AS agg FROM {} WHERE (fieldname1 = "test" OR fieldname2 = "tessst")'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg == 5'.format(inner_query)
self.validate(detection, expected_result)
def test_wildcards(self):
# wildcard: *
detection = {"selection": {"fieldname": "test*"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test%"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
# wildcard: ?
detection = {"selection": {"fieldname": "test?"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test_"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
# escaping:
detection = {"selection": {"fieldname": r"test\?"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\?"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": r"test\\*"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\\%"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": r"test\*"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\*"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": r"test\\"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\\"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": r"test\abc"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\\abc"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": r"test%"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\%"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
detection = {"selection": {"fieldname": r"test_"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE fieldname LIKE '.format(
self.table) + r'"test\_"' + r" ESCAPE '\'"
self.validate(detection, expected_result)
# multiple options
detection = {"selection": {"fieldname": [
"test*", "*test"]}, "condition": "selection"}
opt1 = 'fieldname LIKE ' + r'"test%"' + r" ESCAPE '\'"
opt2 = 'fieldname LIKE ' + r'"%test"' + r" ESCAPE '\'"
expected_result = 'SELECT * FROM {} WHERE ({} OR {})'.format(
self.table, opt1, opt2)
self.validate(detection, expected_result)
detection = {"selection": {"fieldname|all": [
"test*", "*test"]}, "condition": "selection"}
opt1 = 'fieldname LIKE ' + r'"test%"' + r" ESCAPE '\'"
opt2 = 'fieldname LIKE ' + r'"%test"' + r" ESCAPE '\'"
expected_result = 'SELECT * FROM {} WHERE ({} AND {})'.format(
self.table, opt1, opt2)
self.validate(detection, expected_result)
def test_fieldname_mapping(self):
detection = {"selection": {"fieldname": "test1"},
"condition": "selection"}
expected_result = 'SELECT * FROM {} WHERE mapped_fieldname = "test1"'.format(
self.table)
# configure mapping
config = SigmaConfiguration()
config.fieldmappings["fieldname"] = FieldMapping(
"fieldname", "mapped_fieldname")
self.basic_rule["detection"] = detection
with patch("yaml.safe_load_all", return_value=[self.basic_rule]):
parser = SigmaCollectionParser("any sigma io", config, None)
backend = SQLBackend(config, self.table)
assert len(parser.parsers) == 1
for p in parser.parsers:
self.assertEqual(expected_result, backend.generate(p))
def test_not_implemented(self):
# near aggregation not implemented
detection = {"selection": {"fieldname": "test"}, "filter": {
"fieldname": "test2"}, "condition": "selection | near selection and filter"}
expected_result = NotImplementedError()
self.validate(detection, expected_result)
# re modifier is not implemented
detection = {"selection": {"fieldname|re": "test"},
"condition": "selection"}
expected_result = NotImplementedError()
self.validate(detection, expected_result)
#Full Text Search is not implemented
detection = {"selection": ["test1"], "condition": "selection"}
expected_result = NotImplementedError()
self.validate(detection, expected_result)
def validate(self, detection, expectation):
config = SigmaConfiguration()
self.basic_rule["detection"] = detection
with patch("yaml.safe_load_all", return_value=[self.basic_rule]):
parser = SigmaCollectionParser("any sigma io", config, None)
backend = SQLBackend(config, self.table)
assert len(parser.parsers) == 1
for p in parser.parsers:
if isinstance(expectation, str):
self.assertEqual(expectation, backend.generate(p))
elif isinstance(expectation, Exception):
self.assertRaises(type(expectation), backend.generate, p)
if __name__ == '__main__':
unittest.main()