This file is indexed.

/usr/lib/python2.7/dist-packages/rekall/plugin.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
# Rekall Memory Forensics
# Copyright (C) 2012 Michael Cohen
# Copyright 2013 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
#

"""Plugins allow the core rekall system to be extended."""

__author__ = "Michael Cohen <scudette@gmail.com>"

import collections
import copy
import re
import StringIO

from rekall import config
from rekall import obj
from rekall import registry
from rekall import utils
from rekall.ui import text as text_renderer


class Error(Exception):
    """Raised for plugin errors."""


class PluginError(Error):
    """An error occured in a plugin."""


class InvalidArgs(Error):
    """Invalid arguments."""


class Abort(Error):
    """Signal aborting of the plugin."""


class CommandOption(object):
    """An option specification."""

    def __init__(self, name=None, default=None, type="String", choices=None,
                 help="", positional=False, required=False, override=False,
                 hidden=False):
        self.name = name
        self.default = default
        self.type = type
        self.help = help
        self._choices = choices
        self.required = required
        self.positional = positional
        self.override = override
        self.hidden = hidden

    @utils.safe_property
    def choices(self):
        if callable(self._choices):
            return list(self._choices())

        return self._choices

    def add_argument(self, parser):
        """Add ourselves to the parser."""
        prefix = "" if self.positional else "--"
        parser.add_argument(prefix + self.name, default=self.default,
                            type=self.type, help=self.help,
                            positional=self.positional, hidden=self.hidden,
                            required=self.required, choices=self.choices)

    def parse(self, value, session):
        """Parse the value as passed."""
        if value is None:
            if self.default is None:
                # Default values for various types.
                if self.type == "AddressSpace":
                    return session.GetParameter("default_address_space")

                if self.type in ["ArrayStringParser", "ArrayString",
                                 "ArrayIntParser", "Array"]:
                    return []

                if self.type in ["Bool", "Boolean"]:
                    return False

            elif self.type == "RegEx":
                if isinstance(self.default, basestring):
                    return re.compile(self.default)

            return self.default

        # Validate the parsed type. We only support a small set of types right
        # now, so this is good enough.

        # Handle addresses specifically though the address resolver.
        if self.type == "Address" or self.type == "SymbolAddress":
            value = session.address_resolver.get_address_by_name(value)

        elif self.type == "IntParser":
            if isinstance(value, basestring):
                value = int(value, 0)
            else:
                value = int(value)

        elif self.type == "Choices":
            if value not in self.choices:
                raise TypeError("Arg %s must be one of %s" % (
                    self.name, self.choices))

        elif self.type == "ChoiceArray":
            if isinstance(value, basestring):
                value = [value]  # pylint: disable=redefined-variable-type

            for item in value:
                if item not in self.choices:
                    raise TypeError("Arg %s must be one of %s" % (
                        self.name, self.choices))

        elif self.type in ["ArrayString", "ArrayStringParser"]:
            if isinstance(value, basestring):
                value = [value]

            if not isinstance(value, collections.Iterable):
                raise TypeError("Arg %s must be a list of strings" % self.name)

            for item in value:
                if not isinstance(item, basestring):
                    raise TypeError("Arg %s must be a list of strings" %
                                    self.name)

        elif self.type == "Array":
            if isinstance(value, basestring):
                value = [value]

            if not isinstance(value, collections.Iterable):
                raise TypeError("Arg %s must be a list of strings" % self.name)

        elif self.type == "RegEx":
            if isinstance(value, basestring):
                value = re.compile(value, re.I)

        elif self.type == "ArrayIntParser":
            try:
                value = [int(value)]  # pylint: disable=redefined-variable-type
            except (ValueError, TypeError):
                result = []
                for x in value:
                    # RowTuple are treated especially in order to simplify
                    # Efilter syntax.
                    if x.__class__.__name__ == "RowTuple":
                        if len(x) != 1:
                            raise PluginError(
                                "Subselect must only select a single row when "
                                "expanding into a list.")
                        x = int(x[0])
                    else:
                        x = int(x)
                    result.append(x)
                value = result

        # Allow address space to be specified.
        elif self.type == "AddressSpace":
            load_as = session.plugins.load_as(session=session)
            value = load_as.ResolveAddressSpace(value)

        return value


