diff --git a/docs/commands/index.md b/docs/commands/index.md index e27aee6e0..fc8fd8cb8 100644 --- a/docs/commands/index.md +++ b/docs/commands/index.md @@ -261,6 +261,7 @@ - [mallocng-find](musl/mallocng-find.md) - Find slot which contains the given address. - [mallocng-group](musl/mallocng-group.md) - Print out information about a mallocng group at the given address. - [mallocng-meta](musl/mallocng-meta.md) - Print out information about a mallocng group given the address of its meta. +- [mallocng-slot-start](musl/mallocng-slot-start.md) - Dump information about a mallocng slot, given its start address. - [mallocng-slot-user](musl/mallocng-slot-user.md) - Dump information about a mallocng slot, given its user address. diff --git a/docs/commands/musl/mallocng-slot-start.md b/docs/commands/musl/mallocng-slot-start.md new file mode 100644 index 000000000..b71958cbd --- /dev/null +++ b/docs/commands/musl/mallocng-slot-start.md @@ -0,0 +1,26 @@ + +# mallocng-slot-start + +```text +usage: mallocng-slot-start [-h] [-a] address + +``` + +Dump information about a mallocng slot, given its start address. + +**Alias:** ng-slots +### Positional arguments + +|Positional Argument|Help| +| :--- | :--- | +|address|The start of the slot (not including IB).| + +### Optional arguments + +|Short|Long|Help| +| :--- | :--- | :--- | +|-h|--help|show this help message and exit| +|-a|--all|Print out all information. Including meta and group data.| + + + diff --git a/pwndbg/commands/mallocng.py b/pwndbg/commands/mallocng.py index d9071de03..5e79adc05 100644 --- a/pwndbg/commands/mallocng.py +++ b/pwndbg/commands/mallocng.py @@ -331,42 +331,12 @@ def dump_meta(meta: mallocng.Meta) -> str: return output -parser = argparse.ArgumentParser( - description=""" -Dump information about a mallocng slot, given its user address. - """, -) -parser.add_argument( - "address", - type=int, - help="The start of user memory. Referred to as `p` in the source.", -) -parser.add_argument( - "-a", - "--all", - action="store_true", - help="Print out all information. Including meta and group data.", -) - - -@pwndbg.commands.Command( - parser, - category=CommandCategory.MUSL, - aliases=["ng-slotu"], -) -@pwndbg.commands.OnlyWhenRunning -def mallocng_slot_user(address: int, all: bool) -> None: - if not memory.is_readable_address(address): - print(message.error(f"Address {address:#x} not readable.")) - return - - slot = mallocng.Slot(address) - +def dump_slot(slot: mallocng.Slot, all: bool) -> str: try: slot.preload() except pwndbg.dbg_mod.Error as e: print(message.error(f"Error while reading slot: {e}")) - return + return "" read_success: bool = True @@ -483,11 +453,79 @@ def mallocng_slot_user(address: int, all: bool) -> None: pp.add(inband_group) pp.end_section() - pp.print() + output = pp.dump() if all: - print(dump_group(slot.group), end="") - print(dump_meta(slot.meta), end="") + output += dump_group(slot.group) + output += dump_meta(slot.meta) + + return output + + +parser = argparse.ArgumentParser( + description=""" +Dump information about a mallocng slot, given its user address. + """, +) +parser.add_argument( + "address", + type=int, + help="The start of user memory. Referred to as `p` in the source.", +) +parser.add_argument( + "-a", + "--all", + action="store_true", + help="Print out all information. Including meta and group data.", +) + + +@pwndbg.commands.Command( + parser, + category=CommandCategory.MUSL, + aliases=["ng-slotu"], +) +@pwndbg.commands.OnlyWhenRunning +def mallocng_slot_user(address: int, all: bool) -> None: + if not memory.is_readable_address(address): + print(message.error(f"Address {address:#x} not readable.")) + return + + slot = mallocng.Slot(address) + print(dump_slot(slot, all), end="") + + +parser = argparse.ArgumentParser( + description=""" +Dump information about a mallocng slot, given its start address. + """, +) +parser.add_argument( + "address", + type=int, + help="The start of the slot (not including IB).", +) +parser.add_argument( + "-a", + "--all", + action="store_true", + help="Print out all information. Including meta and group data.", +) + + +@pwndbg.commands.Command( + parser, + category=CommandCategory.MUSL, + aliases=["ng-slots"], +) +@pwndbg.commands.OnlyWhenRunning +def mallocng_slot_start(address: int, all: bool) -> None: + if not memory.is_readable_address(address): + print(message.error(f"Address {address:#x} not readable.")) + return + + slot = mallocng.Slot.from_start(address) + print(dump_slot(slot, all), end="") parser = argparse.ArgumentParser( @@ -630,4 +668,4 @@ def mallocng_find( print(message.info("No slot found containing that address.")) return - mallocng_slot_user(mallocng.Slot.from_start(slot_start).p, all=all) + mallocng_slot_start(slot_start, all=all)