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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests the event extraction worker."""
import collections
import unittest
from dfvfs.lib import definitions as dfvfs_definitions
from dfvfs.resolver import context
from dfvfs.path import factory as path_spec_factory
from plaso.containers import events
from plaso.containers import sessions
from plaso.engine import configurations
from plaso.engine import worker
from plaso.parsers import mediator as parsers_mediator
from plaso.storage.fake import writer as fake_writer
from tests.analyzers import manager as analyzers_manager_test
from tests import test_lib as shared_test_lib
class EventExtractionWorkerTest(shared_test_lib.BaseTestCase):
"""Tests for the event extraction worker."""
# pylint: disable=protected-access
def _GetEventDataOfEvent(self, storage_writer, event):
"""Retrieves the event data of an event.
Args:
storage_writer (FakeStorageWriter): storage writer.
event (EventObject): event.
Return:
EventData: event data corresponding to the event.
"""
event_data_identifier = event.GetEventDataIdentifier()
return storage_writer.GetAttributeContainerByIdentifier(
events.EventData.CONTAINER_TYPE, event_data_identifier)
def _GetEventDataStreamOfEventData(self, storage_writer, event_data):
"""Retrieves the event data stream of event data.
Args:
storage_writer (FakeStorageWriter): storage writer.
event_data (EventData): event data.
Return:
EventDataStream: event data stream corresponding to the event data.
"""
event_data_stream_identifier = event_data.GetEventDataStreamIdentifier()
return storage_writer.GetAttributeContainerByIdentifier(
events.EventDataStream.CONTAINER_TYPE, event_data_stream_identifier)
def _GetTestFilePathSpec(self, path_segments):
"""Retrieves a path specification of a test file in the test data directory.
Args:
path_segments (list[str]): components of a path to a test file, relative
to the test_data directory.
Returns:
dfvfs.PathSpec: path specification.
Raises:
SkipTest: if the path inside the test data directory does not exist and
the test should be skipped.
"""
test_file_path = self._GetTestFilePath(path_segments)
self._SkipIfPathNotExists(test_file_path)
return path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
def _TestProcessPathSpec(
self, storage_writer, path_spec, expected_event_data_counts,
archive_types_string=None, extraction_worker=None):
"""Tests processing a path specification.
Args:
storage_writer (StorageWriter): storage writer.
path_spec (dfvfs.PathSpec): path specification.
expected_event_data_counts (dict[str, int|list[int]]): expected counts
of number of event data containers per data type.
archive_types_string (Optional[str]): comma separated archive types for
which embedded file entries should be processed.
extraction_worker (Optional[EventExtractionWorker]): worker to process
the path specification. If None, a new worker will be created.
"""
session = sessions.Session()
resolver_context = context.Context()
parser_mediator = parsers_mediator.ParserMediator(
resolver_context=resolver_context)
parser_mediator.SetStorageWriter(storage_writer)
if not extraction_worker:
configuration = configurations.ExtractionConfiguration()
configuration.archive_types_string = archive_types_string
extraction_worker = worker.EventExtractionWorker()
extraction_worker.SetExtractionConfiguration(configuration)
storage_writer.Open()
try:
storage_writer.AddAttributeContainer(session)
extraction_worker.ProcessPathSpec(parser_mediator, path_spec)
event_source = storage_writer.GetFirstWrittenEventSource()
while event_source:
extraction_worker.ProcessPathSpec(
parser_mediator, event_source.path_spec)
event_source = storage_writer.GetNextWrittenEventSource()
storage_writer.UpdateAttributeContainer(session)
if expected_event_data_counts:
self.CheckEventDataCounts(storage_writer, expected_event_data_counts)
finally:
storage_writer.Close()
def CheckEventDataCounts(self, storage_writer, expected_event_data_counts):
"""Asserts that the number of events per data type matches.
Args:
storage_writer (FakeStorageWriter): storage writer.
expected_event_data_counts (dict[str, int|list[int]]): expected counts
of number of event data containers per data type.
"""
event_counters = collections.Counter()
for event_data in storage_writer.GetAttributeContainers('event_data'):
event_counters[event_data.data_type] += 1
for data_type, expected_event_count in expected_event_data_counts.items():
event_count = event_counters.pop(data_type, 0)
if isinstance(expected_event_count, list):
self.assertIn(event_count, expected_event_count)
else:
error_message = (
'data type: "{0:s}" does not match expected value').format(
data_type)
self.assertEqual(event_count, expected_event_count, error_message)
# Ensure there are no events left unaccounted for.
self.assertEqual(event_counters, collections.Counter())
def testAnalyzeDataStream(self):
"""Tests the _AnalyzeDataStream function."""
session = sessions.Session()
resolver_context = context.Context()
parser_mediator = parsers_mediator.ParserMediator(
resolver_context=resolver_context)
storage_writer = fake_writer.FakeStorageWriter()
parser_mediator.SetStorageWriter(storage_writer)
extraction_worker = worker.EventExtractionWorker()
test_analyzer = analyzers_manager_test.TestAnalyzer()
self.assertEqual(len(test_analyzer.GetResults()), 0)
extraction_worker._analyzers = [test_analyzer]
storage_writer.Open()
storage_writer.AddAttributeContainer(session)
file_entry = self._GetTestFileEntry(['syslog.tgz'])
parser_mediator.SetFileEntry(file_entry)
display_name = parser_mediator.GetDisplayName()
event_data_stream = events.EventDataStream()
extraction_worker._AnalyzeDataStream(
file_entry, '', display_name, event_data_stream)
storage_writer.UpdateAttributeContainer(session)
storage_writer.Close()
self.assertIsNotNone(event_data_stream)
event_attribute = getattr(event_data_stream, 'test_result', None)
self.assertEqual(event_attribute, 'is_vegetable')
def testAnalyzeFileObject(self):
"""Tests the _AnalyzeFileObject function."""
session = sessions.Session()
resolver_context = context.Context()
parser_mediator = parsers_mediator.ParserMediator(
resolver_context=resolver_context)
storage_writer = fake_writer.FakeStorageWriter()
parser_mediator.SetStorageWriter(storage_writer)
extraction_worker = worker.EventExtractionWorker()
test_analyzer = analyzers_manager_test.TestAnalyzer()
self.assertEqual(len(test_analyzer.GetResults()), 0)
extraction_worker._analyzers = [test_analyzer]
storage_writer.Open()
storage_writer.AddAttributeContainer(session)
file_entry = self._GetTestFileEntry(['syslog.tgz'])
parser_mediator.SetFileEntry(file_entry)
file_object = file_entry.GetFileObject()
display_name = parser_mediator.GetDisplayName()
event_data_stream = events.EventDataStream()
extraction_worker._AnalyzeFileObject(
file_object, display_name, event_data_stream)
storage_writer.UpdateAttributeContainer(session)
storage_writer.Close()
self.assertIsNotNone(event_data_stream)
event_attribute = getattr(event_data_stream, 'test_result', None)
self.assertEqual(event_attribute, 'is_vegetable')
def testCanSkipDataStream(self):
"""Tests the _CanSkipDataStream function."""
extraction_worker = worker.EventExtractionWorker()
file_entry = self._GetTestFileEntry(['syslog.tgz'])
result = extraction_worker._CanSkipDataStream(file_entry, None)
self.assertFalse(result)
def testCanSkipContentExtraction(self):
"""Tests the _CanSkipContentExtraction function."""
extraction_worker = worker.EventExtractionWorker()
file_entry = self._GetTestFileEntry(['syslog.tgz'])
result = extraction_worker._CanSkipContentExtraction(file_entry)
self.assertFalse(result)
def testExtractContentFromDataStream(self):
"""Tests the _ExtractContentFromDataStream function."""
session = sessions.Session()
resolver_context = context.Context()
parser_mediator = parsers_mediator.ParserMediator(
resolver_context=resolver_context)
storage_writer = fake_writer.FakeStorageWriter()
parser_mediator.SetStorageWriter(storage_writer)
extraction_worker = worker.EventExtractionWorker()
test_analyzer = analyzers_manager_test.TestAnalyzer()
self.assertEqual(len(test_analyzer.GetResults()), 0)
extraction_worker._analyzers = [test_analyzer]
storage_writer.Open()
storage_writer.AddAttributeContainer(session)
file_entry = self._GetTestFileEntry(['syslog.tgz'])
parser_mediator.SetFileEntry(file_entry)
extraction_worker._ExtractContentFromDataStream(
parser_mediator, file_entry, '')
storage_writer.UpdateAttributeContainer(session)
storage_writer.Close()
# TODO: check results in storage writer
def testExtractMetadataFromFileEntry(self):
"""Tests the _ExtractMetadataFromFileEntry function."""
session = sessions.Session()
resolver_context = context.Context()
parser_mediator = parsers_mediator.ParserMediator(
resolver_context=resolver_context)
storage_writer = fake_writer.FakeStorageWriter()
parser_mediator.SetStorageWriter(storage_writer)
extraction_worker = worker.EventExtractionWorker()
test_analyzer = analyzers_manager_test.TestAnalyzer()
self.assertEqual(len(test_analyzer.GetResults()), 0)
extraction_worker._analyzers = [test_analyzer]
storage_writer.Open()
storage_writer.AddAttributeContainer(session)
file_entry = self._GetTestFileEntry(['syslog.tgz'])
parser_mediator.SetFileEntry(file_entry)
extraction_worker._ExtractMetadataFromFileEntry(
parser_mediator, file_entry, '')
storage_writer.UpdateAttributeContainer(session)
storage_writer.Close()
# TODO: check results in storage writer
def testGetCompressedStreamTypes(self):
"""Tests the _GetCompressedStreamTypes function."""
session = sessions.Session()
resolver_context = context.Context()
parser_mediator = parsers_mediator.ParserMediator(
resolver_context=resolver_context)
storage_writer = fake_writer.FakeStorageWriter()
parser_mediator.SetStorageWriter(storage_writer)
extraction_worker = worker.EventExtractionWorker()
test_analyzer = analyzers_manager_test.TestAnalyzer()
self.assertEqual(len(test_analyzer.GetResults()), 0)
extraction_worker._analyzers = [test_analyzer]
storage_writer.Open()
storage_writer.AddAttributeContainer(session)
extraction_worker = worker.EventExtractionWorker()
path_spec = self._GetTestFilePathSpec(['syslog.tgz'])
type_indicators = extraction_worker._GetCompressedStreamTypes(
parser_mediator, path_spec)
self.assertEqual(type_indicators, [dfvfs_definitions.TYPE_INDICATOR_GZIP])
storage_writer.UpdateAttributeContainer(session)
storage_writer.Close()
def testIsMetadataFile(self):
"""Tests the _IsMetadataFile function."""
extraction_worker = worker.EventExtractionWorker()
file_entry = self._GetTestFileEntry(['syslog.tgz'])
result = extraction_worker._IsMetadataFile(file_entry)
self.assertFalse(result)
# TODO: add tests for _ProcessArchiveTypes
# TODO: add tests for _ProcessCompressedStreamTypes
# TODO: add tests for _ProcessDirectory
# TODO: add tests for _ProcessFileEntry
# TODO: add tests for _ProcessFileEntryDataStream
# TODO: add tests for _ProcessMetadataFile
# TODO: add tests for _SetHashers
# TODO: add tests for _SetYaraRules
# TODO: add tests for GetAnalyzerNames
def testProcessPathSpecFile(self):
"""Tests the ProcessPathSpec function on a file."""
path_spec = self._GetTestFilePathSpec(['syslog', 'syslog'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1,
'syslog:cron:task_run': 3,
'syslog:line': 13}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpecCompressedFileGZIP(self):
"""Tests the ProcessPathSpec function on a gzip compressed file."""
path_spec = self._GetTestFilePathSpec(['syslog.gz'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 2,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpecCompressedFileBZIP2(self):
"""Tests the ProcessPathSpec function on a bzip2 compressed file."""
path_spec = self._GetTestFilePathSpec(['syslog.bz2'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpecCompressedFileXZ(self):
"""Tests the ProcessPathSpec function on a xz compressed file."""
path_spec = self._GetTestFilePathSpec(['syslog.xz'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpec(self):
"""Tests the ProcessPathSpec function on an archive file."""
test_file_path = self._GetTestFilePath(['syslog.tar'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TAR, location='/syslog',
parent=path_spec)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
# Process an archive file without "process archive files" mode.
path_spec = self._GetTestFilePathSpec(['syslog.tar'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
# Process an archive file with "process archive files" mode.
path_spec = self._GetTestFilePathSpec(['syslog.tar'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 2,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='tar,zip')
def testProcessPathSpecCompressedArchive(self):
"""Tests the ProcessPathSpec function on a compressed archive file."""
test_file_path = self._GetTestFilePath(['syslog.tgz'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_GZIP, parent=path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TAR, location='/syslog',
parent=path_spec)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
# Process an archive file with "process archive files" mode.
path_spec = self._GetTestFilePathSpec(['syslog.tgz'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 3,
'syslog:cron:task_run': 3,
'syslog:line': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='tar,zip')
def testProcessPathSpecDMG(self):
"""Tests the ProcessPathSpec function on a DMG image."""
test_file_path = self._GetTestFilePath(['hfsplus_zlib.dmg'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_MODI, parent=path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_GPT, location='/p1',
parent=path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_HFS, location='/',
parent=path_spec)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 7}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpecTarWithDMG(self):
"""Tests the ProcessPathSpec function on a TAR with a DMG image."""
test_file_path = self._GetTestFilePath(['hfsplus_zlib.dmg.tar'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 2}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='tar')
expected_event_data_counts = {
'fs:stat': 9}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='modi,tar')
def testProcessPathSpecISO(self):
"""Tests the ProcessPathSpec function on an ISO image."""
test_file_path = self._GetTestFilePath(['iso9660.raw'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_RAW, parent=path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TSK, location='/',
parent=path_spec)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 5}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpecTarWithISO(self):
"""Tests the ProcessPathSpec function on a TAR with an ISO image."""
test_file_path = self._GetTestFilePath(['iso9660.raw.tar'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 2}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='tar')
expected_event_data_counts = {
'fs:stat': 7}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='iso9660,tar')
def testProcessPathSpecVHD(self):
"""Tests the ProcessPathSpec function on a VHD image."""
test_file_path = self._GetTestFilePath(['image.vhd'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_VHDI, parent=path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_EXT, location='/',
parent=path_spec)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 5}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
def testProcessPathSpecTarWithVHD(self):
"""Tests the ProcessPathSpec function on a TAR with a VHD image."""
test_file_path = self._GetTestFilePath(['image.vhd.tar'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 2}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='tar')
expected_event_data_counts = {
'fs:stat': 7}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
archive_types_string='tar,vhdi')
def testProcessPathSpecVMDK(self):
"""Tests the ProcessPathSpec function on a VMDK with symbolic links."""
test_file_path = self._GetTestFilePath(['image.vmdk'])
self._SkipIfPathNotExists(test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_file_path)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_VMDK, parent=path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_EXT, location='/',
parent=path_spec)
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 6}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts)
# TODO: add tests for SetExtractionConfiguration
# TODO: add tests for SetAnalyzersProfiler
# TODO: add tests for SetProcessingProfiler
# TODO: add tests for SignalAbort
def testExtractionWorkerHashing(self):
"""Test that the worker sets up and runs hashing code correctly."""
extraction_worker = worker.EventExtractionWorker()
extraction_worker._SetHashers('md5')
self.assertIn('hashing', extraction_worker.GetAnalyzerNames())
path_spec = self._GetTestFilePathSpec(['empty_file'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
extraction_worker=extraction_worker)
storage_writer.Open()
empty_file_md5 = 'd41d8cd98f00b204e9800998ecf8427e'
for event in storage_writer.GetSortedEvents():
event_data = self._GetEventDataOfEvent(storage_writer, event)
event_data_stream = self._GetEventDataStreamOfEventData(
storage_writer, event_data)
self.assertEqual(event_data_stream.md5_hash, empty_file_md5)
storage_writer.Close()
def testExtractionWorkerYara(self):
"""Tests that the worker applies Yara matching code correctly."""
yara_rule_path = self._GetTestFilePath(['rules.yara'])
self._SkipIfPathNotExists(yara_rule_path)
with open(yara_rule_path, 'r', encoding='utf-8') as file_object:
rule_string = file_object.read()
extraction_worker = worker.EventExtractionWorker()
extraction_worker._SetYaraRules(rule_string)
self.assertIn('yara', extraction_worker.GetAnalyzerNames())
path_spec = self._GetTestFilePathSpec(['test_pe.exe'])
storage_writer = fake_writer.FakeStorageWriter()
expected_event_data_counts = {
'fs:stat': 1,
'pe_coff:dll_import': 2,
'pe_coff:file': 1}
self._TestProcessPathSpec(
storage_writer, path_spec, expected_event_data_counts,
extraction_worker=extraction_worker)
storage_writer.Open()
expected_yara_match = 'PEfileBasic,PEfile'
for event in storage_writer.GetSortedEvents():
event_data = self._GetEventDataOfEvent(storage_writer, event)
event_data_stream = self._GetEventDataStreamOfEventData(
storage_writer, event_data)
self.assertEqual(event_data_stream.yara_match, expected_yara_match)
storage_writer.Close()
if __name__ == '__main__':
unittest.main()
|