* addressify #2506

* changes for addressify #2506

* fix lint

* renaming changes

* Update pwndbg/commands/hex2ptr.py

* tests format changes

---------

Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>
pull/2529/head
Dejan 1 year ago committed by GitHub
parent a534af1c28
commit cd918e435a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -747,6 +747,7 @@ def load_commands() -> None:
import pwndbg.commands.elf
import pwndbg.commands.flags
import pwndbg.commands.heap
import pwndbg.commands.hex2ptr
import pwndbg.commands.hexdump
import pwndbg.commands.leakfind
import pwndbg.commands.mmap

@ -0,0 +1,26 @@
from __future__ import annotations
import argparse
import pwndbg.color.message as M
import pwndbg.commands
from pwndbg.commands import CommandCategory
from pwndbg.lib.common import hex2ptr_common
# Define an argument parser for the command
parser = argparse.ArgumentParser(
description="Converts a space-separated hex string to a little-endian address.",
)
parser.add_argument(
"hex_string", type=str, help="Hexadecimal string to convert (e.g., '00 70 75 c1 cd ef 59 00')."
)
@pwndbg.commands.ArgparsedCommand(parser, category=CommandCategory.MISC)
def hex2ptr(hex_string) -> None:
combined_args = hex_string.replace(" ", "")
try:
result = hex2ptr_common(combined_args)
print(M.success(f"{hex(result)}"))
except Exception as e:
print(M.error(str(e)))

@ -15,6 +15,7 @@ import gdb
import pwndbg.gdblib.elf
import pwndbg.gdblib.proc
from pwndbg.lib.common import hex2ptr_common
functions: List[_GdbFunction] = []
@ -67,3 +68,16 @@ def base(name_pattern: gdb.Value | str) -> int:
if name in p.objfile:
return p.vaddr
raise ValueError(f"No mapping named {name}")
@GdbFunction(only_when_running=True)
def hex2ptr(hex_string: gdb.Value | str) -> int:
"""
Converts a hex string to a little-endian address and returns the address.
Example usage: $hex2ptr("00 70 75 c1 cd ef 59 00")
"""
if isinstance(hex_string, gdb.Value):
hex_string = hex_string.string()
address = hex2ptr_common(hex_string)
return address

@ -0,0 +1,16 @@
from __future__ import annotations
# common functions
def hex2ptr_common(arg: str) -> int:
"""Converts a hex string to a little-endian integer address."""
arg = "".join(filter(str.isalnum, arg))
if len(arg) % 2 != 0:
raise ValueError("Hex string must contain an even number of characters.")
try:
big_endian_num = int(arg, 16)
num_bytes = big_endian_num.to_bytes((len(arg) + 1) // 2, byteorder="big")
little_endian_num = int.from_bytes(num_bytes, byteorder="little")
except ValueError as e:
raise ValueError(f"Invalid hex string: {e}")
return little_endian_num

@ -0,0 +1,20 @@
from __future__ import annotations
import pytest
from pwndbg.lib.common import hex2ptr_common
def test_hex2ptr_common_valid_hex():
assert hex2ptr_common("00 70 75 c1 cd ef 59 00") == 0x59EFCDC1757000
assert hex2ptr_common("12 34 56 78") == 0x78563412
def test_hex2ptr_common_invalid_hex():
# Test for odd-length hex string
with pytest.raises(ValueError, match="Hex string must contain an even number of characters."):
hex2ptr_common("12345")
# Test for invalid hex characters
with pytest.raises(ValueError, match="Invalid hex string"):
hex2ptr_common("zz zz zz")
Loading…
Cancel
Save