-
Notifications
You must be signed in to change notification settings - Fork 21
/
check_if_compliant.py
145 lines (122 loc) · 4.52 KB
/
check_if_compliant.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
import os
import sys
import argparse
from os import listdir
from pprint import pprint
from os.path import isfile, join
# CheckIfSiegmaCompliant.py
# By Wesley
# https://github.com/wesley587
# global vars
exit_status_value = 0
class parsing:
def __init__(self, dir):
self.agg = ['count(', 'min(', 'max(', 'avg(', 'sum(']
self.dir = dir
def firstParsing(self, folders):
# if string then this means that this a single file needs to be checked for compliance
if type(folders) == str:
folders = [folders]
self.Parsing_file(folders)
# else whole folders
else:
for folder in folders:
path = get_slashes().join([self.dir, folder])
files = [x for x in listdir(path) if isfile(join(path, x))]
# pprint(files)
f = [x for x in listdir(path) if not isfile(join(path, x))]
# pprint(f)
if len(files) > 0:
self.Parsing_files(files, path)
if len(f) > 0:
self.Parsing_folders(folder, f)
def Parsing_file(self, file):
global exit_status_value
error = dict()
for x in file:
yaml_file = open(x, 'r', encoding='utf8')
detect_count = 0
local = x
error[local] = list()
for k in yaml_file:
if 'detection:' in k:
detect_count += 1
if 'condition:' in k:
for x in self.agg:
if x in k:
x = x.replace('(', '')
error[local].append(f'\t* An unsupported Aggregation expression ({x}) is present on the sigma rule.')
if detect_count > 1:
error[local].append('\t* There is more than 1 instance of detection on this sigma rule.')
for k, v in error.items():
if len(v) > 0:
exit_status_value = 1
print(f'ERROR FOUND - Sigma Rule: {k}')
for x in v:
print(x)
print("\n")
def Parsing_files(self, files, path):
global exit_status_value
error = dict()
for x in files:
yaml_file = open(get_slashes().join([path, x]), 'r', encoding='utf8')
detect_count = 0
local = get_slashes().join([path, x])
error[local] = list()
for k in yaml_file:
if 'detection:' in k:
detect_count += 1
if 'condition:' in k:
for x in self.agg:
if x in k:
x = x.replace('(', '')
error[local].append(f'\t* An unsupported Aggregation expression ({x}) is present on the sigma rule.')
if detect_count > 1:
error[local].append('\t* There is more than 1 instance of detection on this sigma rule.')
for k, v in error.items():
if len(v) > 0:
exit_status_value = 1
print(f'ERROR FOUND - Sigma Rule: {k}')
for x in v:
print(x)
print("\n")
def Parsing_folders(self, path, folder):
for o in folder:
path2 = get_slashes().join([self.dir, path, o])
files2 = [x for x in listdir(path2) if isfile(join(path2, x))]
self.Parsing_files(files2, path2)
def get_slashes():
ret = '\\'
if os.name != 'nt':
ret = '/'
# print('Non-windows machine...')
# else:
# print('Windows machine...')
# print('Returning {}...'.format(ret))
return ret
def main():
parse = argparse.ArgumentParser()
parse.add_argument('-p', help='Parsing the sigma rules in a Directory')
path = vars(parse.parse_args())
dir = path['p']
folders = ''
# for single file
if isfile(dir):
file = dir
start = parsing(dir=file)
start.firstParsing(file)
# for folders
else:
folders = [x for x in listdir(dir) if not isfile(join(dir, x))]
if folders == []:
# if single folder with n files
folders = [x for x in listdir(dir) if isfile(join(dir, x))]
start = parsing(dir=dir)
start.Parsing_files(folders, dir)
else:
start = parsing(dir=dir)
start.firstParsing(folders)
# pprint(folders)
print('Ending script with exit status {}'.format(exit_status_value))
sys.exit(exit_status_value)
main()