I want to call a function in a remote process of an injected DLL that I’ve made.
I have successfully injected my DLL with:
CreateRemoteThread(pHandle, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("kernel32"), "LoadLibraryA"), pLibRemote, 0, NULL);
The DllMain is executed and the DLL is running in a stand-by mode. What I would like to do is somehow call the remotely loaded DLL in order to do some work.
I have tried exporting the function like this:
extern "C" __declspec(dllexport) void MyFunc(void)
and then executing the function like this:
CreateRemoteThread(pHandle, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("mydll"), "MyFunc"), NULL, 0, NULL);
but it results in a crash.
How can I solve this?
Calling
GetModuleHandleas you have will get the base of the DLL as it is mapped into your process (if at all). So what you need to do is first make sure to export the function in the DLL. You can do as you have done or create a.deffile as shown here. Thereafter:In Theory
GetProcAddressto find the offset between the exported function and the base of the DLL.CreateRemoteThreadat this location.In Practice
When doing your DLL injection, it is possible for you to get the base that your DLL is loaded into the target.
hInjectedwill be the base of the injected DLL. I then have another function:What this does is first load the payload into our own virtual address space. Afterwards,
we can use
GetProcAddressto get the address of the exported function. From this, we can get the offset of the function from the base of the DLL. Adding this offset to thehInjectedwe got earlier will tell us where theCreateRemoteThreadcall should be made. So you could make a call like so:This is all code that is ripped out of an old project I have. You’re welcome to take the code and do whatever you want with it but I know if I were to rewrite the code now, I would do a lot of things differently.