Originally Posted by
n1mda
So after FreeLibrary I added a second WaitForSingleObject() - it returns WAIT_OBJECT_0
but next comes VirtualFreeEx() wich fails:
Code:
if(!VirtualFreeEx(hOpenProcess, lpRemoteMemory, 0, MEM_RELEASE))
{
printf("[-] VirtualFreeEx() Failed: %d\n", GetLastError());
return -1;
}
Had to "touch up" your code a little bit to fix the above error....
Code:
int Inject(DWORD ProcID, LPCSTR szDllPath)
{
LPVOID lpRemoteMemory;
HANDLE hRemoteThread;
HANDLE hOpenProcess;
SIZE_T nSize = strlen(szDllPath);
unsigned long IDProcess = ProcID;
HMODULE hKernel;
DWORD hLibModule;
DWORD ret;
FARPROC hLocLoadLibrary;
//1. OpenProcess() - retrieve a HANDLE to the remote process
hOpenProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, IDProcess);
if(hOpenProcess == NULL)
{
printf("OpenProcess is NULL: %d\n", GetLastError());
return -1;
}
printf("[+] OpenProcess: %d\n\n", hOpenProcess);
//2. VirtualAllocEx() - Allocate memory in remote process addres-space
lpRemoteMemory = VirtualAllocEx(hOpenProcess, NULL, strlen(szDllPath), MEM_COMMIT, PAGE_READWRITE);
if(!lpRemoteMemory)
{
printf("[-] VirtualAlloc() Error: %d\n", GetLastError());
return -1;
}
printf("[+] VirtualAlloc(): 0x%x\n\n", lpRemoteMemory);
// GetModuleHandle() - kernel32.dll API CALL
hKernel = GetModuleHandle("KERNEL32.DLL");
if(!hKernel)
{
printf("[-] KernelModule Error: %d\n", GetLastError());
return -1;
}
printf("[+] KernelModule loaded KERNEL32.DLL!\n\n");
//3. WriteProcessMemory() - Copy initialised injection data strucuture to allocated memory
if(!WriteProcessMemory(hOpenProcess, lpRemoteMemory, (LPVOID)szDllPath, strlen(szDllPath), NULL))
{
printf("[-] WriteProcessMemory() error: %d\n", GetLastError());
return -1;
}
printf("[+] WriteProcessMemory() Succeeded :)\n");
printf("[*] Size of DLL: %d\n\n", nSize);
hLocLoadLibrary = GetProcAddress(hKernel, "LoadLibraryA");
// CreateRemoteThread() - Start the remote copy
hRemoteThread = CreateRemoteThread(hOpenProcess, NULL, 0, (LPTHREAD_START_ROUTINE)hLocLoadLibrary, lpRemoteMemory, 0, NULL);
if(!hRemoteThread)
{
printf("[-] CreateRemoteThread Error: %d\n", GetLastError());
return -1;
}
printf("[+]CreateRemoteThread(): %d\n\n", hRemoteThread);
// No luxury poop, have to clean up
// 1. Wait for the thread to complete
if(hRemoteThread)
ret = WaitForSingleObject(hRemoteThread, INFINITE);
switch(ret)
{
case WAIT_ABANDONED: /* should not occure with thread handle */
break;
case WAIT_OBJECT_0:
/* wait successful */
printf("[+] WaitThreadObject Complete: 0x%x\n\n", ret);
break;
case WAIT_TIMEOUT:
/* should not occure with waiting INFINITE */
break;
case WAIT_FAILED:
/* wait failed */
ret = GetLastError();
printf("[-] WaitForSingleObject failed with error 0x%x\n", ret);
return -1;
break;
}
if(!GetExitCodeThread(hRemoteThread, &hLibModule))
{
printf("[-] GetExitCodeThread() Error: %d\n", GetLastError());
return -1;
}
printf("[+] GetExitCodeThread() Complete\n\n");
if(!VirtualFreeEx(hOpenProcess, lpRemoteMemory, 0, MEM_RELEASE))
{
printf("[-] VirtualFreeEx() Failed: %d\n", GetLastError());
return -1;
}
printf("[+] VirtualFreeEx() Complete!\n");
if(!CloseHandle(hRemoteThread) && !CloseHandle(hOpenProcess))
{
printf("[-] Handle NOT closed: %d\n", GetLastError());
return -1;
}
printf("[+] CloseHandle() finished\n");
return 0;
}