|
发表于 2023-11-14 12:19:22
高大上手机用户
|
显示全部楼层
安徽省合肥市
#include <iostream>
#include <windows.h>
#include <detours.h>
BYTE OriginalReadProcessMemory[1024]; // 保存原始ReadProcessMemory函数的代码
BYTE NewReadProcessMemory[1024]; // 保存自定义的ReadProcessMemory函数代码
BOOL DetourReadProcessMemory(HANDLE hProcess, LPCVOID lpBaseAddress, LPVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead) {
// 在这里实现自定义逻辑,例如修改参数、读取内存、调用原始函数等
// 调用原始ReadProcessMemory函数
BOOL result = OriginalReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead);
// 在这里实现自定义逻辑,例如修改返回值、修改数据等
return result;
}
int main() {
// 初始化Detours
DetourRestoreAfterWith();
// 保存原始ReadProcessMemory函数的代码
DWORD oldProtect;
VirtualProtect(NewReadProcessMemory, sizeof(NewReadProcessMemory), PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(NewReadProcessMemory, (void*)ReadProcessMemory, sizeof(OriginalReadProcessMemory));
VirtualProtect(NewReadProcessMemory, sizeof(NewReadProcessMemory), oldProtect, &oldProtect);
// Hook ReadProcessMemory函数
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)ReadProcessMemory, DetourReadProcessMemory);
DetourTransactionCommit();
// 在这里测试Hook,例如调用ReadProcessMemory函数
// 恢复原始ReadProcessMemory函数
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)ReadProcessMemory, DetourReadProcessMemory);
DetourTransactionCommit();
return 0;
}
这是C++例子 |
|