class ModeBasedActiveMixin(object):
    # Specify this mode to decleratively activate this class. To make this work,
    # you will need to define a kb.ParameterHook() that can calculate if the
    # session is running in the specified mode.
    mode = None

    @classmethod
    def is_active(cls, session):
        """Checks we are active.

        This method will be called with the session to check if this specific
        class is active. This mechanism allows multiple implementations to all
        share the same name, as long as only one is actually active. For
        example, we can have a linux, windows and mac version of plugins with
        the "pslist" name.

        This mixin provides the mixed class with a basic is_active() method
        which honors a mode member defined on the class and all its
        subclasses. The mode is additive (meaning each class and its subclasses
        are only active if the mode is active).
        """
        for subclass in cls.__mro__:
            mode = getattr(subclass, "mode", None)

            if isinstance(mode, basestring):
                if not session.GetParameter(mode):
                    return False

            elif isinstance(mode, (list, tuple)):
                for i in mode:
                    if not session.GetParameter(i):
                        return False

        return True



class Command(ModeBasedActiveMixin):
    """A command can be run from the rekall command line.

    Commands can be automatically imported into the shell's namespace and are
    expected to produce textual (or other) output.

    In order to define a new command simply extend this class.
    """

    # these attribute are not inherited.

    # The name of this command (The command will be registered under this
    # name). If empty, the command will not be imported into the namespace but
    # will still be available from the Factory below.
    __name = ""

    # Name of the category of this command. This is used when showing help and
    # in the UI.
    __category = ""

    # This class will not be registered (but extensions will).
    __abstract = True
    __metaclass__ = registry.MetaclassRegistry

    # This declares that this plugin only exists in the interactive session.
    interactive = False

    # This declares that the plugin should not be called upon to collect
    # structs - the default behavior.
    producer = False

    # This will hold the error status from running this plugin.
    error_status = None

    mode = None

    @classmethod
    def args(cls, parser):
        """Declare the command line args this plugin needs."""

    @classmethod
    def GetPrototype(cls, session):
        """Return an instance of this plugin with suitable default arguments.

        In most general applications, types are declared at compile time and
        remain immutable, or at least available throughout the program's
        lifecycle. Rekall, on the other hand, leave many of the decisions
        usually made at type declaration time until late in the runtime,
        when the profile data is available. For this reason, in many of the
        cases when other applications would interrogate classes (for attributes
        and properties, among other things), in Rekall we must interrogate
        their instances, which have access to profile data. In order to
        make this possible slightly earlier in the runtime than when running
        the plugin, we introduce the concept of prototypes, which are
        instances of the plugin or struct with the current session and profile
        available, but with no data or arguments set.

        Arguments:
            session

        Returns:
            And instance of this Command with suitable default arguments.
        """
        try:
            return cls(session=session, ignore_required=True)
        except (TypeError, ValueError):
            raise NotImplementedError("Subclasses must override GetPrototype "
                                      "if they require arguments.")

    @registry.classproperty
    def name(cls):  # pylint: disable=no-self-argument
        return getattr(cls, "_%s__name" % cls.__name__, None)

    def __init__(self, ignore_required=False, **kwargs):
        """The constructor for this command.

        Commands can take arbitrary named args and have access to the running
        session.

        Args:
          session: The session we will use. Many options are taken from the
            session by default, if not provided. This allows users to omit
            specifying many options.

          ignore_required: If this is true plugin constructors must allow the
            plugin to be instantiated with no parameters. All parameter
            validation shall be disabled and construction must succeed.
        """
        session = kwargs.pop("session", None)
        if kwargs:
            raise InvalidArgs("Invalid arguments: %s" % unicode(kwargs.keys()))

        super(Command, self).__init__(**kwargs)

        if session == None:
            raise InvalidArgs("A session must be provided.")

        self.session = session
        self.ignore_required = ignore_required

    def get_plugin(self, name, **kwargs):
        """Returns an instance of the named plugin.

        The new plugin will initialized with the current session and optional
        kwargs.
        Args:
          name: The generic name of the plugin (i.e. the __name attribute,
             e.g. pslist).
          kwargs: Extra args to use for instantiating the plugin.
        """
        for cls in self.classes.values():
            if cls.name == name and cls.is_active(self.session):
                return cls(session=self.session, profile=self.profile,
                           **kwargs)

    def __str__(self):
        """Render into a string using the text renderer."""
        fd = StringIO.StringIO()
        ui_renderer = text_renderer.TextRenderer(
            session=self.session, fd=fd)

        with ui_renderer.start(plugin_name=self.name):
            self.render(ui_renderer)

        return fd.getvalue()

    def __repr__(self):
        return "Plugin: %s (%s)" % (self.name, self.__class__.__name__)

    def __iter__(self):
        """Make plugins that define collect iterable, as convenience.

        Because this:
            for x in session.plugins.get_some_data():
                # do stuff

        Is nicer than this:
            for x in session.plugins.get_some_data().collect():
                # do stuff
        """
        if callable(getattr(self, "collect", None)):
            for x in self.collect():
                if x:
                    yield x

        else:
            raise TypeError("%r is not iterable." % self)

    def render(self, renderer):
        """Produce results on the renderer given.

        Each plugin should implement this method to produce output on the
        renderer. The framework will initialize the plugin and provide it with
        some kind of renderer to write output on. The plugin should not assume
        that the renderer is actually TextRenderer, only that the methods
        defined in the BaseRenderer exist.

        Args:
          renderer: A renderer based at rekall.ui.renderer.BaseRenderer.
        """

    @classmethod
    def GetActiveClasses(cls, session):
        """Return only the active commands based on config."""
        for command_cls in cls.classes.values():
            if command_cls.is_active(session):
                yield command_cls


