ParsePE.cpp
上传用户:chaoyu
上传日期:2013-04-28
资源大小:18k
文件大小:4k
源码类别:

杀毒

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "MemFileObject.h"
  3. #include ".ParsePE.h"
  4. CParsePE::CParsePE(void)
  5. {
  6. }
  7. CParsePE::~CParsePE(void)
  8. {
  9. }
  10. bool CParsePE::BasicParse(IN CMemFileObject* pScanObj, OUT FSPE* pFSPE)
  11. {
  12. if(!pScanObj->IsOpened())
  13. return false;
  14. DWORD dwObjSize = pScanObj->GetObjectSize();
  15. //Check size
  16. if( dwObjSize < sizeof(PIMAGE_DOS_HEADER) ) return FALSE;
  17. pFSPE->m_pImageDosHeader = (PIMAGE_DOS_HEADER)pScanObj->GetBuffer();
  18. //check "MZ" signature
  19. if( IMAGE_DOS_SIGNATURE != pFSPE->m_pImageDosHeader->e_magic )
  20. {
  21. pFSPE->m_bMZFile = false;
  22. return false;
  23. }
  24. pFSPE->m_bMZFile = true;
  25. if(pFSPE->m_pImageDosHeader->e_lfanew+sizeof(IMAGE_NT_SIGNATURE)>dwObjSize)
  26. {
  27. pFSPE->m_bPEFile = false;
  28. return true;
  29. }
  30. pFSPE->m_pNtHeaders = (PIMAGE_NT_HEADERS)(pFSPE->m_pImageDosHeader->e_lfanew + pScanObj->GetBuffer());
  31. //check "PE" signature
  32. if( IMAGE_NT_SIGNATURE != pFSPE->m_pNtHeaders->Signature)
  33. {
  34. pFSPE->m_bPEFile = false;
  35. return true;
  36. }
  37. pFSPE->m_bPEFile = true;
  38. pFSPE->m_pFileHeader = &pFSPE->m_pNtHeaders->FileHeader;
  39. pFSPE->m_pOptionalHeader = &pFSPE->m_pNtHeaders->OptionalHeader;
  40. //SECTIONS
  41. PIMAGE_SECTION_HEADER pSectionHeader = (PIMAGE_SECTION_HEADER)(pFSPE->m_pOptionalHeader + 1);
  42. pFSPE->m_nSectionCount = pFSPE->m_pFileHeader->NumberOfSections;
  43. ASSERT( pFSPE->m_nSectionCount < MAX_SECTIONS );
  44. for(int i=0; i<pFSPE->m_nSectionCount; i++)
  45. {
  46. pFSPE->m_aSectionHeaders[i] = pSectionHeader;
  47. pSectionHeader++;
  48. }
  49. // Entry point
  50. pFSPE->m_pEntryPoint = AddrM2F(pFSPE, (LPVOID)pFSPE->m_pOptionalHeader->AddressOfEntryPoint) + pScanObj->GetBuffer();
  51. if( pFSPE->m_pEntryPoint > (dwObjSize + pScanObj->GetBuffer()) )
  52. return false;
  53. //IMPORT TABLE
  54. PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(AddrM2F(pFSPE, (LPVOID)pFSPE->m_pOptionalHeader->DataDirectory[1].VirtualAddress) + pScanObj->GetBuffer());
  55. if( (LPBYTE)pImportDescriptor > (dwObjSize + pScanObj->GetBuffer()) )
  56. return false;
  57. for(i=0; pImportDescriptor&&pImportDescriptor->Characteristics; i++,pImportDescriptor++)
  58. {
  59. ASSERT( i<MAX_IMPORTS );
  60. pFSPE->m_aImportDescriptors[i] = pImportDescriptor;
  61. }
  62. pFSPE->m_nImportCount = i;
  63. //EXPORT TABLE
  64. if(pFSPE->m_pOptionalHeader->DataDirectory[0].VirtualAddress)
  65. pFSPE->m_pExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(AddrM2F(pFSPE, (LPVOID)pFSPE->m_pOptionalHeader->DataDirectory[0].VirtualAddress) + pScanObj->GetBuffer());
  66. if( (LPBYTE)pFSPE->m_pExportDirectory > (dwObjSize + pScanObj->GetBuffer()) )
  67. return false;
  68. //RESOURCE
  69. if(pFSPE->m_pOptionalHeader->DataDirectory[2].VirtualAddress)
  70. pFSPE->m_pResourceDirectory = (PIMAGE_RESOURCE_DIRECTORY)(AddrM2F(pFSPE, (LPVOID)pFSPE->m_pOptionalHeader->DataDirectory[2].VirtualAddress) + pScanObj->GetBuffer());
  71. if( (LPBYTE)pFSPE->m_pResourceDirectory > (dwObjSize + pScanObj->GetBuffer()) )
  72. return false;
  73. return true;
  74. }
  75. DWORD CParsePE::AddrM2F(IN FSPE* pFSPE, IN LPVOID lpMemAddr)
  76. {
  77. if( lpMemAddr < (LPVOID)pFSPE->m_aSectionHeaders[0]->VirtualAddress &&
  78. lpMemAddr >= NULL)
  79. return (DWORD)lpMemAddr;
  80. for(INT i=0; i<pFSPE->m_nSectionCount; i++)
  81. {
  82. if( lpMemAddr >= LPVOID(pFSPE->m_aSectionHeaders[i]->VirtualAddress) &&
  83. lpMemAddr <= LPVOID(pFSPE->m_aSectionHeaders[i]->VirtualAddress + pFSPE->m_aSectionHeaders[i]->Misc.VirtualSize) )
  84. {
  85. DWORD dwOffset = DWORD( (LPBYTE)lpMemAddr - (LPBYTE)pFSPE->m_aSectionHeaders[i]->VirtualAddress );
  86. if( dwOffset<pFSPE->m_aSectionHeaders[i]->SizeOfRawData )
  87. return pFSPE->m_aSectionHeaders[i]->PointerToRawData + dwOffset;
  88. }
  89. }
  90. return 0;
  91. }