I’ve injected a DLL into a program to implement a chat UI over the applications main window. I figured I can get the applications main window handle, then get it’s DC, and draw onto it. The window has a predictable title, which means I can use FindWindow to get the handle. The only problem is, the DLL is injected when the process starts. At that time, the window hasn’t been created. Which means FindWindow finds nothing!
What are some solutions to this? Could I create a thread in the DLL and sleep for a while until I know the window is created? This seems very unstable so I’d rather not do it.
What I tried to do was use SetWindowsHookEx in the DLL to hook the global WndProc. I could scan the messages until I find one from my window (which means it was created). Then I could save the handle and go on with my program. I’m not too worried about there being multiple windows with the same name at the time. The only problem is that my hook never gets called.
I create the hook like this:
m_hWndProcHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)WndProc, m_hModule, 0);
if(!m_hWndProcHook)
{
oss << "Failed to set wndproc hook. Error code: " << GetLastError();
Log(oss.str().c_str());
return false;
}
Which returns a valid hook. The WndProc look like this:
LRESULT CALLBACK CChatLibrary::WndProc(int code, WPARAM wParam, LPARAM lParam)
{
CWPSTRUCT* pData;
ostringstream oss;
char wndName[256];
gChatLib->Log("WNDPROC");
if(code < 0)
return CallNextHookEx(gChatLib->GetWndProcHookHandle(), code, wParam, lParam);
else
{
//Get the data for the wndproc
pData = (CWPSTRUCT*)lParam;
//Log the message
GetWindowText(pData->hwnd, wndName, 256);
oss << "Message from window \"" << wndName << "\"";
gChatLib->Log(oss.str().c_str());
return CallNextHookEx(gChatLib->GetWndProcHookHandle(), code, wParam, lParam);
}
}
But no “WNDPROC” messages are logged into my log file… Earlier, I had a MessageBox instead of a log to see if it worked, which turned out to be a terrible idea. All the programs froze because they were waiting for me to click “OK”, and I had to do a hard reset… When I turned my computer back on and replaced the MessageBox with a log command, it didn’t work. I know my log works, though, because it works everywhere else. I’m extremely confused with whats happening with this.
Are there any other methods of obtaining the main window (preferably when it is created)? Or is my hook method good, but just executed wrong? Thank you for any feedback.
You can always inject the DLL when application has already started. It’s quite complicated nowadays because of ASLR in Windows Vista/7, but not impossible. You would have to write a short application which would inject selected DLL into the process with given PID. Here is what should be done in order to inject DLL into the running process:
Write a shellcode which would find address of the
kernel32.dlllibrary. Here is my old code in NASM:kernel32.dllbase address in target process (thanks to the ASLR it might be not the same as in your injector process).LoadLibraryAfunction inkernel32.dllfrom your process.kernel32.dllbase address to the offset in order to compute base address ofLoadLibraryAfunction in the remote process.CreateRemoteThreadfunction giving the computed address ofLoadLibraryAas a function to call and DLL path as it’s parameter.I had to figure this all on my own some time ago (I couldn’t find any description), but recently I found something similiar: http://syprog.blogspot.com/2012/05/createremotethread-bypass-windows.html
Happy hacking!