-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVEH-Hook.h
99 lines (90 loc) · 2.21 KB
/
VEH-Hook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
#include <Windows.h>
#include <vector>
#define PAGE_EXCEPTION 0
#define INT3_BREAKPOINT 1 //vs调试器可能会优先处理,因为异常处理时,调试器>VEH>SEH
#if _WIN64
#define XIP Rip
#define RegValue DWORD64
#else
#define RegValue DWORD
#define XIP Eip
#endif
class VEH
{
public:
typedef void(Handler)(PCONTEXT);
bool SetHook(void* orgFunc, Handler* hookHandlerFunc);
void UnHook(void* oldFunc);
VEH();
~VEH();
struct Hook
{
void* orgFuncAddr;
Handler* HandlerFunc;
DWORD oldProtect;
#if INT3_BREAKPOINT
byte orgByte[];
#endif
};
private:
static std::vector<Hook>HookList;
static void End() { return; };
static LONG NTAPI ExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo)
{
#if PAGE_EXCEPTION
if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION)
{
#else
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
{
#endif
for (Hook x : HookList)
{
if ((RegValue)x.orgFuncAddr == ExceptionInfo->ContextRecord->XIP)
{
#if PAGE_EXCEPTION
VirtualProtect(x.orgFuncAddr, 1, PAGE_EXECUTE_READ | PAGE_GUARD, &x.oldProtect);
#else
x.HandlerFunc(ExceptionInfo->ContextRecord);
#endif
ExceptionInfo->ContextRecord->XIP = (RegValue)End;
return EXCEPTION_CONTINUE_EXECUTION;
}
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
};
std::vector<VEH::Hook>VEH::HookList;
bool VEH::SetHook(void* orgFunc, Handler* hookHandlerFunc)
{
Hook h{ orgFunc ,hookHandlerFunc };
AddVectoredExceptionHandler(1, ExceptionHandler);
#if PAGE_EXCEPTION
bool retV = VirtualProtect(orgFunc, 1, PAGE_EXECUTE_READ | PAGE_GUARD, &h.oldProtect);
HookList.push_back(h);
return retV;
#else
VirtualProtect(orgFunc, 1, PAGE_EXECUTE_READWRITE, &h.oldProtect);
HookList.push_back(h);
return *(byte*)orgFunc = 0xCC;
#endif
}
void VEH::UnHook(void* orgFunc)
{
std::vector<Hook>::iterator it = std::remove_if(HookList.begin(), HookList.end(), [&](const Hook& h)
{
return h.orgFuncAddr == orgFunc;
});
if (it != HookList.end()) {
VirtualProtect(it->orgFuncAddr, 1, it->oldProtect, &it->oldProtect);
HookList.erase(it);
}
}
VEH::VEH()
{
}
VEH::~VEH()
{
}