From a2eb60ed39194dd1f411ed7b1da90c5d7aa8974b Mon Sep 17 00:00:00 2001 From: Mike Auty Date: Sun, 22 Jan 2017 14:16:13 +0000 Subject: [PATCH] Add support for HexBytes to the text renderer. --- .../framework/renderers/format_hints.py | 4 +++ volatility/framework/renderers/text.py | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/volatility/framework/renderers/format_hints.py b/volatility/framework/renderers/format_hints.py index e01b227b1..36391dab4 100644 --- a/volatility/framework/renderers/format_hints.py +++ b/volatility/framework/renderers/format_hints.py @@ -8,3 +8,7 @@ Text renderers should attempt to honour all hints provided in this module where class Hex(int): """A class to indicate that the integer value should be represented as a hexidecimal value""" + + +class HexBytes(int): + """A class to indicate that the bytes should be display in an extended format showing hexadecimal and ascii printable display""" diff --git a/volatility/framework/renderers/text.py b/volatility/framework/renderers/text.py index 10ea028b9..9a6bedf37 100644 --- a/volatility/framework/renderers/text.py +++ b/volatility/framework/renderers/text.py @@ -4,7 +4,32 @@ from volatility.framework import interfaces from volatility.framework.renderers import format_hints +def hex_bytes_as_text(value): + """Renders HexBytes as text""" + if not isinstance(value, bytes): + raise TypeError("hex_bytes_as_text takes bytes not: {}".format(type(value))) + ascii = [] + hex = [] + count = 0 + output = "" + for byte in value: + hex.append("{:02x}".format(byte)) + ascii.append(chr(byte) if 0x20 < byte <= 0x7E else ".") + if (count % 8) == 7: + output += " ".join(hex[count - 7: count]) + output += "\t" + output += "".join(ascii[count - 7: count]) + output += "\n" + count += 1 + return output + + class TextRenderer(interfaces.renderers.Renderer): + type_renderers = {format_hints.Hex: lambda x: "{:x}".format(x), + format_hints.HexBytes: hex_bytes_as_text, + bytes: lambda x: x.decode("utf-8"), + 'default': lambda x: "{}".format(x)} + def __init__(self, options = None): super().__init__(options) @@ -22,10 +47,8 @@ class TextRenderer(interfaces.renderers.Renderer): def visitor(node, accumulator): for column in grid.columns: - text_format = "\t{}" - if column.type == format_hints.Hex: - text_format = "\t{:x}" - accumulator.write(text_format.format(node.values[column.index])) + renderer = self.type_renderers.get(column.type, self.type_renderers['default']) + accumulator.write("\t" + renderer(node.values[column.index])) accumulator.write("\n") return accumulator