Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better display for canary command #2044

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions pwndbg/commands/canary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import argparse

import pwndbg.auxv
import pwndbg.commands
import pwndbg.commands.telescope
Expand All @@ -9,6 +11,8 @@
from pwndbg.color import message
from pwndbg.commands import CommandCategory

DEFAULT_NUM_CANARIES_TO_DISPLAY = 1


def canary_value():
at_random = pwndbg.auxv.get().AT_RANDOM
Expand All @@ -23,11 +27,18 @@
return global_canary, at_random


@pwndbg.commands.ArgparsedCommand(
"Print out the current stack canary.", category=CommandCategory.STACK
parser = argparse.ArgumentParser(description="Print out the current stack canary.")
parser.add_argument(
"-a",
"--all",
action="store_true",
help="Print out stack canaries for all threads instead of the current thread only.",
)


@pwndbg.commands.ArgparsedCommand(parser, command_name="canary", category=CommandCategory.STACK)
@pwndbg.commands.OnlyWhenRunning
def canary() -> None:
def canary(all) -> None:
global_canary, at_random = canary_value()

if global_canary is None or at_random is None:
Expand All @@ -39,16 +50,43 @@
)
print(message.notice("Canary = 0x%x (may be incorrect on != glibc)" % global_canary))

stack_canaries = list(
pwndbg.search.search(
pwndbg.gdblib.arch.pack(global_canary), mappings=pwndbg.gdblib.stack.get().values()
found_canaries = False
results_hidden = False
global_canary_packed = pwndbg.gdblib.arch.pack(global_canary)
thread_stacks = pwndbg.gdblib.stack.get()

for thread in thread_stacks:
thread_stack = thread_stacks[thread]

stack_canaries = list(
pwndbg.search.search(
global_canary_packed, start=thread_stack.start, end=thread_stack.end
)
)
)

if not stack_canaries:
print(message.warn("No valid canaries found on the stacks."))
return
if not stack_canaries:
continue

found_canaries = True
num_canaries = len(stack_canaries)
num_canaries_to_display = num_canaries
some_canaries_not_shown = False

Check warning on line 73 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L70-L73

Added lines #L70 - L73 were not covered by tests

if not all:
num_canaries_to_display = min(DEFAULT_NUM_CANARIES_TO_DISPLAY, num_canaries)

Check warning on line 76 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L76

Added line #L76 was not covered by tests
if num_canaries_to_display < num_canaries:
some_canaries_not_shown = True

Check warning on line 78 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L78

Added line #L78 was not covered by tests

if num_canaries > 1:
print(message.success(f"Thread {thread}: Found valid canaries."))

Check warning on line 81 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L81

Added line #L81 was not covered by tests
else:
print(message.success(f"Thread {thread}: Found valid canary."))

Check warning on line 83 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L83

Added line #L83 was not covered by tests

for stack_canary in stack_canaries[:num_canaries_to_display]:
pwndbg.commands.telescope.telescope(address=stack_canary, count=1)

Check warning on line 86 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L86

Added line #L86 was not covered by tests

if found_canaries is False:
print(message.warn("No canaries found."))

print(message.success("Found valid canaries on the stacks:"))
for stack_canary in stack_canaries:
pwndbg.commands.telescope.telescope(address=stack_canary, count=1)
if some_canaries_not_shown is True:
print(message.warn("Additional results hidden. Use --all to see them."))

Check warning on line 92 in pwndbg/commands/canary.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/canary.py#L92

Added line #L92 was not covered by tests
Loading