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
| #include <windows.h>
typedef int (WINAPI *PfnMsgA)( _In_opt_ HWND hWnd, _In_opt_ LPCSTR lpText, _In_opt_ LPCSTR lpCaption, _In_ UINT uType);
PfnMsgA g_OldPfnMsgA = nullptr;
HMODULE hModImageBase = GetModuleHandle(NULL);
PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)(DWORD)hModImageBase;
DWORD dwTemp = (DWORD)pDosHead + (DWORD)pDosHead->e_lfanew; PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)dwTemp;
PIMAGE_FILE_HEADER pFileHead = (PIMAGE_FILE_HEADER)& pNtHead->FileHeader;
PIMAGE_OPTIONAL_HEADER pOptHead = (PIMAGE_OPTIONAL_HEADER)& pNtHead->OptionalHeader;
DWORD dwExportLocal=pOptHead->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
PIMAGE_IMPORT_DESCRIPTOR pImport = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)GetModuleHandle(NULL) + dwExportLocal);
int WINAPI MyMessageBox(_In_opt_ HWND hWnd, _In_opt_ LPCSTR lpText, _In_opt_ LPCSTR lpCaption, _In_ UINT uType) {
char szHookText[] = "Hook成功"; if (g_OldPfnMsgA != nullptr) { return g_OldPfnMsgA(hWnd, szHookText, lpCaption, uType); } return 0; }
void SetIatHook() { MessageBoxA(NULL, "开始进行HOOK", NULL, NULL); PVOID pHookAddress = nullptr; pHookAddress = GetProcAddress(GetModuleHandleA("user32.dll"), "MessageBoxA"); if (nullptr == pHookAddress) { OutputDebugString(TEXT("获取函数地址失败")); MessageBoxA(NULL, "获取函数地址失败HOOK", NULL, NULL);
return; } g_OldPfnMsgA = (PfnMsgA)pHookAddress;
PIMAGE_IMPORT_DESCRIPTOR pCurrent = pImport; DWORD *pFirstThunk; while (pCurrent->Characteristics && pCurrent->FirstThunk != NULL) { dwTemp = pCurrent->FirstThunk + (DWORD)GetModuleHandle(NULL); pFirstThunk = (DWORD *)dwTemp; while (*pFirstThunk != NULL) { if (*pFirstThunk == (DWORD)g_OldPfnMsgA) { DWORD oldProtected; VirtualProtect(pFirstThunk, 0x1000, PAGE_EXECUTE_READWRITE, &oldProtected); dwTemp = (DWORD)MyMessageBox; memcpy(pFirstThunk, (DWORD *)&dwTemp, 4); VirtualProtect(pFirstThunk, 0x1000, oldProtected, &oldProtected); } pFirstThunk++; } pCurrent++; }
}
void UnIatHook() { MessageBoxA(NULL, "开始进行HOOK", NULL, NULL); PVOID pHookAddress = nullptr; pHookAddress = MyMessageBox; if (nullptr == pHookAddress) { OutputDebugString(TEXT("获取函数地址失败")); MessageBoxA(NULL, "恢复函数地址失败HOOK", NULL, NULL); return; }
PIMAGE_IMPORT_DESCRIPTOR pCurrent = pImport; DWORD* pFirstThunk; while (pCurrent->Characteristics && pCurrent->FirstThunk != NULL) { dwTemp = pCurrent->FirstThunk + (DWORD)GetModuleHandle(NULL); pFirstThunk = (DWORD*)dwTemp; while (*pFirstThunk != NULL) { if (*pFirstThunk == (DWORD)MyMessageBox) { DWORD oldProtected; VirtualProtect(pFirstThunk, 0x1000, PAGE_EXECUTE_READWRITE, &oldProtected); dwTemp = (DWORD)GetProcAddress(GetModuleHandleA("user32.dll"), "MessageBoxA"); memcpy(pFirstThunk, (DWORD*)& dwTemp, 4); VirtualProtect(pFirstThunk, 0x1000, oldProtected, &oldProtected); } pFirstThunk++; } pCurrent++; }
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: SetIatHook(); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: break; } return TRUE; }
|