Subject: [WIP PATCH v2 2/2] gnu: Add python-pwntools.

From: Jakub Kądziołka <[email-masked]>
Date: Sun, 7 Jun 2020 22:19:41 +0200

* gnu/packages/cybersecurity.scm (python-pwntools): New variable.
---

Changes from v1: added a patch to fix the installed commands' behavior
when wrapped by Guix - exec doesn't fool Python's sys.argv[0]. Resolved
the optional dependency on binutils for various architectures. Some
unresolved questions here mentioned in the comments.

I tried moving python-intervaltree out of bioinformatics.scm, but I
couldn't find a good place for intervaltree (the C library) to go with
it. python-xyz.scm feels wrong, since it's not in Python, and leaving it
in bioinformatics.scm creates a cycle.

 gnu/local.mk                                  |  1 +
 gnu/packages/cybersecurity.scm                | 83 ++++++++++++++++++-
 .../python-pwntools-guix-wrappers.patch       | 14 ++++
 3 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 gnu/packages/patches/python-pwntools-guix-wrappers.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index ae8a2275f7..4384ad952d 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1431,6 +1431,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/python-packaging-test-arch.patch		\
   %D%/packages/patches/python2-parameterized-docstring-test.patch	\
   %D%/packages/patches/python-paste-remove-timing-test.patch	\
+  %D%/packages/patches/python-pwntools-guix-wrappers.patch	\
   %D%/packages/patches/python-pycrypto-CVE-2013-7459.patch	\
   %D%/packages/patches/python-pycrypto-time-clock.patch		\
   %D%/packages/patches/python2-pygobject-2-gi-info-type-error-domain.patch \
