Repository: pwndbg/pwndbg

Issue:
subprocess.CalledProcessError: Command '['/usr/local/bin/checksec', '--file', '/tmp/xxx']' returned non-zero exit status 2.
```
pwndbg> checksec 
Traceback (most recent call last):
  File "/root/pwndbg/pwndbg/commands/__init__.py", line 109, in __call__
    return self.function(*args, **kwargs)
  File "/root/pwndbg/pwndbg/commands/__init__.py", line 189, in _OnlyWithFile
    return function(*a, **kw)
  File "/root/pwndbg/pwndbg/commands/checksec.py", line 16, in checksec
    print(pwndbg.wrappers.checksec.get_raw_out())
  File "/root/pwndbg/pwndbg/commands/__init__.py", line 189, in _OnlyWithFile
    return function(*a, **kw)
  File "/root/pwndbg/pwndbg/wrappers/__init__.py", line 28, in _OnlyWithCommand
    return function(*a, **kw)
  File "/root/pwndbg/pwndbg/memoize.py", line 48, in __call__
    value = self.func(*args, **kwargs)
  File "/root/pwndbg/pwndbg/wrappers/checksec.py", line 20, in get_raw_out
    return pwndbg.wrappers.call_cmd(cmd)
  File "/root/pwndbg/pwndbg/wrappers/__init__.py", line 35, in call_cmd
    return subprocess.check_output(cmd, stderr=STDOUT).decode('utf-8')
  File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.7/subprocess.py", line 487, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/usr/local/bin/checksec', '--file', '/tmp/forgot']' returned non-zero exit status 2.
```

when checksec running in python3 pwntools environment, raise a failure. Here's the reason:
```
root@kali:/tmp# checksec --file forgot
usage: checksec [-h] elf [elf ...]
checksec: error: unrecognized arguments: --file
```


So modify pwndbg/pwndbg/wrappers/checksec.py: 19 with:
`cmd = [get_raw_out.cmd_path, local_path]`

Then it will be solved


Discussion:
Seems like it may be clash of checksec from checksec.sh repo and from pwntools...

Can you see if this is the case?
> Seems like it may be clash of checksec from checksec.sh repo and from pwntools...
> 
> Can you see if this is the case?

Nope, I think it just the checksec from python3-pwntools doesn't accept the `--file` arg. So simply modify the command with `if else` would be ok
That's exactly what I mean, the https://github.com/slimm609/checksec.sh does accept and require the `--file` flag while pwntools' checksec does not.

We should probably detect which one is there and either pass the flag or not :f.
> That's exactly what I mean, the https://github.com/slimm609/checksec.sh does accept and require the `--file` flag while pwntools' checksec does not.
> 
> We should probably detect which one is there and either pass the flag or not :f.

Agree with you, so I rechecked the checksec.py in python3-pwntools (https://github.com/Gallopsled/pwntools), detect the arg parse bug. Unfortunately, the project is not maintained anymore. I will close this issue.

Resolving patch:
diff --git a/pwndbg/wrappers/checksec.py b/pwndbg/wrappers/checksec.py
--- a/pwndbg/wrappers/checksec.py
+++ b/pwndbg/wrappers/checksec.py
@@ -5,6 +5,10 @@
 from __future__ import print_function
 from __future__ import unicode_literals
 
+import subprocess
+from re import search
+from subprocess import STDOUT
+
 import pwndbg.commands
 import pwndbg.memoize
 import pwndbg.wrappers
@@ -14,10 +18,17 @@
 @pwndbg.wrappers.OnlyWithCommand(cmd_name)
 @pwndbg.memoize.reset_on_objfile
 def get_raw_out():
-
     local_path = pwndbg.file.get_file(pwndbg.proc.exe)
-    cmd = [get_raw_out.cmd_path, "--file", local_path]
-    return pwndbg.wrappers.call_cmd(cmd)
+    try:
+        version_output = subprocess.check_output([get_raw_out.cmd_path, "--version"], stderr=STDOUT).decode('utf-8')
+        match = search('checksec v([\\w.]+),', version_output)
+        if match:
+            version = tuple(map(int, (match.group(1).split("."))))
+            if version >= (2, 0):
+                return pwndbg.wrappers.call_cmd([get_raw_out.cmd_path, "--file=" + local_path])
+    except Exception:
+        pass
+    return pwndbg.wrappers.call_cmd([get_raw_out.cmd_path, "--file", local_path])
 
 @pwndbg.wrappers.OnlyWithCommand(cmd_name)
 def relro_status():


Test patch: