-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
165 lines (129 loc) · 4.29 KB
/
models.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
import re
from functools import reduce
from typing import Dict, List, Literal, Optional, Union
from pydantic import (
AnyUrl,
BaseModel,
Field,
StrictFloat,
StrictInt,
conlist,
constr,
field_validator,
ConfigDict,
field_serializer,
)
from pydantic_core import PydanticCustomError
from pydantic_core.core_schema import FieldValidationInfo
from typing_extensions import Annotated, TypedDict
def extract_mustached_keys(commands: List[Optional[str]]) -> List[str]:
result = []
for command in commands:
if command:
matches = re.finditer(r"#{(.*?)}", command, re.MULTILINE)
keys = [list(i.groups()) for i in matches]
keys = list(reduce(lambda x, y: x + y, keys, []))
result.extend(keys)
return list(set(result))
InputArgType = Literal["url", "string", "float", "integer", "path"]
Platform = Literal[
"windows",
"macos",
"linux",
"office-365",
"azure-ad",
"google-workspace",
"saas",
"iaas",
"containers",
"iaas:gcp",
"iaas:azure",
"iaas:aws",
]
ExecutorType = Literal["manual", "powershell", "sh", "bash", "command_prompt"]
class BaseArgument(TypedDict):
description: str
class UrlArg(BaseArgument):
default: AnyUrl
type: Literal["url"]
@field_serializer("default")
def serialize_url(self, value):
return str(value)
class StringArg(BaseArgument):
default: str
type: Literal["string", "path"]
class IntArg(BaseArgument):
default: StrictInt
type: Literal["integer"]
class FloatArg(BaseArgument):
default: StrictFloat
type: Literal["float"]
Argument = Annotated[Union[FloatArg, IntArg, UrlArg, StringArg], Field(discriminator="type")]
class Executor(BaseModel):
name: ExecutorType
elevation_required: bool = False
class ManualExecutor(Executor):
name: Literal["manual"]
steps: str
class CommandExecutor(Executor):
name: Literal["powershell", "sh", "bash", "command_prompt"]
command: constr(min_length=1)
cleanup_command: Optional[str] = None
@field_serializer("cleanup_command")
def serialize_gpc(self, command):
if command == "":
return None
return command
class Dependency(BaseModel):
description: constr(min_length=1)
prereq_command: constr(min_length=1)
get_prereq_command: Optional[str]
@field_serializer("get_prereq_command")
def serialize_gpc(self, command):
if command == "":
return None
return command
class Atomic(BaseModel):
model_config = ConfigDict(validate_default=True)
name: constr(min_length=1)
description: constr(min_length=1)
supported_platforms: conlist(Platform, min_length=1)
executor: Union[ManualExecutor, CommandExecutor] = Field(..., discriminator="name")
input_arguments: Optional[Dict[str, Argument]] = {}
dependencies: Optional[List[Dependency]] = []
dependency_executor: Optional[ExecutorType] = None
@classmethod
def extract_mustached_keys(cls, value: dict) -> List[str]:
commands = []
executor = value.get("executor")
if isinstance(executor, CommandExecutor):
commands = [executor.command, executor.cleanup_command]
for d in value.get("dependencies", []):
commands.extend([d.get_prereq_command, d.prereq_command])
return extract_mustached_keys(commands)
@field_validator("input_arguments", mode="before") # noqa
@classmethod
def validate(cls, v, info: FieldValidationInfo):
atomic = info.data
keys = cls.extract_mustached_keys(atomic)
for key, _value in v.items():
if key not in keys:
raise PydanticCustomError(
"unused_input_argument",
f"'{key}' is not used in any of the commands",
{"loc": ["input_arguments", key]},
)
else:
keys.remove(key)
if len(keys) > 0:
for x in keys:
raise PydanticCustomError(
"missing_input_argument",
f"{x} is not defined in input_arguments",
{"loc": ["input_arguments"]},
)
return v
class Technique(BaseModel):
attack_technique: str
display_name: str
atomic_tests: List[Atomic]