API hook代码

momentarily28 8 0 .zip 2022-07-10 12:07:23

API hook代码

IAT hook,hotfix Hook, inline Hook

// 开启钩子 BOOL MessageBoxHookOn(){ DWORD dwTemp = 0; // 修改后的内存保护属性 DWORD dwOldProtect = 0; // 之前的内存保护属性 DWORD dwAddress = 0; // 自定义函数偏移地址 DWORD dwRet = 0; // 内存写入成功标志,0不成功、1成功 SIZE_T ulWrite = 0; // 写入进程内存的字节数 SIZE_T ulRead = 0; // 从进程内存读取的字节数 BOOL bRet = FALSE; // 获取函数地址 HMODULE hMod = GetModuleHandle(L"user32.dll"); if (hMod == NULL) { return bRet; } pfMessageBox = (FARPROC)::GetProcAddress(hMod, "MessageBoxW"); // 指针为空则结束运行 if (pfMessageBox == NULL) { return bRet; } // 更改虚拟内存保护 VirtualProtectEx(hProcess, (LPVOID)((DWORD)pfMessageBox - 5), 7, PAGE_READWRITE, &dwOldProtect); // 将原API中的入口代码保存入 oldMessageBoxCode[] dwRet = ReadProcessMemory(hProcess, pfMessageBox, oldMessageBoxCode, 2, &ulRead); if (0 != dwRet && 0 != ulRead) { // 构造JMP指令 newMessageBoxCode[0] = 0xe9; // 计算JMP后面要跟的地址 dwAddress = (DWORD)myMessageBox - (DWORD)pfMessageBox; memcpy(&newMessageBoxCode[1], &dwAddress, 4); memcpy(&newMessageBoxCode[5], newMessageBoxShortCode, 2); // 将两条JMP指令写入内存 dwRet = WriteProcessMemory(hProcess, (LPVOID)((DWORD)pfMessageBox - 5), newMessageBoxCode, 7, &ulWrite); if (0 != dwRet && 0 != ulWrite) { bRet = TRUE; } } // 恢复内存保护状态 VirtualProtectEx(hProcess, (LPVOID)((DWORD)pfMessageBox - 5), 7, dwOldProtect, &dwTemp); return bRet;}

用户评论
请输入评论内容
评分:
暂无评论