This file is indexed.

/usr/lib/python2.7/dist-packages/rekall/ipython_support.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
#!/usr/bin/python

# Rekall Memory Forensics
# 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
#

"""Support IPython 4.0."""

# pylint: disable=protected-access

__author__ = "Michael Cohen <scudette@gmail.com>"
import logging
import re
import time

import readline

from pygments.token import Token
from pygments import styles

import IPython
from IPython.core import page
from IPython.core import oinspect
from IPython.core.interactiveshell import InteractiveShell
from IPython.terminal import embed
from IPython.terminal import prompts

try:
    from traitlets.config.loader import Config
except ImportError:
    from IPython.config.loader import Config

from rekall import constants
from rekall import config
from rekall import session as session_module
from rekall import utils


config.DeclareOption(
    "--highlighting_style", default="monokai", type="Choices",
    choices=list(styles.get_all_styles()),
    help="Highlighting style for interactive console.")


def RekallCompleter(self, text):
    """Sophisticated command line completer for Rekall."""
    try:
        command_parts = self.line_buffer.split(" ")
        command = command_parts[0]

        if command.startswith("plugins."):
            command = command[len("plugins."):]

        global_matches = set(self.global_matches(command))

        # Complete strings which look like symbol names.
        m = re.match("\"([^!]+![^\"]*)$", command_parts[-1])
        if m:
            session = self.namespace.get("session")

            # If this is the only match, close the quotes to save typing.
            result = session.address_resolver.search_symbol(m.group(1) + "*")
            if len(result) == 1:
                result = [result[0] + "\""]

            result = [utils.SmartUnicode(x.split("!", 1)[1]) for x in result]
            return result

        # Only complete if there is exactly one object which matches and a space
        # was typed after it. e.g.: pslist <cursor>
        if (command in global_matches and len(command_parts) > 1):
            # Get the object and ask it about the list of args that it supports.
            obj = self.namespace.get(command)
            if obj:
                try:
                    matches = [
                        "%s=" % x["name"] for x in obj.Metadata()["arguments"]]
                    return [utils.SmartUnicode(x)
                            for x in matches if x.startswith(text)]
                except Exception:
                    pass

        return []

    # Wide exception is necessary here because otherwise the completer will
    # swallow all errors.
    except Exception as e:
        logging.debug(e)

    return []


class RekallObjectInspector(oinspect.Inspector):
    """Rekall specific object inspector.

    Rekall populates the environment with "plugin runners" which are proxies of
    the actual plugin that will be invoked. The exact plugin will be invoked
    depending on the profile availability.

    In order to make ipython's ? and ?? operators work, we need to implement
    specialized inspection to present the doc strings and arg list of the actual
    plugin.
    """

    def _format_parameter(self, displayfields, arg):
        desc = arg["help"]
        try:
            type = arg["type"]
            if type in ["Choices", "ChoiceArray"] and arg["choices"]:
                desc += " (type: %s: %s)" % (type, ", ".join(arg["choices"]))
            else:
                desc += " (type: %s)" % type

        except KeyError:
            pass

        displayfields.append(("  " + arg["name"], desc))

    def format_parameters(self, plugin_class, positional=True):
        displayfields = []
        command_metadata = config.CommandMetadata(plugin_class).Metadata()

        # First format positionals:
        for arg in command_metadata["arguments"]:
            if arg.get("hidden"):
                continue

            if arg.get("positional", False) == positional:
                self._format_parameter(displayfields, arg)

        if displayfields:
            return self._format_fields(displayfields)

        return ""

    def plugin_pinfo(self, runner, detail_level=0):
        """Generate info dict for a plugin from a plugin runner."""
        plugin_class = getattr(
            runner.session.plugins, runner.plugin_name)._target

        display_fields = [
            ("file", oinspect.find_file(plugin_class)),
            ("Plugin", "%s (%s)" % (plugin_class.__name__, plugin_class.name))]
        if getattr(plugin_class, "table_header", None):
            display_fields.append(
                ("", "This is a Typed Plugin."))

        display_fields += [
            ("Positional Args",
             self.format_parameters(plugin_class, True)),
            ("Keyword Args",
             self.format_parameters(plugin_class, False)),
            ("Docstring", oinspect.getdoc(plugin_class) or ""),
            ("Link", (
                "http://www.rekall-forensic.com/epydocs/%s.%s-class.html" % (
                    plugin_class.__module__, plugin_class.__name__))),
        ]

        # Include the source if required.
        if detail_level > 0:
            info = self.info(plugin_class, detail_level=detail_level)
            display_fields.append(("source", self.format(info["source"])))

        return self._format_fields(display_fields)

    def pinfo(self, obj, oname='', formatter=None, info=None,
              detail_level=0, **kw):
        if isinstance(obj, session_module.PluginRunner):
            # Delegate info generation for PluginRunners.
            result = self.plugin_pinfo(obj, detail_level=detail_level)
            if result:
                page.page(result)

        else:
            oinspect.Inspector.pinfo(
                self, obj, oname=oname, formatter=formatter,
                info=info, detail_level=detail_level)


