pe.cpp
上传用户:yll456
上传日期:2022-03-08
资源大小:19064k
文件大小:4k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include "pe.h"
  2. #include <imagehlp.h>
  3. #pragma comment(lib,"imagehlp.lib")
  4. BOOL  LoadFileR(LPTSTR lpFilename,PMAP_FILE_STRUCT pstMapFile)
  5. {
  6. HANDLE hFile;
  7. HANDLE hMapping;
  8. LPVOID ImageBase;
  9. memset(pstMapFile,0,sizeof(MAP_FILE_STRUCT));
  10. hFile=CreateFile(lpFilename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING, 
  11. FILE_ATTRIBUTE_NORMAL,0);
  12. if (!hFile)    
  13. return FALSE;
  14.  hMapping=CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);
  15. if(!hMapping)
  16. {
  17. CloseHandle(hFile);
  18. return FALSE;
  19. }
  20. ImageBase=MapViewOfFile(hMapping,FILE_MAP_READ,0,0,0);
  21.     if(!ImageBase)
  22. {
  23. CloseHandle(hMapping);
  24. CloseHandle(hFile);
  25. return FALSE;
  26. }
  27. pstMapFile->hFile=hFile;
  28. pstMapFile->hMapping=hMapping;
  29. pstMapFile->ImageBase=ImageBase;
  30. return TRUE;
  31. }
  32. void UnLoadFile(PMAP_FILE_STRUCT pstMapFile)
  33. {
  34. if(pstMapFile->ImageBase)
  35. UnmapViewOfFile(pstMapFile->ImageBase);
  36. if(pstMapFile->hMapping)
  37. CloseHandle(pstMapFile->hMapping);
  38. if(pstMapFile->hFile)
  39. CloseHandle(pstMapFile->hFile);
  40. }
  41. BOOL IsPEFile(LPVOID ImageBase)
  42. {
  43.     PIMAGE_DOS_HEADER  pDH=NULL;
  44.     PIMAGE_NT_HEADERS  pNtH=NULL;
  45.   
  46.     if(!ImageBase)
  47.   return FALSE;
  48.    
  49.     pDH=(PIMAGE_DOS_HEADER)ImageBase;
  50.     if(pDH->e_magic!=IMAGE_DOS_SIGNATURE)
  51.          return FALSE;
  52.     pNtH=(PIMAGE_NT_HEADERS32)((DWORD)pDH+pDH->e_lfanew);
  53.     if (pNtH->Signature != IMAGE_NT_SIGNATURE )
  54.         return FALSE;
  55.     return TRUE;
  56. }
  57. //
  58. PIMAGE_NT_HEADERS  GetNtHeaders(LPVOID ImageBase)
  59. {
  60.     
  61. if(!IsPEFile(ImageBase))
  62. return NULL;
  63. PIMAGE_NT_HEADERS  pNtH;
  64. PIMAGE_DOS_HEADER  pDH;
  65. pDH=(PIMAGE_DOS_HEADER)ImageBase;
  66. pNtH=(PIMAGE_NT_HEADERS)((DWORD)pDH+pDH->e_lfanew);
  67. return pNtH;
  68. }
  69. //
  70. PIMAGE_FILE_HEADER   GetFileHeader(LPVOID ImageBase)
  71. {
  72.     PIMAGE_DOS_HEADER  pDH=NULL;
  73.     PIMAGE_NT_HEADERS  pNtH=NULL;
  74.     PIMAGE_FILE_HEADER pFH=NULL;
  75.     
  76.     if(!IsPEFile(ImageBase))
  77. return NULL;
  78.     pDH=(PIMAGE_DOS_HEADER)ImageBase;
  79.     pNtH=(PIMAGE_NT_HEADERS)((DWORD)pDH+pDH->e_lfanew);
  80.     pFH=&pNtH->FileHeader;
  81.     return pFH;
  82. }
  83. PIMAGE_OPTIONAL_HEADER GetOptionalHeader(LPVOID ImageBase)
  84. {
  85.     PIMAGE_DOS_HEADER  pDH=NULL;
  86.     PIMAGE_NT_HEADERS  pNtH=NULL;
  87.     PIMAGE_OPTIONAL_HEADER pOH=NULL;
  88.    
  89.      if(!IsPEFile(ImageBase))
  90. return NULL;
  91.     pDH=(PIMAGE_DOS_HEADER)ImageBase;
  92.     pNtH=(PIMAGE_NT_HEADERS)((DWORD)pDH+pDH->e_lfanew);
  93.     pOH=&pNtH->OptionalHeader;
  94.     return pOH;
  95. }
  96. PIMAGE_SECTION_HEADER GetFirstSectionHeader(LPVOID ImageBase)
  97. {
  98.    PIMAGE_NT_HEADERS     pNtH=NULL;
  99.     PIMAGE_SECTION_HEADER pSH=NULL;
  100.     
  101.     pNtH=GetNtHeaders(ImageBase);
  102.     pSH=IMAGE_FIRST_SECTION(pNtH);
  103.   return  pSH;
  104. }
  105. LPVOID RvaToPtr(PIMAGE_NT_HEADERS pNtH,LPVOID ImageBase,DWORD dwRVA)
  106. {
  107.   return ImageRvaToVa(pNtH,ImageBase,dwRVA,NULL);
  108. }
  109. LPVOID GetDirectoryEntryToData(LPVOID ImageBase,USHORT DirectoryEntry)
  110. {
  111. DWORD dwDataStartRVA;
  112. LPVOID pDirData=NULL;
  113. PIMAGE_NT_HEADERS     pNtH=NULL;
  114. PIMAGE_OPTIONAL_HEADER pOH=NULL;
  115. pNtH=GetNtHeaders(ImageBase);
  116. if(!pNtH)
  117. return NULL;
  118. pOH=GetOptionalHeader(ImageBase);
  119. if(!pOH)
  120. return NULL;
  121.     dwDataStartRVA=pOH->DataDirectory[DirectoryEntry].VirtualAddress;
  122.       if(!dwDataStartRVA)
  123.         return NULL;
  124.   
  125. pDirData=RvaToPtr(pNtH,ImageBase,dwDataStartRVA);
  126.    if(!pDirData)
  127. return NULL;  
  128.     return  pDirData;
  129. }
  130. PIMAGE_EXPORT_DIRECTORY  GetExportDirectory(LPVOID ImageBase)
  131. {
  132.    
  133. PIMAGE_EXPORT_DIRECTORY pExportDir=NULL;
  134. pExportDir=(PIMAGE_EXPORT_DIRECTORY)GetDirectoryEntryToData(ImageBase,IMAGE_DIRECTORY_ENTRY_EXPORT);
  135.     if(!pExportDir)
  136. return NULL;  
  137.     return  pExportDir;
  138. }
  139. PIMAGE_IMPORT_DESCRIPTOR  GetFirstImportDesc(LPVOID ImageBase)
  140. {
  141. PIMAGE_IMPORT_DESCRIPTOR pImportDesc;
  142. pImportDesc=(PIMAGE_IMPORT_DESCRIPTOR)GetDirectoryEntryToData(ImageBase,IMAGE_DIRECTORY_ENTRY_IMPORT);
  143.     if(!pImportDesc)
  144. return NULL;  
  145.     return  pImportDesc;
  146. }
  147. DWORD   GetNumOfExportFuncs(LPVOID ImageBase,PIMAGE_EXPORT_DIRECTORY pExportDir)
  148. {
  149. DWORD   dwnum=0;
  150.     PDWORD pdwRvas=NULL;
  151. /* if(!IsPEFile(ImageBase))
  152. return NULL;
  153. */
  154.  PIMAGE_NT_HEADERS pNtH=GetNtHeaders(ImageBase);
  155.  
  156.   pdwRvas=(PDWORD)RvaToPtr(pNtH,ImageBase,pExportDir->AddressOfFunctions);
  157. for(DWORD i=0;i<pExportDir->NumberOfFunctions;i++)
  158. {
  159. if(*pdwRvas)
  160. ++dwnum;
  161. ++pdwRvas;
  162. return dwnum;
  163. }
  164. BOOL  IsDataDirPresent(LPVOID ImageBase,USHORT DirectoryEntry)
  165. {
  166.     if(!GetDirectoryEntryToData(ImageBase,DirectoryEntry))
  167. return FALSE;
  168. return TRUE;
  169. }