class ProfileCommand(Command):
    """A baseclass for all commands which require a profile."""

    __abstract = True

    PROFILE_REQUIRED = True

    @classmethod
    def args(cls, metadata):
        # Top level args.
        metadata.add_argument(
            "-p", "--profile", critical=True, hidden=True,
            help="Name of the profile to load. This is the "
            "filename of the profile found in the profiles "
            "directory. Profiles are searched in the profile "
            "path order.")

        metadata.add_requirement("profile")

    @classmethod
    def is_active(cls, session):
        if cls.PROFILE_REQUIRED:
            # Note! This will trigger profile autodetection if this plugin is
            # needed. This might be slightly unexpected: When command line
            # completing the available plugins we will trigger profile
            # autodetection in order to determine which plugins are active.
            profile = (session.profile != None and
                       super(ProfileCommand, cls).is_active(session))

            return profile

        else:
            return super(ProfileCommand, cls).is_active(session)

    def __init__(self, profile=None, **kwargs):
        """Baseclass for all plugins which accept a profile.

        Args:
          profile: The kernel profile to use for this command.
        """
        super(ProfileCommand, self).__init__(**kwargs)

        # If a profile was provided we must set it into the session and then use
        # it. (The new profile must control the presence of other dependent
        # plugins and so forms part of the session's state.).
        if profile is not None:
            self.session.profile = profile

        # If the session already has a profile, use it.
        if self.session.HasParameter("profile_obj"):
            self.profile = self.session.profile

        # If the profile is required but the session has nothing yet, force
        # autodetection.
        elif self.PROFILE_REQUIRED:
            # Force autodetection...
            self.profile = self.session.profile

            # Nothing found... bail out!
            if not self.profile:
                raise PluginError(
                    "Profile could not detected. "
                    "Try specifying one explicitly.")
        else:
            self.profile = obj.NoneObject("No profile")


class PluginHeader(object):
    header = None
    by_name = None

    def __init__(self, *columns):
        self.by_name = {}

        for column in columns:
            if not isinstance(column, dict):
                raise TypeError("Plugins declaring table header ahead of "
                                "time MUST do so using the new format ("
                                "using dicts, NOT tuples). Table header %r "
                                "is invalid." % columns)

            name = column.get("name")
            if not name:
                raise ValueError(
                    "Plugins declaring table headers ahead of "
                    "time MUST specify 'name' for each column. "
                    "Table header %r is invalid." % (columns,))

            self.by_name[name] = column

        self.header = copy.deepcopy(columns)

    @utils.safe_property
    def types_in_output(self):
        """What types of thing does this plugin output?

        Returns a set of declared types, each type being either a class object
        or a string name of the class (for profile types, mostly).

        This helps the self-documentation features find plugins based on their
        declared headers. It's also used by 'collect' to find producers.
        """
        for column in self.header:
            t = column.get("type")
            if t:
                yield t

    def __iter__(self):
        return iter(self.header)

    def __getitem__(self, idx):
        return self.header[idx]

    def fill_dict(self, row):
        """Fills out dict with all the declared columns."""
        for header in self.header:
            column_name = header["name"]
            if column_name not in row:
                row[column_name] = None

        return row

    def dictify(self, row):
        """Convert an ordered row into a dict.

        Uses the internal column order to map row names to the dict.
        """
        result = {}
        for idx, header in enumerate(self.header):
            column_name = header["name"]

            try:
                result[column_name] = row[idx]
            except IndexError:
                result[column_name] = None

        return result

    @utils.safe_property
    def all_names(self):
        return set(self.by_name.iterkeys())

    def find_column(self, name):
        """Get the column spec in 'name'."""
        return self.by_name.get(name)

