| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""The artifact definitions registry.""" |
| 3 |
|
| 4 |
from artifacts import definitions |
| 5 |
from artifacts import errors |
| 6 |
from artifacts import source_type |
| 7 |
|
| 8 |
|
| 9 |
class ArtifactDefinitionsRegistry(object): |
| 10 |
"""Artifact definitions registry.""" |
| 11 |
|
| 12 |
_source_type_classes = { |
| 13 |
definitions.TYPE_INDICATOR_ARTIFACT_GROUP: |
| 14 |
source_type.ArtifactGroupSourceType, |
| 15 |
definitions.TYPE_INDICATOR_COMMAND: source_type.CommandSourceType, |
| 16 |
definitions.TYPE_INDICATOR_DIRECTORY: source_type.DirectorySourceType, |
| 17 |
definitions.TYPE_INDICATOR_FILE: source_type.FileSourceType, |
| 18 |
definitions.TYPE_INDICATOR_PATH: source_type.PathSourceType, |
| 19 |
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_KEY: |
| 20 |
source_type.WindowsRegistryKeySourceType, |
| 21 |
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE: |
| 22 |
source_type.WindowsRegistryValueSourceType, |
| 23 |
definitions.TYPE_INDICATOR_WMI_QUERY: source_type.WMIQuerySourceType} |
| 24 |
|
| 25 |
def __init__(self): |
| 26 |
"""Initializes an artifact definitions registry.""" |
| 27 |
super(ArtifactDefinitionsRegistry, self).__init__() |
| 28 |
self._artifact_definitions_by_alias = {} |
| 29 |
self._artifact_definitions_by_name = {} |
| 30 |
self._artifact_name_references = set() |
| 31 |
self._defined_artifact_names = set() |
| 32 |
|
| 33 |
@classmethod |
| 34 |
def CreateSourceType(cls, type_indicator, attributes): |
| 35 |
"""Creates a source type object. |
| 36 |
|
| 37 |
Args: |
| 38 |
type_indicator (str): source type indicator. |
| 39 |
attributes (dict[str, object]): source attributes. |
| 40 |
|
| 41 |
Returns: |
| 42 |
SourceType: a source type. |
| 43 |
|
| 44 |
Raises: |
| 45 |
FormatError: if the type indicator is not set or unsupported, |
| 46 |
or if required attributes are missing. |
| 47 |
""" |
| 48 |
if type_indicator not in cls._source_type_classes: |
| 49 |
raise errors.FormatError( |
| 50 |
f'Unsupported type indicator: {type_indicator:s}.') |
| 51 |
|
| 52 |
return cls._source_type_classes[type_indicator](**attributes) |
| 53 |
|
| 54 |
def DeregisterDefinition(self, artifact_definition): |
| 55 |
"""Deregisters an artifact definition. |
| 56 |
|
| 57 |
Artifact definitions are identified based on their lower case name. |
| 58 |
|
| 59 |
Args: |
| 60 |
artifact_definition (ArtifactDefinition): an artifact definition. |
| 61 |
|
| 62 |
Raises: |
| 63 |
KeyError: if an artifact definition is not set for the corresponding name. |
| 64 |
""" |
| 65 |
artifact_definition_name = artifact_definition.name.lower() |
| 66 |
if artifact_definition_name not in self._artifact_definitions_by_name: |
| 67 |
raise KeyError(( |
| 68 |
f'Artifact definition not set for name: ' |
| 69 |
f'{artifact_definition.name:s}.')) |
| 70 |
|
| 71 |
for alias in artifact_definition.aliases: |
| 72 |
if alias.lower() not in self._artifact_definitions_by_alias: |
| 73 |
raise KeyError(f'Artifact definition not set for alias: {alias:s}.') |
| 74 |
|
| 75 |
del self._artifact_definitions_by_name[artifact_definition_name] |
| 76 |
|
| 77 |
for alias in artifact_definition.aliases: |
| 78 |
del self._artifact_definitions_by_alias[alias.lower()] |
| 79 |
|
| 80 |
@classmethod |
| 81 |
def DeregisterSourceType(cls, source_type_class): |
| 82 |
"""Deregisters a source type. |
| 83 |
|
| 84 |
Source types are identified based on their type indicator. |
| 85 |
|
| 86 |
Args: |
| 87 |
source_type_class (type): source type. |
| 88 |
|
| 89 |
Raises: |
| 90 |
KeyError: if a source type is not set for the corresponding type |
| 91 |
indicator. |
| 92 |
""" |
| 93 |
if source_type_class.TYPE_INDICATOR not in cls._source_type_classes: |
| 94 |
raise KeyError(( |
| 95 |
f'Source type not set for type: ' |
| 96 |
f'{source_type_class.TYPE_INDICATOR:s}.')) |
| 97 |
|
| 98 |
del cls._source_type_classes[source_type_class.TYPE_INDICATOR] |
| 99 |
|
| 100 |
def GetDefinitionByAlias(self, alias): |
| 101 |
"""Retrieves a specific artifact definition by alias. |
| 102 |
|
| 103 |
Args: |
| 104 |
alias (str): alias of the artifact definition. |
| 105 |
|
| 106 |
Returns: |
| 107 |
ArtifactDefinition: an artifact definition or None if not available. |
| 108 |
""" |
| 109 |
if not alias: |
| 110 |
return None |
| 111 |
|
| 112 |
return self._artifact_definitions_by_alias.get(alias.lower(), None) |
| 113 |
|
| 114 |
def GetDefinitionByName(self, name): |
| 115 |
"""Retrieves a specific artifact definition by name. |
| 116 |
|
| 117 |
Args: |
| 118 |
name (str): name of the artifact definition. |
| 119 |
|
| 120 |
Returns: |
| 121 |
ArtifactDefinition: an artifact definition or None if not available. |
| 122 |
""" |
| 123 |
if not name: |
| 124 |
return None |
| 125 |
|
| 126 |
return self._artifact_definitions_by_name.get(name.lower(), None) |
| 127 |
|
| 128 |
def GetDefinitions(self): |
| 129 |
"""Retrieves the artifact definitions. |
| 130 |
|
| 131 |
Returns: |
| 132 |
list[ArtifactDefinition]: artifact definitions. |
| 133 |
""" |
| 134 |
return self._artifact_definitions_by_name.values() |
| 135 |
|
| 136 |
def GetUndefinedArtifacts(self): |
| 137 |
"""Retrieves the names of undefined artifacts used by artifact groups. |
| 138 |
|
| 139 |
Returns: |
| 140 |
set[str]: undefined artifacts names. |
| 141 |
""" |
| 142 |
return self._artifact_name_references - self._defined_artifact_names |
| 143 |
|
| 144 |
def RegisterDefinition(self, artifact_definition): |
| 145 |
"""Registers an artifact definition. |
| 146 |
|
| 147 |
Artifact definitions are identified based on their lower case name. |
| 148 |
|
| 149 |
Args: |
| 150 |
artifact_definition (ArtifactDefinition): an artifact definition. |
| 151 |
|
| 152 |
Raises: |
| 153 |
KeyError: if artifact definition is already set for the corresponding |
| 154 |
name or alias. |
| 155 |
""" |
| 156 |
artifact_definition_name = artifact_definition.name.lower() |
| 157 |
if artifact_definition_name in self._artifact_definitions_by_name: |
| 158 |
raise KeyError(( |
| 159 |
f'Artifact definition already set for name: ' |
| 160 |
f'{artifact_definition.name:s}.')) |
| 161 |
|
| 162 |
for alias in artifact_definition.aliases: |
| 163 |
alias_lower = alias.lower() |
| 164 |
if alias_lower in self._artifact_definitions_by_alias: |
| 165 |
raise KeyError(f'Artifact definition already set for alias: {alias:s}.') |
| 166 |
|
| 167 |
if alias_lower in self._artifact_definitions_by_name: |
| 168 |
raise KeyError( |
| 169 |
f'Artifact definition alias: {alias:s} already used as name.') |
| 170 |
|
| 171 |
self._artifact_definitions_by_name[artifact_definition_name] = ( |
| 172 |
artifact_definition) |
| 173 |
self._defined_artifact_names.add(artifact_definition.name) |
| 174 |
|
| 175 |
for alias in artifact_definition.aliases: |
| 176 |
self._artifact_definitions_by_alias[alias.lower()] = artifact_definition |
| 177 |
|
| 178 |
for source in artifact_definition.sources: |
| 179 |
if source.type_indicator == definitions.TYPE_INDICATOR_ARTIFACT_GROUP: |
| 180 |
self._artifact_name_references.update(source.names) |
| 181 |
|
| 182 |
@classmethod |
| 183 |
def RegisterSourceType(cls, source_type_class): |
| 184 |
"""Registers a source type. |
| 185 |
|
| 186 |
Source types are identified based on their type indicator. |
| 187 |
|
| 188 |
Args: |
| 189 |
source_type_class (type): source type. |
| 190 |
|
| 191 |
Raises: |
| 192 |
KeyError: if source types is already set for the corresponding |
| 193 |
type indicator. |
| 194 |
""" |
| 195 |
if source_type_class.TYPE_INDICATOR in cls._source_type_classes: |
| 196 |
raise KeyError(( |
| 197 |
f'Source type already set for type: ' |
| 198 |
f'{source_type_class.TYPE_INDICATOR:s}.')) |
| 199 |
|
| 200 |
cls._source_type_classes[source_type_class.TYPE_INDICATOR] = ( |
| 201 |
source_type_class) |
| 202 |
|
| 203 |
@classmethod |
| 204 |
def RegisterSourceTypes(cls, source_type_classes): |
| 205 |
"""Registers source types. |
| 206 |
|
| 207 |
Source types are identified based on their type indicator. |
| 208 |
|
| 209 |
Args: |
| 210 |
source_type_classes (list[type]): source types. |
| 211 |
""" |
| 212 |
for source_type_class in source_type_classes: |
| 213 |
cls.RegisterSourceType(source_type_class) |
| 214 |
|
| 215 |
def ReadFromDirectory(self, artifacts_reader, path, extension='yaml'): |
| 216 |
"""Reads artifact definitions into the registry from files in a directory. |
| 217 |
|
| 218 |
This function does not recurse sub directories. |
| 219 |
|
| 220 |
Args: |
| 221 |
artifacts_reader (ArtifactsReader): an artifacts reader. |
| 222 |
path (str): path of the directory to read from. |
| 223 |
extension (Optional[str]): extension of the filenames to read. |
| 224 |
|
| 225 |
Raises: |
| 226 |
KeyError: if a duplicate artifact definition is encountered. |
| 227 |
""" |
| 228 |
for artifact_definition in artifacts_reader.ReadDirectory( |
| 229 |
path, extension=extension): |
| 230 |
self.RegisterDefinition(artifact_definition) |
| 231 |
|
| 232 |
def ReadFromFile(self, artifacts_reader, filename): |
| 233 |
"""Reads artifact definitions into the registry from a file. |
| 234 |
|
| 235 |
Args: |
| 236 |
artifacts_reader (ArtifactsReader): an artifacts reader. |
| 237 |
filename (str): name of the file to read from. |
| 238 |
""" |
| 239 |
for artifact_definition in artifacts_reader.ReadFile(filename): |
| 240 |
self.RegisterDefinition(artifact_definition) |
| 241 |
|
| 242 |
def ReadFileObject(self, artifacts_reader, file_object): |
| 243 |
"""Reads artifact definitions into the registry from a file-like object. |
| 244 |
|
| 245 |
Args: |
| 246 |
artifacts_reader (ArtifactsReader): an artifacts reader. |
| 247 |
file_object (file): file-like object to read from. |
| 248 |
""" |
| 249 |
for artifact_definition in artifacts_reader.ReadFileObject(file_object): |
| 250 |
self.RegisterDefinition(artifact_definition) |