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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the log2timeline CLI tool."""
from __future__ import unicode_literals
import collections
import os
import platform
import unittest
from plaso.cli import log2timeline_tool
from plaso.lib import definitions
from plaso.lib import errors
from plaso.storage.sqlite import sqlite_file
from tests import test_lib as shared_test_lib
from tests.cli import test_lib
class Log2TimelineToolTest(test_lib.CLIToolTestCase):
"""Tests for the log2timeline CLI tool."""
# pylint: disable=protected-access
_BDE_PASSWORD = 'bde-TEST'
_OUTPUT_ENCODING = 'utf-8'
def _CheckOutput(self, output, expected_output):
"""Compares the output against the expected output.
The actual processing time is ignored, since it can vary.
Args:
output (str): tool output.
expected_output (list[str]): expected tool output.
"""
output = output.split('\n')
self.assertEqual(output[:3], expected_output[:3])
self.assertTrue(output[3].startswith('Processing time\t\t: '))
self.assertEqual(output[4:], expected_output[4:])
def _CreateExtractionOptions(self, source_path, password=None):
"""Create options for testing extraction.
Args:
source_path (str): path of the source (test) data.
password (Optional[str]): password to unlock test data.
Returns:
TestOptions: options for testing extraction.
"""
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.quiet = True
options.single_process = True
options.status_view_mode = 'none'
options.source = source_path
if password:
options.credentials = ['password:{0:s}'.format(password)]
return options
# TODO: add tests for _CheckStorageFile
# TODO: add tests for _CreateProcessingConfiguration
def testGetPluginData(self):
"""Tests the _GetPluginData function."""
test_tool = log2timeline_tool.Log2TimelineTool()
test_tool._data_location = self._GetTestFilePath([])
plugin_info = test_tool._GetPluginData()
self.assertIn('Hashers', plugin_info)
available_hasher_names = [name for name, _ in plugin_info['Hashers']]
self.assertIn('sha256', available_hasher_names)
self.assertIn('sha1', available_hasher_names)
self.assertIn('Parsers', plugin_info)
self.assertIsNotNone(plugin_info['Parsers'])
self.assertIn('Parser Plugins', plugin_info)
self.assertIsNotNone(plugin_info['Parser Plugins'])
def CheckEventCounters(self, storage_file, expected_event_counters):
"""Asserts that the number of events per data type matches.
Args:
storage_file (StorageFile): storage file.
expected_event_counters (dict[str, int|list[int]]): expected event
counters per event data type.
"""
event_counters = collections.Counter()
for event in storage_file.GetSortedEvents():
event_data_identifier = event.GetEventDataIdentifier()
event_data = storage_file.GetEventDataByIdentifier(event_data_identifier)
event_counters[event_data.data_type] += 1
for data_type, expected_event_count in expected_event_counters.items():
event_count = event_counters.pop(data_type, 0)
if isinstance(expected_event_count, list):
self.assertIn(event_count, expected_event_count)
else:
self.assertEqual(event_count, expected_event_count)
# Ensure there are no events left unaccounted for.
self.assertEqual(event_counters, collections.Counter())
# TODO: add tests for _PrintProcessingSummary
def testParseArguments(self):
"""Tests the ParseArguments function."""
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
result = test_tool.ParseArguments([])
self.assertFalse(result)
# TODO: check output.
# TODO: improve test coverage.
def testParseOptions(self):
"""Tests the ParseOptions function."""
test_artifacts_path = self._GetTestFilePath(['artifacts'])
self._SkipIfPathNotExists(test_artifacts_path)
test_file_path = self._GetTestFilePath(['testdir'])
self._SkipIfPathNotExists(test_file_path)
yara_rules_path = self._GetTestFilePath(['yara.rules'])
self._SkipIfPathNotExists(yara_rules_path)
options = test_lib.TestOptions()
options.artifact_definitions_path = test_artifacts_path
options.source = test_file_path
options.storage_file = 'storage.plaso'
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
options.yara_rules_path = yara_rules_path
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
test_tool.ParseOptions(options)
self.assertIsNotNone(test_tool._yara_rules_string)
options = test_lib.TestOptions()
options.artifact_definitions_path = test_artifacts_path
# ParseOptions will raise if source is not set.
with self.assertRaises(errors.BadConfigOption):
test_tool.ParseOptions(options)
options = test_lib.TestOptions()
options.artifact_definitions_path = test_artifacts_path
options.source = test_file_path
with self.assertRaises(errors.BadConfigOption):
test_tool.ParseOptions(options)
# TODO: improve test coverage.
def testExtractEventsFromSourcesOnDirectory(self):
"""Tests the ExtractEventsFromSources function on a directory."""
test_file_path = self._GetTestFilePath(['testdir'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: directory',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesOnAPFSImage(self):
"""Tests the ExtractEventsFromSources function on APFS image."""
test_file_path = self._GetTestFilePath(['apfs.dmg'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: storage media image',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesOnBDEImage(self):
"""Tests the ExtractEventsFromSources function on BDE image."""
test_file_path = self._GetTestFilePath(['bdetogo.raw'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(
test_file_path, password=self._BDE_PASSWORD)
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: storage media image',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesImage(self):
"""Tests the ExtractEventsFromSources function on single partition image."""
test_file_path = self._GetTestFilePath(['ímynd.dd'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: storage media image',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesPartitionedImage(self):
"""Tests the ExtractEventsFromSources function on multi partition image."""
# Note that the source file is a RAW (VMDK flat) image.
test_file_path = self._GetTestFilePath(['multi_partition_image.vmdk'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
options.partitions = 'all'
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: storage media image',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesOnVSSImage(self):
"""Tests the ExtractEventsFromSources function on VSS image."""
test_file_path = self._GetTestFilePath(['vsstest.qcow2'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
options.vss_stores = 'all'
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: storage media image',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'Number of warnings generated while extracting events: 3.',
'',
'Use pinfo to inspect warnings in more detail.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesOnFile(self):
"""Tests the ExtractEventsFromSources function on a file."""
test_file_path = self._GetTestFilePath(['System.evtx'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: single file',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
@unittest.skipIf(platform.system() == 'Windows', 'not supported on Windows')
@unittest.skipIf(
platform.release().endswith('Microsoft'),
'not supported on Windows Subsystem for Linux')
def testExtractEventsFromSourcesOnLinkToDirectory(self):
"""Tests the ExtractEventsFromSources function on a symlink to directory."""
test_file_path = self._GetTestFilePath(['link_to_testdir'])
self._SkipIfPathNotExists(test_file_path)
options = self._CreateExtractionOptions(test_file_path)
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: directory',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
@unittest.skipIf(platform.system() == 'Windows', 'not supported on Windows')
@unittest.skipIf(
platform.release().endswith('Microsoft'),
'not supported on Windows Subsystem for Linux')
def testExtractEventsFromSourcesOnLinkToFile(self):
"""Tests the ExtractEventsFromSources function on a symlink to file."""
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
source_path = self._GetTestFilePath(['link_to_System.evtx'])
options = self._CreateExtractionOptions(source_path)
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
expected_output = [
'',
'Source path\t\t: {0:s}'.format(options.source),
'Source type\t\t: single file',
'Processing time\t\t: 00:00:00',
'',
'Processing started.',
'Processing completed.',
'',
'']
output = output_writer.ReadOutput()
self._CheckOutput(output, expected_output)
def testExtractEventsFromSourcesWithFilestat(self):
"""Tests the ExtractEventsFromSources function with filestat parser."""
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
source_path = self._GetTestFilePath(['test_pe.exe'])
options = self._CreateExtractionOptions(source_path)
options.parsers = 'filestat,pe'
with shared_test_lib.TempDirectory() as temp_directory:
options.storage_file = os.path.join(temp_directory, 'storage.plaso')
options.storage_format = definitions.STORAGE_FORMAT_SQLITE
options.task_storage_format = definitions.STORAGE_FORMAT_SQLITE
test_tool.ParseOptions(options)
test_tool.ExtractEventsFromSources()
storage_file = sqlite_file.SQLiteStorageFile()
try:
storage_file.Open(path=options.storage_file, read_only=True)
except IOError as exception:
self.fail((
'Unable to open storage file after processing with error: '
'{0!s}.').format(exception))
# There should be 3 filestat and 3 pe parser generated events.
# Typically there are 3 filestat events, but there can be 4 on platforms
# that support os.stat_result st_birthtime.
expected_event_counters = {
'fs:stat': [3, 4],
'pe:delay_import:import_time': 1,
'pe:import:import_time': 1,
'pe:compilation:compilation_time': 1}
self.CheckEventCounters(storage_file, expected_event_counters)
def testShowInfo(self):
"""Tests the output of the tool in info mode."""
output_writer = test_lib.TestOutputWriter(encoding=self._OUTPUT_ENCODING)
test_tool = log2timeline_tool.Log2TimelineTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.show_info = True
test_tool.ParseOptions(options)
test_tool.ShowInfo()
output = output_writer.ReadOutput()
section_headings = [
'Hashers', 'Parsers', 'Parser Plugins', 'Parser Presets',
'Versions']
for heading in section_headings:
self.assertIn(heading, output)
self.assertNotIn('<class', output)
if __name__ == '__main__':
unittest.main()
|