From 10d01abbc3d450207dc0bcad9236a1ff70489755 Mon Sep 17 00:00:00 2001 From: "Matt." Date: Wed, 2 Oct 2024 10:53:01 -0300 Subject: [PATCH] Encode `bytes`-typed strings passed to the `search` command (#2458) --- pwndbg/commands/search.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pwndbg/commands/search.py b/pwndbg/commands/search.py index 9b8adb941..d8bb0ab97 100644 --- a/pwndbg/commands/search.py +++ b/pwndbg/commands/search.py @@ -255,6 +255,26 @@ def search( bits_for_arch = pwnlib.context.context.architectures.get(arch, {}).get("bits") value = pwnlib.asm.asm(value, arch=arch, bits=bits_for_arch) + # `pwndbg.search.search` expects a `bytes` object for its pattern. Convert the string pattern we + # were given to a bytes object by encoding it as an UTF-8 byte sequence. This matches the behavior + # we previously got by calling `gdb.Inferior.search_memory` with an `str`, since right about GDB + # version 7.x or 8.x[1], as it uses a `Py_buffer` object populated with an `'s*'` pattern, which + # has been encoding `str` object as a UTF-8 byte sequence since Python 3.1[2]. + # + # [1]: https://sourceware.org/git/?p=binutils-gdb.git;a=blame;f=gdb/python/py-inferior.c;h=a1042ee72ac733091f7572bc04b072546d3c1519;hb=23c84db5b3cb4e8a0d555c76e1a0ab56dc8355f3 + # [2]: https://docs.python.org/3.1/c-api/arg.html#strings-and-buffers + + elif type == "bytes": + try: + value = value.encode("utf-8") + except UnicodeError as what: + print( + message.error( + f"Invalid pattern '{value}'. Patterns of type `bytes` must be encodable in UTF-8: {what}" + ) + ) + return + # Find the mappings that we're looking for mappings = pwndbg.aglib.vmmap.get()