I've done it many times so it is absolutely possible. Use the following steps to get started.
- Have your program launch the target application with
CreateProcess
and passCREATE_SUSPENDED
to thedwCreationFlags
argument. This will start the target application but won't start execution. - Inject the DLL that contains your "trampoline" functions.
- Hook the Winsock function
gethostbyname
. - Call
ResumeThread
on the target processes main thread to start execution. - In your hook function call the real
gethostbyname
Winsock function when it returns a valid pointer to ahostent
structure change the address data to make it point to your loopback device (localhost).
Depending on how the target application works you may need to hook additional Winsock functions but hooking gethostbyname
is usually sufficient for this scenario.
You will want to write the hook functions that are in your DLL in C++ or C. Using C# is probably not an option here plus it will pull in a lot of dependencies that may cause conflicts with the target application.