So basically, i want to create process with subprocess
module, i also want to take control of packets.
What i mean ?
Consider this code.
import socket
client = socket.socket()
client.connect((..., ...))
client.send("e".encode())
client.close()
(Pretend that filename is client.py
)
What i want to do here, is i want to make another python file, run client.py
with subprocess
module, and i want to listen in the parent process what packets it is being sent, and i want to modify them, basically tamper child process socket's packet, if that is ever possible.
CodePudding user response:
Yes, that is possible. It's essentially what a debugger does.
At the system/binary level, there is a system call ptrace(2)
which allows a process to control another process. That includes setting breakpoints in the controlled process, stopping it, continuing it, modifying data in it, and so forth. To operate it from a python program, there is this module: https://python-ptrace.readthedocs.io/en/latest/usage.html
You could also debug at the python statement level. This is what the python debugger module (pdb
) does. I don't know the best way to invoke this in such a way that you can run it programmatically but I'm sure that's possible. (At worst, you could simply run the interactive debugger and pipe commands into it.)
Both of these options, however, will have significant learning curves. At a high level, you would set a breakpoint in the sub-process so that the program stops at some point before the send
system call is executed. When the breakpoint is triggered, you can then examine the buffer contents and/or replace them with your own, then allow the program to continue.