darlingm2
asked on
Simple Remote Thread DLL Injection Not Working
Download VS2008 project at http://sigma.homeunix.com/SampleInjection.zip -- or use the code directly from: http://www.rohitab.com/discuss/index.php?showtopic=17456 -- if using the site from the rohitab.com link, make sure to download his 1KB zip file with two functions not posted in his thread.
Any help would be MUCH appreciated. I've spent most of today beating my head against a wall, getting nowhere. I have many years of programming experience, but this is my first attempt at a Windows app, and it's driving me nuts!
Only two changes, both seem necessary to the code on the thread (changes are made in the project downloadable at http://sigma.homeunix.com/SampleInjection.zip) :
(1) He accidentally (I think) typed taskmgr where he meant notepad. Changed 'hProcess = GetProcessHandle("taskmgr. exe")' where he meant '...notepad.exe'
(2) FindWindow() at least in VS08 has two paramters, LPCSTR lpClassName and LPCSTR lpWindowName. So, I tried both 'FindWindow("Untitled - Notepad", NULL)' and 'FindWindow(NULL, "Untitled - Notepad")'. I'm virtually certain "Untitled - Notepad" is for lpWindowName, but I tried both just in case.
Oh, I also threw in a MessageBox that states the Dll injection was successful, so there isn't just one upon an error.
The program message box'es success, but the injected dll never seems to execute. Even with a MessageBox as the first instruction in DllMain, nothing seems to happen.
I'm running on Vista, if it matters. Changing the attached project to release form (and moving both solutions over to multi-byte character sets) and copying the .exe and .dll to an XP machine is even worse. The .exe eats up a ton of memory and brings my system to its knees.
Any help would be MUCH appreciated. I've spent most of today beating my head against a wall, getting nowhere. I have many years of programming experience, but this is my first attempt at a Windows app, and it's driving me nuts!
Only two changes, both seem necessary to the code on the thread (changes are made in the project downloadable at http://sigma.homeunix.com/SampleInjection.zip) :
(1) He accidentally (I think) typed taskmgr where he meant notepad. Changed 'hProcess = GetProcessHandle("taskmgr.
(2) FindWindow() at least in VS08 has two paramters, LPCSTR lpClassName and LPCSTR lpWindowName. So, I tried both 'FindWindow("Untitled - Notepad", NULL)' and 'FindWindow(NULL, "Untitled - Notepad")'. I'm virtually certain "Untitled - Notepad" is for lpWindowName, but I tried both just in case.
Oh, I also threw in a MessageBox that states the Dll injection was successful, so there isn't just one upon an error.
The program message box'es success, but the injected dll never seems to execute. Even with a MessageBox as the first instruction in DllMain, nothing seems to happen.
I'm running on Vista, if it matters. Changing the attached project to release form (and moving both solutions over to multi-byte character sets) and copying the .exe and .dll to an XP machine is even worse. The .exe eats up a ton of memory and brings my system to its knees.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After coming across the same article darlingm2 mentioned, I found the above code from drichards very useful, so thanks drichards for that!
I also spent some time figuring out why my code would not work in Unicode mode: for future searchers I post the simple changes I made here. Basically it is about modifying DllInject() to writ ethe correct number of bytes out within WriteProcessMemory...
I also spent some time figuring out why my code would not work in Unicode mode: for future searchers I post the simple changes I made here. Basically it is about modifying DllInject() to writ ethe correct number of bytes out within WriteProcessMemory...
BOOL DllInject(HANDLE hProcess, LPCTSTR lpszDllPath)
{
HMODULE hmKernel = GetModuleHandle(_T("Kernel 32"));//he res the DLL
if(hmKernel == NULL || hProcess == NULL) return FALSE;
int nBytes = ( _tcslen(lpszDllPath) + 1 ) * sizeof ( TCHAR );
LPVOID lpvMem = VirtualAllocEx(hProcess, NULL, nBytes, MEM_COMMIT, PAGE_READWRITE);
BOOL bRet = WriteProcessMemory(hProces s, lpvMem, lpszDllPath, nBytes, NULL);
DWORD dwWaitResult, dwExitResult = 0;
#ifdef _UNICODE
HANDLE hThread = CreateRemoteThread(hProces s, NULL, 0, (LPTHREAD_START_ROUTINE)Ge tProcAddre ss(hmKerne l, "LoadLibraryW"), lpvMem, 0, NULL);
#else
HANDLE hThread = CreateRemoteThread(hProces s, NULL, 0, (LPTHREAD_START_ROUTINE)Ge tProcAddre ss(hmKerne l, "LoadLibraryA"), lpvMem, 0, NULL);
#endif
if(hThread != NULL){
dwWaitResult = WaitForSingleObject(hThrea d, 10000); // 10 seconds
bRet = GetExitCodeThread(hThread, &dwExitResult);
bRet = CloseHandle(hThread);
}
VirtualFreeEx(hProcess, lpvMem, 0, MEM_RELEASE);
return ((dwWaitResult != WAIT_TIMEOUT) && (dwExitResult > 0));
}
{
HMODULE hmKernel = GetModuleHandle(_T("Kernel
if(hmKernel == NULL || hProcess == NULL) return FALSE;
int nBytes = ( _tcslen(lpszDllPath) + 1 ) * sizeof ( TCHAR );
LPVOID lpvMem = VirtualAllocEx(hProcess, NULL, nBytes, MEM_COMMIT, PAGE_READWRITE);
BOOL bRet = WriteProcessMemory(hProces
DWORD dwWaitResult, dwExitResult = 0;
#ifdef _UNICODE
HANDLE hThread = CreateRemoteThread(hProces
#else
HANDLE hThread = CreateRemoteThread(hProces
#endif
if(hThread != NULL){
dwWaitResult = WaitForSingleObject(hThrea
bRet = GetExitCodeThread(hThread,
bRet = CloseHandle(hThread);
}
VirtualFreeEx(hProcess, lpvMem, 0, MEM_RELEASE);
return ((dwWaitResult != WAIT_TIMEOUT) && (dwExitResult > 0));
}
ASKER