class ArgsParserMixin(object):
    """A Mixin which provides argument parsing and validation."""
    # Each plugin mixin should define a list of CommandOption instances with
    # this name (__args). The constructor will collect these definitions into a
    # self.args parameter available for the plugins at runtime.
    __args = []

    # This will contain the parsed constructor args after the plugin is
    # instantiated.
    plugin_args = None

    def __init__(self, *pos_args, **kwargs):
        self.ignore_required = kwargs.get("ignore_required", False)

        # If this is set we do not enforce required args. This is useful when
        # callers want to instantiate a plugin in order to use its methods as a
        # utility.
        if self.plugin_args is None:
            self.plugin_args = utils.AttributeDict()

        # Collect args in the declared order (basically follow the mro
        # backwards).
        definitions = []
        definitions_classes = {}
        for cls in self.__class__.__mro__:
            args_definition = getattr(cls, "_%s__args" % cls.__name__, [])
            for definition in args_definition:
                # Definitions can be just simple dicts.
                if isinstance(definition, dict):
                    definition = CommandOption(**definition)

                # We have seen this arg before.
                previous_definition = definitions_classes.get(definition.name)
                if previous_definition:
                    # Since we traverse the definition in reverse MRO order,
                    # later definitions should be masked by earlier (more
                    # derived) definitions.
                    continue

                definitions_classes[definition.name] = cls
                definitions.append(definition)

        # Handle positional args by consuming them off the pos_args array in
        # definition order. This allows positional args to be specified either
        # by position, or by keyword.
        positional_args = [x for x in definitions if x.positional]
        if len(positional_args) < len(pos_args):
            raise TypeError("Too many positional args provided.")

        for pos_arg, definition in zip(pos_args, positional_args):
            # If the positional arg is also defined as a keyword arg this is a
            # bug.
            if definition.name in kwargs:
                raise TypeError(
                    "Positional Args %s is also supplied as a keyword arg." %
                    definition.name)

            kwargs[definition.name] = pos_arg

        # Collect all the declared args and parse them.
        for definition in definitions:
            value = kwargs.pop(definition.name, None)
            if (value is None and definition.required and
                    not self.ignore_required):
                raise InvalidArgs("%s is required." % definition.name)

            self.plugin_args[definition.name] = definition.parse(
                value, session=kwargs.get("session"))

        super(ArgsParserMixin, self).__init__(**kwargs)


