Gallopsled/pwntools

Error when disassembling with binutils on Windows

Closed this issue · 3 comments

Error occurs when using the disasm function on Windows with GNUToolchains MinGW64 binutils 2.39

Pwntools version

PS C:\Users\test\Desktop > pip freeze | findstr pwntools
pwntools==4.14.1

Systeminfo

OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19045 N/A Build 19045

PoC

from pwn import *

print(disasm(b"\x90"))

Expected behavior

PS C:\Users\test\Desktop > py .\test.py
   0:   90                      nop

Actual behavior

The error message is very long, so I have removed some duplicate sections.

PS C:\Users\test\Desktop > py .\test.py

...

[ERROR] An error occurred while disassembling:
    b'\x90'
    Traceback (most recent call last):
      File "C:\Users\test\Desktop\.venv\Lib\site-packages\pwnlib\asm.py", line 917, in disasm
        _run(objcopy + [step1, step2])
      File "C:\Users\test\Desktop\.venv\Lib\site-packages\pwnlib\asm.py", line 434, in _run
        log.error(msg, *args)
      File "C:\Users\test\Desktop\.venv\Lib\site-packages\pwnlib\log.py", line 439, in error
        raise PwnlibException(message % args)
    pwnlib.exception.PwnlibException: There was an error running ['C:\\SysGCC\\mingw64\\bin\\objcopy.exe', '-I', 'binary', '-O', 'elf32-i386', '-B', 'i386', '--set-section-flags', '.data=code', '--rename-section', '.data=.text', '-w', '-N', '*', 'C:\\Users\\test\\AppData\\Local\\Temp\\pwn-disasm-ydmweaad\\step1', 'C:\\Users\\test\\AppData\\Local\\Temp\\pwn-disasm-ydmweaad\\step2']:
    It had the exitcode 1.
    It had this on stdout:
    Usage: C:\SysGCC\mingw64\bin\objcopy.exe [option(s)] in-file [out-file]
     Copies a binary file, possibly transforming it in the process
     The options are:
      -I --input-target <bfdname>      Assume input file is in format <bfdname>
      ....
      -v --verbose                     List all object files modified
      @<file>                          Read options from <file>
      -V --version                     Display this program's version number
      -h --help                        Display this output
         --info                        List object formats & architectures supported
    C:\SysGCC\mingw64\bin\objcopy.exe: supported targets: pe-x86-64 pei-x86-64 pe-bigobj-x86-64 elf64-x86-64 pe-i386 pei-i386 elf32-i386 elf32-iamcu elf64-little elf64-big elf32-little elf32-big srec symbolsrec verilog tekhex binary ihex plugin

Probable cause

objcopy on windows doesn't support using -N * to strip all symbols. Might be caused by wildcard expansion on Windows?

Workaround

Use -S instead. Not sure if this has any unintended side effects

--- a/asm.py
+++ b/asm.py
@@ -907,7 +907,7 @@ def disasm(data, vma = 0, byte = True, offset = True, instructions = True):
     if arch == 'thumb':
         objcopy += ['--prefix-symbol=$t.']
     else:
-        objcopy += ['-w', '-N', '*']
+        objcopy += ['-w', '-S']

     try:

Interesting, I've used the ubuntu tool chain without such issues. Can you try if it works with that one instead of mingw64 please?

https://sysprogs.com/getfile/1253/ubuntu-gcc9.3.0.exe

It works with the ubuntu toolchain

Ok, I'll add a note to the docs telling people to use that instead of the mingw build.