From 825efda796e5f84a6fadedf589c22b87ae11570e Mon Sep 17 00:00:00 2001 From: Disconnect3d Date: Sun, 15 Jan 2023 23:38:42 +0100 Subject: [PATCH] cyclic command: improve UX (#1522) --- pwndbg/commands/cyclic.py | 15 ++++++++-- tests/gdb-tests/tests/test_command_cyclic.py | 30 ++++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/pwndbg/commands/cyclic.py b/pwndbg/commands/cyclic.py index c82822b5e..66cfa8ba4 100644 --- a/pwndbg/commands/cyclic.py +++ b/pwndbg/commands/cyclic.py @@ -66,10 +66,19 @@ def cyclic_cmd(alphabet, length, lookup, count=100) -> None: lookup = bytes(lookup, "utf-8") if len(lookup) != length: - print(message.error(f"Lookup pattern must be {length} bytes")) + print( + message.error( + f"Lookup pattern must be {length} bytes (use `-n ` to lookup pattern of different length)" + ) + ) return - print(message.notice(f"Lookup value: {str(lookup)}")) + hexstr = "0x" + lookup.hex() + print( + message.notice( + f"Finding cyclic pattern of {length} bytes: {str(lookup)} (hex: {hexstr})" + ) + ) if any(c not in alphabet for c in lookup): print(message.error("Pattern contains characters not present in the alphabet")) @@ -80,7 +89,7 @@ def cyclic_cmd(alphabet, length, lookup, count=100) -> None: if offset == -1: print(message.error("Given lookup pattern does not exist in the sequence")) else: - print(message.success(offset)) + print(message.success(f"Found at offset {offset}")) else: sequence = cyclic(int(count), alphabet, length) print(sequence.decode()) diff --git a/tests/gdb-tests/tests/test_command_cyclic.py b/tests/gdb-tests/tests/test_command_cyclic.py index d2582eced..7fd6a400c 100644 --- a/tests/gdb-tests/tests/test_command_cyclic.py +++ b/tests/gdb-tests/tests/test_command_cyclic.py @@ -21,7 +21,10 @@ def test_command_cyclic_value(start_binary): val = int.from_bytes(pattern[test_offset : test_offset + ptr_size], pwndbg.gdblib.arch.endian) out = gdb.execute(f"cyclic -l {hex(val)}", to_string=True) - assert int(out.split("\n")[1]) == test_offset + assert out == ( + "Finding cyclic pattern of 8 bytes: b'aaafaaaa' (hex: 0x6161616661616161)\n" + "Found at offset 37\n" + ) def test_command_cyclic_register(start_binary): @@ -38,7 +41,10 @@ def test_command_cyclic_register(start_binary): ) out = gdb.execute("cyclic -l $rdi", to_string=True) - assert int(out.split("\n")[1]) == test_offset + assert out == ( + "Finding cyclic pattern of 8 bytes: b'aaagaaaa' (hex: 0x6161616761616161)\n" + "Found at offset 45\n" + ) def test_command_cyclic_address(start_binary): @@ -54,4 +60,22 @@ def test_command_cyclic_address(start_binary): pwndbg.gdblib.memory.write(addr, pattern) out = gdb.execute(f"cyclic -l '{{unsigned long}}{hex(addr + test_offset)}'", to_string=True) - assert int(out.split("\n")[1]) == test_offset + assert out == ( + "Finding cyclic pattern of 8 bytes: b'gaaaaaaa' (hex: 0x6761616161616161)\n" + "Found at offset 48\n" + ) + + +def test_command_cyclic_wrong_alphabet(): + out = gdb.execute("cyclic -l 1234", to_string=True) + assert out == ( + "Finding cyclic pattern of 4 bytes: b'\\xd2\\x04\\x00\\x00' (hex: 0xd2040000)\n" + "Pattern contains characters not present in the alphabet\n" + ) + + +def test_command_cyclic_wrong_length(): + out = gdb.execute("cyclic -l qwerty", to_string=True) + assert out == ( + "Lookup pattern must be 4 bytes (use `-n ` to lookup pattern of different length)\n" + )