class TypedProfileCommand(ArgsParserMixin):
    """Mixin that provides the plugin with standardized table output."""

    # Subclasses must override. Has to be a list of column specifications
    # (i.e. list of dicts specifying the columns).
    table_header = None
    table_options = {}

    __args = [
        dict(name="verbosity", default=1, type="IntParser",
             help="An integer reflecting the amount of desired output: "
             "0 = quiet, 10 = noisy."),
    ]

    def __init__(self, *pos_args, **kwargs):
        super(TypedProfileCommand, self).__init__(*pos_args, **kwargs)
        if isinstance(self.table_header, (list, tuple)):
            self.table_header = PluginHeader(*self.table_header)

        # Switch off hidden column when verbosity is high.
        if self.plugin_args.verbosity > 1:
            for descriptor in self.table_header:
                descriptor["hidden"] = False

        super(TypedProfileCommand, self).__init__(*pos_args, **kwargs)

    @classmethod
    def args(cls, parser):
        super(TypedProfileCommand, cls).args(parser)

        # Collect all the declared args and add them to the parser.
        for cls_i in cls.__mro__:
            args_definition = getattr(cls_i, "_%s__args" % cls_i.__name__, [])
            for definition in args_definition:
                if isinstance(definition, dict):
                    definition = CommandOption(**definition)

                # Allow derived classes to override args from base classes.
                if definition.name in parser.args:
                    continue

                definition.add_argument(parser)

    def column_types(self):
        """Returns instances for each column definition.

        The actual objects that are returned when the plugin runs are often
        determined at run time because they depend on the profile loaded.

        This method is used in order to introspect the types of each column
        without actually running the plugin. A plugin must provide an instance
        for each column without running any code. This allows interospectors to
        learn about the output format before running the actual plugin.

        Note that this method should almost always be overloaded. We try to do
        our best here but it is not ideal. Ultimately all plugins will override
        this method and just declare a column_types() method.
        """
        self.session.logging.warn(
            "FIXME: Plugin %s (%s) does not produce typed output. "
            "Please define a column_types() method.",
            self.name, self.__class__.__name__)

        result = {}
        columns = []
        for column in self.table_header:
            column_name = column["name"]

            columns.append(column_name)
            result[column_name] = None

        try:
            for row_count, row in enumerate(self.collect()):
                if isinstance(row, dict):
                    result.update(row)

                elif isinstance(row, (tuple, list)):
                    for item, column_name in zip(row, columns):
                        if result.get(column_name) is None:
                            result[column_name] = item

                # One row is sometimes sufficient to figure out types, but
                # sometimes a plugin will send None as some of its columns
                # so we try a few more rows.
                if None not in result.values() or row_count > 5:
                    break

        except (NotImplementedError, TypeError):
            pass

        return result

    def collect(self):
        """Collect data that will be passed to renderer.table_row."""
        raise NotImplementedError()

    def collect_as_dicts(self):
        for row in self.collect():
            # Its already a dict.
            if isinstance(row, dict):
                yield self.table_header.fill_dict(row)
            else:
                yield self.table_header.dictify(row)

    # Row members which control some output.
    ROW_OPTIONS = set(
        ["depth",
         "annotation",
         "highlight",
         "nowrap",
         "hex_width"]
    )
    def render(self, renderer, **options):
        table_options = self.table_options.copy()
        table_options.update(options)

        output_style = self.session.GetParameter("output_style")
        if output_style == "full":
            table_options["hidden"] = False

        renderer.table_header(self.table_header, **table_options)
        for row in self.collect():
            if isinstance(row, (list, tuple)):
                renderer.table_row(*row, **options)
            else:
                new_row = []
                for column in self.table_header:
                    new_row.append(
                        row.pop(column["name"], None)
                    )

                if set(row) - self.ROW_OPTIONS:
                    raise RuntimeError(
                        "Plugin produced more data than defined columns (%s)." %
                        (list(row),))

                renderer.table_row(*new_row, **row)

    def reflect(self, member):
        column = self.table_header.by_name.get(member)
        if not column:
            raise KeyError("Plugin %r has no column %r." % (self, member))

        t = column.get("type")

        if isinstance(t, type):
            return t

        if not t:
            return None

        if isinstance(t, basestring):
            return self.profile.object_classes.get(t)

    def getkeys(self):
        return self.table_header.keys()

    def get_column(self, name):
        for row in self.collect_as_dicts():
            yield row[name]

    def get_column_type(self, name):
        column = self.table_header.find_column(name)
        if not column:
            return

        type_name = column.get("type")

        # If we don't have a type then we have to actually get the instance from
        # the profile, which will cause a type to be generated at runtime.
        return self.session.profile.GetPrototype(type_name)


class Producer(TypedProfileCommand):
    """Finds and outputs structs of a particular type.

    Producers are very simple plugins that output only a single column
    which contains a struct of 'type_name'. A good example of a producer are
    the individual pslist enumeration methods.
    """

    # The type of the structs that's returned out of collect and render.
    type_name = None

    # Declare that this plugin may be called upon to collect structs.
    producer = True

    @registry.classproperty
    @registry.memoize
    def table_header(self):
        return PluginHeader(dict(type=self.type_name, name=self.type_name))

    def collect(self):
        raise NotImplementedError()

    def produce(self):
        """Like collect, but yields the first column instead of whole row."""
        for row in self.collect():
            yield row[0]


class CachedProducer(Producer):
    """A producer backed by a cached session parameter hook."""

    @utils.safe_property
    def hook_name(self):
        """By convention, the hook name should be the same as our name."""
        # Override if you really want to.
        return self.name

    def collect(self):
        for offset in self.session.GetParameter(self.hook_name):
            yield [self.session.profile.Object(
                type_name=self.type_name,
                offset=offset)]


