cyclic command: improve UX (#1522)

pull/1526/head
Disconnect3d 3 years ago committed by GitHub
parent ae5298fc27
commit 825efda796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -66,10 +66,19 @@ def cyclic_cmd(alphabet, length, lookup, count=100) -> None:
lookup = bytes(lookup, "utf-8") lookup = bytes(lookup, "utf-8")
if len(lookup) != length: 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 <length>` to lookup pattern of different length)"
)
)
return 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): if any(c not in alphabet for c in lookup):
print(message.error("Pattern contains characters not present in the alphabet")) 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: if offset == -1:
print(message.error("Given lookup pattern does not exist in the sequence")) print(message.error("Given lookup pattern does not exist in the sequence"))
else: else:
print(message.success(offset)) print(message.success(f"Found at offset {offset}"))
else: else:
sequence = cyclic(int(count), alphabet, length) sequence = cyclic(int(count), alphabet, length)
print(sequence.decode()) print(sequence.decode())

@ -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) 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) 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): 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) 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): def test_command_cyclic_address(start_binary):
@ -54,4 +60,22 @@ def test_command_cyclic_address(start_binary):
pwndbg.gdblib.memory.write(addr, pattern) pwndbg.gdblib.memory.write(addr, pattern)
out = gdb.execute(f"cyclic -l '{{unsigned long}}{hex(addr + test_offset)}'", to_string=True) 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 <length>` to lookup pattern of different length)\n"
)

Loading…
Cancel
Save