Hijacking programs' using socket(s) / a proxy in python


Hijacking programs' using socket(s) / a proxy in python
Currently I'm attempting to read/send UDP(/TCP) packets a running process is receiving and sending. So far I figured out several methods:
Normal:
Program <-> socket <-> Destination
Proxy:
Program <-> socket <-> proxy <-> Destination
Socket hijack:
Program <-> socket <-> Destination
^ | ^
| v |
+---"Shared" Socket ---+
Python
A Proxy would be possible but the ports change on startup and detecting that would be a pain.
So I reasoned that it would be possible to get some kind of copy/reference of the socket of the running process so that I could use that socket to read/send packets in python.
Edit: To clarify my question:
How would you get a python socket that is a "copy" of a process' socket.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
And the question is...?
– Adam Brinded
1 hour ago