From b31f9eb6e20d0292eb76865aecb5f2a6c830ef9b Mon Sep 17 00:00:00 2001 From: Mike Auty Date: Wed, 27 May 2020 23:55:52 +0100 Subject: [PATCH] Registry: Move control of the StrLike output to the UIs --- volatility/cli/text_renderer.py | 13 +++++++++++++ volatility/framework/renderers/format_hints.py | 6 +----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/volatility/cli/text_renderer.py b/volatility/cli/text_renderer.py index e81cbedea..2def90361 100644 --- a/volatility/cli/text_renderer.py +++ b/volatility/cli/text_renderer.py @@ -53,6 +53,17 @@ def hex_bytes_as_text(value: bytes) -> str: return output +def strlike_as_text(value: format_hints.StrLike) -> str: + """Renders the bytes as a string where possible, otherwise it displays hex data + + This attempts to convert the string based on its encoding and if no data's been lost due to the split on the null character, then it displays it as is + """ + string_representation = str(value, encoding = value.encoding, errors = 'replace') + if len(string_representation) - 1 <= len(string_representation.split("\x00")[0]) <= len(string_representation): + return string_representation.split("\x00")[0] + return hex_bytes_as_text(value) + + def optional(func): @wraps(func) @@ -117,6 +128,7 @@ class QuickTextRenderer(CLIRenderer): format_hints.Bin: optional(lambda x: "0b{:b}".format(x)), format_hints.Hex: optional(lambda x: "0x{:x}".format(x)), format_hints.HexBytes: optional(hex_bytes_as_text), + format_hints.StrLike: quoted_optional(strlike_as_text), interfaces.renderers.Disassembly: optional(display_disassembly), bytes: optional(lambda x: " ".join(["{0:2x}".format(b) for b in x])), datetime.datetime: optional(lambda x: x.strftime("%Y-%m-%d %H:%M:%S.%f %Z")), @@ -172,6 +184,7 @@ class CSVRenderer(CLIRenderer): format_hints.Bin: quoted_optional(lambda x: "0b{:b}".format(x)), format_hints.Hex: quoted_optional(lambda x: "0x{:x}".format(x)), format_hints.HexBytes: quoted_optional(hex_bytes_as_text), + format_hints.StrLike: quoted_optional(strlike_as_text), interfaces.renderers.Disassembly: quoted_optional(display_disassembly), bytes: quoted_optional(lambda x: " ".join(["{0:2x}".format(b) for b in x])), datetime.datetime: quoted_optional(lambda x: x.strftime("%Y-%m-%d %H:%M:%S.%f %Z")), diff --git a/volatility/framework/renderers/format_hints.py b/volatility/framework/renderers/format_hints.py index 69e4a054f..ab5641a08 100644 --- a/volatility/framework/renderers/format_hints.py +++ b/volatility/framework/renderers/format_hints.py @@ -32,9 +32,5 @@ class StrLike(bytes): return super().__new__(cls, original) def __init__(self, original: bytes, encoding: str = 'utf-16-le'): - self.original = original - self._encoding = encoding + self.encoding = encoding bytes.__init__(original) - - def __str__(self): - return str(self.original, encoding = self._encoding, errors = 'replace').split("\x00")[0]