class RekallShell(embed.InteractiveShellEmbed):
    banner = display_banner = constants.BANNER

    def atexit_operations(self):
        self.user_module.session.Flush()

    def init_inspector(self):
        super(RekallShell, self).init_inspector()

        # This is a hack but seems the only way to make get_ipython() work
        # properly.
        InteractiveShell._instance = self
        ipython_version = IPython.__version__

        # IPython 5 (4 should work too) is the one we standardize on right
        # now. This means we support earlier ones but turn off the bells and
        # whistles.
        if "4.0.0" <= ipython_version < "6.0.0":
            self.inspector = RekallObjectInspector()

        else:
            self.user_ns.session.logging.warn(
                "Warning: IPython version %s not fully supported. "
                "We recommend installing IPython version 5.",
                ipython_version)


REGISTERED_MAGICS = []

class RekallPrompt(prompts.Prompts):
    def in_prompt_tokens(self, cli=None):
        session = self.shell.user_module.session
        style = session.GetParameter("highlighting_style")
        old_style = self.shell.highlighting_style
        if style != old_style:
            try:
                self.shell.highlighting_style = style
            except Exception:
                self.shell.highlighting_style = old_style
                session.logging.error(
                    "Style %s not valid. Valid styles are %s" %
                    (style, list(styles.get_all_styles())))

        return [
            (Token.Prompt, "["),
            (Token.Name.Variable, str(session.session_id)),
            (Token.Prompt, "] "),
            (Token.Name.Class, str(session.session_name)),
            (Token.Prompt, " "),
            (Token.Comment, time.strftime("%H:%M:%S")),
            (Token.Prompt, "> "),
        ]

    def out_prompt_tokens(self):
        return [
            (Token.OutPrompt, 'Out<'),
            (Token.Comment, time.strftime("%H:%M:%S")),
            (Token.OutPrompt, '> '),
        ]


def Shell(user_session):
    # This should bring back the old autocall behaviour. e.g.:
    # In [1]: pslist
    cfg = Config()
    cfg.InteractiveShellEmbed.autocall = 2
    cfg.TerminalInteractiveShell.prompts_class = RekallPrompt
    cfg.InteractiveShell.separate_in = ''
    cfg.InteractiveShell.separate_out = ''
    cfg.InteractiveShell.separate_out2 = ''

    shell = RekallShell(config=cfg, user_ns=user_session.locals)

    shell.Completer.merge_completions = False
    shell.exit_msg = constants.GetQuote()
    shell.set_custom_completer(RekallCompleter, 0)

    # Do we need to pre-run something?
    if user_session.run != None:
        execfile(user_session.run, user_session.locals)

    user_session.shell = shell

    # Set known delimeters for the completer. This varies by OS so we need to
    # set it to ensure consistency.
    readline.set_completer_delims(' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?')

    for magic in REGISTERED_MAGICS:
        shell.register_magics(magic)

    shell(module=user_session.locals, )

    return True