这篇博客其实和上一篇差不多,上一篇讲是进程在完全运行时注入,这一篇是在启动进程时,先暂停了进程,然后注入了DLL后,再运行进程。
第一步是要先启动目的进程,使用函数:
BOOL CreateProcess( LPCWSTR pszImageName, LPCWSTR pszCmdLine, LPSECURITY_ATTRIBUTES psaProcess, LPSECURITY_ATTRIBUTES psaThread, BOOL fInheritHandles, DWORD fdwCreate, LPVOID pvEnvironment, LPWSTR pszCurDir, LPSTARTUPINFOW psiStartInfo, LPPROCESS_INFORMATION pProcInfo);
传入参数:CREATE_SUSPENDED,这样进程就会暂停住
第二步就是使用远程线程注入DLL的方法,将 DLL注入到进程里
第三步就是恢复目的进程执行。
完整的代码是这样的:
bool InjectDll(HANDLE handle_process, const char* dll_path)
{
auto dll_path_len = strlen(dll_path) + 1;
auto memory_address = VirtualAllocEx(handle_process, nullptr, dll_path_len, MEM_COMMIT, PAGE_READWRITE);
if (memory_address == nullptr)
{
return false;
}
auto sp_memory_address = sp_unique_ptr<void*>{ memory_address, std::bind(ReleaseWinMemory, handle_process, std::placeholders::_1, dll_path_len) };
if (!WriteProcessMemory(handle_process, memory_address, dll_path, dll_path_len, nullptr))
{
return false;
}
auto handle_thread = CreateRemoteThread(handle_process, nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(LoadLibraryA), memory_address, 0, nullptr);
if (handle_thread == nullptr)
{
return false;
}
auto sp_handle_thread = sp_unique_ptr<HANDLE>{handle_thread, CloseHandle };
WaitForSingleObject(handle_thread, INFINITE);
return true;
}
bool InjectDll(char* exe_path, const char* dll_path)
{
PROCESS_INFORMATION pi{};
STARTUPINFOA si{};
si.cb = sizeof(STARTUPINFOA);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
auto bRet = CreateProcessA(nullptr, exe_path, nullptr, nullptr, FALSE, CREATE_SUSPENDED, nullptr, nullptr, &si, &pi);
if (!bRet)
{
return false;
}
auto sp_handle_thread = sp_unique_ptr<HANDLE>{pi.hThread, CloseHandle};
auto sp_handle_process = sp_unique_ptr<HANDLE>{pi.hProcess, CloseHandle};
if (!InjectDll(pi.hProcess, dll_path))
{
return false;
}
ResumeThread(pi.hThread);
return true;
}
这次就不传工程了,大家可以看我上一篇的文章,里面有个工程的链接。如果有什么问题的话,可以联系我的QQ:403887828