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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
# -*- coding: utf-8 -*-
"""Tests for the artifact definitions readers."""
from __future__ import unicode_literals
import io
import unittest
import yaml
from artifacts import definitions
from artifacts import errors
from artifacts import reader
from tests import test_lib
class YamlArtifactsReaderTest(test_lib.BaseTestCase):
"""YAML artifacts reader tests."""
_DEFINITION_INVALID_LABELS = """\
name: BadLabel
doc: badlabel.
sources:
- type: ARTIFACT_GROUP
attributes:
names:
- 'SystemEventLogEvtx'
labels: Logs
supported_os: [Windows]
"""
_DEFINITION_INVALID_SUPPORTED_OS_1 = """\
name: BadSupportedOS
doc: supported_os should be an array of strings.
sources:
- type: ARTIFACT_GROUP
attributes:
names:
- 'SystemEventLogEvtx'
labels: [Logs]
supported_os: Windows
"""
_DEFINITION_INVALID_SUPPORTED_OS_2 = """\
name: BadTopSupportedOS
doc: Top supported_os should match supported_os from sources.
sources:
- type: ARTIFACT_GROUP
attributes:
names:
- 'SystemEventLogEvtx'
supported_os: [Windows]
labels: [Logs]
"""
_DEFINITION_INVALID_URLS = """\
name: BadUrls
doc: badurls.
sources:
- type: ARTIFACT_GROUP
attributes:
names:
- 'SystemEventLogEvtx'
supported_os: [Windows]
urls: 'http://example.com'
"""
_DEFINITION_WITH_EXTRA_KEY = """\
name: WithExtraKey
doc: definition with extra_key
sources:
- type: ARTIFACT_GROUP
attributes:
names:
- 'SystemEventLogEvtx'
extra_key: 'wrong'
labels: [Logs]
supported_os: [Windows]
"""
_DEFINITION_WITH_RETURN_TYPES = """\
name: WithReturnTypes
doc: definition with return_types
sources:
- type: ARTIFACT_GROUP
attributes:
names: [WindowsRunKeys, WindowsServices]
returned_types: [PersistenceFile]
"""
_DEFINITION_WITHOUT_DOC = """\
name: NoDoc
sources:
- type: ARTIFACT_GROUP
attributes:
names:
- 'SystemEventLogEvtx'
"""
_DEFINITION_WITHOUT_NAME = """\
name: NoNames
doc: Missing names attr.
sources:
- type: ARTIFACT_GROUP
attributes:
- 'SystemEventLogEvtx'
"""
_DEFINITION_WITHOUT_SOURCES = """\
name: BadSources
doc: must have one sources.
labels: [Logs]
supported_os: [Windows]
"""
def testReadFileObject(self):
"""Tests the ReadFileObject function."""
test_file = self._GetTestFilePath(['definitions.yaml'])
self._SkipIfPathNotExists(test_file)
artifact_reader = reader.YamlArtifactsReader()
with open(test_file, 'rb') as file_object:
artifact_definitions = list(artifact_reader.ReadFileObject(file_object))
self.assertEqual(len(artifact_definitions), 7)
# Artifact with file source type.
artifact_definition = artifact_definitions[0]
self.assertEqual(artifact_definition.name, 'SecurityEventLogEvtx')
expected_description = (
'Windows Security Event log for Vista or later systems.')
self.assertEqual(artifact_definition.description, expected_description)
self.assertEqual(len(artifact_definition.sources), 1)
source_type = artifact_definition.sources[0]
self.assertIsNotNone(source_type)
self.assertEqual(
source_type.type_indicator, definitions.TYPE_INDICATOR_FILE)
expected_paths = [
'%%environ_systemroot%%\\System32\\winevt\\Logs\\Security.evtx'
]
self.assertEqual(sorted(source_type.paths), sorted(expected_paths))
self.assertEqual(len(artifact_definition.conditions), 1)
expected_condition = 'os_major_version >= 6'
self.assertEqual(artifact_definition.conditions[0], expected_condition)
self.assertEqual(len(artifact_definition.labels), 1)
self.assertEqual(artifact_definition.labels[0], 'Logs')
self.assertEqual(len(artifact_definition.supported_os), 1)
self.assertEqual(artifact_definition.supported_os[0], 'Windows')
self.assertEqual(len(artifact_definition.urls), 1)
expected_url = (
'http://www.forensicswiki.org/wiki/Windows_XML_Event_Log_(EVTX)')
self.assertEqual(artifact_definition.urls[0], expected_url)
# Artifact with Windows Registry key source type.
artifact_definition = artifact_definitions[1]
self.assertEqual(
artifact_definition.name, 'AllUsersProfileEnvironmentVariable')
self.assertEqual(len(artifact_definition.sources), 1)
source_type = artifact_definition.sources[0]
self.assertIsNotNone(source_type)
self.assertEqual(
source_type.type_indicator,
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_KEY)
expected_key1 = (
'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\'
'ProfileList\\ProfilesDirectory')
expected_key2 = (
'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\'
'ProfileList\\AllUsersProfile')
expected_keys = [expected_key1, expected_key2]
self.assertEqual(sorted(source_type.keys), sorted(expected_keys))
# Artifact with Windows Registry value source type.
artifact_definition = artifact_definitions[2]
self.assertEqual(artifact_definition.name, 'CurrentControlSet')
self.assertEqual(len(artifact_definition.sources), 1)
source_type = artifact_definition.sources[0]
self.assertIsNotNone(source_type)
self.assertEqual(
source_type.type_indicator,
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE)
self.assertEqual(len(source_type.key_value_pairs), 1)
key_value_pair = source_type.key_value_pairs[0]
expected_key = 'HKEY_LOCAL_MACHINE\\SYSTEM\\Select'
self.assertEqual(key_value_pair['key'], expected_key)
self.assertEqual(key_value_pair['value'], 'Current')
# Artifact with WMI query source type.
artifact_definition = artifact_definitions[3]
self.assertEqual(artifact_definition.name, 'WMIProfileUsersHomeDir')
expected_provides = sorted(['users.homedir'])
self.assertEqual(sorted(artifact_definition.provides), expected_provides)
self.assertEqual(len(artifact_definition.sources), 1)
source_type = artifact_definition.sources[0]
self.assertIsNotNone(source_type)
self.assertEqual(
source_type.type_indicator, definitions.TYPE_INDICATOR_WMI_QUERY)
expected_query = (
'SELECT * FROM Win32_UserProfile WHERE SID=\'%%users.sid%%\'')
self.assertEqual(source_type.query, expected_query)
# Artifact with artifact definition source type.
artifact_definition = artifact_definitions[4]
self.assertEqual(artifact_definition.name, 'EventLogs')
self.assertEqual(len(artifact_definition.sources), 1)
source_type = artifact_definition.sources[0]
self.assertIsNotNone(source_type)
self.assertEqual(
source_type.type_indicator, definitions.TYPE_INDICATOR_ARTIFACT_GROUP)
# Artifact with command definition source type.
artifact_definition = artifact_definitions[5]
self.assertEqual(artifact_definition.name, 'RedhatPackagesList')
self.assertEqual(len(artifact_definition.sources), 1)
source_type = artifact_definition.sources[0]
self.assertIsNotNone(source_type)
self.assertEqual(
source_type.type_indicator, definitions.TYPE_INDICATOR_COMMAND)
# Artifact with COMMAND definition collector definition.
artifact_definition = artifact_definitions[5]
self.assertEqual(artifact_definition.name, 'RedhatPackagesList')
self.assertEqual(len(artifact_definition.sources), 1)
collector_definition = artifact_definition.sources[0]
self.assertIsNotNone(collector_definition)
self.assertEqual(
collector_definition.type_indicator, definitions.TYPE_INDICATOR_COMMAND)
def testReadFileObjectInvalidLabels(self):
"""Tests the ReadFileObject function on an invalid labels."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_INVALID_LABELS)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectInvalidSupportedOS(self):
"""Tests the ReadFileObject function on an invalid supported_os."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(
initial_value=self._DEFINITION_INVALID_SUPPORTED_OS_1)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
file_object = io.StringIO(
initial_value=self._DEFINITION_INVALID_SUPPORTED_OS_2)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectInvalidURLs(self):
"""Tests the ReadFileObject function on an invalid urls."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_INVALID_URLS)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectWithExtraKey(self):
"""Tests the ReadFileObject function on a definition with extra key."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_WITH_EXTRA_KEY)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectWithReturnTypes(self):
"""Tests the ReadFileObject function on a definition with return types."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_WITH_RETURN_TYPES)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectWithoutDoc(self):
"""Tests the ReadFileObject function on a definition without doc."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_WITHOUT_DOC)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectWithoutName(self):
"""Tests the ReadFileObject function on a definition without name."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_WITHOUT_NAME)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadFileObjectWithoutSources(self):
"""Tests the ReadFileObject function on a definition without sources."""
artifact_reader = reader.YamlArtifactsReader()
file_object = io.StringIO(initial_value=self._DEFINITION_WITHOUT_SOURCES)
with self.assertRaises(errors.FormatError):
_ = list(artifact_reader.ReadFileObject(file_object))
def testReadYamlFile(self):
"""Tests the ReadFile function."""
test_file = self._GetTestFilePath(['definitions.yaml'])
self._SkipIfPathNotExists(test_file)
artifact_reader = reader.YamlArtifactsReader()
artifact_definitions = list(artifact_reader.ReadFile(test_file))
self.assertEqual(len(artifact_definitions), 7)
def testReadDirectory(self):
"""Tests the ReadDirectory function."""
artifact_reader = reader.YamlArtifactsReader()
test_file = self._GetTestFilePath(['.'])
artifact_definitions = list(artifact_reader.ReadDirectory(test_file))
self.assertEqual(len(artifact_definitions), 7)
def testArtifactAsDict(self):
"""Tests the AsDict function."""
test_file = self._GetTestFilePath(['definitions.yaml'])
self._SkipIfPathNotExists(test_file)
artifact_reader = reader.YamlArtifactsReader()
with open(test_file, 'r') as file_object:
for artifact_definition in yaml.safe_load_all(file_object):
artifact_object = artifact_reader.ReadArtifactDefinitionValues(
artifact_definition)
self.assertEqual(artifact_definition, artifact_object.AsDict())
def testDefinitionsAsDict(self):
"""Tests the AsDict function."""
artifact_reader = reader.YamlArtifactsReader()
artifact_definitions = list(artifact_reader.ReadDirectory('data'))
last_artifact_definition = None
for artifact in artifact_definitions:
try:
artifact_definition = artifact.AsDict()
except errors.FormatError:
error_location = 'At start'
if last_artifact_definition:
error_location = 'After: {0}'.format(last_artifact_definition.name)
self.fail('{0} failed to convert to dict'.format(error_location))
last_artifact_definition = artifact_definition
class JsonArtifactsReaderTest(test_lib.BaseTestCase):
"""JSON artifacts reader tests."""
def testReadJsonFile(self):
"""Tests the ReadFile function."""
test_file = self._GetTestFilePath(['definitions.json'])
self._SkipIfPathNotExists(test_file)
artifact_reader = reader.JsonArtifactsReader()
artifact_definitions = list(artifact_reader.ReadFile(test_file))
self.assertEqual(len(artifact_definitions), 7)
if __name__ == '__main__':
unittest.main()
|