NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.32k stars 13.56k forks source link

`pythonPackages.pwntools` needs cross-compiled version of `binutils` #87978

Open Pamplemousse opened 4 years ago

Pamplemousse commented 4 years ago

Describe the bug When wanting to write shellcodes for foreign architectures, pwntools complains about the absence of a cross-compiled binutils.

To Reproduce

$ nix-shell -p "python3.withPackages (p: with p; [ pwntools ])" --run python
>>> from pwn import *; shellcode = asm(['mov rax, rbx'], arch='aarch64')

Gives the following error: [ERROR] Could not find 'as' installed for ContextType(arch = 'aarch64', bits = 64, endian = 'little')

see full error ``` [ERROR] Could not find 'as' installed for ContextType(arch = 'aarch64', bits = 64, endian = 'little') Try installing binutils for this architecture: https://docs.pwntools.com/en/stable/install/binutils.html Traceback (most recent call last): File "", line 1, in File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/context/__init__.py", line 1395, in setter return function(*a, **kw) File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/asm.py", line 646, in asm assembler = _assembler() File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/asm.py", line 212, in _assembler gas = which_binutils('as') File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/context/__init__.py", line 1392, in setter return function(*a) File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/asm.py", line 207, in which_binutils print_binutils_instructions(util, context) File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/asm.py", line 138, in print_binutils_instructions """.strip() % locals()) File "/home/pamplemousse/Workspace/ctf/defcon/venv/lib/python2.7/site-packages/pwnlib/log.py", line 417, in error raise PwnlibException(message % args) pwnlib.exception.PwnlibException: Could not find 'as' installed for ContextType(arch = 'aarch64', bits = 64, endian = 'little') Try installing binutils for this architecture: https://docs.pwntools.com/en/stable/install/binutils.html ```

Notify maintainers

@bennofs , @kristoff3r

Metadata

$ nix-shell -p nix-info --run "nix-info -m"                                                                         ~/Workspace/ctf
these paths will be fetched (0.00 MiB download, 0.00 MiB unpacked):
  /nix/store/9c9mjrmfdschylny0bhkxxd7y0m90piw-nix-info
copying path '/nix/store/9c9mjrmfdschylny0bhkxxd7y0m90piw-nix-info' from 'https://cache.nixos.org'...
 - system: `"x86_64-linux"`
 - host os: `Linux 5.4.33, NixOS, 20.09pre222244.22a3bf9fb9e (Nightingale)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.3.4`
 - channels(pamplemousse): `"nixos-hardware"`
 - channels(root): `"nixos-20.09pre222244.22a3bf9fb9e, nixos-hardware"`
 - nixpkgs: `/home/pamplemousse/Workspace/tools/nixpkgs`
Pamplemousse commented 4 years ago

Here is the pwntools code raising this error: https://github.com/Gallopsled/pwntools/blob/dev/pwnlib/asm.py#L140-L205 . It's expecting aarch64-linux-gnu-as, which is not present.

I spent some time fiddling around Cross Compiling without being able to get a derivation working, so I am not sure it's the right way to go...

FRidh commented 4 years ago

Cross-compiling Python packages is still a bit of an open issue.

Pamplemousse commented 4 years ago

Would that be a solution though?

I mean, I would expect the Python package to be compiled to target the host nonetheless, but with some cross-compiled dependencies (for several architectures - say you want to write shellcode to target arm then aarch64, you don't want to reinstall pwntools).

Or maybe I am misunderstanding what you mean by "cross-compiled Python packages"...

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

Pamplemousse commented 3 years ago

For those of us that will run into the same kind of issue, and in the waiting of a robust way to deal with this, here is a simplified version of the shell.nix I use:

with import <nixpkgs> { };

let
  binutils_commands = "as ld objcopy readelf";
  armhf-binutils = pkgsCross.armhf-embedded.buildPackages.binutils;

in
  mkShell rec {
    buildInputs = [
      pwntools

      armhf-binutils
      makeWrapper
      which
    ];

    shellHook = ''
      # `pwntools` is `which`ing for Debian looking commands...
      for COMMAND in ${binutils_commands}; do
        makeWrapper "$(which "arm-none-eabihf-$COMMAND")" "arm-linux-gnu-$COMMAND"
      done
      PATH="$(pwd):$PATH"
    '';

    exitHook = ''
      for COMMAND in ${binutils_commands}; do
        rm "arm-linux-gnu-$COMMAND"
      done
    '';
  }
nixos-discourse commented 3 years ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/wishing-for-pkgscross-mips-to-exist-failed-to-contribute-it/13158/1

stale[bot] commented 2 years ago

I marked this as stale due to inactivity. → More info