class KernelASMixin(object):
    """A mixin for those plugins which require a valid kernel address space.

    This class ensures a valid kernel AS exists or an exception is raised.
    """

    __args = [
        dict(name="dtb", type="IntParser", default=None, hidden=True,
             help="The DTB physical address.")
    ]

    def __init__(self, *args, **kwargs):
        """A mixin for plugins which require a valid kernel address space.

        Args:
          dtb: A potential dtb to be used.
        """
        super(KernelASMixin, self).__init__(*args, **kwargs)

        # If the dtb is specified use that as the kernel address space.
        if self.plugin_args.dtb is not None:
            self.kernel_address_space = (
                self.session.kernel_address_space.__class__(
                    base=self.physical_address_space,
                    dtb=self.plugin_args.dtb))
        else:
            # Try to load the AS from the session if possible.
            self.kernel_address_space = self.session.kernel_address_space

        if self.kernel_address_space == None:
            # Try to guess the AS
            self.session.plugins.load_as().GetVirtualAddressSpace()

            self.kernel_address_space = self.session.kernel_address_space

        if self.kernel_address_space == None:
            raise PluginError("kernel_address_space not specified.")


class PhysicalASMixin(object):
    """A mixin for those plugins which require a valid physical address space.

    This class ensures a valid physical AS exists or an exception is raised.
    """

    PHYSICAL_AS_REQUIRED = True

    @classmethod
    def args(cls, metadata):
        super(PhysicalASMixin, cls).args(metadata)
        metadata.add_requirement("physical_address_space")

    def __init__(self, *args, **kwargs):
        """A mixin for those plugins requiring a physical address space.

        Args:
          physical_address_space: The physical address space to use. If not
            specified we use the following options:

            1) session.physical_address_space,

            2) Guess using the load_as() plugin,

            3) Use session.kernel_address_space.base.

        """
        super(PhysicalASMixin, self).__init__(*args, **kwargs)
        self.physical_address_space = self.session.physical_address_space

        if not self.physical_address_space:
            # Try to guess the AS
            self.session.plugins.load_as().GetPhysicalAddressSpace()
            self.physical_address_space = self.session.physical_address_space

        if self.PHYSICAL_AS_REQUIRED and not self.physical_address_space:
            raise PluginError("Physical address space is not set. "
                              "(Try plugins.load_as)")


class PrivilegedMixIn(object):
    def __init__(self, **kwargs):
        super(PrivilegedMixIn, self).__init__(**kwargs)
        if not self.session.privileged:
            raise PluginError(
                "Live analysis is only available for interactive or "
                "privileged sessions.")


class DataInterfaceMixin(object):
    """This declares a plugin to present a table-like data interface."""

    COLUMNS = ()


class PluginOutput(dict):
    plugin_cls = DataInterfaceMixin


class PluginMetadataDatabase(object):
    """A database of all the currently registered plugin's metadata."""

    def __init__(self, session):
        if session == None:
            raise RuntimeError("Session must be set")

        self.session = session
        self.Rebuild()

    def Rebuild(self):
        self.db = {}

        for plugin_cls in Command.classes.itervalues():
            plugin_name = plugin_cls.name
            self.db.setdefault(plugin_name, []).append(
                config.CommandMetadata(plugin_cls))

    def MetadataByName(self, name):
        """Return all Implementations that implement command name."""
        for command_metadata in self.db[name]:
            yield command_metadata

    def GetActivePlugin(self, plugin_name):
        results = []
        for command_metadata in self.db.get(plugin_name, []):
            plugin_cls = command_metadata.plugin_cls
            if plugin_cls.is_active(self.session):
                results.append(command_metadata)

        # We assume there can only be one active plugin implementation. It
        # is an error to have multiple implementations active at the same
        # time.
        if len(results) > 1:
            raise RuntimeError("Multiple plugin implementations for %s: %s" % (
                plugin_name, [x.plugin_cls for x in results]))

        if results:
            return results[0]

        return obj.NoneObject("Plugin not active")

    def Serialize(self):
        result = {}
        for name in self.db:
            command_metadata = self.GetActivePlugin(name)
            if command_metadata:
                result[name] = command_metadata.Metadata()

        return result

    def GetRequirments(self, command_name):
        result = set()
        for metadata in self.db[command_name]:
            result.update(metadata.requirements)

        return result