Advertisement
keybode

ApplyHookIAT

Nov 23rd, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. FARPROC ApplyHookEAT ( const char* ModuleName, FARPROC TargetProcedure, FARPROC HookedProcedure )
  2. {
  3.     HMODULE Module = GetModuleHandleA ( ModuleName );
  4.  
  5.     if ( !Module )
  6.         return 0;
  7.  
  8.     IMAGE_DOS_HEADER* DosHeader = (IMAGE_DOS_HEADER*)Module;
  9.  
  10.     if ( !DosHeader )
  11.         return 0;
  12.  
  13.     if ( DosHeader->e_magic != IMAGE_DOS_SIGNATURE )
  14.         return 0;
  15.  
  16.     IMAGE_NT_HEADERS* NtHeader = (IMAGE_NT_HEADERS*)( Module + DosHeader->e_lfanew );
  17.  
  18.     if ( !NtHeader )
  19.         return 0;
  20.  
  21.     if ( NtHeader->Signature != IMAGE_NT_SIGNATURE )
  22.         return 0;
  23.  
  24.     IMAGE_EXPORT_DIRECTORY* ExportList;
  25.  
  26.     FARPROC* ExportedFunctionList;
  27.  
  28.     ExportList = (IMAGE_EXPORT_DIRECTORY*) GetPointerFromRVA ( NtHeader->OptionalHeader.DataDirectory[0].VirtualAddress, NtHeader, Module, true );
  29.  
  30.     ExportedFunctionList = (FARPROC*) GetPointerFromRVA ( ExportList->AddressOfFunctions, NtHeader, Module, true );
  31.  
  32.     int i = 0;
  33.  
  34.     for ( i = 0; i < ExportList->NumberOfFunctions; i++ )
  35.     {
  36.         if ( (FARPROC)( (DWORD_PTR)Module + (DWORD_PTR)ExportedFunctionList[i] ) == TargetProcedure )
  37.             break;
  38.     }
  39.  
  40.     DWORD Protect = 0;
  41.  
  42.     FARPROC Original = 0;
  43.  
  44.     VirtualProtect ( &ExportedFunctionList[i], sizeof(DWORD_PTR), PAGE_READWRITE, &Protect );
  45.  
  46.     Original = ExportedFunctionList[i];
  47.  
  48.     ExportedFunctionList[i] = (FARPROC)( (DWORD_PTR)HookedProcedure - (DWORD_PTR)Module );
  49.  
  50.     VirtualProtect ( &ExportedFunctionList[i], sizeof(DWORD_PTR), Protect, &Protect );
  51.  
  52.     return Original;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement