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
| #include <Windows.h>
HANDLE WINAPI MyCreateFileW( _In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes, _In_opt_ HANDLE hTemplateFile ) { if(MessageBox(NULL, L"打开文件操作被拦截", L"提示", MB_YESNO) == IDYES) { return CreateFileW( lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile ); } else { MessageBox(NULL, L"文件打开失败", L"警告", NULL); return FALSE; } }
BOOL IATHook(LPCWSTR lpModuleName, const char* szFuncName) { DWORD64 dwFuncAddr = (DWROD64)GetProcAddress(GetModuleHandle(lpModuleName), szFuncName); HMODULE hModule = GetModuleHandle(NULL); PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule; PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((DWORD64)hModule + pDosHeader->e_lfanew); DWORD64 dwImportRVA = pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress; PIMAGE_IMPORT_DESCRIPTOR pImport = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD64)hModule + dwImportRVA); while(pImport->Nmae) { const char* szDllName = (char*)((DWORD64)hModule + pImport->Name); char szName[MAXBYTE] = { 0 }; strcpy_s(szName, szDllName) if(strcmp(_strlwr(szName), "kernel32.dll") == 0) { PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((DWORD64)hModule + pImport->FirstThunk); while(pThunk->Function) { if(pThunk->Function == dwFuncAddr) { DWORD64 dwOldProtect; VirtualProtectEx(GetCurrentProcess(), (LPVOID)&pThunk->Function, 8, PAGE_EXECUTE_READWRITE, &dwOldProtect); pThunk->Function = (DWORD64)MyCreateFileW; break; } pThunk++; } break; } pImport++; } }
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: IATHook(L"kernel32.dll", L"CreateFileW"); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: break; } return TRUE; }
|