ImageInterpreter.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "ImageInterpreter.hpp"
  2. #undef __BASE_FILE__
  3. #define __BASE_FILE__ "ImageInterpreter.cpp"
  4. ImageInterpreter::ImageInterpreter() :
  5. _ImageBasePtr(nullptr),
  6. _NTHeadersPtr(nullptr),
  7. _SectionHeaderTablePtr(nullptr) {}
  8. bool ImageInterpreter::ParseImage(const PVOID PtrToImageBase, bool DisableRelocParsing) {
  9. if (PtrToImageBase == nullptr)
  10. return false;
  11. PIMAGE_DOS_HEADER PtrToDosHeader =
  12. reinterpret_cast<PIMAGE_DOS_HEADER>(PtrToImageBase);
  13. if (PtrToDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
  14. return false;
  15. PIMAGE_NT_HEADERS PtrToNtHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>(
  16. reinterpret_cast<uint8_t*>(PtrToImageBase) +
  17. PtrToDosHeader->e_lfanew
  18. );
  19. if (PtrToNtHeaders->Signature != IMAGE_NT_SIGNATURE)
  20. return false;
  21. if (PtrToNtHeaders->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
  22. return false;
  23. #if defined(_M_AMD64)
  24. if (PtrToNtHeaders->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64)
  25. return false;
  26. #elif defined(_M_IX86)
  27. if (PtrToNtHeaders->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
  28. return false;
  29. #else
  30. #error "Unsupported architecture."
  31. #endif
  32. PIMAGE_SECTION_HEADER PtrToSectionHeaderTable =
  33. reinterpret_cast<PIMAGE_SECTION_HEADER>(
  34. reinterpret_cast<char*>(&PtrToNtHeaders->OptionalHeader) +
  35. PtrToNtHeaders->FileHeader.SizeOfOptionalHeader
  36. );
  37. std::map<uint64_t, size_t> SectionNameTable;
  38. std::map<uintptr_t, size_t> SectioMapAddressTable;
  39. std::map<uintptr_t, size_t> RelocationAddressTable;
  40. for (WORD i = 0; i < PtrToNtHeaders->FileHeader.NumberOfSections; ++i) {
  41. uint64_t SectionName =
  42. *reinterpret_cast<uint64_t*>(PtrToSectionHeaderTable[i].Name);
  43. if (SectionNameTable.find(SectionName) != SectionNameTable.end())
  44. continue;
  45. SectionNameTable[SectionName] = i;
  46. uintptr_t SectionMapAddress =
  47. PtrToSectionHeaderTable[i].VirtualAddress;
  48. SectioMapAddressTable[SectionMapAddress] = i;
  49. }
  50. if (DisableRelocParsing == false &&
  51. PtrToNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0) {
  52. DWORD RelocTableRva =
  53. PtrToNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
  54. PIMAGE_BASE_RELOCATION PtrToRelocTable = nullptr;
  55. {
  56. auto v = SectioMapAddressTable.lower_bound(RelocTableRva);
  57. if (v != SectioMapAddressTable.end()) {
  58. if (v->first != RelocTableRva) {
  59. if (v != SectioMapAddressTable.begin()) {
  60. --v;
  61. PtrToRelocTable = reinterpret_cast<PIMAGE_BASE_RELOCATION>(
  62. reinterpret_cast<uint8_t*>(PtrToImageBase) +
  63. PtrToSectionHeaderTable[v->second].PointerToRawData
  64. );
  65. }
  66. } else {
  67. PtrToRelocTable = reinterpret_cast<PIMAGE_BASE_RELOCATION>(
  68. reinterpret_cast<uint8_t*>(PtrToImageBase) +
  69. PtrToSectionHeaderTable[v->second].PointerToRawData
  70. );
  71. }
  72. }
  73. }
  74. while (PtrToRelocTable != nullptr && PtrToRelocTable->VirtualAddress != 0) {
  75. DWORD Rva = PtrToRelocTable->VirtualAddress;
  76. PWORD RelocItems = reinterpret_cast<PWORD>(PtrToRelocTable + 1);
  77. DWORD RelocItemsCount = (PtrToRelocTable->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
  78. for (DWORD i = 0; i < RelocItemsCount; ++i) {
  79. int RelocType = RelocItems[i] >> 12;
  80. switch (RelocType) {
  81. case IMAGE_REL_BASED_ABSOLUTE:
  82. break;
  83. case IMAGE_REL_BASED_HIGH:
  84. case IMAGE_REL_BASED_LOW:
  85. case IMAGE_REL_BASED_HIGHADJ:
  86. RelocationAddressTable[Rva + (RelocItems[i] & 0x0fff)] = 2;
  87. break;
  88. case IMAGE_REL_BASED_HIGHLOW:
  89. RelocationAddressTable[Rva + (RelocItems[i] & 0x0fff)] = 4;
  90. break;
  91. #if defined(IMAGE_REL_BASED_DIR64)
  92. case IMAGE_REL_BASED_DIR64:
  93. RelocationAddressTable[Rva + (RelocItems[i] & 0x0fff)] = 8;
  94. break;
  95. #endif
  96. default:
  97. break;
  98. }
  99. }
  100. PtrToRelocTable = reinterpret_cast<PIMAGE_BASE_RELOCATION>(
  101. &RelocItems[RelocItemsCount]
  102. );
  103. }
  104. }
  105. _ImageBasePtr = PtrToImageBase;
  106. _NTHeadersPtr = PtrToNtHeaders;
  107. _SectionHeaderTablePtr = PtrToSectionHeaderTable;
  108. _SectionNameTable = std::move(SectionNameTable);
  109. _SectionMapAddressTable = std::move(SectioMapAddressTable);
  110. _RelocationAddressTable = std::move(RelocationAddressTable);
  111. return true;
  112. }
  113. PIMAGE_DOS_HEADER ImageInterpreter::GetImageDosHeader() const {
  114. return reinterpret_cast<PIMAGE_DOS_HEADER>(_ImageBasePtr);
  115. }
  116. PIMAGE_NT_HEADERS ImageInterpreter::GetImageNTHeaders() const {
  117. return _NTHeadersPtr;
  118. }
  119. PIMAGE_SECTION_HEADER ImageInterpreter::GetSectionHeaderTable() const {
  120. return _SectionHeaderTablePtr;
  121. }
  122. PIMAGE_SECTION_HEADER ImageInterpreter::GetSectionHeader(const char* SectionName) const {
  123. uint64_t NameValue = 0;
  124. for (int i = 0; i < sizeof(NameValue) && SectionName[i]; ++i)
  125. reinterpret_cast<char*>(&NameValue)[i] = SectionName[i];
  126. auto v = _SectionNameTable.find(NameValue);
  127. if (v == _SectionNameTable.end())
  128. return nullptr;
  129. else
  130. return &_SectionHeaderTablePtr[v->second];
  131. }
  132. PIMAGE_SECTION_HEADER ImageInterpreter::GetSectionHeader(uintptr_t Rva) const {
  133. auto v = _SectionMapAddressTable.lower_bound(Rva);
  134. if (v == _SectionMapAddressTable.end())
  135. return nullptr;
  136. if (v->first != Rva) {
  137. if (v == _SectionMapAddressTable.begin())
  138. return nullptr;
  139. --v;
  140. return &_SectionHeaderTablePtr[v->second];
  141. } else {
  142. return &_SectionHeaderTablePtr[v->second];
  143. }
  144. }
  145. bool ImageInterpreter::IsRvaRangeInRelocTable(uintptr_t Rva, size_t Size) const {
  146. auto v = _RelocationAddressTable.lower_bound(Rva);
  147. if (v == _RelocationAddressTable.end())
  148. return false;
  149. if (v->first == Rva) {
  150. return true;
  151. } else {
  152. auto w = v--;
  153. if (v->first <= Rva && Rva < v->first + v->second)
  154. return true;
  155. if (Rva + Size <= w->first)
  156. return false;
  157. return true;
  158. }
  159. }