好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 w0lker 于 2021-3-1 10:19 编辑
在使用 `SetWindowsHookExA` 对特定进程进行Dll注入时,发现在DLL的回调函数中不能使用 `MessageBox` 。一旦调用该函数,就会造成被注入的目标进程崩溃!
希望解答一下!
DLL函数部分的代码如下:
```c
#include <windows.h>
#include <TlHelp32.h>
#include <stdio.h>
#pragma data_seg(".shared")
CHAR szBuff[100] = {0};
HHOOK g_hHook = NULL;
#pragma data_seg()
#pragma comment(linker,"/section:.shared,rws")
// 钩子回调函数
LRESULT MessageProc(
int nCode,
WPARAM wParam,
LPARAM lParam);
DWORD GetWindowThreadID(LPSTR lpszWindowName)
{
// 初始化
HANDLE hProcess;
HANDLE hThreadSnap = NULL; // 模块快照
DWORD th32ThreadID = 0;
DWORD dwPid = 0x00;
THREADENTRY32 thread32 = { 0 };
BOOL bMoreOfThread;
//获取进程句柄
HWND hwnd = FindWindow(NULL, lpszWindowName);
if (!hwnd)
{
return FALSE;
}
GetWindowThreadProcessId(hwnd, &dwPid);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
//获取线程ID
thread32.dwSize = sizeof(thread32);
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
return FALSE;
}
bMoreOfThread = Thread32First(hThreadSnap, &thread32);
while (bMoreOfThread)
{
if (thread32.th32OwnerProcessID == dwPid)
{
th32ThreadID = thread32.th32ThreadID;
break;
}
bMoreOfThread = Thread32Next(hThreadSnap, &thread32);
}
CloseHandle(hThreadSnap);
return th32ThreadID;
}
// 设置全局钩子
BOOL SetGlobalHook(int idHook, LPSTR lpszWindowName)
{
DWORD threadID = GetWindowThreadID(lpszWindowName);
if (!threadID)
{
return FALSE;
}
g_hHook = SetWindowsHookEx(idHook, (HOOKPROC)MessageProc, GetModuleHandle("TestDll"), threadID);
if (NULL == g_hHook)
{
return FALSE;
}
return TRUE;
}
// 卸载钩子
BOOL UnsetGlobalHook()
{
if (g_hHook)
{
UnhookWindowsHookEx(g_hHook);
return TRUE;
}
else
return FALSE;
}
// 钩子回调函数
LRESULT MessageProc(
int nCode,
WPARAM wParam,
LPARAM lParam)
{
if (nCode == HC_ACTION)
{
PCWPSTRUCT pcw = (PCWPSTRUCT)lParam;
if(pcw->message == WM_USER+1)
{
MessageBox(0,0,0,0);
}
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
```
测试函数部分代码:
```c
typedef BOOL(*typedef_SetGlobalHook)(int idHook, LPSTR lpszWindowName);
typedef BOOL(*typedef_UnsetGlobalHook)();
LPSTR lpszWindowName = "test";
HMODULE hModule = LoadLibrary("C:\\vc6++\\MyProjects\\TestDll\\Debug\\TestDll.dll");
//获取进程句柄
HWND hwnd = FindWindow(NULL, lpszWindowName);
if (!hwnd)
{
printf("获取进程句柄失败!\n");
return FALSE;
}
typedef_SetGlobalHook SetGlobalHook = (typedef_SetGlobalHook)GetProcAddress(hModule, "SetGlobalHook");
BOOL bRet = SetGlobalHook(WH_CALLWNDPROC, lpszWindowName);
SendMessage(hwnd, WM_USER+1, 12345678, 12341234);
typedef_UnsetGlobalHook UnsetGlobalHook = (typedef_UnsetGlobalHook)GetProcAddress(hModule, "UnsetGlobalHook");
UnsetGlobalHook();
```
调试图如下:
当调用 `MessageBox` 时,并没有立刻弹出对话框,而是在 `EIP` 在对调函数中一直循环。当我放开断点执行时,才弹出对话框,点完确定后,程序崩溃,提示 `USER32.dll` 中出现访存错误。[/md]
在使用 `SetWindowsHookExA` 对特定进程进行Dll注入时,发现在DLL的回调函数中不能使用 `MessageBox` 。一旦调用该函数,就会造成被注入的目标进程崩溃!
希望解答一下! |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|