forked from SigmaHQ/sigma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend_sqlite.py
More file actions
148 lines (116 loc) · 6.43 KB
/
test_backend_sqlite.py
File metadata and controls
148 lines (116 loc) · 6.43 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
# 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.sqlite import SQLiteBackend
from sigma.parser.collection import SigmaCollectionParser
from sigma.config.mapping import FieldMapping
from sigma.configuration import SigmaConfiguration
class TestFullTextSearch(unittest.TestCase):
def setUp(self):
self.basic_rule = {"title": "Test", "level": "testing"}
self.table = "eventlog"
def test_full_text_search(self):
detection = {"selection": ["test1"], "condition": "selection"}
expected_result = 'SELECT * FROM {0} WHERE {0} MATCH (\'"test1"\')'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": [5], "condition": "selection"}
expected_result = 'SELECT * FROM {0} WHERE {0} MATCH (\'"5"\')'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": ["test1", "test2"], "condition": "selection"}
expected_result = 'SELECT * FROM {0} WHERE ({0} MATCH (\'"test1" OR "test2"\'))'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": ["test1"], "filter":["test2"], "condition": "selection and filter"}
expected_result = 'SELECT * FROM {0} WHERE ({0} MATCH (\'"test1" AND "test2"\'))'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": [5, 6], "condition": "selection"}
expected_result = 'SELECT * FROM {0} WHERE ({0} MATCH (\'"5" OR "6"\'))'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": ["test1"], "filter": [
"test2"], "condition": "selection or filter"}
expected_result = 'SELECT * FROM {0} WHERE ({0} MATCH (\'"test1" OR "test2"\'))'.format(
self.table)
self.validate(detection, expected_result)
detection = {"selection": ["test1"], "filter": [
"test2"], "condition": "selection and filter"}
expected_result = 'SELECT * FROM {0} WHERE ({0} MATCH (\'"test1" AND "test2"\'))'.format(
self.table)
self.validate(detection, expected_result)
def test_full_text_search_aggregation(self):
# aggregation with fts
detection = {"selection": ["test"],
"condition": "selection | count() > 5"}
inner_query = 'SELECT *,count(*) AS agg FROM {0} WHERE {0} MATCH (\'"test"\')'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
detection = {"selection": ["test1", "test2"],
"condition": "selection | count() > 5"}
inner_query = 'SELECT *,count(*) AS agg FROM {0} WHERE ({0} MATCH (\'"test1" OR "test2"\'))'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
# aggregation + group by + fts
detection = {"selection": ["test1", "test2"],
"condition": "selection | count() by fieldname > 5"}
inner_query = 'SELECT *,count(*) AS agg FROM {0} WHERE ({0} MATCH (\'"test1" OR "test2"\')) GROUP BY fieldname'.format(
self.table)
expected_result = 'SELECT * FROM ({}) WHERE agg > 5'.format(inner_query)
self.validate(detection, expected_result)
def test_not_implemented(self):
# fts not implemented with wildcards
detection = {"selection": ["test*"], "condition": "selection"}
expected_result = NotImplementedError()
self.validate(detection, expected_result)
detection = {"selection": ["test?"], "condition": "selection"}
expected_result = NotImplementedError()
self.validate(detection, expected_result)
detection = {"selection": ["test\\"], "condition": "selection"}
expected_result = NotImplementedError()
self.validate(detection, expected_result)
# fts is not implemented for nested conditions
detection = {"selection": ["test"], "filter": [
"test2"], "condition": "selection and filter"} # this is ok
detection = {"selection": ["test"], "filter": [
"test2"], "condition": "selection or filter"} # this is ok
detection = {"selection": ["test"], "filter": [
"test2"], "condition": "selection and not filter"} # this is already nested
expected_result = NotImplementedError()
self.validate(detection, expected_result)
detection = {"selection": ["test"], "filter": [
"test2"], "condition": "selection and filter and filter"} # this is nested
expected_result = NotImplementedError()
self.validate(detection, expected_result)
detection = {"selection": ["test"], "filter": [
"test2"], "condition": "selection and filter or filter"} # this is nested
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 = SQLiteBackend(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()