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

https://github.com/giantbranch/python-hacker-code · Python · 64 lines · 35 code · 4 blank · 25 comment · 1 complexity · 6bf15099428f6c8fa7828a1ffdebec38 MD5 · raw file

  1. # Volatility
  2. #
  3. # Authors:
  4. # Adam Boileau <metlstorm@storm.net.nz>
  5. # Mike Auty <mike.auty@gmail.com>
  6. #
  7. # This file is part of Volatility.
  8. #
  9. # Volatility is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License Version 2 as
  11. # published by the Free Software Foundation. You may not use, modify or
  12. # distribute this program under any other version of the GNU General
  13. # Public License.
  14. #
  15. # Volatility is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with Volatility. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. # *Heavily* based upon http://www.storm.net.nz/static/files/bioskbsnarf
  24. import struct
  25. import volatility.plugins.common as common
  26. import volatility.utils as utils
  27. import volatility.debug as debug
  28. class BiosKbd(common.AbstractWindowsCommand):
  29. """Reads the keyboard buffer from Real Mode memory"""
  30. BASE = 0x400
  31. OFFSET = 0x17
  32. BUFOFFSET = 0x1e
  33. LEN = 39
  34. FORMAT = "<BBBHH32s"
  35. def render_text(self, outfd, data):
  36. """Displays the character codes"""
  37. outfd.write("Ascii Scancode\n")
  38. for c, s in data:
  39. outfd.write("{0} (0x{1:02x}) 0x{2:02x}\n".format(self.format_char(c), ord(c), s))
  40. def format_char(self, c):
  41. """Prints out an ascii printable character"""
  42. if c in '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]{};\'#:@~,./<>?!"$%^&*()_+-=`\\|':
  43. return c
  44. return "."
  45. def calculate(self):
  46. """Calculate returns the results of the bios keyboard reading"""
  47. addr_space = utils.load_as(self._config, astype = 'physical')
  48. data = addr_space.read(self.BASE + self.OFFSET, self.LEN)
  49. if not data or len(data) != self.LEN:
  50. debug.error("Failed to read keyboard buffer, please check this is a physical memory image.")
  51. _shifta, _shiftb, _alt, readp, _writep, buf = struct.unpack(self.FORMAT, data)
  52. unringed = buf[readp - self.BUFOFFSET:]
  53. unringed += buf[:readp - self.BUFOFFSET]
  54. results = []
  55. for i in range(0, len(unringed) - 2, 2):
  56. if ord(unringed[i]) != 0:
  57. results.append((unringed[i], ord(unringed[i + 1])))
  58. return results