1 #include "CApiHook.h"
2
3 CApiHook::CApiHook()
4 {
5 memset(this, 0, sizeof(CApiHook));
6 }
7
8 bool CApiHook::Install(PSTR szModuleName, PSTR szFunName, FARPROC pFun)
9 {
10 if (status == true)
11 return false;
12
13 hModule = GetModuleHandleA(szModuleName);
14 if (hModule == NULL)
15 {
16 hModule = LoadLibraryA(szModuleName);
17 if (hModule == NULL)
18 return false;
19 }
20
21 lpNewFunAddr = (LPVOID)pFun;
22 lpOldFunAddr = (LPVOID)GetProcAddress(hModule, szFunName);
23 if (lpOldFunAddr == NULL)
24 {
25 CloseHandle(hModule);
26 return false;
27 }
28
29 RtlMoveMemory(bOldByte, lpOldFunAddr, 5);
30
31 bNewByte[0] = 0xE9;
32 *((PDWORD)(&(bNewByte[1]))) = (DWORD)lpNewFunAddr - (DWORD)lpOldFunAddr - 5;
33
34
35 MEMORY_BASIC_INFORMATION mbi;
36 if (VirtualQueryEx(GetCurrentProcess(), lpOldFunAddr, &mbi, sizeof(mbi)) == 0)
37 {
38 CloseHandle(hModule);
39 return false;
40 }
41
42 DWORD dwOldProtect;
43 if (VirtualProtectEx(GetCurrentProcess(), mbi.BaseAddress, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect) == 0)
44 {
45 CloseHandle(hModule);
46 return false;
47 }
48
49 DWORD dwWriteByte;
50 if (WriteProcessMemory(GetCurrentProcess(), (LPVOID)lpOldFunAddr, bNewByte, 5, &dwWriteByte) == 0)
51 {
52 CloseHandle(hModule);
53 return false;
54 }
55
56 if (VirtualProtectEx(GetCurrentProcess(), mbi.BaseAddress, 5, dwOldProtect, &dwOldProtect) == 0)
57 {
58 CloseHandle(hModule);
59 return false;
60 }
61
62 return true;
63 }