This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ruleparser.py
227 lines (190 loc) · 8.02 KB
/
ruleparser.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
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
import re
import hashlib
from django.db import transaction
# Import Database classes
from rule_manager.models import Rule, MetaData, RuleStrings, Condition, Category
# Create Rules from Database
blank_rule = '''
rule [[name]]
{
meta:
[[meta]]
strings:
[[strings]]
condition:
[[condition]]
}
'''
def create_single_rule(rule_id):
#try:
# get elements
# get the rule
rule_details = Rule.objects.get(id=rule_id)
# get metadata
meta_list = rule_details.metadata_set.all()
meta_string = ''
for meta in meta_list:
meta_string += '\t\t{0} = "{1}"\n'.format(meta.meta_key, meta.meta_value)
meta_string += '\t\tGenerated Using = "YaraManager"\n'
# get strings
string_list = rule_details.rulestrings_set.all()
strings_string = ''
for strings in string_list:
if strings.string_type == 'String':
strings_string += '\t\t{0} = "{1}"'.format(strings.string_name, strings.string_value)
if strings.string_nocase:
strings_string += ' nocase'
if strings.string_wide:
strings_string += ' wide'
if strings.string_full:
strings_string += ' fullword'
if strings.string_ascii:
strings_string += ' ascii'
strings_string += '\n'
if strings.string_type == 'Hex':
strings_string += '\t\t{0} = {{{1}}}\n'.format(strings.string_name, strings.string_value)
if strings.string_type == 'RegEx':
strings_string += '\t\t{0} = /{1}/\n'.format(strings.string_name, strings.string_value)
# get condition
condition = rule_details.condition_set.all()[0]
# Compile Rule
final_rule = blank_rule.replace('[[name]]', rule_details.rule_name.replace('\n', ''))
final_rule = final_rule.replace('[[meta]]', meta_string)
final_rule = final_rule.replace('[[strings]]', strings_string)
final_rule = final_rule.replace('[[condition]]', condition.condition)
# Return The rule
return rule_details.rule_name.replace('\n', ''), final_rule
#except:
#return "Failed"
def create_multi_rule(cat_name):
final_rule = ''
# Get rules ids for cat name
cat = Category.objects.filter(cat_name=cat_name).first()
rules = Rule.objects.filter(rule_category=cat.id)
for rule in rules:
name, raw = create_single_rule(rule.id)
if rule.rule_active:
final_rule += '{0}'.format(raw)
return cat_name, final_rule
# Parse Rules from a file
def split_rules(rule_dict):
print("Running Rules")
raw_rules = rule_dict['rule_data']
rule_list = re.findall('rule .*?condition:.*?}', raw_rules.decode('utf8','ignore'), re.DOTALL)
for rule in rule_list:
process_rule(rule, rule_dict)
def Disable_Rule(rule_id):
rule_details = Rule.objects.get(id=rule_id)
rule_details.rule_active = False
rule_details.save()
def Enable_Rule(rule_id):
rule_details = Rule.objects.get(id=rule_id)
rule_details.rule_active = True
rule_details.save()
def Add_Tag(rule_id, cat_name):
rule_details = Rule.objects.get(id=rule_id)
cat = Category.objects.filter(cat_name=cat_name).first()
rule_details.rule_category.add(cat)
rule_details.save()
def Del_Tag(rule_id, cat_name):
rule_details = Rule.objects.get(id=rule_id)
cat = Category.objects.filter(cat_name=cat_name).first()
rule_details.rule_category.remove(cat)
rule_details.save()
def AddNew_Tag(cat_name):
cat_list = []
for name in Category.objects.all():
cat_list.append(name.cat_name)
# If the catergori is not in the list , save it.
if cat_name not in cat_list:
cat = Category(cat_name=cat_name)
cat.save()
else:
cat = Category.objects.filter(cat_name=cat_name).first()
return(cat)
def process_rule(single_rule, rule_dict):
cat = AddNew_Tag(rule_dict['rule_category'])
# Store the category
# Break a rule down in to sections
new_rule = Rule()
# Unique hash body of rule
new_rule.rule_hash = hashlib.sha256(single_rule.encode('utf8')).hexdigest()
new_rule.rule_name = single_rule.split('{')[0].replace('rule ', '')
new_rule.rule_source = rule_dict['rule_source']
new_rule.rule_version = 1
# With integrity error avoid duplicate
try:
new_rule.save()
new_rule.rule_category.add(cat)
except:
# IntegrityError as e:
return()
rule_id = new_rule.id
# MetaData
meta_list = re.findall('meta:(.*)strings:', single_rule, re.DOTALL)
if len(meta_list) > 0:
with transaction.atomic():
for line in meta_list[0].split('\n'):
if '=' in line:
meta_lines = line.split('=')
key = meta_lines[0]
try:
value = re.findall('"(.*)"', line)[0]
except:
value = meta_lines[1]
rule_meta = MetaData(rule=new_rule, meta_key=key.strip(), meta_value=value.strip())
rule_meta.save()
# Strings
string_list = re.findall('strings:(.*)condition:', single_rule, re.DOTALL)
if len(string_list) > 0:
with transaction.atomic():
for line in string_list[0].split('\n'):
if '=' in line:
string_type = False
# get the string ID
key = line.split('=')[0].strip()
string_data = line.split('=')[1]
string_nocase = string_wide = string_full = string_ascii = False
if string_data.strip().startswith('"'):
standard_string = re.findall('"(.*)"', line)
if len(standard_string) != 0:
string_type = 'String'
string_value = standard_string[0]
if 'nocase' in line.split('"')[-1]:
string_nocase = True
if 'wide' in line.split('"')[-1]:
string_wide = True
if 'fullword' in line.split('"')[-1]:
string_full = True
if 'ascii' in line.split('"')[-1]:
string_ascii = True
# Check for a hex string
if not string_type and string_data.strip().startswith('{'):
hex_string = re.findall('{(.*)}', line)
if len(hex_string) != 0:
string_type = 'Hex'
string_value = hex_string[0]
# Check for a regex
# This has an annoying habbit of matching comments
if not string_type and string_data.strip().startswith('/'):
reg_string = re.findall('/(.*)/', line)
if len(reg_string) != 0:
if reg_string[0] not in ['', '/']:
string_type = 'RegEx'
string_value = reg_string[0]
if string_type:
rule_strings = RuleStrings(rule=new_rule,
string_type = string_type,
string_name = key,
string_value = string_value,
string_nocase = string_nocase,
string_wide = string_wide,
string_full = string_full,
string_ascii = string_ascii
)
rule_strings.save()
# Condition
condition = re.findall('condition:(.*)}', single_rule, re.DOTALL)
condition = condition[0].strip()
cond_string = Condition(rule=new_rule, condition=condition)
cond_string.save()