gpt4 book ai didi

c++ - 从正在运行的进程中注入(inject) DLL 后弹出

转载 作者:可可西里 更新时间:2023-11-01 17:17:08 27 4
gpt4 key购买 nike

我写了这个函数来将 DLL 注入(inject)到正在运行的进程中:

DLL_Results CDLL_Loader::InjectDll()
{
DWORD ThreadTeminationStatus;
LPVOID VirtualMem;
HANDLE hProcess, hRemoteThread;
HMODULE hModule;

if (!isInit())
return NOT_INIT;

if (isInjected())
return DLL_ALREADY_HOOKED;

hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
if (hProcess == NULL)
return PROCESS_ERROR_OPEN;

VirtualMem = VirtualAllocEx (hProcess, NULL, strlen(DllFilePath), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (VirtualMem == NULL)
return PROCESS_ERRORR_VALLOC;

if (WriteProcessMemory(hProcess, (LPVOID)VirtualMem, DllFilePath, strlen(DllFilePath), NULL) == 0)
{
VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE|MEM_COMMIT);
CloseHandle(hProcess);
return PROCESS_ERROR_WRITE;
}

hModule = GetModuleHandle(L"kernel32.dll");
hRemoteThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "LoadLibraryA"),
(LPVOID)VirtualMem, 0, NULL);

if (hRemoteThread == NULL)
{
FreeLibrary(hModule);
VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE | MEM_COMMIT);
CloseHandle(hProcess);
return PROCESS_ERROR_CREATE_RTHREAD;
}

WaitForSingleObject(hRemoteThread, INFINITE);
GetExitCodeThread(hRemoteThread, &ThreadTeminationStatus);
FreeLibrary(hModule);

VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE | MEM_COMMIT);
CloseHandle(hRemoteThread);
CloseHandle(hProcess);
injected = true;
return DLLHOOK_OK;
}

它工作得很好,但是当我试图弹出 dll 时,我无法找到关于 unhooking 的信息。我试图构建一些函数来完成它,我想我已经接近这是我到目前为止所得到的:

这是正确的方法吗?如果是这样,我应该在 VirtualMem 的 createRemoteThread instade 中传递什么参数(在注入(inject)函数中使用)...

DLL_Results CDLL_Loader::EjectDll()
{
DWORD ThreadTeminationStatus;
HANDLE hProcess, hRemoteThread;
HMODULE hModule;

if (isInjected())
return DLLEJECT_OK;

hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
if (hProcess == NULL)
return PROCESS_ERROR_OPEN;

hModule = GetModuleHandle(L"kernel32.dll");
hRemoteThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "FreeLibrary"),
/*(LPVOID)VirtualMem <- What do i need to send here?*/, 0, NULL);

if (hRemoteThread != NULL)
{
WaitForSingleObject(hRemoteThread, INFINITE);
GetExitCodeThread(hRemoteThread, &ThreadTeminationStatus);
}

CloseHandle(hRemoteThread);
CloseHandle(hProcess);
injected = false;
return DLLEJECT_OK;
}

最佳答案

在 32 位系统上,GetExitCodeThread 之后的 ThreadTeminationStatus 的值包含远程进程中 LoadLibraryA 的返回值。这是新加载的 dll 的模块句柄。您可以将它用作远程线程中 FreeLibrary 的参数。

如果您想在 64 位 Windows 上使用该代码,线程退出代码将被截断为 32 位 DWORD,因此无法使用。您必须在远程进程中创建一个可调用例程(如 Necrolis 所建议的那样)或求助于通过 psapi 或 Toolhelp 查找 DLL 的模块库API(CreateToolhelp32SnapshotModule32FirstModule32Next)。

关于c++ - 从正在运行的进程中注入(inject) DLL 后弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8372524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com