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

https://github.com/giantbranch/python-hacker-code · Python · 110 lines · 58 code · 21 blank · 31 comment · 6 complexity · 2862ccc87705f64678b8b4d38d3f5d66 MD5 · raw file

  1. # Volatility
  2. #
  3. # Authors:
  4. # Michael Cohen <scudette@users.sourceforge.net>
  5. #
  6. # This file is part of Volatility.
  7. #
  8. # Volatility is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License Version 2 as
  10. # published by the Free Software Foundation. You may not use, modify or
  11. # distribute this program under any other version of the GNU General
  12. # Public License.
  13. #
  14. # Volatility is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with Volatility. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. """ This plugin contains CORE classes used by lots of other plugins """
  23. import volatility.scan as scan
  24. import volatility.obj as obj
  25. import volatility.debug as debug #pylint: disable-msg=W0611
  26. import volatility.commands as commands
  27. #pylint: disable-msg=C0111
  28. class AbstractWindowsCommand(commands.Command):
  29. @staticmethod
  30. def is_valid_profile(profile):
  31. return profile.metadata.get('os', 'unknown') == 'windows'
  32. def pool_align(vm, object_name, align):
  33. """Returns the size of the object accounting for pool alignment."""
  34. size_of_obj = vm.profile.get_obj_size(object_name)
  35. # Size is rounded to pool alignment
  36. extra = size_of_obj % align
  37. if extra:
  38. size_of_obj += align - extra
  39. return size_of_obj
  40. ## The following are checks for pool scanners.
  41. class PoolTagCheck(scan.ScannerCheck):
  42. """ This scanner checks for the occurance of a pool tag """
  43. def __init__(self, address_space, tag = None, **kwargs):
  44. scan.ScannerCheck.__init__(self, address_space, **kwargs)
  45. self.tag = tag
  46. def skip(self, data, offset):
  47. try:
  48. nextval = data.index(self.tag, offset + 1)
  49. return nextval - offset
  50. except ValueError:
  51. ## Substring is not found - skip to the end of this data buffer
  52. return len(data) - offset
  53. def check(self, offset):
  54. data = self.address_space.read(offset, len(self.tag))
  55. return data == self.tag
  56. class CheckPoolSize(scan.ScannerCheck):
  57. """ Check pool block size """
  58. def __init__(self, address_space, condition = (lambda x: x == 8), **kwargs):
  59. scan.ScannerCheck.__init__(self, address_space, **kwargs)
  60. self.condition = condition
  61. def check(self, offset):
  62. pool_hdr = obj.Object('_POOL_HEADER', vm = self.address_space,
  63. offset = offset - 4)
  64. block_size = pool_hdr.BlockSize.v()
  65. pool_alignment = obj.VolMagic(self.address_space).PoolAlignment.v()
  66. return self.condition(block_size * pool_alignment)
  67. class CheckPoolType(scan.ScannerCheck):
  68. """ Check the pool type """
  69. def __init__(self, address_space, paged = False,
  70. non_paged = False, free = False, **kwargs):
  71. scan.ScannerCheck.__init__(self, address_space, **kwargs)
  72. self.non_paged = non_paged
  73. self.paged = paged
  74. self.free = free
  75. def check(self, offset):
  76. pool_hdr = obj.Object('_POOL_HEADER', vm = self.address_space,
  77. offset = offset - 4)
  78. return ((self.non_paged and pool_hdr.NonPagedPool) or
  79. (self.free and pool_hdr.FreePool) or
  80. (self.paged and pool_hdr.PagedPool))
  81. class CheckPoolIndex(scan.ScannerCheck):
  82. """ Checks the pool index """
  83. def __init__(self, address_space, value = 0, **kwargs):
  84. scan.ScannerCheck.__init__(self, address_space, **kwargs)
  85. self.value = value
  86. def check(self, offset):
  87. pool_hdr = obj.Object('_POOL_HEADER', vm = self.address_space,
  88. offset = offset - 4)
  89. return pool_hdr.PoolIndex == self.value