|
发表于 2023-7-17 04:39:29
|
显示全部楼层
////////////////////////////////////////////////////////////////////////////////////
BOOL WINAPI ModuleInJectRemoteThread32(IN HWND hwndDlg, IN DWORD dwProcessID, IN LPTSTR szDllPath)
{
////////////////////////////////////////////////////////
// 利用远程线程实现模块注入
////////////////////////////////////////////////////////
if (!dwProcessID)
{
::MessageBox(hwndDlg, TEXT("PID的值不能为空"), TEXT("失败"), MB_ICONWARNING);
return RETURN_ERROR;
}
if (!_tcscmp(szDllPath, TEXT("")))
{
::MessageBox(hwndDlg, TEXT("请先选择要注入的模块"), TEXT("失败"), MB_ICONWARNING);
return RETURN_ERROR;
}
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
if (!hProcess)
{
::MessageBox(hwndDlg, TEXT("获取进程句柄失败"), TEXT("失败"), MB_ICONWARNING);
return RETURN_ERROR;
}
// 得到字符串的长度
DWORD dwLength = ::_tcslen(szDllPath);
// 在目标进程中申请此长度的空间
LPVOID lpBaseAddress = ::VirtualAllocEx(hProcess, NULL, dwLength * 2, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!lpBaseAddress)
{
::MessageBox(hwndDlg, TEXT("在其它进程中申请空间失败"), TEXT("失败"), MB_ICONWARNING);
return RETURN_ERROR;
}
// 将字符串写入到目标进程中
if (!WriteProcessMemory(hProcess, lpBaseAddress, szDllPath, dwLength * 2, NULL))
{
::MessageBox(hwndDlg, TEXT("内存写入失败"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
HANDLE hThreadGetModuleHandle = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)&GetModuleHandle, lpBaseAddress, 0, NULL);
if (!hThreadGetModuleHandle)
{
::MessageBox(hwndDlg, TEXT("获取模块句柄远程线程创建失败"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
// 等待远程线程执行完毕
::WaitForSingleObject(hThreadGetModuleHandle, INFINITE);
// 获取到模块句柄
DWORD dwHGetModuleHandle = NULL;
if (!GetExitCodeThread(hThreadGetModuleHandle, &dwHGetModuleHandle))
{
::MessageBox(hwndDlg, TEXT("获取模块句柄失败"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
// 检查是否此模块已经被加载
if (dwHGetModuleHandle)
{
::MessageBox(hwndDlg, TEXT("模块已经加载"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
HANDLE hLoadLibrary = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)&LoadLibrary, lpBaseAddress, 0, NULL);
if (!hLoadLibrary)
{
::MessageBox(hwndDlg, TEXT("远程线程创建失败"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
// 等待远程线程执行完毕
::WaitForSingleObject(hLoadLibrary, INFINITE);
// 获取到加载到目标进程中的模块句柄
DWORD dwHLoadLibrary = NULL;
if (!GetExitCodeThread(hLoadLibrary, &dwHLoadLibrary))
{
::MessageBox(hwndDlg, TEXT("获取远程线程退出码失败"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
if (!dwHLoadLibrary)
{
::MessageBox(hwndDlg, TEXT("获取模块句柄失败"), TEXT("失败"), MB_ICONWARNING);
goto cleanup;
}
::MessageBox(hwndDlg, TEXT("注入成功"), TEXT("成功"), MB_ICONINFORMATION);
cleanup:
// 释放为DLL名字申请的空间
if (!VirtualFreeEx(hProcess, lpBaseAddress, dwLength * 2, MEM_DECOMMIT))
{
::MessageBox(hwndDlg, TEXT("DLL名称空间释放失败"), TEXT("失败"), MB_ICONWARNING);
return RETURN_ERROR;
}
// 关闭进程句柄
if (!CloseHandle(hProcess))
{
::MessageBox(hwndDlg, TEXT("关闭句柄失败"), TEXT("失败"), MB_ICONWARNING);
return RETURN_ERROR;
}
return RETURN_SUCCESS;
}
// 以前写的一个小程序里的一段, 如果目标进程没有保护应该是能注入的 [注意,32位程序只能注32位进程 64位注64位] |
|