def __init__(self, env=None, use_config_file=True, **kwargs): """Creates an interactive session. Args: env: If passed we use this dict as the local environment. use_config_file: If True we merge the system's config file into the session. This helps set the correct profile paths for example. kwargs: Arbitrary parameters to store in the session. Returns: an interactive session object. """ # When this session was created. self._start_time = time.time() # These keep track of the last run plugin. self._last_plugin = None # Fill the session with helpful defaults. self.pager = obj.NoneObject("Set this to your favourite pager.") # Set the session name self.session_name = kwargs.pop("session_name", u"Default session") super(InteractiveSession, self).__init__() # Prepare the local namespace for the interactive session. self.locals = DynamicNameSpace( session=self, # Prepopulate the namespace with our most important modules. profile=self.profile, v=self.v, # Some useful modules which should be available always. sys=sys, os=os, # A list of sessions. session_list=self.session_list, # Pass additional environment. **(env or {})) with self.state: self.state.Set("session_list", self.session_list) self.state.Set("session_name", self.session_name) self.state.Set("session_id", self._new_session_id()) # Configure the session from the config file and kwargs. if use_config_file: with self.state: config.MergeConfigOptions(self.state, self) with self.state: for k, v in kwargs.items(): self.state.Set(k, v)
def MakeUserSession(self, config_options=None): if config_options is None: config_options = self.config_options or {} user_session = rekall_session.InteractiveSession() with user_session.state as state: config.MergeConfigOptions(state, user_session) for k, v in config_options.items(): if k.startswith("--"): state.Set(k[2:], v) return user_session
def LoadProfileIntoSession(parser, argv, user_session): # Figure out the profile. argv = argv or sys.argv known_args, _ = parser.parse_known_args(args=_TruncateARGV(argv)) # Force debug level logging with the verbose flag. if getattr(known_args, "verbose", None): known_args.logging = "DEBUG" with user_session.state as state: config.MergeConfigOptions(state) for arg, value in known_args.__dict__.items(): state.Set(arg, value) # Now load the third party user plugins. These may introduce additional # plugins with args. LoadPlugins(user_session.state.plugin)
def LoadProfileIntoSession(parser, argv, user_session): # Figure out the profile. argv = argv or sys.argv known_args, _ = parser.parse_known_args(args=_TruncateARGV(argv)) with user_session.state as state: config.MergeConfigOptions(state) for arg, value in vars(known_args).items(): state.Set(arg, value) # Enforce the appropriate logging level if user supplies the --verbose # or --quiet command line flags. verbose_flag = getattr(known_args, "verbose", None) quiet_flag = getattr(known_args, "quiet", None) if verbose_flag and quiet_flag: raise ValueError("Cannot set both --verbose and --quiet!") if verbose_flag: state.Set("logging", "debug") elif quiet_flag: state.Set("logging", "critical") else: state.Set("logging", "warn") # Now load the third party user plugins. These may introduce additional # plugins with args. LoadPlugins(user_session.state.plugin) session_filename = getattr(known_args, "session_filename", None) if session_filename: try: user_session.LoadFromFile(session_filename) # Set the command line args once again, in case they override # something in the stored session. with user_session.state as state: for arg, value in known_args.__dict__.items(): state.Set(arg, value) except IOError: pass
def ImportEnvironment(**kwargs): """Initialize a caller's environment. Creates a new interactive environment and installs it into the caller's local namespace. After this call the usual rekall interactive environment will be added in the caller's local namespace. For example: from rekall import interactive interactive.ImportEnvironment() # Update the filename, load profile etc. rekal filename="xpimage.dd" # Run the pslist command rendering to stdout. print pslist() """ isession = session.InteractiveSession(**kwargs) with isession.state as state: config.MergeConfigOptions(state) stack = inspect.stack() # pylint: disable=protected-access isession._locals = stack[1][0].f_locals isession._prepare_local_namespace() # For IPython fix up the completion. try: shell = IPython.get_ipython() if shell: shell.Completer.matchers.insert( 0, lambda x: ipython_support.RekallCompleter(shell.Completer, x)) shell.Completer.merge_completions = False except Exception, e: print e
def __init__(self, env=None, use_config_file=True, **kwargs): """Creates an interactive session. Args: env: If passed we use this dict as the local environment. use_config_file: If True we merge the system's config file into the session. This helps set the correct profile paths for example. kwargs: Arbitrary parameters to store in the session. Returns: an interactive session object. """ self._locals = env or {} # These are the command plugins which we exported to the local # namespace. self._start_time = time.time() # These keep track of the last run plugin. self._last_plugin = None # Fill the session with helpful defaults. self.pager = obj.NoneObject("Set this to your favourite pager.") self.help_profile = None super(InteractiveSession, self).__init__() with self: if use_config_file: config.MergeConfigOptions(self.state) for k, v in kwargs.items(): self.SetParameter(k, v) self.UpdateFromConfigObject()