Guest User

Untitled

a guest
May 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. // TODO: bounds checking on for loops
  2. void _api::ReDirectFunction(char* strDllName, char* strFunctionName, unsigned long funcaddr)
  3. {
  4. DWORD dwBackup,dwIndex,dwOffset;
  5. PDWORD pdwIAT,pdwINT;
  6. HMODULE hmHL; // Stores Half-Life module handle.
  7. PIMAGE_DATA_DIRECTORY pDataDirectory; // Stores Import Function Table address.
  8. PIMAGE_DOS_HEADER pDosHeader; // Holds DOS Header.
  9. PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor; // Holds dll information.
  10. PIMAGE_IMPORT_BY_NAME pImportName; // Holds the function name info.
  11. PIMAGE_OPTIONAL_HEADER pOptionalHeader; // Holds the Optional Header part of PE Header.
  12. PIMAGE_NT_HEADERS pPeHeader; // Holds the PE Header.
  13. PSTR strCurrent; // Buffer used when looking for GetProcAddress.
  14.  
  15. hmHL = ::GetModuleHandle(NULL); // Get the base address of Half-Life’s program.
  16. pDosHeader = PIMAGE_DOS_HEADER(hmHL); // Fill the IMAGE_DOS_HEADER structure.
  17. dwOffset = pDosHeader->e_lfanew; // Get the offset of the PE header.
  18. pPeHeader = PIMAGE_NT_HEADERS(LONG(hmHL) + dwOffset);
  19. pOptionalHeader = &pPeHeader->OptionalHeader; // Fill the IMAGE_OPTION_HEADER structure.
  20. pDataDirectory = pOptionalHeader->DataDirectory; // Fill the IMAGE_DATA_DIRECTORY structure.
  21. dwOffset = pDataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress; // Get the offset to the Import Function Table.
  22. pImportDescriptor = PIMAGE_IMPORT_DESCRIPTOR(LONG(hmHL) + dwOffset); // Fill the IMAGE_IMPORT_DESCRIPTOR structure.
  23.  
  24. for(dwIndex = 0; true; dwIndex++){ // Set up a never ending loop.
  25. dwOffset = pImportDescriptor[dwIndex].Name; // Get the offset to the dll name.
  26. strCurrent = PSTR(LONG(hmHL) + dwOffset);
  27. //logit("IMPORT: %s",strCurrent);
  28. if(_stricmp(strCurrent, strDllName) == 0)
  29. break; // If its kernel32.dll break, otherwise loop to
  30. } // the next structure.
  31.  
  32. dwOffset = pImportDescriptor[dwIndex].FirstThunk; // Get the offset to the Import Address Table.
  33. pdwIAT = PDWORD(LONG(hmHL) + dwOffset);
  34. dwOffset = pImportDescriptor[dwIndex].OriginalFirstThunk; // Get the offset to the Import Name Table.
  35. pdwINT = PDWORD(LONG(hmHL) + dwOffset);
  36.  
  37. for(dwIndex = 0; true; dwIndex++){ // Set up a never ending loop.
  38. dwOffset = pdwINT[dwIndex]; // Get the offset to the function name.
  39. pImportName = PIMAGE_IMPORT_BY_NAME(LONG(hmHL) + dwOffset);
  40. strCurrent = PSTR(pImportName->Name); // Get the current function name.
  41. //logit("IMPORT: dll->%s",strCurrent);
  42. if(_stricmp(strCurrent, strFunctionName) == 0)
  43. break; // If the name is strFunctionName break,
  44. } // otherwise loop to the next dword.
  45.  
  46. VirtualProtect(&pdwIAT[dwIndex], sizeof(DWORD), PAGE_READWRITE, &dwBackup); // Make sure we have write access.
  47. pdwIAT[dwIndex] = funcaddr;//PtrToUlong(); // Replace the address of GetProcAddress.
  48. VirtualProtect(&pdwIAT[dwIndex], sizeof(DWORD), dwBackup, &dwOffset); // Instill the original access protection.
  49. }
Add Comment
Please, Sign In to add comment