Advertisement
keybode

ApplyHookEAT

Nov 23rd, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #define makeptr(cast, address, offset)      (cast)( (DWORD_PTR)address + (DWORD_PTR)offset )
  2. #define makedelta(cast, address, offset)    (cast)( (DWORD_PTR)address - (DWORD_PTR)offset )
  3.  
  4. IMAGE_SECTION_HEADER* GetEnclosingSectionHeader ( uintptr_t rva, IMAGE_NT_HEADERS* ntHdr )
  5. {
  6.     IMAGE_SECTION_HEADER* pSection;
  7.  
  8.     uintptr_t i, dwSize;
  9.    
  10.     // go through all the sections in this module
  11.     pSection = IMAGE_FIRST_SECTION( ntHdr );
  12.     for ( i = 0; i < ntHdr->FileHeader.NumberOfSections; i++, pSection++ ) {
  13.         // size of this section
  14.         dwSize = pSection->Misc.VirtualSize ? pSection->Misc.VirtualSize : pSection->SizeOfRawData;
  15.         // is this the section containing our address?
  16.         if ( (rva >= pSection->VirtualAddress) && (rva < (pSection->VirtualAddress + dwSize)) )
  17.             return pSection;
  18.     }
  19.  
  20.     return 0;
  21. }
  22. //=========================================================
  23. uintptr_t GetMappedSectionOffset ( IMAGE_NT_HEADERS* ntHdr, IMAGE_SECTION_HEADER* pSectionHeader, void* pBase )
  24. {
  25.     IMAGE_SECTION_HEADER* pSection;
  26.  
  27.     uintptr_t dwOffset;
  28.  
  29.     unsigned short i;
  30.    
  31.     // get the first section of this module
  32.     pSection = IMAGE_FIRST_SECTION( ntHdr );
  33.     // just in case our section isn't here...
  34.     dwOffset = makedelta(uintptr_t, pSection, pBase);
  35.     // go through all the sections in this module
  36.     for ( i = 0; i < ntHdr->FileHeader.NumberOfSections; i++, pSection++ ) {
  37.         // is this the section we specified?
  38.         if ( pSection->Name == pSectionHeader->Name ) {
  39.             // offset from imagebase + rva
  40.             dwOffset = makedelta(uintptr_t, pSection->VirtualAddress, pSection->PointerToRawData);
  41.             break;
  42.         }
  43.     }
  44.  
  45.     return dwOffset;
  46. }
  47. //=========================================================
  48. uintptr_t GetPtrFromRVA ( uintptr_t rva, IMAGE_NT_HEADERS* ntHdr, unsigned char* lpImage, bool bMapped )
  49. {
  50.     IMAGE_SECTION_HEADER* pSectionHeader;
  51.  
  52.     uintptr_t dwResult = 0, dwOffset = 0;
  53.  
  54.     // section of this pointer
  55.     pSectionHeader = GetEnclosingSectionHeader ( rva, ntHdr );
  56.     // relative offset for this section
  57.     if ( bMapped )
  58.         dwOffset = GetMappedSectionOffset ( ntHdr, pSectionHeader, lpImage );
  59.     // did we find our ptr in this PE?
  60.     if ( pSectionHeader ) {
  61.         // get address of RVA in this image
  62.         dwResult = makeptr(uintptr_t, lpImage, rva);
  63.         // start at the base of this section
  64.         dwResult -= makedelta(int, pSectionHeader->VirtualAddress, pSectionHeader->PointerToRawData);
  65.         // now add the offset to this section to get our ptr
  66.         dwResult += dwOffset;
  67.     }
  68.  
  69.     return dwResult;
  70. }
  71.  
  72. void ApplyHook ( HMODULE Module, FARPROC TargetProcedure, FARPROC HookedProcedure )
  73. {
  74.     IMAGE_DOS_HEADER* DosHeader = (IMAGE_DOS_HEADER*)Module;
  75.  
  76.     if ( DosHeader->e_magic != IMAGE_DOS_SIGNATURE )
  77.     {
  78.         printf ( "shit module!\n" );
  79.         return;
  80.     }
  81.    
  82.     IMAGE_NT_HEADERS* NtHeader = (IMAGE_NT_HEADERS*)( (DWORD_PTR)DosHeader + DosHeader->e_lfanew );
  83.  
  84.     if ( NtHeader->Signature != IMAGE_NT_SIGNATURE )
  85.     {
  86.         printf ( "shit module #2!\n" );
  87.         return;
  88.     }
  89.  
  90.     IMAGE_EXPORT_DIRECTORY* ExportDir = (IMAGE_EXPORT_DIRECTORY*) GetPtrFromRVA ( NtHeader->OptionalHeader.DataDirectory[0].VirtualAddress, NtHeader, (unsigned char*)Module, true );
  91.  
  92.     if ( !ExportDir )
  93.     {
  94.         printf ( "export dir fucked up!\n" );
  95.         return;
  96.     }
  97.  
  98.     FARPROC* ExportedFunctionList = (FARPROC*) GetPtrFromRVA ( ExportDir->AddressOfFunctions, NtHeader, (unsigned char*)Module, true );
  99.  
  100.     if ( !ExportedFunctionList )
  101.     {
  102.         printf ( "exported func list fucked up!\n" );
  103.         return;
  104.     }
  105.  
  106.     int i = 0;
  107.  
  108.     for ( i = 0; i < ExportDir->NumberOfFunctions; i++ )
  109.     {
  110.         if ( (FARPROC)( Module + (DWORD_PTR)ExportedFunctionList[i] ) == TargetProcedure )
  111.             break;
  112.     }
  113.  
  114.     DWORD Protect;
  115.  
  116.     VirtualProtect ( &ExportedFunctionList[i], sizeof(DWORD_PTR), PAGE_READWRITE, &Protect );
  117.  
  118.     ExportedFunctionList[i] = (FARPROC)( (DWORD_PTR)HookedProcedure - (DWORD_PTR)Module );
  119.  
  120.     VirtualProtect ( &ExportedFunctionList[i], sizeof(DWORD_PTR), Protect, &Protect );
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement