Skip to content

Commit

Permalink
add support for hex strings to print-format (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsheinen committed Nov 30, 2021
1 parent 97ad7f7 commit 5b91420
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/commands.md
Expand Up @@ -38,6 +38,7 @@ improve it.
|`pattern` | This command will create or search a De Bruijn cyclic pattern to facilitate determining the offset in memory. The algorithm used is the same as the one used by pwntools, and can therefore be used in conjunction.|
|`pcustom` | Dump user defined structure. This command attempts to reproduce WinDBG awesome `dt` command for GDB and allows to apply structures (from symbols or custom) directly to an address. Custom structures can be defined in pure Python using ctypes, and should be stored in a specific directory, whose path must be stored in the `pcustom.struct_path` configuration setting. (alias: `dt`)|
|`pie` | Base command to support PIE breakpoints. PIE breakpoints is that you can set to a PIE binary, and use pie series commands to attach or create a new process, and it will automatically set the real breakpoint when the binary is running.
|`print-format` | Command to dump memory in a variety of formats, such as programming language array literals. (alias: `pf`)|
|`process-search` | List and filter process. (alias: `ps`)|
|`process-status` | Extends the info given by GDB `info proc`, by giving an exhaustive description of the process status.|
|`registers` | Display full details on one, many or all registers value from current architecture.|
Expand Down
5 changes: 3 additions & 2 deletions docs/commands/print-format.md
@@ -1,17 +1,18 @@
## Command print-format ##

The command `print-format` (alias `pf`) will dump an arbitrary location as an array of bytes following the syntax of the programming language specified. Currently, the output language supported are
The command `print-format` (alias `pf`) will dump an arbitrary location as an array of bytes following the format specified. Currently, the output formats supported are

- Python (`py` - default)
- C (`c`)
- Assembly (`asm`)
- Javascript (`js`)
- Hex string (`hex`)


```
gef➤ print-format -h
[+] print-format [--lang LANG] [--bitlen SIZE] [(--length,-l) LENGTH] [--clip] LOCATION
--lang LANG specifies the output format for programming language (available: ['py', 'c', 'js', 'asm'], default 'py').
--lang LANG specifies the output format for programming language (available: ['py', 'c', 'js', 'asm', 'hex'], default 'py').
--bitlen SIZE specifies size of bit (possible values: [8, 16, 32, 64], default is 8).
--length LENGTH specifies length of array (default is 256).
--clip The output data will be copied to clipboard
Expand Down
8 changes: 5 additions & 3 deletions gef.py
Expand Up @@ -4762,7 +4762,7 @@ def do_invoke(self, argv):

@register_command
class PrintFormatCommand(GenericCommand):
"""Print bytes format in high level languages."""
"""Print bytes format in commonly used formats, such as literals in high level languages."""

format_matrix = {
8: (endian_str() + "B", "char", "db"),
Expand All @@ -4771,12 +4771,12 @@ class PrintFormatCommand(GenericCommand):
64: (endian_str() + "Q", "long long", "dq"),
}

valid_formats = ["py", "c", "js", "asm"]
valid_formats = ["py", "c", "js", "asm", "hex"]

_cmdline_ = "print-format"
_aliases_ = ["pf",]
_syntax_ = """{} [--lang LANG] [--bitlen SIZE] [(--length,-l) LENGTH] [--clip] LOCATION
\t--lang LANG specifies the output format for programming language (available: {}, default 'py').
\t--lang LANG specifies the output format (available: {}, default 'py').
\t--bitlen SIZE specifies size of bit (possible values: {}, default is 8).
\t--length LENGTH specifies length of array (default is 256).
\t--clip The output data will be copied to clipboard
Expand Down Expand Up @@ -4825,6 +4825,8 @@ def do_invoke(self, argv, *args, **kwargs):
elif args.lang == "asm":
asm_type = self.format_matrix[args.bitlen][2]
out = "buf {0} {1}".format(asm_type, sdata)
elif args.lang == "hex":
out = binascii.hexlify(read_memory(start_addr, end_addr-start_addr)).decode()

if args.clip:
if copy_to_clipboard(gef_pybytes(out)):
Expand Down
3 changes: 3 additions & 0 deletions tests/runtests.py
Expand Up @@ -536,6 +536,9 @@ def test_cmd_print_format(self):
res = gdb_start_silent_cmd("print-format --lang js $sp")
self.assertNoException(res)
self.assertTrue("var buf = [" in res)
res = gdb_start_silent_cmd("print-format --lang hex $sp")
self.assertNoException(res)
self.assertTrue("f7ff7f" in res)
res = gdb_start_silent_cmd("print-format --lang iDontExist $sp")
self.assertNoException(res)
self.assertTrue("Language must be in:" in res)
Expand Down

0 comments on commit 5b91420

Please sign in to comment.