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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| #include "YPageHook.h" #include <map> #include <iostream>using namespace std;
#pragma code_seg(".func")
void func() { std::cout << "test1"; }
#pragma code_seg("") void HookCallBack(LPCONTEXT context) { std::cout << "mypackhook";
}
int main(int argc char* argv[]) { PageHook hook;
hook.install(char)(func, HookCallBack); func(); std::cout << "验证"; system("pause"); return 0; }
struct PageRecord { LPVOID pageBase; size_t count; DWORD protect; }; static std::map<LPVOID, PageRecord> gs_pageHook_base; static std::map<LPVOID, PageHook&> gs_pageHook_addr; static std::map<DWORD, PageRecord&> gs_pageHook_step;
static LPVOID PageAlignment(LPVOID addr) { return (LPVOID)((UINT_PTR)addr & (UINT_PTR)(~0xfff)); }
static LONG NTAPI ExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo) {
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
LPVOID pageBase = PageAlignment(ExceptionInfo->ExceptionRecord->ExceptionAddress); auto it_base = gs_pageHook_base.find(pageBase); if (it_base == gs_pageHook_base.end()) { . return EXCEPTION_CONTINUE_SEARCH; }
DWORD uselessProtect; VirtualProtect(pageBase, 0x1000, it_base->second.protect, &uselessProtect);
LPCONTEXT context = ExceptionInfo->ContextRecord;
auto it_addr = gs_pageHook_addr.find(ExceptionInfo->ExceptionRecord->ExceptionAddress); if (it_addr != gs_pageHook_addr.end()) {
it_addr->second.m_callback(context); }
context->EFlags |= 0x100;
gs_pageHook_step.insert(std::pair<DWORD, PageRecord&>(GetCurrentThreadId(), it_base->second));
return EXCEPTION_CONTINUE_EXECUTION;
} else if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) { LPCONTEXT pContext = ExceptionInfo->ContextRecord;
if (pContext->Dr6 & 0xf) { return EXCEPTION_CONTINUE_SEARCH; } else { auto it = gs_pageHook_step.find(GetCurrentThreadId()); if (it == gs_pageHook_step.end()) { return EXCEPTION_CONTINUE_SEARCH; }
DWORD uselessProtect; VirtualProtect(it->second.pageBase, 0x1000, PAGE_READWRITE, &uselessProtect);
gs_pageHook_step.erase(GetCurrentThreadId());
return EXCEPTION_CONTINUE_EXECUTION; }
}
return EXCEPTION_CONTINUE_SEARCH; }
YPageHook::YPageHook() { m_status = Status::invalid; m_hookAddr = nullptr; m_callback = nullptr;
m_exceptionHandlerHandle = AddVectoredExceptionHandler(TRUE, ExceptionHandler); }
YPageHook::~YPageHook() { RemoveVectoredExceptionHandler(m_exceptionHandlerHandle);
uninstall(); }
void YPageHook::install(LPVOID hookAddr, HookCallBack callback) {
if (m_status == Status::valid) { throw Error::repeatInstall; }
auto it_addr = gs_pageHook_addr.find(hookAddr); if (it_addr != gs_pageHook_addr.end()) { throw Error::duplicateAddress; }
LPVOID pageBase = PageAlignment(hookAddr);
m_hookAddr = hookAddr; m_callback = callback; m_status = Status::valid;
gs_pageHook_addr.insert(std::pair<LPVOID, PageHook&>(hookAddr, *this)); auto it_base = gs_pageHook_base.find(pageBase); if (it_base == gs_pageHook_base.end()) { PageRecord pageRecord; pageRecord.count = 1; pageRecord.pageBase = pageBase; pageRecord.protect = 0; gs_pageHook_base.insert(std::pair<LPVOID, PageRecord>(pageBase, pageRecord)); it_base = gs_pageHook_base.find(pageBase); if (!VirtualProtect(pageBase, 0x1000, PAGE_READWRITE, &it_base->second.protect)) { uninstall(); throw Error::setProtectFailed; } } else { ++it_base->second.count; } }
void YPageHook::uninstall() { if (m_status == Status::invalid) { throw Error::repeatUninstall; }
LPVOID pageBase = PageAlignment(m_hookAddr); auto it_base = gs_pageHook_base.find(pageBase);
if (it_base != gs_pageHook_base.end()) { if (it_base->second.count == 1) { if (!VirtualProtect(pageBase, 0x1000, it_base->second.protect, &it_base->second.protect)) { throw Error::setProtectFailed; } gs_pageHook_base.erase(it_base); } else { --it_base->second.count; } }
gs_pageHook_addr.erase(m_hookAddr);
m_hookAddr = nullptr; m_callback = nullptr;
m_status = Status::invalid;
}
|