| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Report statistics about the artifact collection.""" |
| 4 |
|
| 5 |
import collections |
| 6 |
import sys |
| 7 |
import time |
| 8 |
|
| 9 |
from artifacts import definitions |
| 10 |
from artifacts import reader |
| 11 |
|
| 12 |
|
| 13 |
class ArtifactStatistics(object): |
| 14 |
"""Generate and print statistics about artifact definitions.""" |
| 15 |
|
| 16 |
def __init__(self): |
| 17 |
"""Initializes artifact statistics.""" |
| 18 |
super(ArtifactStatistics, self).__init__() |
| 19 |
self._os_counts = collections.Counter() |
| 20 |
self._path_count = 0 |
| 21 |
self._reg_key_count = 0 |
| 22 |
self._source_type_counts = {} |
| 23 |
self._total_count = 0 |
| 24 |
|
| 25 |
def _PrintDictAsTable(self, title, src_dict): |
| 26 |
"""Prints a table of artifact definitions. |
| 27 |
|
| 28 |
Args: |
| 29 |
title (str): title of the table. |
| 30 |
src_dict (dict[str, ArtifactDefinition]): artifact definitions by name. |
| 31 |
""" |
| 32 |
print(f'### {title:s}') |
| 33 |
print('') |
| 34 |
print('Identifier | Number') |
| 35 |
print('--- | ---') |
| 36 |
|
| 37 |
for key, value in sorted(src_dict.items()): |
| 38 |
print(f'{key:s} | {value:d}') |
| 39 |
|
| 40 |
print('') |
| 41 |
|
| 42 |
def PrintOSTable(self): |
| 43 |
"""Prints a table of artifact definitions by operating system.""" |
| 44 |
self._PrintDictAsTable('Operating systems', self._os_counts) |
| 45 |
|
| 46 |
def PrintSourceTypeTable(self): |
| 47 |
"""Prints a table of artifact definitions by source type.""" |
| 48 |
self._PrintDictAsTable( |
| 49 |
'Artifact definition source types', self._source_type_counts) |
| 50 |
|
| 51 |
def PrintSummaryTable(self): |
| 52 |
"""Prints a summary table.""" |
| 53 |
date_time_string = time.strftime('%Y-%m-%d') |
| 54 |
|
| 55 |
print(f"""Status of the repository as of {date_time_string:s} |
| 56 |
|
| 57 |
Description | Number |
| 58 |
--- | --- |
| 59 |
Number of artifact definitions: | {self._total_count:d} |
| 60 |
Number of file paths: | {self._path_count:d} |
| 61 |
Number of Windows Registry key paths: | {self._reg_key_count:d} |
| 62 |
""") |
| 63 |
|
| 64 |
def BuildStats(self): |
| 65 |
"""Builds the statistics.""" |
| 66 |
artifact_reader = reader.YamlArtifactsReader() |
| 67 |
self._os_counts = collections.Counter() |
| 68 |
self._path_count = 0 |
| 69 |
self._reg_key_count = 0 |
| 70 |
self._source_type_counts = {} |
| 71 |
self._total_count = 0 |
| 72 |
|
| 73 |
for artifact_definition in artifact_reader.ReadDirectory('data'): |
| 74 |
sources_supported_os = set() |
| 75 |
for source in artifact_definition.sources: |
| 76 |
self._total_count += 1 |
| 77 |
source_type = source.type_indicator |
| 78 |
self._source_type_counts[source_type] = self._source_type_counts.get( |
| 79 |
source_type, 0) + 1 |
| 80 |
|
| 81 |
if source_type == definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_KEY: |
| 82 |
self._reg_key_count += len(source.keys) |
| 83 |
elif source_type == definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE: |
| 84 |
self._reg_key_count += len(source.key_value_pairs) |
| 85 |
elif source_type in (definitions.TYPE_INDICATOR_FILE, |
| 86 |
definitions.TYPE_INDICATOR_DIRECTORY): |
| 87 |
self._path_count += len(source.paths) |
| 88 |
|
| 89 |
sources_supported_os.update(set(source.supported_os)) |
| 90 |
|
| 91 |
# Fallback to the supported_os defined at definition level if none |
| 92 |
# of the sources specified supported operating systems. |
| 93 |
if not sources_supported_os: |
| 94 |
sources_supported_os = set(artifact_definition.supported_os) |
| 95 |
|
| 96 |
for os_str in sources_supported_os: |
| 97 |
self._os_counts[os_str] += 1 |
| 98 |
|
| 99 |
def PrintStats(self): |
| 100 |
"""Build stats and print in MarkDown format.""" |
| 101 |
data_directory_url = ( |
| 102 |
'https://github.com/ForensicArtifacts/artifacts/tree/main/data') |
| 103 |
|
| 104 |
style_guide_url = ( |
| 105 |
'https://artifacts.readthedocs.io/en/latest/sources/' |
| 106 |
'Format-specification.html') |
| 107 |
|
| 108 |
print(f"""## Statistics |
| 109 |
|
| 110 |
The artifact definitions can be found in the |
| 111 |
[data directory]({data_directory_url:s}) and the format is described in detail |
| 112 |
in the [Style Guide]({style_guide_url:s}). |
| 113 |
""") |
| 114 |
|
| 115 |
self.BuildStats() |
| 116 |
self.PrintSummaryTable() |
| 117 |
self.PrintSourceTypeTable() |
| 118 |
self.PrintOSTable() |
| 119 |
|
| 120 |
|
| 121 |
def Main(): |
| 122 |
"""The main program function. |
| 123 |
|
| 124 |
Returns: |
| 125 |
bool: True if successful or False if not. |
| 126 |
""" |
| 127 |
statsbuilder = ArtifactStatistics() |
| 128 |
statsbuilder.PrintStats() |
| 129 |
return True |
| 130 |
|
| 131 |
|
| 132 |
if __name__ == '__main__': |
| 133 |
if not Main(): |
| 134 |
sys.exit(1) |
| 135 |
else: |
| 136 |
sys.exit(0) |