Skip to content

Commit bbcbc60

Browse files
authored
Merge pull request #144 from zeffy/master
Add some checks that should fix some issues with certain packers
2 parents 8125eac + 4fba08f commit bbcbc60

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

NativeCore/Windows/EnumerateRemoteSectionsAndModules.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,27 @@ void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer process, Enumerate
9696
const auto sectionAddress = reinterpret_cast<size_t>(me32.modBaseAddr) + sectionHeader.VirtualAddress;
9797
for (auto j = it; j != std::end(sections); ++j)
9898
{
99-
if (sectionAddress >= reinterpret_cast<size_t>(j->BaseAddress) && sectionAddress < reinterpret_cast<size_t>(j->BaseAddress) + static_cast<size_t>(j->Size))
99+
if (sectionAddress >= reinterpret_cast<size_t>(j->BaseAddress)
100+
&& sectionAddress < reinterpret_cast<size_t>(j->BaseAddress) + static_cast<size_t>(j->Size)
101+
&& sectionHeader.VirtualAddress + sectionHeader.Misc.VirtualSize <= me32.modBaseSize )
100102
{
101-
// Copy the name because it is not null padded.
102-
char buffer[IMAGE_SIZEOF_SHORT_NAME + 1] = { 0 };
103-
std::memcpy(buffer, sectionHeader.Name, IMAGE_SIZEOF_SHORT_NAME);
104-
105-
if (std::strcmp(buffer, ".text") == 0 || std::strcmp(buffer, "code") == 0)
103+
if ((sectionHeader.Characteristics & IMAGE_SCN_CNT_CODE) == IMAGE_SCN_CNT_CODE)
106104
{
107105
j->Category = SectionCategory::CODE;
108106
}
109-
else if (std::strcmp(buffer, ".data") == 0 || std::strcmp(buffer, "data") == 0 || std::strcmp(buffer, ".rdata") == 0 || std::strcmp(buffer, ".idata") == 0)
107+
else if (sectionHeader.Characteristics & (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA))
110108
{
111109
j->Category = SectionCategory::DATA;
112110
}
113111

114-
MultiByteToUnicode(buffer, j->Name, IMAGE_SIZEOF_SHORT_NAME);
112+
try {
113+
// Copy the name because it is not null padded.
114+
char buffer[IMAGE_SIZEOF_SHORT_NAME + 1] = { 0 };
115+
std::memcpy(buffer, sectionHeader.Name, IMAGE_SIZEOF_SHORT_NAME);
116+
MultiByteToUnicode(buffer, j->Name, IMAGE_SIZEOF_SHORT_NAME);
117+
} catch (std::range_error &) {
118+
std::memset(j->Name, 0, sizeof j->Name);
119+
}
115120
std::memcpy(j->ModulePath, me32.szExePath, std::min(MAX_PATH, PATH_MAXIMUM_LENGTH));
116121

117122
break;

0 commit comments

Comments
 (0)