Gallopsled/pwntools

Add a terminate method to the process object that invokes a sigterm, or alternitive.

JeremyGDiamond opened this issue · 2 comments

Problem statement

The pwntools process object is the best wrapper for subprocess.popen in the python language that I know of. For this reason, I use it to automate process control in projects that have nothing to do with exploitation. The pwn.process.kill() method is the only way to end a running process without accessing the underlying subprocess.popen object. However, this sends a sigkill and there is no option for a sigterm. In some environments, sigkill'ed processes don't clean up resources, leaking memory, storage, etc. There are likely exploitations that would prefer not to leak these resources.

Proposed feature

Add a terminate method to the pwn.process class, which sends a sigterm instead. Alternatively, add an input arg to the existing pwn.process.kill() to allow the programmer to specify the signal type. At minimum, add a line in the docks about this.

Tests

Unit tests which spawn processes known to leak resources when sigkill'ed and not when sigterm'ed.

I'm open to doing this myself and sending a pull request but would like some direction on if the feature would be rejected. If it was accepted, which approach would be preferred.

Since process.kill() is an alias for close() I don't think having an option to control the signal freely there is useful, since you'd want the process to be gone after calling close. But adding a terminate() and/or a send_signal function seems useful as proxies and calling .poll() afterwards to detect process termination immediately.

Regarding resources - maybe an default-false argument to .close() to call .terminate() before .kill() if desired too. So you can be mindful if you care. That wouldn't work with with process() as x: context managers, but offers at least more control.

Please do open a pull request if you have the time!