-
Notifications
You must be signed in to change notification settings - Fork 2
/
mydata.py
152 lines (133 loc) · 5.08 KB
/
mydata.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
import copy
from collections import OrderedDict
class my_data:
def __init__(self, yaml):
self.yaml = yaml
self.data = {
"Attack_name": "",
"Attack_description": "",
"guid": "",
"name": "",
"tactic": [],
"technique": [],
"os": "",
"description": "",
"executor": "",
"sigma": False,
"sigma_rule": [],
}
def clean(self):
self.data = {
"Attack_name": "",
"Attack_description": "",
"guid": "",
"name": "",
"tactic": [],
"technique": [],
"os": "",
"description": "",
"executor": "",
"sigma": False,
"sigma_rule": [],
}
# Use to add missing field when update class
def check(self):
# pass
# file deepcode ignore UpdateAPI: <please specify a reason of ignoring this>
if not "executor" in self.data.keys():
self.data["executor"]= ""
def add(self, head_info, test):
self.data["Attack_name"] = head_info["name"]
self.data["Attack_description"] = head_info["description"]
self.data["guid"] = test["auto_generated_guid"]
self.data["name"] = test["name"]
self.data["os"] = test["supported_platforms"]
self.data["description"] = test["description"]
self.data["executor"] = test["executor"]["name"]
for tactic in head_info["tactic"]: # better way to do?
if not tactic in self.data["tactic"]:
self.data["tactic"].append(tactic)
for technique in head_info["technique"]:
if not technique in self.data["technique"]:
self.data["technique"].append(technique)
if self.data["sigma_rule"] is None:
self.data["sigma"] = False
self.data["sigma_rule"] = []
self.data["sigma"] = True if len(self.data["sigma_rule"])>0 else False
def order(self):
old_yml = copy.deepcopy(self.data)
self.data = {}
self.data["Attack_name"] = old_yml["Attack_name"]
self.data["Attack_description"] = old_yml["Attack_description"]
self.data["guid"] = old_yml["guid"]
self.data["name"] = old_yml["name"]
self.data["tactic"] = old_yml["tactic"]
self.data["technique"] = old_yml["technique"]
self.data["os"] = old_yml["os"]
self.data["description"] = old_yml["description"]
self.data["executor"] = old_yml["executor"]
self.data["sigma"] = old_yml["sigma"]
self.data["sigma_rule"] = old_yml["sigma_rule"]
def load(self, filepath):
with filepath.open("r", encoding="UTF-8") as file:
self.data = self.yaml.load(file)
self.check()
def save(self, filepath): # very bad because of scolar -> |2
filepath.parent.mkdir(parents=True, exist_ok=True)
with filepath.open("w", encoding="UTF-8", newline="\n") as file:
self.yaml.dump(self.data, file)
with filepath.open("r", encoding="UTF-8", newline="\n") as file:
file_lines = file.readlines()
with filepath.open("w", encoding="UTF-8", newline="\n") as file:
for line in file_lines:
file.write(line.replace("|4", "|-").replace("|2", "|-"))
def build_md(self, filepath):
test_str = """
[back](../index.md)
Find sigma rule %%state%%
# Attack: %%Attack_name%%
%%Attack_description%%
# MITRE
## Tactic
%%tactic%%
## technique
%%technique%%
# Test : %%test_name%%
## OS
%%os%%
## Description:
%%description%%
## Executor
%%executor%%
# Sigma Rule
%%sigma_rule%%
[back](../index.md)
"""
state = ":heavy_check_mark:" if self.data["sigma"] == True else ":x:"
str_tactic = ""
for tactic in self.data["tactic"]:
str_tactic += f" - {tactic}\n"
str_technique = ""
for technique in self.data["technique"]:
str_technique += f" - {technique}\n"
str_os = ""
for os in self.data["os"]:
str_os += f" - {os}\n"
str_sigma = ""
for sigma in self.data["sigma_rule"]:
str_sigma += f" - {sigma['name']} (id: {sigma['id']})\n\n"
test_str = test_str.replace("%%state%%", state)
test_str = test_str.replace("%%Attack_name%%", self.data["Attack_name"])
test_str = test_str.replace(
"%%Attack_description%%", self.data["Attack_description"]
)
test_str = test_str.replace("%%tactic%%", str_tactic)
test_str = test_str.replace("%%technique%%", str_technique)
test_str = test_str.replace("%%test_name%%", self.data["name"])
test_str = test_str.replace("%%os%%", str_os)
test_str = test_str.replace("%%description%%", self.data["description"])
test_str = test_str.replace("%%executor%%", self.data["executor"])
test_str = test_str.replace("%%sigma_rule%%", str_sigma)
filepath.parent.mkdir(parents=True, exist_ok=True)
with filepath.open("w", encoding="UTF-8", newline="\n") as file:
file.write(test_str)