Skip to content

Commit

Permalink
Add -p (--prefix) and -s (--separator) arguments to hex command (#2117
Browse files Browse the repository at this point in the history
)

* Add formats for hex

* Add formats for hex coverage

Relates to #2117. Adding coverage as requested.

* Use util.lists.group

* Update CHANGELOG

* Fix Python 2 support

* Simplify hex formatting

Co-authored-by: Arusekk <[email protected]>

---------

Co-authored-by: marcan2020 <[email protected]>
Co-authored-by: carydrew <[email protected]>
Co-authored-by: Peace-Maker <[email protected]>
Co-authored-by: Arusekk <[email protected]>
  • Loading branch information
5 people committed Jun 4, 2023
1 parent 61bce7f commit 91be8af
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ jobs:
pwn disasm --color ff3424c3ebfe
pwn asm -f hex nop
pwn hex ABCD
pwn hex ABCD --separator ' '
pwn hex ABCD --prefix '\x'
pwn hex ABCD -p '0x' -s ' '
pwn hex abcd
pwn unhex 4141 4141
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ The table below shows which release corresponds to each branch, and what date th

## 4.12.0 (`dev`)
- [#2202][2202] Fix `remote` and `listen` in sagemath
- [#2117][2117] Add -p (--prefix) and -s (--separator) arguments to `hex` command

[2202]: https://github.com/Gallopsled/pwntools/pull/2202
[2117]: https://github.com/Gallopsled/pwntools/pull/2117

## 4.11.0 (`beta`)

Expand Down
29 changes: 27 additions & 2 deletions pwnlib/commandline/hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pwnlib.commandline import common
from pwnlib.util.fiddling import enhex
from pwnlib.util.lists import group

parser = common.parser_commands.add_parser(
'hex',
Expand All @@ -16,14 +17,38 @@
parser.add_argument('data', nargs='*',
help='Data to convert into hex')

parser.add_argument(
'-p', '--prefix',
metavar = 'prefix',
type = str,
default = '',
help = 'Insert a prefix before each byte',
)

parser.add_argument(
'-s', '--separator',
metavar = 'separator',
type = str,
default = '',
help = 'Add a separator between each byte',
)

def format_hex(hex_string, prefix, separator):
return separator.join([prefix + x for x in group(2, hex_string)])

def main(args):
if not args.data:
print(enhex(getattr(sys.stdin, 'buffer', sys.stdin).read()))
encoded = enhex(getattr(sys.stdin, 'buffer', sys.stdin).read())
else:
data = ' '.join(args.data)
if not hasattr(data, 'decode'):
data = data.encode('utf-8', 'surrogateescape')
print(enhex(data))
encoded = enhex(data)

if args.prefix or args.separator:
encoded = format_hex(encoded, args.prefix, args.separator)

print(encoded)

if __name__ == '__main__':
common.main(__file__)

0 comments on commit 91be8af

Please sign in to comment.