-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
183 lines (152 loc) · 6.32 KB
/
models.py
File metadata and controls
183 lines (152 loc) · 6.32 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
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
# import re
from typing import List, Optional, Dict
from pydantic import BaseModel, Field, validator, root_validator
"""
[
{
"testCaseData": {
"name": "Bulk Template Test 1",
"description": "testing this input",
"operatorGuidance": "Do something suspicious like listen to ska music",
"phase": "Execution",
"technique": "T1110",
"organization": "Security Risk Advisors",
"status": "INPROGRESS",
"outcome": "NOTDETECTED",
"activityLogged": "YES",
"redTools": [{
"name": "Native Windows Commands"
}]
}
}
"""
class TestCase(BaseModel):
name: str = Field(alias="Variant")
description: Optional[str] = Field(alias="Objective")
phase: Optional[str] = Field(alias="Phase")
technique: Optional[str] = Field(alias="MitreID")
tags: Optional[List[str]] = Field(alias="Tags")
organization: Optional[str] = Field(alias="Organizations")
status: Optional[str] = Field(alias="Status")
targets: Optional[List[str]] = Field(alias="TargetAssets")
sources: Optional[List[str]] = Field(alias="SourceIps")
defenses: Optional[List[str]] = Field(alias="ExpectedDetectionLayers")
detectionSteps: Optional[List[str]] = Field(alias="Detection Recommendations")
outcome: Optional[str] = Field(alias="Outcome")
outcomePath: Optional[str] = Field(alias="Outcome Path")
outcomeNotes: Optional[str] = Field(alias="Outcome Notes")
alertSeverity: Optional[str] = Field(alias="Alert Severity")
alertTriggered: Optional[str] = Field(alias="Alert Triggered")
activityLogged: Optional[str] = Field(alias="Activity Logged")
detectionTime: Optional[float] = Field(alias="Detection Time Epoch")
detectingDefenseTools: Optional[List[Dict[str, str]]] = Field(alias="DetectingTools")
references: Optional[List[str]] = Field(alias="References")
redTools: Optional[List[Dict[str, str]]] = Field(alias="Attacker Tools")
operatorGuidance: Optional[str] = Field(alias="Command")
attackStart: Optional[float] = Field(alias="Start Time Epoch")
attackStop: Optional[float] = Field(alias="Stop Time Epoch")
@root_validator(pre=True)
def check_technique(cls, values):
if 'MitreID' in values and values['MitreID']:
# everything is fine, MitreID exists, continue
return values
if 'Method' in values and values['Method']:
values['MitreID'] = values['Method']
return values
print(values)
raise ValueError("Non-empty Method (Attack Technique) or MitreID required for Test Case creation")
# @TODO - combine for reuse, getting weird behavior with multiple annotations
@validator('sources', pre=True, allow_reuse=True)
def validate_sources(cls, v: str) -> Optional[List[str]]:
sources = v.split(',')
if not sources:
return None
return list(filter(None, sources))
@validator('references', pre=True, allow_reuse=True)
def validate_references(cls, v: str) -> Optional[List[str]]:
refs = v.split(',')
if not refs:
return None
return list(filter(None, refs))
@validator('tags', pre=True, allow_reuse=True)
def validate_tags(cls, v: str) -> Optional[List[str]]:
tags = v.split(',')
if not tags:
return None
return list(filter(None, tags))
@validator('organization', pre=True, allow_reuse=True)
def validate_organization(cls, v: str) -> Optional[str]:
orgs = v.split(',')
if orgs:
return orgs[0]
else:
return None
@validator('defenses', pre=True, allow_reuse=True)
def validate_defenses(cls, v: str) -> Optional[List[str]]:
defenses = v.split(',')
if not defenses:
return None
return list(filter(None, defenses))
@validator('detectionSteps', pre=True, allow_reuse=True)
def validate_detection_steps(cls, v: str) -> Optional[List[str]]:
if not v:
return None
return [v]
@validator('detectingDefenseTools', pre=True, allow_reuse=True)
def validate_detecting_tools(cls, v: str) -> List[Dict[str, str]]:
tools = []
tool_names = v.split(',')
tool_names = list(filter(None, tool_names))
for tool_name in tool_names:
tools.append({"name": tool_name})
return tools
@validator('redTools', pre=True, allow_reuse=True)
def validate_attack_tools(cls, v: str) -> List[Dict[str, str]]:
tools = []
tool_names = v.split(',')
tool_names = list(filter(None, tool_names))
for tool_name in tool_names:
tools.append({"name": tool_name})
return tools
@validator('targets', pre=True, allow_reuse=True)
def validate_targets(cls, v: str) -> Optional[List[str]]:
targets = v.split(',')
if not targets:
return None
return list(filter(None, targets))
@validator('status', pre=True, allow_reuse=True)
def validate_upper_enum1(cls, v: str) -> str:
return v
@validator('outcome', pre=True, allow_reuse=True)
def validate_upper_enum2(cls, v: str) -> str:
return v
@validator('alertSeverity', pre=True, allow_reuse=True)
def validate_upper_enum3(cls, v: str) -> str:
return v
@validator('alertTriggered', pre=True, allow_reuse=True)
def validate_upper_enum4(cls, v: str) -> str:
return v
@validator('activityLogged', pre=True, allow_reuse=True)
def validate_upper_enum5(cls, v: str) -> str:
return v
@validator('attackStart', pre=True, allow_reuse=True)
def validate_attack_start(cls, v: str) -> Optional[float]:
if not v:
return None
return float(v)
@validator('attackStop', pre=True, allow_reuse=True)
def validate_attack_stop(cls, v: str) -> Optional[float]:
if not v:
return None
return float(v)
@validator('detectionTime', pre=True, allow_reuse=True)
def validate_detection_time(cls, v: str) -> Optional[float]:
if not v:
return None
return float(v)
class Campaign(BaseModel):
name: str
test_cases: Optional[List[TestCase]]
class Assessment(BaseModel):
name: str
campaigns: Optional[Dict[str, Campaign]]