-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredcannary_id.py
253 lines (195 loc) · 7.88 KB
/
redcannary_id.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
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
# 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/>.
"""
Project: redcannary_id.py
Date: 2022/01/27
Author: frack113
Version: 1.4.2
Description:
generate file for redcannary index.yaml
Requirements:
python :)
"""
import pathlib
from ruamel.yaml import YAML
from mydata import my_data
import csv
import requests
from collections import OrderedDict
import time
import re
from dataclasses import dataclass
yaml = YAML()
yaml.preserve_quotes = True
yaml.explicit_start = False
yaml.width = 200
yaml.indent(sequence=4, offset=2)
redcannary_info = my_data(yaml)
@dataclass
class sigma:
uuid = {}
name = {}
url = {}
tag = {}
use = {}
@dataclass
class csvdata:
header = [
"tactic",
"technique",
"executor",
"os",
"name",
"guid",
"sigma",
"nmr_test",
]
data = [header]
def load_sigma_yaml(paths:list) -> sigma:
sigma_data = sigma()
for path in paths:
sigmahq_files = pathlib.Path(path).glob("**/*.yml")
for sigmahq_yml in sigmahq_files:
with sigmahq_yml.open("r", encoding="UTF-8") as file:
yml_sigma = yaml.load(file)
sigma_data.uuid[yml_sigma["id"]] = sigmahq_yml.name
sigma_data.name[sigmahq_yml.name] = yml_sigma["id"]
sigma_data.url[sigmahq_yml.name] = str(
pathlib.PurePosixPath(sigmahq_yml)
).replace("../sigma", "https://github.com/SigmaHQ/sigma/tree/master")
if "tags" in yml_sigma:
for tag in yml_sigma["tags"]:
if re.match("attack.t\d+.*", tag):
mitre = tag.replace("attack.t", "T")
if not mitre in sigma_data.tag:
sigma_data.tag[mitre] = []
sigma_data.tag[mitre].append(sigmahq_yml.name)
sigma_data.use = {name: False for name in sigma_data.name.keys()}
return sigma_data
def check_redcanary_yaml(path, delta):
need_to_download = False
if pathlib.Path(path).exists():
epoch_now = time.time()
file_lstat = pathlib.Path(path).lstat()
delta_time = epoch_now - file_lstat.st_mtime
if delta_time > delta:
need_to_download = True
pathlib.Path(path).unlink()
else:
need_to_download = True
if need_to_download:
my_file = requests.get(
"https://github.com/redcanaryco/atomic-red-team/raw/master/atomics/Indexes/index.yaml"
)
with pathlib.Path(path).open("w", encoding="UTF-8", newline="\n") as file:
file.write(my_file.content.decode())
sigma_data = load_sigma_yaml(["../sigma/rules","../sigma/rules-emerging-threats","../sigma/rules-threat-hunting"])
check_redcanary_yaml("index.yaml", 86400)
full_csv = csvdata()
test_csv = []
warning_log = []
valid_guid = []
with pathlib.Path("index.yaml").open("r", encoding="UTF-8") as file:
print("Load index.yaml...")
yml_index = yaml.load(file)
for tactic in yml_index.keys():
for technique in yml_index[tactic]:
atomic_tests = yml_index[tactic][technique]["atomic_tests"]
nb_tests = len(atomic_tests)
print(f"Found {tactic} / {technique} : {nb_tests} tests")
head_info = {
"description": "",
"name": "",
"technique": [],
"tactic": [],
}
if "technique" in yml_index[tactic][technique]:
head_info["description"] = yml_index[tactic][technique]["technique"][
"description"
]
head_info["name"] = yml_index[tactic][technique]["technique"]["name"]
for ext_ref in yml_index[tactic][technique]["technique"][
"external_references"
]:
if ext_ref["source_name"] == "mitre-attack":
head_info["technique"].append(ext_ref["external_id"])
for kill_ref in yml_index[tactic][technique]["technique"][
"kill_chain_phases"
]:
if kill_ref["kill_chain_name"] == "mitre-attack":
head_info["tactic"].append(kill_ref["phase_name"])
if nb_tests > 0:
nmr_test = 0
for test in atomic_tests:
nmr_test += 1
redcannary_info.clean()
guid = test["auto_generated_guid"]
valid_guid.append(guid)
yml_file = pathlib.Path(f"yml/{guid}.yml")
if yml_file.exists():
redcannary_info.load(yml_file)
redcannary_info.add(head_info, test)
if len(redcannary_info.data["sigma_rule"]) > 0:
for rule in redcannary_info.data["sigma_rule"]:
if rule["id"] in sigma_data.uuid:
rule["name"] = sigma_data.uuid[rule["id"]]
sigma_data.use[rule["name"]] = True
else:
if rule["name"] in sigma_data.name:
rule["id"] = sigma_data.name[rule["name"]]
sigma_data.use[rule["name"]] = True
else:
warning_log.append(
f'No fix found in {guid} file for Unidentified sigma id : {rule["id"]} / {rule["name"]}'
)
redcannary_info.order()
redcannary_info.save(yml_file)
full_csv.data.append(
[
tactic,
technique,
redcannary_info.data["executor"],
redcannary_info.data["os"],
redcannary_info.data["name"],
redcannary_info.data["guid"],
redcannary_info.data["sigma"],
nmr_test,
]
)
else:
if technique in sigma_data.tag:
print(
f"Found {len(sigma_data.tag[technique])} sigma rule(s)"
)
info = [
tactic,
technique,
",".join(sigma_data.tag[technique]),
]
test_csv.append(info)
csv_file = pathlib.Path("Full_tests.csv")
with csv_file.open("w", encoding="UTF-8", newline="\n") as csvfile:
writer = csv.writer(csvfile, delimiter=";", quoting=csv.QUOTE_MINIMAL)
writer.writerows(full_csv.data)
if len(test_csv) > 0:
csv_file = pathlib.Path("missing_tests.csv")
with csv_file.open("w", encoding="UTF-8", newline="\n") as csvfile:
writer = csv.writer(csvfile, delimiter=";", quoting=csv.QUOTE_MINIMAL)
writer.writerows(test_csv)
if len(warning_log) > 0:
print("--------- Warning Found ---------")
for warn_srt in warning_log:
print(warn_srt)
print("--------- Check remove Test ---------")
guid_files = pathlib.Path("yml/").glob("**/*.yml")
for guid_file in guid_files:
if guid_file.stem not in valid_guid:
print(f"{guid_file.name} remove")
guid_file.unlink()
csv_file = pathlib.Path("sigma_rule.csv")
with csv_file.open("w", encoding="UTF-8", newline="\n") as csvfile:
writer = csv.writer(csvfile, delimiter=";", quoting=csv.QUOTE_MINIMAL)
my_row= [[key,value] for key,value in sigma_data.use.items()]
writer.writerows(my_row)