/usr/lib/python2.7/dist-packages/rekall/plugins/response/forensic_artifacts.py is in python-rekall-core 1.6.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 | # Rekall Memory Forensics
# Copyright 2016 Google Inc. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""This module implements plugins related to forensic artifacts.
https://github.com/ForensicArtifacts
"""
__author__ = "Michael Cohen <scudette@google.com>"
import csv
import datetime
import json
import platform
import os
import StringIO
import sys
import zipfile
import yaml
from artifacts import definitions
from artifacts import errors
from rekall import plugin
from rekall import registry
from rekall import obj
from rekall import yaml_utils
from rekall.ui import text
from rekall.ui import json_renderer
from rekall.plugins.response import common
class ArtifactResult(object):
"""Bundle all the results from an artifact."""
def __init__(self, artifact_name=None, result_type=None, fields=None):
self.artifact_name = artifact_name
self.result_type = result_type
self.results = []
self.fields = fields or []
def __iter__(self):
return iter(self.results)
def add_result(self, **data):
if data:
self.results.append(data)
def merge(self, other):
self.results.extend(other)
def as_dict(self):
return dict(fields=self.fields,
results=self.results,
artifact_name=self.artifact_name,
result_type=self.result_type)
class BaseArtifactResultWriter(object):
"""Writes the results of artifacts."""
__abstract = True
__metaclass__ = registry.MetaclassRegistry
def __init__(self, session=None, copy_files=False,
create_timeline=False):
self.session = session
self.copy_files = copy_files
self.create_timeline = create_timeline
def write_result(self, result):
"""Writes the artifact result."""
def _create_timeline(self, artifact_result):
"""Create a new timeline result from the given result.
We use the output format suitable for the timesketch tool:
https://github.com/google/timesketch/wiki/UserGuideTimelineFromFile
"""
artifact_fields = artifact_result.fields
fields = [
dict(name="message", type="unicode"),
dict(name="timestamp", type="int"),
dict(name="datetime", type="unicode"),
dict(name="timestamp_desc", type="unicode"),
] + artifact_fields
new_result = ArtifactResult(
artifact_name=artifact_result.artifact_name,
result_type="timeline",
fields=fields)
for field in artifact_fields:
# This field is a timestamp - copy the entire row into the timeline.
if field["type"] == "epoch":
for row in artifact_result.results:
new_row = row.copy()
timestamp = row.get(field["name"])
if timestamp is None:
continue
new_row["timestamp"] = int(timestamp)
new_row["datetime"] = datetime.datetime.utcfromtimestamp(
timestamp).strftime("%Y-%m-%dT%H:%M:%S+00:00")
new_row["timestamp_desc"] = artifact_result.artifact_name
new_row["message"] = " ".join(
unicode(row[field["name"]]) for field in artifact_fields
if field["name"] in row)
new_result.add_result(**new_row)
return new_result
def __enter__(self):
return self
def __exit__(self, unused_type, unused_value, unused_traceback):
return
class DirectoryBasedWriter(BaseArtifactResultWriter):
name = "Directory"
def __init__(self, output=None, **kwargs):
super(DirectoryBasedWriter, self).__init__(**kwargs)
self.dump_dir = output
# Check if the directory already exists.
if not os.path.isdir(self.dump_dir):
raise plugin.PluginError("%s is not a directory" % self.dump_dir)
def write_file(self, result):
"""Writes a FileInformation object."""
for row in result.results:
filename = row["filename"]
with open(filename, "rb") as in_fd:
with self.session.GetRenderer().open(
directory=self.dump_dir,
filename=filename, mode="wb") as out_fd:
while 1:
data = in_fd.read(1024*1024)
if not data:
break
out_fd.write(data)
def _write_csv_file(self, out_fd, result):
fieldnames = [x["name"] for x in result.fields]
writer = csv.DictWriter(
out_fd, dialect="excel",
fieldnames=fieldnames)
writer.writeheader()
for row in result.results:
writer.writerow(row)
def write_result(self, result):
"""Writes the artifact result."""
if self.copy_files and result.result_type == "file_information":
try:
self.write_file(result)
except (IOError, OSError) as e:
self.session.logging.warn(
"Unable to copy file %s into output: %s",
result["filename"], e)
with self.session.GetRenderer().open(
directory=self.dump_dir,
filename="artifacts/%s.json" % result.artifact_name,
mode="wb") as out_fd:
out_fd.write(json.dumps(result.as_dict(), sort_keys=True))
with self.session.GetRenderer().open(
directory=self.dump_dir,
filename="artifacts/%s.csv" % result.artifact_name,
mode="wb") as out_fd:
self._write_csv_file(out_fd, result)
if self.create_timeline:
with self.session.GetRenderer().open(
directory=self.dump_dir,
filename="artifacts/%s.timeline.csv" %
result.artifact_name,
mode="wb") as out_fd:
self._write_csv_file(out_fd, self._create_timeline(result))
class ZipBasedWriter(BaseArtifactResultWriter):
name = "Zip"
def __init__(self, output=None, **kwargs):
super(ZipBasedWriter, self).__init__(**kwargs)
self.output = output
def __enter__(self):
self.out_fd = self.session.GetRenderer().open(
filename=self.output, mode="wb").__enter__()
self.outzip = zipfile.ZipFile(self.out_fd, mode="w",
compression=zipfile.ZIP_DEFLATED)
return self
def __exit__(self, *args):
self.outzip.close()
self.out_fd.__exit__(*args)
def _write_csv_file(self, out_fd, result):
fieldnames = [x["name"] for x in result.fields]
writer = csv.DictWriter(
out_fd, dialect="excel",
fieldnames=fieldnames)
writer.writeheader()
for row in result.results:
writer.writerow(row)
def write_file(self, result):
for row in result.results:
filename = row["filename"]
self.outzip.write(filename)
def write_result(self, result):
"""Writes the artifact result."""
if self.copy_files and result.result_type == "file_information":
try:
self.write_file(result)
except (IOError, OSError) as e:
self.session.logging.warn(
"Unable to copy file %s into output: %s",
result["filename"], e)
self.outzip.writestr("artifacts/%s.json" % result.artifact_name,
json.dumps(result.as_dict(), sort_keys=True),
zipfile.ZIP_DEFLATED)
tmp_fd = StringIO.StringIO()
self._write_csv_file(tmp_fd, result)
self.outzip.writestr("artifacts/%s.csv" % result.artifact_name,
tmp_fd.getvalue(),
zipfile.ZIP_DEFLATED)
if self.create_timeline:
tmp_fd = StringIO.StringIO()
self._write_csv_file(tmp_fd, self._create_timeline(result))
self.outzip.writestr("artifacts/%s.timeline.csv" %
result.artifact_name,
tmp_fd.getvalue(),
zipfile.ZIP_DEFLATED)
# Rekall defines a new artifact type.
TYPE_INDICATOR_REKALL = "REKALL_EFILTER"
class _FieldDefinitionValidator(object):
"""Loads and validates fields in a dict.
We check their name, types and if they are optional according to a template
in _field_definitions.
"""
_field_definitions = []
def _LoadFieldDefinitions(self, data, field_definitions):
for field in field_definitions:
name = field["name"]
default = field.get("default")
required_type = field.get("type")
if required_type in (str, unicode):
required_type = basestring
if default is None and required_type is not None:
# basestring cant be instantiated.
if required_type is basestring:
default = ""
else:
default = required_type()
if required_type is None and default is not None:
required_type = type(default)
if not field.get("optional"):
if name not in data:
raise errors.FormatError(
u'Missing fields {}.'.format(name))
value = data.get(name, default)
if default is not None and not isinstance(value, required_type):
raise errors.FormatError(
u'field {} has type {} should be {}.'.format(
name, type(data[name]), required_type))
if field.get("checker"):
value = field["checker"](self, data)
setattr(self, name, value)
class SourceType(_FieldDefinitionValidator):
"""All sources inherit from this."""
# Common fields for all sources.
_common_fields = [
dict(name="type", optional=False),
dict(name="supported_os", optional=True, type=list,
default=list(definitions.SUPPORTED_OS)),
]
def __init__(self, source_definition, artifact=None):
attributes = source_definition["attributes"]
# The artifact that owns us.
self.artifact = artifact
self.source_definition = source_definition
self.type_indicator = source_definition["type"]
self._LoadFieldDefinitions(attributes, self._field_definitions)
self._LoadFieldDefinitions(source_definition, self._common_fields)
def is_active(self, **_):
"""Indicates if the source is applicable to the environment."""
return True
def apply(self, artifact_name=None, fields=None, result_type=None, **_):
"""Generate ArtifactResult instances."""
return ArtifactResult(artifact_name=artifact_name,
result_type=result_type,
fields=fields)
# These are the valid types of Rekall images. They can be used to restrict
# REKALL_EFILTER artifacts to specific types of images. The types which end in
# API refer to the API only version of the similar plugins.
REKALL_IMAGE_TYPES = [
"Windows", "WindowsAPI",
"Linux", "LinuxAPI",
"Darwin", "DarwinAPI"
]
class RekallEFilterArtifacts(SourceType):
"""Class to support Rekall Efilter artifact types."""
allowed_types = {
"int": int,
"unicode": unicode, # Unicode data.
"str": str, # Used for binary data.
"float": float,
"epoch": float, # Dates as epoch timestamps.
"any": str # Used for opaque types that can not be further processed.
}
_field_definitions = [
dict(name="query", type=basestring),
dict(name="query_parameters", default=[], optional=True),
dict(name="fields", type=list),
dict(name="type_name", type=basestring),
dict(name="image_type", type=list, optional=True,
default=REKALL_IMAGE_TYPES),
]
def __init__(self, source_definition, **kw):
super(RekallEFilterArtifacts, self).__init__(source_definition, **kw)
for column in self.fields:
if "name" not in column or "type" not in column:
raise errors.FormatError(
u"Field definition should have both name and type.")
mapped_type = column["type"]
if mapped_type not in self.allowed_types:
raise errors.FormatError(
u"Unsupported type %s." % mapped_type)
def GetImageType(self, session):
"""Returns one of the standard image types based on the session."""
result = session.profile.metadata("os").capitalize()
if session.GetParameter("live_mode") == "API":
result += "API"
return result
def is_active(self, session=None):
"""Determine if this source is active."""
return (self.image_type and
self.GetImageType(session) in self.image_type)
def apply(self, session=None, **kwargs):
result = super(RekallEFilterArtifacts, self).apply(
fields=self.fields, result_type=self.type_name, **kwargs)
if not self.is_active(session):
return
search = session.plugins.search(
query=self.query,
query_parameters=self.query_parameters)
for match in search.solve():
row = {}
for column in self.fields:
name = column["name"]
type = column["type"]
value = match.get(name)
if value is None:
continue
row[name] = RekallEFilterArtifacts.allowed_types[
type](value)
result.add_result(**row)
yield result
class LiveModeSourceMixin(object):
def is_active(self, session=None):
"""Determine if this source is active."""
# We are only active in Live mode (API or Memory).
return (session.GetParameter("live_mode") != None and
session.profile.metadata("os").capitalize() in
self.supported_os)
class FileSourceType(LiveModeSourceMixin, SourceType):
_field_definitions = [
dict(name="paths", default=[]),
dict(name="separator", default="/", type=basestring,
optional=True),
]
# These fields will be present in the ArtifactResult object we return.
_FIELDS = [
dict(name="st_mode", type="unicode"),
dict(name="st_nlink", type="int"),
dict(name="st_uid", type="unicode"),
dict(name="st_gid", type="unicode"),
dict(name="st_size", type="int"),
dict(name="st_mtime", type="epoch"),
dict(name="filename", type="unicode"),
]
def apply(self, session=None, **kwargs):
result = super(FileSourceType, self).apply(
fields=self._FIELDS, result_type="file_information", **kwargs)
for hits in session.plugins.glob(
self.paths, path_sep=self.separator,
root=self.separator).collect():
# Hits are FileInformation objects, and we just pick some of the
# important fields to report.
info = hits["path"]
row = {}
for field in self._FIELDS:
name = field["name"]
type = RekallEFilterArtifacts.allowed_types[field["type"]]
row[name] = type(getattr(info, name))
result.add_result(**row)
yield result
class ArtifactGroupSourceType(SourceType):
_field_definitions = [
dict(name="names", type=list),
dict(name="supported_os", optional=True,
default=definitions.SUPPORTED_OS),
]
def apply(self, collector=None, **_):
for name in self.names:
for result in collector.collect_artifact(name):
yield result
class WMISourceType(LiveModeSourceMixin, SourceType):
_field_definitions = [
dict(name="query", type=basestring),
dict(name="fields", type=list, optional=True, default=[]),
dict(name="type_name", type=basestring, optional=True),
dict(name="supported_os", optional=True,
default=definitions.SUPPORTED_OS),
]
fields = None
def _guess_returned_fields(self, sample):
result = []
for key, value in sample.iteritems():
field_type = type(value)
if field_type is int:
field_type = "int"
elif field_type is str:
field_type = "unicode"
else:
field_type = "unicode"
result.append(dict(name=key, type=field_type))
return result
def apply(self, session=None, **kwargs):
result = super(WMISourceType, self).apply(
result_type=self.type_name, **kwargs)
wmi = session.plugins.wmi(query=self.query)
# The wmi plugin may not exist on non-windows systems.
if wmi == None:
return
for collected in wmi.collect():
match = collected["Result"]
row = {}
# If the user did not specify the fields, we must
# deduce them from the first returned row.
if not self.fields:
self.fields = self._guess_returned_fields(match)
result.fields = self.fields
for column in self.fields:
name = column["name"]
type = column["type"]
value = match.get(name)
if value is None:
continue
row[name] = RekallEFilterArtifacts.allowed_types[
type](value)
result.add_result(**row)
yield result
class RegistryKeySourceType(LiveModeSourceMixin, SourceType):
_field_definitions = [
dict(name="keys", default=[]),
dict(name="supported_os", optional=True,
default=["Windows"]),
]
_FIELDS = [
dict(name="st_mtime", type="epoch"),
dict(name="hive", type="unicode"),
dict(name="key_name", type="unicode"),
dict(name="value", type="str"),
dict(name="value_type", type="str"),
]
def apply(self, session=None, **kwargs):
result = super(RegistryKeySourceType, self).apply(
fields=self._FIELDS, result_type="registry_key", **kwargs)
for hits in session.plugins.glob(
self.keys, path_sep="\\", filesystem="Reg",
root="\\").collect():
# Hits are FileInformation objects, and we just pick some of the
# important fields to report.
info = hits["path"]
row = {}
for field in self._FIELDS:
name = field["name"]
field_type = RekallEFilterArtifacts.allowed_types[field["type"]]
data = info.get(name)
if data is not None:
row[name] = field_type(data)
result.add_result(**row)
yield result
class RegistryValueSourceType(LiveModeSourceMixin, SourceType):
def CheckKeyValuePairs(self, source):
key_value_pairs = source["key_value_pairs"]
for pair in key_value_pairs:
if (not isinstance(pair, dict) or "key" not in pair or
"value" not in pair):
raise errors.FormatError(
u"key_value_pairs should consist of dicts with key and "
"value items.")
return key_value_pairs
_field_definitions = [
dict(name="key_value_pairs", default=[],
checker=CheckKeyValuePairs),
dict(name="supported_os", optional=True,
default=["Windows"]),
]
_FIELDS = [
dict(name="st_mtime", type="epoch"),
dict(name="hive", type="unicode"),
dict(name="key_name", type="unicode"),
dict(name="value_name", type="unicode"),
dict(name="value_type", type="str"),
dict(name="value", type="str"),
]
def apply(self, session=None, **kwargs):
result = super(RegistryValueSourceType, self).apply(
fields=self._FIELDS, result_type="registry_value", **kwargs)
globs = [u"%s\\%s" % (x["key"], x["value"])
for x in self.key_value_pairs]
for hits in session.plugins.glob(
globs, path_sep="\\", filesystem="Reg",
root="\\").collect():
info = hits["path"]
row = {}
for field in self._FIELDS:
name = field["name"]
field_type = RekallEFilterArtifacts.allowed_types[field["type"]]
data = info.get(name)
if data is not None:
row[name] = field_type(data)
result.add_result(**row)
yield result
# This lookup table maps between source type name and concrete implementations
# that we support. Artifacts which contain sources which are not implemented
# will be ignored.
SOURCE_TYPES = {
TYPE_INDICATOR_REKALL: RekallEFilterArtifacts,
definitions.TYPE_INDICATOR_FILE: FileSourceType,
definitions.TYPE_INDICATOR_ARTIFACT_GROUP: ArtifactGroupSourceType,
definitions.TYPE_INDICATOR_WMI_QUERY: WMISourceType,
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_KEY: RegistryKeySourceType,
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE: RegistryValueSourceType,
}
class ArtifactDefinition(_FieldDefinitionValidator):
"""The main artifact class."""
def CheckLabels(self, art_definition):
"""Ensure labels are defined."""
labels = art_definition.get("labels", [])
# Keep unknown labels around in case callers want to check for complete
# label coverage. In most cases it is desirable to allow users to extend
# labels but when super strict validation is required we want to make
# sure that users dont typo a label.
self.undefined_labels = set(labels).difference(definitions.LABELS)
return labels
def BuildSources(self, art_definition):
sources = art_definition["sources"]
result = []
self.unsupported_source_types = []
for source in sources:
if not isinstance(source, dict):
raise errors.FormatError("Source is not a dict.")
source_type_name = source.get("type")
if source_type_name is None:
raise errors.FormatError("Source has no type.")
source_cls = self.source_types.get(source_type_name)
if source_cls:
result.append(source_cls(source, artifact=self))
else:
self.unsupported_source_types.append(source_type_name)
if not result:
if self.unsupported_source_types:
raise errors.FormatError(
"No supported sources: %s" % (
self.unsupported_source_types,))
raise errors.FormatError("No available sources.")
return result
def SupportedOS(self, art_definition):
supported_os = art_definition.get(
"supported_os", definitions.SUPPORTED_OS)
undefined_supported_os = set(supported_os).difference(
definitions.SUPPORTED_OS)
if undefined_supported_os:
raise errors.FormatError(
u'supported operating system: {} '
u'not defined.'.format(
u', '.join(undefined_supported_os)))
return supported_os
_field_definitions = [
dict(name="name", type=basestring),
dict(name="doc", type=basestring),
dict(name="labels", default=[],
checker=CheckLabels, optional=True),
dict(name="sources", default=[],
checker=BuildSources),
dict(name="supported_os",
checker=SupportedOS, optional=True),
dict(name="conditions", default=[], optional=True),
dict(name="returned_types", default=[], optional=True),
dict(name="provides", type=list, optional=True),
dict(name="urls", type=list, optional=True)
]
name = "unknown"
source_types = SOURCE_TYPES
def __init__(self, data, source_types=None):
self.source_types = source_types or SOURCE_TYPES
self.data = data
try:
self._LoadDefinition(data)
except Exception as e:
exc_info = sys.exc_info()
raise errors.FormatError(
"Definition %s: %s" % (self.name, e)), None, exc_info[2]
def set_implementations(self, source_types):
return self.__class__(self.data, source_types)
def _LoadDefinition(self, data):
if not isinstance(data, dict):
raise errors.FormatError(
"Artifact definition must be a dict.")
different_keys = set(data) - definitions.TOP_LEVEL_KEYS
if different_keys:
raise errors.FormatError(u'Undefined keys: {}'.format(
different_keys))
self._LoadFieldDefinitions(data, self._field_definitions)
class ArtifactDefinitionProfileSectionLoader(obj.ProfileSectionLoader):
"""Loads artifacts from the artifact profiles."""
name = "$ARTIFACTS"
def LoadIntoProfile(self, session, profile, art_definitions):
for definition in art_definitions:
try:
profile.AddDefinition(definition)
except errors.FormatError as e:
session.logging.debug(
"Skipping Artifact %s: %s", definition.get("name"), e)
return profile
class ArtifactProfile(obj.Profile):
"""A profile containing artifact definitions."""
# This will contain the definitions.
def __init__(self, *args, **kwargs):
super(ArtifactProfile, self).__init__(*args, **kwargs)
self.definitions = []
self.definitions_by_name = {}
def AddDefinition(self, definition):
"""Add a new definition from a dict."""
self.definitions.append(definition)
self.definitions_by_name[definition["name"]] = definition
def GetDefinitionByName(self, name, source_types=None):
if source_types is None:
source_types = SOURCE_TYPES
definition = self.definitions_by_name[name]
return ArtifactDefinition(definition, source_types)
def GetDefinitions(self, source_types=None):
if source_types is None:
source_types = SOURCE_TYPES
for definition in self.definitions:
try:
yield ArtifactDefinition(definition, source_types)
except errors.FormatError:
pass
class ArtifactsCollector(plugin.TypedProfileCommand,
plugin.Command):
"""Collects artifacts."""
name = "artifact_collector"
__args = [
dict(name="artifacts", positional=True, required=True,
type="ArrayStringParser",
help="A list of artifact names to collect."),
dict(name="artifact_files", type="ArrayStringParser",
help="A list of additional yaml files to load which contain "
"artifact definitions."),
dict(name="definitions", type="ArrayStringParser",
help="An inline artifact definition in yaml format."),
dict(name="create_timeline", type="Bool", default=False,
help="Also generate a timeline file."),
dict(name="copy_files", type="Bool", default=False,
help="Copy files into the output."),
dict(name="writer", type="Choices",
choices=lambda: (
x.name for x in BaseArtifactResultWriter.classes.values()),
help="Writer for artifact results."),
dict(name="output_path",
help="Path suitable for dumping files."),
]
table_header = [
dict(name="divider", type="Divider"),
dict(name="result"),
]
table_options = dict(
suppress_headers=True
)
def column_types(self):
return dict(path=common.FileInformation(filename="/etc"))
def __init__(self, *args, **kwargs):
super(ArtifactsCollector, self).__init__(*args, **kwargs)
self.artifact_profile = self.session.LoadProfile("artifacts")
extra_definitions = [
open(x).read() for x in self.plugin_args.artifact_files]
extra_definitions.extend(self.plugin_args.definitions or [])
# Make a copy of the artifact registry.
if extra_definitions:
self.artifact_profile = self.artifact_profile.copy()
for definition in extra_definitions:
for definition_data in yaml.safe_load_all(definition):
self.artifact_profile.AddDefinition(definition_data)
self.seen = set()
self.supported_os = self.get_supported_os(self.session)
if self.supported_os is None:
raise plugin.PluginError(
"Unable to determine running environment.")
# Make sure the args make sense.
if self.plugin_args.output_path is None:
if self.plugin_args.copy_files:
raise plugin.PluginError(
"Can only copy files when an output file is specified.")
if self.plugin_args.create_timeline:
raise plugin.PluginError(
"Can only create timelines when an output file "
"is specified.")
@classmethod
def get_supported_os(cls, session):
# Determine which context we are running in. If we are running in live
# mode, we use the platform to determine the supported OS, otherwise we
# determine it from the profile.
if session.GetParameter("live"):
return platform.system()
elif session.profile.metadata("os") == "linux":
return "Linux"
elif session.profile.metadata("os") == "windows":
return "Windows"
elif session.profile.metadata("os") == "darwin":
return "Darwin"
def _evaluate_conditions(self, conditions):
# TODO: Implement an expression parser for these. For now we just return
# True always.
return True
def collect_artifact(self, artifact_name):
if artifact_name in self.seen:
return
self.seen.add(artifact_name)
try:
definition = self.artifact_profile.GetDefinitionByName(
artifact_name)
except KeyError:
self.session.logging.error("Unknown artifact %s" % artifact_name)
return
# This artifact is not for us.
if self.supported_os not in definition.supported_os:
self.session.logging.debug(
"Skipping artifact %s: Supported OS: %s, but we are %s",
definition.name, definition.supported_os,
self.supported_os)
return
if not self._evaluate_conditions(definition.conditions):
return
yield dict(divider="Artifact: %s" % definition.name)
for source in definition.sources:
# This source is not for us.
if not source.is_active(session=self.session):
continue
for result in source.apply(
artifact_name=definition.name,
session=self.session,
collector=self):
if isinstance(result, dict):
yield result
else:
yield dict(result=result)
def collect(self):
# Figure out a sensible default for the output writer.
if (self.plugin_args.output_path is not None and
self.plugin_args.writer is None):
if os.path.isdir(self.plugin_args.output_path):
self.plugin_args.writer = "Directory"
else:
self.plugin_args.writer = "Zip"
if self.plugin_args.writer:
impl = BaseArtifactResultWriter.ImplementationByName(
self.plugin_args.writer)
with impl(session=self.session,
copy_files=self.plugin_args.copy_files,
create_timeline=self.plugin_args.create_timeline,
output=self.plugin_args.output_path) as writer:
for x in self._collect(writer=writer):
yield x
else:
for x in self._collect():
yield x
def _collect(self, writer=None):
for artifact_name in self.plugin_args.artifacts:
for hit in self.collect_artifact(artifact_name):
if "result" in hit and writer:
writer.write_result(hit["result"])
yield hit
class ArtifactsView(plugin.TypedProfileCommand,
plugin.Command):
name = "artifact_view"
__args = [
dict(name="artifacts", type="ArrayStringParser", positional=True,
help="A list of artifacts to display")
]
table_header = [
dict(name="divider", type="Divider"),
dict(name="Message")
]
def collect(self):
artifact_profile = self.session.LoadProfile("artifacts")
for artifact in self.plugin_args.artifacts:
definition = artifact_profile.definitions_by_name.get(artifact)
if definition:
yield dict(divider=artifact)
yield dict(Message=yaml_utils.safe_dump(definition))
class ArtifactsList(plugin.TypedProfileCommand,
plugin.Command):
"""List details about all known artifacts."""
name = "artifact_list"
__args = [
dict(name="regex", type="RegEx",
default=".",
help="Filter the artifact name."),
dict(name="supported_os", type="ArrayStringParser", required=False,
help="If specified show for these OSs, otherwise autodetect "
"based on the current image."),
dict(name="labels", type="ArrayStringParser",
help="Filter by these labels."),
dict(name="all", type="Bool",
help="Show all artifacts."),
]
table_header = [
dict(name="Name", width=30),
dict(name="OS", width=8),
dict(name="Labels", width=20),
dict(name="Types", width=20),
dict(name="Description", width=50),
]
def collect(self):
# Empty means autodetect based on the image.
if not self.plugin_args.supported_os:
supported_os = set([
ArtifactsCollector.get_supported_os(self.session)])
else:
supported_os = set(self.plugin_args.supported_os)
for definition in self.session.LoadProfile(
"artifacts").GetDefinitions():
if (not self.plugin_args.all and
not supported_os.intersection(definition.supported_os)):
continue
# Determine the type:
types = set()
for source in definition.sources:
if self.plugin_args.all or source.is_active(
session=self.session):
types.add(source.type_indicator)
if self.plugin_args.regex.match(definition.name):
yield (definition.name, definition.supported_os,
definition.labels, sorted(types), definition.doc)
class ArtifactResult_TextObjectRenderer(text.TextObjectRenderer):
renders_type = "ArtifactResult"
def render_row(self, target, **_):
column_names = [x["name"] for x in target.fields]
table = text.TextTable(
columns=target.fields,
renderer=self.renderer,
session=self.session)
if not target.results:
return text.Cell("")
result = [
text.JoinedCell(*[text.Cell(x) for x in column_names]),
text.JoinedCell(*[text.Cell("-" * len(x)) for x in column_names])]
for row in target.results:
ordered_row = []
for column in column_names:
ordered_row.append(row.get(column))
result.append(table.get_row(*ordered_row))
result = text.StackedCell(*result)
return result
class ArtifactResult_DataExportObjectRenderer(
json_renderer.StateBasedObjectRenderer):
renders_type = "ArtifactResult"
renderers = ["DataExportRenderer"]
def GetState(self, item, **_):
return dict(artifact_name=item.artifact_name,
result_type=item.result_type,
fields=item.fields,
results=item.results)
|