One of the methods to run your code in another process(remote process)(so-called code injection) is using Windows Hook. The process is as follows:
- You write your code in a function called hook function.
- You put the function in a dll.
- You write another exe program to install the hook function using SetWindowsHookEx. The prototype of SetWindowsHookEx is:
HHOOK WINAPI SetWindowsHookEx(
_In_ int idHook,
_In_ HOOKPROC lpfn,
_In_ HINSTANCE hMod,
_In_ DWORD dwThreadId
);
So, typically, you will need to load the dll to get the dll instance(hMod) and the address of your hook function(lpfn). dwThreadId is the thread you want to hook. If dwThreadId is 0, all threads are hooked. - When a message(generated by the system, the user input, or any program including yours) is sent to the hooked thread(may be in a remote process), your dll will be mapped to the target process.
- Your hook function in that dll will be called by the system and your code is then executed.
Here is a good article about this kind of code injection technique.
Comments are closed, but trackbacks and pingbacks are open.