-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow calling GDB Python API #1617
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
Comments
Yes, this looks like a useful feature, and a PoC would be a great starting point. I mainly thought about (ab)using gdbscript parameter as a means of that, since the exploitation flow, and the debugged victim flow are kind of connected, but logically separate (you can e.g. break a program while the exploitation chain is halted waiting for output, or, in gdb, skip through multiple layers of irrelevant stuff that the exploit does). Most interesting debugged actions normally happen after a victim receives some payload, which would be difficult to catch from an exploit's perspective. What use do you find the most illustrative? |
What prompted me to think about this was solving CTF heap challenges, where I have to constantly go back and forth between exploit, gdbscript trace output and gdb itself, making sure that modifications to exploit produce desired results and don't break anything. What's especially frustrating is realizing at some point that you need to modify the very beginning of the exploitation chain, and then figure out what other parts need to be adjusted. To keep track of all that I normally write comments like "here heap should be in XYZ state" and, when needed, compare them with what I see in gdb, but having the ability to automatically check all that would be invaluable. The simplest use case therefore would be:
Catching victim processing in simple cases might look like this:
|
While this is a really cool idea, I think it’s worth keeping in mind that ultimately it would be useful for Pwndbg/Pwntools to merge in some way.
|
Probably the easiest way to do this would be to inject an XMLRPCServer stub into GDB and communicate with it that way, but it would be a large undertaking and I'm not sure how well GDB handles multiple Python threads while the inferior is running. |
I made a small working prototype with RPyC. The advantage of RPyC is that everything is transparent - pwntools users would interact with GDB API directly without us having to wrap or register each individual call. Unfortunately, RPyC developers said that inheriting from remote classes is a hard problem, therefore there would have to be some special magic for Regarding threading, yes, the server code has to be liberally sprinkled with |
I finally got some time to look into this again and made some progress - now the minimal example (which includes breakpoints) is fully working. The next step is to try using more GDB scripting features. |
Attaching and dumping registers/memory seems to work fine. I've extended the example and added some doctests - at the moment they hang because |
I didn't manage to implement a perfect solution to this, so I settled for the following compromise: I set a new An alternative was to set this variable inside the doctest, but there doesn't seem to be a way to hide it from the resulting documentation, which is ugly. |
Not sure what's causing test failures ( Edit: The bridge dies with This raises an interesting question - what to do if pwntools is used from a virtualenv? It might be tempting to try to play with GDB's |
Co-authored-by: Arusekk <[email protected]>
It would be great if pwntools could allow using GDB's Python API like this:
This would make it possible to write assertions against program state, e.g. compare leaked variables with their actual values, check that heap massaging produces desired layout, or ensure that sending a certain input at a certain time triggers a certain function with certain arguments, etc.
A possible implementation might be injecting RPyC server into gdb via gdbscript and using
gdb.post_event
to talk to the main loop. RPyC claims to support callbacks so triggering breakpoint processing on pwntools side should be possible.If this looks like a valuable feature, I could at least implement a PoC. Please let me know what you think!
The text was updated successfully, but these errors were encountered: