Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDB api not functionnal for GDB 8.3+ #2458

Closed
SecT0uch opened this issue Sep 11, 2024 · 3 comments
Closed

GDB api not functionnal for GDB 8.3+ #2458

SecT0uch opened this issue Sep 11, 2024 · 3 comments

Comments

@SecT0uch
Copy link
Contributor

SecT0uch commented Sep 11, 2024

I'm having an issue when I want to Use GDB api.

Using:

  • OS: Manjaro X64
  • GNU gdb (GDB) 15.1
  • python 3.12.5
  • Pwntools v4.13.0
  • rpyc 6.0.0

Issue

Sample script:

from pwn import context, gdb

context.binary = "bin.elf"
context.terminal = "wezterm"

p = context.binary.process()
gdb.attach(p, api=True)

And the error:

[x] Starting local process '/usr/bin/gdb' argv=[b'/usr/bin/gdb', b'--nx', b'-bat[◢] Starting local process '/usr/bin/gdb' argv=[b'/usr/bin/gdb', b'--nx', b'-bat[+]  b'-ex', b'python import rpyc; import sys; sys.exit(123)'] : pid 49447
[*] Process '/usr/bin/gdb' stopped with exit code 1 (pid 49447)
[ERROR] Failed to connect to GDB: rpyc is not installed
Traceback (most recent call last):
  File "/home/user/file.py", line 23, in <module>
    gdb.attach(p, api=True)
  File "/usr/lib/python3.12/site-packages/pwnlib/context/__init__.py", line 1582, in setter
    return function(*a, **kw)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/pwnlib/gdb.py", line 1251, in attach
    log.error('Failed to connect to GDB: rpyc is not installed')
  File "/usr/lib/python3.12/site-packages/pwnlib/log.py", line 439, in error
    raise PwnlibException(message % args)
pwnlib.exception.PwnlibException: Failed to connect to GDB: rpyc is not installed
[*] Stopped process '/home/user/bin.elf' (pid 49429)

Analysis

Upon analyzing whats happening I can see how the check is done:

pwntools/pwnlib/gdb.py

Lines 1277 to 1281 in d7817a7

rpyc_check = [gdb_binary, '--nx', '-batch', '-ex',
'python import rpyc; import sys; sys.exit(123)']
if 123 != tubes.process.process(rpyc_check).poll(block=True):
log.error('Failed to connect to GDB: rpyc is not installed')

I tried running the command in the terminal directly:

# Using bash shell
gdb --nx -batch -ex 'python import rpyc; import sys; sys.exit(123)'; echo $?
Python Exception <class 'SystemExit'>: 123
Error occurred in Python: 123
1

Gdb seems to exit with an error code of 1.
And indeed in the GDB 8.3 changelog we have GDB in batch mode now exits with status 1 if the last command to be executed failed.

I wonder how come this has not been spotted since then ?
Am I really the only one wanting to use the GDB api through pwnlib ?

@Arusekk
Copy link
Member

Arusekk commented Sep 14, 2024 via email

@SecT0uch
Copy link
Contributor Author

I confirm this would work:

gdb --nx -batch -ex 'python import rpyc; import os; os._exit(123)'; echo $?
123

@peace-maker
Copy link
Member

peace-maker commented Sep 25, 2024

This isn't a problem with gdb 8.3 but with GDB 15.1 packaged in Ubuntu 24.04 now.
https://sourceware.org/bugzilla/show_bug.cgi?id=31946

Our tests run on Ubuntu 22.04 which is why this wasn't spotted yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants