C++ CreateProcessWithdLex挂钩进程启动,但可以';不恢复
我正在尝试使用microsoft detours获得一个基本的钩子。我的程序能够成功运行CreateProcessWithDllEx并插入dll。然而,我似乎无法恢复实际的钩住程序。我正在使用记事本进行测试,我可以在进程列表中看到notepad.exe正在运行,但记事本窗口实际上从未出现过 我的dll如下所示:C++ CreateProcessWithdLex挂钩进程启动,但可以';不恢复,c++,windows,hook,detours,setwindowshookex,C++,Windows,Hook,Detours,Setwindowshookex,我正在尝试使用microsoft detours获得一个基本的钩子。我的程序能够成功运行CreateProcessWithDllEx并插入dll。然而,我似乎无法恢复实际的钩住程序。我正在使用记事本进行测试,我可以在进程列表中看到notepad.exe正在运行,但记事本窗口实际上从未出现过 我的dll如下所示: #undef UNICODE #include <cstdio> #include <windows.h> #include <detours.h>
#undef UNICODE
#include <cstdio>
#include <windows.h>
#include <detours.h>
#pragma comment(lib, "detours.lib")
typedef void (WINAPI *pFunc)(void);
DWORD WINAPI MyFunc(void);
pFunc FuncToDetour = (pFunc)DetourFindFunction("Winmm.dll", "timeGetTime"); //Set it at address to detour in
//the process
extern "C" __declspec( dllexport )VOID NullExport( VOID )
{
}
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH:
{
DisableThreadLibraryCalls(hDLL);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
//DetourAttach(&(PVOID&)FuncToDetour, MyFunc);
//DetourTransactionCommit();
}
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)FuncToDetour, MyFunc);
DetourTransactionCommit();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
DWORD WINAPI MyFunc()
{
return 0;
}
#undef _UNICODE
#include "stdafx.h"
#include <cstdio>
#include <windows.h>
#include <detours.h>
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
WCHAR DirPath[MAX_PATH+1];
wcscpy_s(DirPath, MAX_PATH, L"C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\Projects\\hbotinjector\\Release");
char DLLPath[MAX_PATH+1] = "C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\Projects\\hbotinjector\\Release\\hbotdll.dll";
WCHAR EXE[MAX_PATH+1]={0};
wcscpy_s( EXE, MAX_PATH, L"C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\Projects\\hbotinjector\\Release\\notepad.exe" );
STARTUPINFO _StartupInfo;
PROCESS_INFORMATION _Information;
ZeroMemory( &_Information, sizeof( PROCESS_INFORMATION ) );
if(DetourCreateProcessWithDllEx( EXE, NULL, NULL, NULL, TRUE,
CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED, NULL, DirPath, &_StartupInfo, &_Information,
DLLPath, NULL ))
{
MessageBoxA(NULL,"INJECTED", NULL, NULL);
ResumeThread(_Information.hThread);
WaitForSingleObject(_Information.hProcess, INFINITE);
}
else
{
char error[100];
sprintf(error, "%d", GetLastError());
MessageBoxA(NULL, error, NULL, NULL);
}
return 0;
}
有人知道是什么原因导致进程无法运行吗?作为补充说明,我也用一个空白dll进行了尝试,其中只包含序号1处所需的函数,没有其他内容,并且似乎有相同的结果
此外,只要notepad.exe进程显示在进程列表中,我的注入器就会永远运行。这是对WaitForSingleObject的回应,它似乎表明该流程已经正确生成。在Hans Passant的评论中,我回去后意识到我已经声明了pi和si以及_信息和_StartupInfo。我没有把我创建的第二个组归零,那是我正在使用的组。因此,我将对CreateProcessWithdLex的调用更改为使用&pi和&si。现在一切都好了
ZeroMemory(&_信息,sizeof(进程信息))代码>否。看看代码开头是如何正确完成的,si变量。你就是那个人。我不知道为什么我声明了同一组结构的两个不同版本。但通过使用最初声明的structs:并将调用更改为:if(DetourCreateProcessWithDllEx(EXE,NULL,NULL,TRUE,CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED,NULL,DirPath,&si,&pi,DLLPath,NULL))解决了这个问题
LIBRARY HBOTDLL
EXPORTS
NullExport @1