| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Tests for the artifact definitions readers.""" |
| 3 |
|
| 4 |
import io |
| 5 |
import unittest |
| 6 |
import yaml |
| 7 |
|
| 8 |
from artifacts import definitions |
| 9 |
from artifacts import errors |
| 10 |
from artifacts import reader |
| 11 |
|
| 12 |
from tests import test_lib |
| 13 |
|
| 14 |
|
| 15 |
class YamlArtifactsReaderTest(test_lib.BaseTestCase): |
| 16 |
"""YAML artifacts reader tests.""" |
| 17 |
|
| 18 |
_DEFINITION_INVALID_SUPPORTED_OS_1 = """\ |
| 19 |
name: BadSupportedOS |
| 20 |
doc: supported_os should be an array of strings. |
| 21 |
sources: |
| 22 |
- type: ARTIFACT_GROUP |
| 23 |
attributes: |
| 24 |
names: |
| 25 |
- 'SystemEventLogEvtx' |
| 26 |
supported_os: Windows |
| 27 |
""" |
| 28 |
|
| 29 |
_DEFINITION_INVALID_SUPPORTED_OS_2 = """\ |
| 30 |
name: BadTopSupportedOS |
| 31 |
doc: Top supported_os should match supported_os from sources. |
| 32 |
sources: |
| 33 |
- type: ARTIFACT_GROUP |
| 34 |
attributes: |
| 35 |
names: |
| 36 |
- 'SystemEventLogEvtx' |
| 37 |
supported_os: [Windows] |
| 38 |
""" |
| 39 |
|
| 40 |
_DEFINITION_INVALID_URLS = """\ |
| 41 |
name: BadUrls |
| 42 |
doc: badurls. |
| 43 |
sources: |
| 44 |
- type: ARTIFACT_GROUP |
| 45 |
attributes: |
| 46 |
names: |
| 47 |
- 'SystemEventLogEvtx' |
| 48 |
supported_os: [Windows] |
| 49 |
urls: 'http://example.com' |
| 50 |
""" |
| 51 |
|
| 52 |
_DEFINITION_WITH_EXTRA_KEY = """\ |
| 53 |
name: WithExtraKey |
| 54 |
doc: definition with extra_key |
| 55 |
sources: |
| 56 |
- type: ARTIFACT_GROUP |
| 57 |
attributes: |
| 58 |
names: |
| 59 |
- 'SystemEventLogEvtx' |
| 60 |
extra_key: 'wrong' |
| 61 |
supported_os: [Windows] |
| 62 |
""" |
| 63 |
|
| 64 |
_DEFINITION_WITH_RETURN_TYPES = """\ |
| 65 |
name: WithReturnTypes |
| 66 |
doc: definition with return_types |
| 67 |
sources: |
| 68 |
- type: ARTIFACT_GROUP |
| 69 |
attributes: |
| 70 |
names: [WindowsRunKeys, WindowsServices] |
| 71 |
returned_types: [PersistenceFile] |
| 72 |
""" |
| 73 |
|
| 74 |
_DEFINITION_WITHOUT_DOC = """\ |
| 75 |
name: NoDoc |
| 76 |
sources: |
| 77 |
- type: ARTIFACT_GROUP |
| 78 |
attributes: |
| 79 |
names: |
| 80 |
- 'SystemEventLogEvtx' |
| 81 |
""" |
| 82 |
|
| 83 |
_DEFINITION_WITHOUT_NAME = """\ |
| 84 |
name: NoNames |
| 85 |
doc: Missing names attr. |
| 86 |
sources: |
| 87 |
- type: ARTIFACT_GROUP |
| 88 |
attributes: |
| 89 |
- 'SystemEventLogEvtx' |
| 90 |
""" |
| 91 |
|
| 92 |
_DEFINITION_WITHOUT_SOURCES = """\ |
| 93 |
name: BadSources |
| 94 |
doc: must have one sources. |
| 95 |
supported_os: [Windows] |
| 96 |
""" |
| 97 |
|
| 98 |
def testReadFileObject(self): |
| 99 |
"""Tests the ReadFileObject function.""" |
| 100 |
test_file = self._GetTestFilePath(['definitions.yaml']) |
| 101 |
self._SkipIfPathNotExists(test_file) |
| 102 |
|
| 103 |
artifact_reader = reader.YamlArtifactsReader() |
| 104 |
|
| 105 |
with open(test_file, 'rb') as file_object: |
| 106 |
artifact_definitions = list(artifact_reader.ReadFileObject(file_object)) |
| 107 |
|
| 108 |
self.assertEqual(len(artifact_definitions), 7) |
| 109 |
|
| 110 |
# Artifact with file source type. |
| 111 |
artifact_definition = artifact_definitions[0] |
| 112 |
self.assertEqual(artifact_definition.name, 'SecurityEventLogEvtxFile') |
| 113 |
|
| 114 |
expected_description = ( |
| 115 |
'Windows Security Event log for Vista or later systems.') |
| 116 |
self.assertEqual(artifact_definition.description, expected_description) |
| 117 |
|
| 118 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 119 |
source_type = artifact_definition.sources[0] |
| 120 |
self.assertIsNotNone(source_type) |
| 121 |
self.assertEqual( |
| 122 |
source_type.type_indicator, definitions.TYPE_INDICATOR_FILE) |
| 123 |
|
| 124 |
expected_paths = [ |
| 125 |
'%%environ_systemroot%%\\System32\\winevt\\Logs\\Security.evtx' |
| 126 |
] |
| 127 |
self.assertEqual(sorted(source_type.paths), sorted(expected_paths)) |
| 128 |
|
| 129 |
self.assertEqual(len(artifact_definition.supported_os), 1) |
| 130 |
self.assertEqual(artifact_definition.supported_os[0], 'Windows') |
| 131 |
|
| 132 |
self.assertEqual(len(artifact_definition.urls), 1) |
| 133 |
expected_url = ( |
| 134 |
'http://www.forensicswiki.org/wiki/Windows_XML_Event_Log_(EVTX)') |
| 135 |
self.assertEqual(artifact_definition.urls[0], expected_url) |
| 136 |
|
| 137 |
# Artifact with Windows Registry key source type. |
| 138 |
artifact_definition = artifact_definitions[1] |
| 139 |
self.assertEqual( |
| 140 |
artifact_definition.name, 'AllUsersProfileEnvironmentVariable') |
| 141 |
|
| 142 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 143 |
source_type = artifact_definition.sources[0] |
| 144 |
self.assertIsNotNone(source_type) |
| 145 |
self.assertEqual( |
| 146 |
source_type.type_indicator, |
| 147 |
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_KEY) |
| 148 |
|
| 149 |
expected_key1 = ( |
| 150 |
'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\' |
| 151 |
'ProfileList\\ProfilesDirectory') |
| 152 |
expected_key2 = ( |
| 153 |
'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\' |
| 154 |
'ProfileList\\AllUsersProfile') |
| 155 |
expected_keys = [expected_key1, expected_key2] |
| 156 |
|
| 157 |
self.assertEqual(sorted(source_type.keys), sorted(expected_keys)) |
| 158 |
|
| 159 |
# Artifact with Windows Registry value source type. |
| 160 |
artifact_definition = artifact_definitions[2] |
| 161 |
self.assertEqual(artifact_definition.name, 'CurrentControlSet') |
| 162 |
|
| 163 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 164 |
source_type = artifact_definition.sources[0] |
| 165 |
self.assertIsNotNone(source_type) |
| 166 |
self.assertEqual( |
| 167 |
source_type.type_indicator, |
| 168 |
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE) |
| 169 |
|
| 170 |
self.assertEqual(len(source_type.key_value_pairs), 1) |
| 171 |
key_value_pair = source_type.key_value_pairs[0] |
| 172 |
|
| 173 |
expected_key = 'HKEY_LOCAL_MACHINE\\SYSTEM\\Select' |
| 174 |
self.assertEqual(key_value_pair['key'], expected_key) |
| 175 |
self.assertEqual(key_value_pair['value'], 'Current') |
| 176 |
|
| 177 |
# Artifact with WMI query source type. |
| 178 |
artifact_definition = artifact_definitions[3] |
| 179 |
self.assertEqual(artifact_definition.name, 'WMIProfileUsersHomeDir') |
| 180 |
|
| 181 |
expected_provides = sorted(['users.homedir']) |
| 182 |
self.assertEqual(sorted(artifact_definition.provides), expected_provides) |
| 183 |
|
| 184 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 185 |
source_type = artifact_definition.sources[0] |
| 186 |
self.assertIsNotNone(source_type) |
| 187 |
self.assertEqual( |
| 188 |
source_type.type_indicator, definitions.TYPE_INDICATOR_WMI_QUERY) |
| 189 |
|
| 190 |
expected_query = ( |
| 191 |
'SELECT * FROM Win32_UserProfile WHERE SID=\'%%users.sid%%\'') |
| 192 |
self.assertEqual(source_type.query, expected_query) |
| 193 |
|
| 194 |
# Artifact with artifact definition source type. |
| 195 |
artifact_definition = artifact_definitions[4] |
| 196 |
self.assertEqual(artifact_definition.name, 'EventLogs') |
| 197 |
|
| 198 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 199 |
source_type = artifact_definition.sources[0] |
| 200 |
self.assertIsNotNone(source_type) |
| 201 |
self.assertEqual( |
| 202 |
source_type.type_indicator, definitions.TYPE_INDICATOR_ARTIFACT_GROUP) |
| 203 |
|
| 204 |
# Artifact with command definition source type. |
| 205 |
artifact_definition = artifact_definitions[5] |
| 206 |
self.assertEqual(artifact_definition.name, 'RedhatPackagesList') |
| 207 |
|
| 208 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 209 |
source_type = artifact_definition.sources[0] |
| 210 |
self.assertIsNotNone(source_type) |
| 211 |
self.assertEqual( |
| 212 |
source_type.type_indicator, definitions.TYPE_INDICATOR_COMMAND) |
| 213 |
|
| 214 |
# Artifact with COMMAND definition collector definition. |
| 215 |
artifact_definition = artifact_definitions[5] |
| 216 |
self.assertEqual(artifact_definition.name, 'RedhatPackagesList') |
| 217 |
|
| 218 |
self.assertEqual(len(artifact_definition.sources), 1) |
| 219 |
collector_definition = artifact_definition.sources[0] |
| 220 |
self.assertIsNotNone(collector_definition) |
| 221 |
self.assertEqual( |
| 222 |
collector_definition.type_indicator, definitions.TYPE_INDICATOR_COMMAND) |
| 223 |
|
| 224 |
def testReadFileObjectInvalidSupportedOS(self): |
| 225 |
"""Tests the ReadFileObject function on an invalid supported_os.""" |
| 226 |
artifact_reader = reader.YamlArtifactsReader() |
| 227 |
|
| 228 |
file_object = io.StringIO( |
| 229 |
initial_value=self._DEFINITION_INVALID_SUPPORTED_OS_1) |
| 230 |
with self.assertRaises(errors.FormatError): |
| 231 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 232 |
|
| 233 |
file_object = io.StringIO( |
| 234 |
initial_value=self._DEFINITION_INVALID_SUPPORTED_OS_2) |
| 235 |
with self.assertRaises(errors.FormatError): |
| 236 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 237 |
|
| 238 |
def testReadFileObjectInvalidURLs(self): |
| 239 |
"""Tests the ReadFileObject function on an invalid urls.""" |
| 240 |
artifact_reader = reader.YamlArtifactsReader() |
| 241 |
|
| 242 |
file_object = io.StringIO(initial_value=self._DEFINITION_INVALID_URLS) |
| 243 |
with self.assertRaises(errors.FormatError): |
| 244 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 245 |
|
| 246 |
def testReadFileObjectWithExtraKey(self): |
| 247 |
"""Tests the ReadFileObject function on a definition with extra key.""" |
| 248 |
artifact_reader = reader.YamlArtifactsReader() |
| 249 |
|
| 250 |
file_object = io.StringIO(initial_value=self._DEFINITION_WITH_EXTRA_KEY) |
| 251 |
with self.assertRaises(errors.FormatError): |
| 252 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 253 |
|
| 254 |
def testReadFileObjectWithReturnTypes(self): |
| 255 |
"""Tests the ReadFileObject function on a definition with return types.""" |
| 256 |
artifact_reader = reader.YamlArtifactsReader() |
| 257 |
|
| 258 |
file_object = io.StringIO(initial_value=self._DEFINITION_WITH_RETURN_TYPES) |
| 259 |
with self.assertRaises(errors.FormatError): |
| 260 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 261 |
|
| 262 |
def testReadFileObjectWithoutDoc(self): |
| 263 |
"""Tests the ReadFileObject function on a definition without doc.""" |
| 264 |
artifact_reader = reader.YamlArtifactsReader() |
| 265 |
|
| 266 |
file_object = io.StringIO(initial_value=self._DEFINITION_WITHOUT_DOC) |
| 267 |
with self.assertRaises(errors.FormatError): |
| 268 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 269 |
|
| 270 |
def testReadFileObjectWithoutName(self): |
| 271 |
"""Tests the ReadFileObject function on a definition without name.""" |
| 272 |
artifact_reader = reader.YamlArtifactsReader() |
| 273 |
|
| 274 |
file_object = io.StringIO(initial_value=self._DEFINITION_WITHOUT_NAME) |
| 275 |
with self.assertRaises(errors.FormatError): |
| 276 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 277 |
|
| 278 |
def testReadFileObjectWithoutSources(self): |
| 279 |
"""Tests the ReadFileObject function on a definition without sources.""" |
| 280 |
artifact_reader = reader.YamlArtifactsReader() |
| 281 |
|
| 282 |
file_object = io.StringIO(initial_value=self._DEFINITION_WITHOUT_SOURCES) |
| 283 |
with self.assertRaises(errors.FormatError): |
| 284 |
_ = list(artifact_reader.ReadFileObject(file_object)) |
| 285 |
|
| 286 |
def testReadYamlFile(self): |
| 287 |
"""Tests the ReadFile function.""" |
| 288 |
test_file = self._GetTestFilePath(['definitions.yaml']) |
| 289 |
self._SkipIfPathNotExists(test_file) |
| 290 |
|
| 291 |
artifact_reader = reader.YamlArtifactsReader() |
| 292 |
|
| 293 |
artifact_definitions = list(artifact_reader.ReadFile(test_file)) |
| 294 |
self.assertEqual(len(artifact_definitions), 7) |
| 295 |
|
| 296 |
def testReadDirectory(self): |
| 297 |
"""Tests the ReadDirectory function.""" |
| 298 |
artifact_reader = reader.YamlArtifactsReader() |
| 299 |
test_file = self._GetTestFilePath(['.']) |
| 300 |
|
| 301 |
artifact_definitions = list(artifact_reader.ReadDirectory(test_file)) |
| 302 |
self.assertEqual(len(artifact_definitions), 7) |
| 303 |
|
| 304 |
def testArtifactAsDict(self): |
| 305 |
"""Tests the AsDict function.""" |
| 306 |
test_file = self._GetTestFilePath(['definitions.yaml']) |
| 307 |
self._SkipIfPathNotExists(test_file) |
| 308 |
|
| 309 |
artifact_reader = reader.YamlArtifactsReader() |
| 310 |
|
| 311 |
with open(test_file, 'r', encoding='utf-8') as file_object: |
| 312 |
for artifact_definition in yaml.safe_load_all(file_object): |
| 313 |
artifact_object = artifact_reader.ReadArtifactDefinitionValues( |
| 314 |
artifact_definition) |
| 315 |
self.assertEqual(artifact_definition, artifact_object.AsDict()) |
| 316 |
|
| 317 |
def testDefinitionsAsDict(self): |
| 318 |
"""Tests the AsDict function.""" |
| 319 |
artifact_reader = reader.YamlArtifactsReader() |
| 320 |
|
| 321 |
artifact_definitions = list(artifact_reader.ReadDirectory('data')) |
| 322 |
|
| 323 |
last_artifact_definition = None |
| 324 |
for artifact in artifact_definitions: |
| 325 |
try: |
| 326 |
artifact_definition = artifact.AsDict() |
| 327 |
except errors.FormatError: |
| 328 |
error_location = 'At start' |
| 329 |
if last_artifact_definition: |
| 330 |
error_location = f'After: {last_artifact_definition.name:s}' |
| 331 |
self.fail(f'{error_location:s} failed to convert to dict') |
| 332 |
last_artifact_definition = artifact_definition |
| 333 |
|
| 334 |
|
| 335 |
class JsonArtifactsReaderTest(test_lib.BaseTestCase): |
| 336 |
"""JSON artifacts reader tests.""" |
| 337 |
|
| 338 |
def testReadJsonFile(self): |
| 339 |
"""Tests the ReadFile function.""" |
| 340 |
test_file = self._GetTestFilePath(['definitions.json']) |
| 341 |
self._SkipIfPathNotExists(test_file) |
| 342 |
|
| 343 |
artifact_reader = reader.JsonArtifactsReader() |
| 344 |
|
| 345 |
artifact_definitions = list(artifact_reader.ReadFile(test_file)) |
| 346 |
|
| 347 |
self.assertEqual(len(artifact_definitions), 7) |
| 348 |
|
| 349 |
|
| 350 |
if __name__ == '__main__': |
| 351 |
unittest.main() |