44#include < algorithm>
55#include < functional>
66
7+ #include " ReClassNET_Plugin.hpp"
8+
79bool IsValidMemoryRange (LPCVOID address, int length)
810{
911 auto endAddress = static_cast <const uint8_t *>(address) + length;
@@ -72,19 +74,9 @@ bool WriteMemory(LPVOID address, const std::vector<uint8_t>& buffer)
7274 return false ;
7375}
7476// ---------------------------------------------------------------------------
75- void EnumerateRemoteSectionsAndModules (const std::function<void (const void *, const void * , std::wstring&&)>& moduleCallback, const std::function<void(const void *, const void * , std::wstring&&, int , int , int , std::wstring&&)>& sectionCallback)
77+ void EnumerateRemoteSectionsAndModules (const std::function<void (RC_Pointer, RC_Pointer , std::wstring&&)>& moduleCallback, const std::function<void(RC_Pointer, RC_Pointer, SectionType, SectionProtection , std::wstring&&, std::wstring&&)>& sectionCallback)
7678{
77- struct SectionInfo
78- {
79- LPVOID BaseAddress;
80- SIZE_T RegionSize;
81- WCHAR Name[IMAGE_SIZEOF_SHORT_NAME + 1 ];
82- DWORD State;
83- DWORD Protection;
84- DWORD Type;
85- WCHAR ModulePath[260 ];
86- };
87- std::vector<SectionInfo> sections;
79+ std::vector<EnumerateRemoteSectionData> sections;
8880
8981 // First enumerate all memory sections.
9082 MEMORY_BASIC_INFORMATION memInfo = { 0 };
@@ -94,12 +86,50 @@ void EnumerateRemoteSectionsAndModules(const std::function<void(const void*, con
9486 {
9587 if (memInfo.State == MEM_COMMIT )
9688 {
97- SectionInfo section = {};
89+ EnumerateRemoteSectionData section = {};
9890 section.BaseAddress = memInfo.BaseAddress ;
99- section.RegionSize = memInfo.RegionSize ;
100- section.State = memInfo.State ;
101- section.Protection = memInfo.Protect ;
102- section.Type = memInfo.Type ;
91+ section.Size = memInfo.RegionSize ;
92+
93+ switch (memInfo.Protect & 0xFF )
94+ {
95+ case PAGE_EXECUTE :
96+ section.Protection = SectionProtection::Execute;
97+ break ;
98+ case PAGE_EXECUTE_READ :
99+ section.Protection = SectionProtection::Execute | SectionProtection::Read;
100+ break ;
101+ case PAGE_EXECUTE_READWRITE :
102+ case PAGE_EXECUTE_WRITECOPY :
103+ section.Protection = SectionProtection::Execute | SectionProtection::Read | SectionProtection::Write;
104+ break ;
105+ case PAGE_NOACCESS :
106+ section.Protection = SectionProtection::NoAccess;
107+ break ;
108+ case PAGE_READONLY :
109+ section.Protection = SectionProtection::Read;
110+ break ;
111+ case PAGE_READWRITE :
112+ case PAGE_WRITECOPY :
113+ section.Protection = SectionProtection::Read | SectionProtection::Write;
114+ break ;
115+ }
116+ if ((memInfo.Protect & PAGE_GUARD ) == PAGE_GUARD )
117+ {
118+ section.Protection |= SectionProtection::Guard;
119+ }
120+
121+ switch (memInfo.Type )
122+ {
123+ case MEM_IMAGE :
124+ section.Type = SectionType::Image;
125+ break ;
126+ case MEM_MAPPED :
127+ section.Type = SectionType::Mapped;
128+ break ;
129+ case MEM_PRIVATE :
130+ section.Type = SectionType::Private;
131+ break ;
132+ }
103133
104134 sections.push_back (std::move (section));
105135 }
@@ -160,9 +190,9 @@ void EnumerateRemoteSectionsAndModules(const std::function<void(const void*, con
160190 auto ldr = reinterpret_cast <LDR_MODULE *>(peb->LoaderData ->InLoadOrderModuleList .Flink );
161191 while (ldr->BaseAddress != nullptr )
162192 {
163- moduleCallback (ldr->BaseAddress , (const void * )(intptr_t )ldr->SizeOfImage , ldr->FullDllName .Buffer );
193+ moduleCallback ((RC_Pointer) ldr->BaseAddress , (RC_Pointer )(intptr_t )ldr->SizeOfImage , ldr->FullDllName .Buffer );
164194
165- auto it = std::lower_bound (std::begin (sections), std::end (sections), ldr->BaseAddress , [§ions](const SectionInfo & lhs, const LPVOID & rhs)
195+ auto it = std::lower_bound (std::begin (sections), std::end (sections), ldr->BaseAddress , [§ions](const auto & lhs, const LPVOID & rhs)
166196 {
167197 return lhs.BaseAddress < rhs;
168198 });
@@ -176,15 +206,15 @@ void EnumerateRemoteSectionsAndModules(const std::function<void(const void*, con
176206 auto sectionAddress = (intptr_t )ldr->BaseAddress + (intptr_t )sectionHeader->VirtualAddress ;
177207 for (auto j = it; j != std::end (sections); ++j)
178208 {
179- if (sectionAddress >= (intptr_t )j->BaseAddress && sectionAddress < (intptr_t )j->BaseAddress + (intptr_t )j->RegionSize )
209+ if (sectionAddress >= (intptr_t )j->BaseAddress && sectionAddress < (intptr_t )j->BaseAddress + (intptr_t )j->Size )
180210 {
181211 // Copy the name because it is not null padded.
182212 char buffer[IMAGE_SIZEOF_SHORT_NAME + 1 ] = { 0 };
183213 std::memcpy (buffer, sectionHeader->Name , IMAGE_SIZEOF_SHORT_NAME );
184214
185215 size_t convertedChars = 0 ;
186216 mbstowcs_s (&convertedChars, j->Name , IMAGE_SIZEOF_SHORT_NAME , buffer, _TRUNCATE);
187- std::memcpy (j->ModulePath , ldr->FullDllName .Buffer , sizeof (SectionInfo ::ModulePath));
217+ std::memcpy (j->ModulePath , ldr->FullDllName .Buffer , sizeof (EnumerateRemoteSectionData ::ModulePath));
188218 break ;
189219 }
190220 }
@@ -195,7 +225,7 @@ void EnumerateRemoteSectionsAndModules(const std::function<void(const void*, con
195225
196226 for (auto && section : sections)
197227 {
198- sectionCallback (section.BaseAddress , (const void * )section.RegionSize , section.Name , section.State , section. Protection , section.Type , section.ModulePath );
228+ sectionCallback (section.BaseAddress , (RC_Pointer )section.Size , section.Type , section.Protection , section.Name , section.ModulePath );
199229 }
200230}
201231// ---------------------------------------------------------------------------
0 commit comments