Skip to content

Commit

Permalink
Support asm/disasm on Windows (#2437)
Browse files Browse the repository at this point in the history
* Support asm/disasm on Windows

Find binutils toolchains like https://gnutoolchains.com/ if they exist in the PATH.

* Update CHANGELOG

* Update binutils install docs

* cpp reads from stdin when no infile is given

No need for a tempfile here.
  • Loading branch information
peace-maker committed Aug 8, 2024
1 parent de67c86 commit 248f451
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The table below shows which release corresponds to each branch, and what date th
- [#2427][2427] Document behaviour of remote()'s sni argument as string.
- [#2382][2382] added optional port, gdb_args and gdbserver_args parameters to gdb.debug()
- [#2435][2435] Speed up gdbserver handshake in gdb.debug()
- [#2437][2437] Support asm/disasm on Windows

[2371]: https://github.com/Gallopsled/pwntools/pull/2371
[2360]: https://github.com/Gallopsled/pwntools/pull/2360
Expand All @@ -109,6 +110,7 @@ The table below shows which release corresponds to each branch, and what date th
[2427]: https://github.com/Gallopsled/pwntools/pull/2405
[2382]: https://github.com/Gallopsled/pwntools/pull/2382
[2435]: https://github.com/Gallopsled/pwntools/pull/2435
[2437]: https://github.com/Gallopsled/pwntools/pull/2437

## 4.13.0 (`beta`)

Expand Down
6 changes: 6 additions & 0 deletions docs/source/install/binutils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ repo <https://github.com/Gallopsled/pwntools-binutils/>`__.
$ wget https://raw.githubusercontent.com/Gallopsled/pwntools-binutils/master/macos/binutils-$ARCH.rb
$ brew install ./binutils-$ARCH.rb
Windows
^^^^^^^^^^^^^^^^

Windows support is experimental. You can try installing a prebuilt version of binutils
for your desired architecture from the `GNU Toolchains <https://gnutoolchains.com/>`__ project.

Alternate OSes
^^^^^^^^^^^^^^^^

Expand Down
13 changes: 10 additions & 3 deletions pwnlib/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ def which_binutils(util, check_version=False):
if platform.system() == 'Darwin':
utils = ['g'+util, util]

if platform.system() == 'Windows':
utils = [util + '.exe']

for arch in arches:
for gutil in utils:
# e.g. objdump
Expand All @@ -220,7 +223,7 @@ def which_binutils(util, check_version=False):
'%s-%s' % (arch, gutil)]

for pattern in patterns:
for dir in environ['PATH'].split(':'):
for dir in environ['PATH'].split(os.pathsep):
for res in sorted(glob(path.join(dir, pattern))):
if check_version:
ver = check_binutils_version(res)
Expand Down Expand Up @@ -457,15 +460,19 @@ def cpp(shellcode):
>>> cpp("SYS_setresuid", os = "freebsd")
'311\n'
"""
if platform.system() == 'Windows':
cpp = which_binutils('cpp')
else:
cpp = 'cpp'

code = _include_header() + shellcode
cmd = [
'cpp',
cpp,
'-C',
'-nostdinc',
'-undef',
'-P',
'-I' + _incdir,
'/dev/stdin'
]
return _run(cmd, code).strip('\n').rstrip() + '\n'

Expand Down

0 comments on commit 248f451

Please sign in to comment.