diff --git a/gnu/packages/cybersecurity.scm b/gnu/packages/cybersecurity.scm
index 8ded081c50..d45618e617 100644
--- a/gnu/packages/cybersecurity.scm
+++ b/gnu/packages/cybersecurity.scm
@@ -18,10 +18,20 @@
 
 (define-module (gnu packages cybersecurity)
   #:use-module (guix download)
+  #:use-module (guix git-download)
   #:use-module (guix packages)
   #:use-module ((guix licenses) #:prefix license:)
   #:use-module (guix build-system python)
-  #:use-module (gnu packages engineering))
+  #:use-module (gnu packages)
+  #:use-module (gnu packages avr)
+  #:use-module (gnu packages bioinformatics)
+  #:use-module (gnu packages cross-base)
+  #:use-module (gnu packages emulators)
+  #:use-module (gnu packages engineering)
+  #:use-module (gnu packages python-crypto)
+  #:use-module (gnu packages python-web)
+  #:use-module (gnu packages python-xyz)
+  #:use-module (gnu packages time))
 
 (define-public ropgadget
   (package
@@ -43,3 +53,74 @@
 gadgets in binaries.  Some facilities are included for automatically generating
 chains of gadgets to execute system calls.")
     (license license:bsd-3)))
+
+(define-public python-pwntools
+  (package
+    (name "python-pwntools")
+    (version "4.1.1")
+    (source
+      (origin
+        (method git-fetch)
+        (uri (git-reference
+               (url "https://github.com/Gallopsled/pwntools")
+               (commit version)))
+        (file-name (git-file-name name version))
+        (sha256
+         (base32
+          "101whqdfj415h0f4b9hz2jrwny44b0jdd9jmbh6rzz5w1yp41d5v"))
+        (patches (search-patches "python-pwntools-guix-wrappers.patch"))))
+    (build-system python-build-system)
+    (arguments
+     `(#:tests? #f)) ; Tests require networking and custom sshd configuration
+    (propagated-inputs
+     `(("paramiko" ,python-paramiko)
+       ("mako" ,python-mako)
+       ("pyelftools" ,python-pyelftools)
+       ("capstone" ,python-capstone)
+       ("ropgadget" ,ropgadget)
+       ("pyserial" ,python-pyserial)
+       ("requests" ,python-requests)
+       ("pygments" ,python-pygments)
+       ("pysocks" ,python-pysocks)
+       ("dateutil" ,python-dateutil)
+       ("packaging" ,python-packaging)
+       ("psutil" ,python-psutil)
+       ("intervaltree" ,python-intervaltree)
+       ("sortedcontainers" ,python-sortedcontainers)
+       ("unicorn" ,unicorn "python")
+
+       ;; See https://docs.pwntools.com/en/stable/install/binutils.html
+       ;; All architectures recognized by pwntools are included.
+       ("binutils:aarch64" ,(cross-binutils "aarch64-linux-gnu"))
+       ("binutils:alpha" ,(cross-binutils "alpha-linux-gnu"))
+       ("binutils:arm" ,(cross-binutils "arm-linux-gnueabihf"))
+       ;; TODO: AVR binutils aren't detected,
+       ;; see https://github.com/Gallopsled/pwntools/pull/1536
+       ("binutils:avr" ,avr-binutils)
+       ("binutils:cris" ,(cross-binutils "cris-linux-gnu"))
+       ("binutils:i686" ,(cross-binutils "i686-linux-gnu"))
+       ("binutils:ia64" ,(cross-binutils "ia64-linux-gnu"))
+       ("binutils:m68k" ,(cross-binutils "m68k-linux-gnu"))
+       ("binutils:mips" ,(cross-binutils "mipsel-linux-gnu"))
+       ("binutils:mips64" ,(cross-binutils "mips64el-linux-gnu"))
+       ;; TODO: MSP430 doesn't work for the same reason as AVR.
+       ("binutils:msp430" ,(cross-binutils "msp430"))
+       ("binutils:powerpc" ,(cross-binutils "powerpc-linux-gnu"))
+       ("binutils:powerpc64" ,(cross-binutils "powerpc64-linux-gnu"))
+       ;; TODO: Attempting to assemble code for arch='s390' complains
+       ;; about bfdname
+       ("binutils:s390" ,(cross-binutils "s390-linux-gnu"))
+       ("binutils:sparc" ,(cross-binutils "sparc-linux-gnu"))
+       ("binutils:sparc64" ,(cross-binutils "sparc64-linux-gnu"))
+       ;; TODO: Should VAX use a -linux-gnu target, or just "vax"?
+       ("binutils:vax" ,(cross-binutils "vax-linux-gnu"))
+       ("binutils:x86_64" ,(cross-binutils "x86_64-linux-gnu"))))
+    (native-inputs
+     `(("tox" ,python-tox)))
+    (home-page "https://github.com/Gallopsled/pwntools")
+    (synopsis "CTF framework and exploit development library")
+    (description "Pwntools is a CTF framework and exploit development library.
+Written in Python, it is designed for rapid prototyping and development, and
+intended to make exploit writing as simple as possible.")
+    ;; See LICENSE-pwntools.txt in the source distribution.
+    (license (list license:expat license:bsd-2 license:gpl2+))))
diff --git a/gnu/packages/patches/python-pwntools-guix-wrappers.patch b/gnu/packages/patches/python-pwntools-guix-wrappers.patch
new file mode 100644
index 0000000000..b79a22320d
--- /dev/null
+++ b/gnu/packages/patches/python-pwntools-guix-wrappers.patch
@@ -0,0 +1,14 @@
+Parse argv properly when the programs are wrapped by Guix.
+========================================================================
+diff --git a/pwnlib/commandline/common.py b/pwnlib/commandline/common.py
+index 75edfdcb..5249b6e6 100644
+--- a/pwnlib/commandline/common.py
++++ b/pwnlib/commandline/common.py
+@@ -28,5 +28,7 @@ parser_commands = parser.add_subparsers(dest='command')
+ def main(file=sys.argv[0]):
+     import pwnlib.commandline.main
+     name = os.path.splitext(os.path.basename(file))[0]
++    if name.startswith('.') and name.endswith('-real'):
++        name = name[1:-5]
+     sys.argv.insert(1, name)
+     pwnlib.commandline.main.main()
-- 
2.26.2