APIHook是不是只能hook到系统函数呢?
我使用以下函数程序代码:
void AddStub(unsigned *func, unsigned *func_stub) { //stubFuncAddr[MAX_STUB] [2] = func_stub; // 函 数 func将被进行打桩测试 SetFuncAddr(func, func_stub); //-------------HOOK 部 分 HMODULE hMod = GetModuleHandle(NULL); PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hMod; PIMAGE_NT_HEADERS pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE *)hMod + pDosHeader->e_lfanew); PIMAGE_OPTIONAL_HEADER pOptHeader = (PIMAGE_OPTIONAL_HEADER)&(pNTHeaders->OptionalHeader); PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((BYTE *)hMod + \ pOptHeader->DataDirectory[1].VirtualAddress); while(pImportDescriptor->FirstThunk) { char * dllname = (char *)((BYTE *)hMod + pImportDescriptor->Name); //printf(" 函 数模块:%s\n",dllname); PIMAGE_THUNK_DATA pThunkData = (PIMAGE_THUNK_DATA)((BYTE *)hMod + pImportDescriptor->OriginalFirstThunk); int no = 1; while(pThunkData->u1.Function) { char * funname = (char *)((BYTE *)hMod + (DWORD)pThunkData->u1.AddressOfData + 2); PDWORD lpAddr = (DWORD *)((BYTE *)hMod + (DWORD)pImportDescriptor->FirstThunk) +(no-1); //printf("%4d: ",no); //printf("%30s",funname); //printf("%8x\n",lpAddr); //printf("%8x\n",*lpAddr); // 修 改内存的部分 if((*lpAddr) == (unsigned)func) { // 修 改内存页的属性 DWORD dwOLD; MEMORY_BASIC_INFORMATION mbi; VirtualQuery(lpAddr,&mbi,sizeof(mbi)); VirtualProtect(lpAddr,sizeof(DWORD),PAGE_READWRITE,&dwOLD); // 写 内存 WriteProcessMemory(GetCurrentProcess(), lpAddr, &func_stub, sizeof(DWORD), NULL); // 恢 复内存页的属性 VirtualProtect(lpAddr,sizeof(DWORD),dwOLD,0); } //--------- no++; pThunkData++; } pImportDescriptor++; } }
发现程序只能hook到系统dll中的API函数,而不能hook自己定义的函数,这是hook的限制吗?