/我手敲的代码(中文注释)/chapter11/volatility-2.3/build/lib/volatility/plugins/gui/sessions.py

https://github.com/giantbranch/python-hacker-code · Python · 109 lines · 56 code · 11 blank · 42 comment · 14 complexity · 0fbce96688b6fffb36b8a42b6b862c13 MD5 · raw file

  1. # Volatility
  2. # Copyright (C) 2007-2013 Volatility Foundation
  3. # Copyright (C) 2010,2011,2012 Michael Hale Ligh <michael.ligh@mnin.org>
  4. #
  5. # This file is part of Volatility.
  6. #
  7. # Volatility is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License Version 2 as
  9. # published by the Free Software Foundation. You may not use, modify or
  10. # distribute this program under any other version of the GNU General
  11. # Public License.
  12. #
  13. # Volatility is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Volatility. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. import volatility.obj as obj
  22. import volatility.utils as utils
  23. import volatility.plugins.common as common
  24. import volatility.win32.modules as modules
  25. import volatility.win32.tasks as tasks
  26. class SessionsMixin(object):
  27. """This is a mixin that plugins can inherit for access to the
  28. main sessions APIs."""
  29. def session_spaces(self, kernel_space):
  30. """ Generators unique _MM_SESSION_SPACE objects
  31. referenced by active processes.
  32. @param space: a kernel AS for process enumeration
  33. @yields _MM_SESSION_SPACE instantiated from the
  34. session space native_vm.
  35. """
  36. seen = []
  37. for proc in tasks.pslist(kernel_space):
  38. if proc.SessionId != None and proc.SessionId.v() not in seen:
  39. ps_ad = proc.get_process_address_space()
  40. if ps_ad != None:
  41. seen.append(proc.SessionId.v())
  42. yield obj.Object("_MM_SESSION_SPACE",
  43. offset = proc.Session.v(), vm = ps_ad)
  44. def find_session_space(self, kernel_space, session_id):
  45. """ Get a session address space by its ID.
  46. @param space: a kernel AS for process enumeration
  47. @param session_id: the session ID to find.
  48. @returns _MM_SESSION_SPACE instantiated from the
  49. session space native_vm.
  50. """
  51. for proc in tasks.pslist(kernel_space):
  52. if proc.SessionId == session_id:
  53. ps_ad = proc.get_process_address_space()
  54. if ps_ad != None:
  55. return obj.Object("_MM_SESSION_SPACE",
  56. offset = proc.Session.v(), vm = ps_ad)
  57. return obj.NoneObject("Cannot locate a session")
  58. class Sessions(common.AbstractWindowsCommand, SessionsMixin):
  59. """List details on _MM_SESSION_SPACE (user logon sessions)"""
  60. def calculate(self):
  61. kernel_space = utils.load_as(self._config)
  62. # Once for each unique _MM_SESSION_SPACE
  63. for session in self.session_spaces(kernel_space):
  64. yield session
  65. def render_text(self, outfd, data):
  66. # Kernel AS for looking up modules
  67. kernel_space = utils.load_as(self._config)
  68. # Modules sorted for address lookups
  69. mods = dict((kernel_space.address_mask(mod.DllBase), mod) for mod in modules.lsmod(kernel_space))
  70. mod_addrs = sorted(mods.keys())
  71. for session in data:
  72. outfd.write("*" * 50 + "\n")
  73. outfd.write("Session(V): {0:x} ID: {1} Processes: {2}\n".format(
  74. session.obj_offset,
  75. session.SessionId,
  76. len(list(session.processes())),
  77. ))
  78. outfd.write("PagedPoolStart: {0:x} PagedPoolEnd {1:x}\n".format(
  79. session.PagedPoolStart,
  80. session.PagedPoolEnd,
  81. ))
  82. for process in session.processes():
  83. outfd.write(" Process: {0} {1} {2}\n".format(
  84. process.UniqueProcessId,
  85. process.ImageFileName,
  86. process.CreateTime,
  87. ))
  88. for image in session.images():
  89. module = tasks.find_module(mods, mod_addrs, kernel_space.address_mask(image.Address))
  90. outfd.write(" Image: {0:#x}, Address {1:x}, Name: {2}\n".format(
  91. image.obj_offset,
  92. image.Address,
  93. str(module and module.BaseDllName or '')
  94. ))