@@ -159,68 +159,96 @@ SIZE_T NTAPI _RtlCompareMemory(
159159 const VOID* Source1,
160160 const VOID* Source2,
161161 SIZE_T Length) {
162- return decltype (&_RtlCompareMemory)(GetProcAddress ((HMODULE)MmpGlobalDataPtr->MmpBaseAddressIndex ->NtdllLdrEntry ->DllBase ," RtlCompareMemory" ))(Source1, Source2, Length);
162+ return decltype (&_RtlCompareMemory)(GetProcAddress ((HMODULE)MmpGlobalDataPtr->MmpBaseAddressIndex ->NtdllLdrEntry ->DllBase , " RtlCompareMemory" ))(Source1, Source2, Length);
163163}
164164#define RtlCompareMemory _RtlCompareMemory
165165#endif
166166
167167NTSTATUS NTAPI RtlFindMemoryBlockFromModuleSection (
168- _In_ HMODULE hModule ,
169- _In_ LPCSTR lpSectionName ,
168+ _In_ HMODULE ModuleHandle ,
169+ _In_ LPCSTR SectionName ,
170170 _Inout_ PSEARCH_CONTEXT SearchContext) {
171171
172172 NTSTATUS status = STATUS_SUCCESS;
173- size_t begin = 0 , buffer = 0 ;
174- DWORD Length = 0 , bufferLength = 0 ;
175173
176174 __try {
177- begin = SearchContext->OutBufferPtr ;
178- Length = SearchContext->RemainingLength ;
179- buffer = SearchContext->InBufferPtr ;
180- bufferLength = SearchContext->BufferLength ;
181- if (!buffer || !bufferLength) {
182- SearchContext->OutBufferPtr = 0 ;
183- SearchContext->RemainingLength = 0 ;
184- return STATUS_INVALID_PARAMETER;
175+
176+ //
177+ // checks if no search pattern and length are provided
178+ //
179+
180+ if (!SearchContext->SearchPattern || !SearchContext->PatternSize ) {
181+ SearchContext->Result = nullptr ;
182+ SearchContext->MemoryBlockSize = 0 ;
183+
184+ status = STATUS_INVALID_PARAMETER;
185+ __leave;
185186 }
186- if (!begin) {
187- PIMAGE_NT_HEADERS headers = RtlImageNtHeader (hModule);
187+
188+ if (SearchContext->Result ) {
189+ ++SearchContext->Result ;
190+ --SearchContext->MemoryBlockSize ;
191+ }
192+ else {
193+
194+ //
195+ // if it is the first search, find the length and start address of the specified section
196+ //
197+
198+ PIMAGE_NT_HEADERS headers = RtlImageNtHeader (ModuleHandle);
188199 PIMAGE_SECTION_HEADER section = nullptr ;
189- if (!headers)return STATUS_INVALID_PARAMETER_1;
190- section = IMAGE_FIRST_SECTION (headers);
191- for (WORD i = 0 ; i < headers->FileHeader .NumberOfSections ; ++i) {
192- if (!_strnicmp (lpSectionName, (LPCSTR)section->Name , 8 )) {
193- begin = SearchContext->OutBufferPtr = (size_t )hModule + section->VirtualAddress ;
194- Length = SearchContext->RemainingLength = section->Misc .VirtualSize ;
195- break ;
200+
201+ if (headers) {
202+ section = IMAGE_FIRST_SECTION (headers);
203+ for (WORD i = 0 ; i < headers->FileHeader .NumberOfSections ; ++i) {
204+ if (!_strnicmp (SectionName, (LPCSTR)section->Name , 8 )) {
205+ SearchContext->Result = (LPBYTE)ModuleHandle + section->VirtualAddress ;
206+ SearchContext->MemoryBlockSize = section->Misc .VirtualSize ;
207+ break ;
208+ }
209+
210+ ++section;
211+ }
212+
213+ if (!SearchContext->Result || !SearchContext->MemoryBlockSize || SearchContext->MemoryBlockSize < SearchContext->PatternSize ) {
214+ SearchContext->Result = nullptr ;
215+ SearchContext->MemoryBlockSize = 0 ;
216+ status = STATUS_NOT_FOUND;
217+ __leave;
196218 }
197- ++section;
198219 }
199- if (!begin || !Length || Length < bufferLength) {
200- SearchContext->OutBufferPtr = 0 ;
201- SearchContext->RemainingLength = 0 ;
202- return STATUS_NOT_FOUND;
220+ else {
221+ status = STATUS_INVALID_PARAMETER_1;
222+ __leave;
203223 }
204224 }
205- else {
206- begin++;
207- Length--;
208- }
209- status = STATUS_NOT_FOUND;
210- for (DWORD i = 0 ; i < Length - bufferLength; ++begin, ++i) {
211- if (RtlCompareMemory ((PVOID)begin, (PVOID)buffer, bufferLength) == bufferLength) {
212- SearchContext->OutBufferPtr = begin;
213- --SearchContext->RemainingLength ;
214- return STATUS_SUCCESS;
225+
226+ //
227+ // perform a linear search on the pattern
228+ //
229+
230+ LPBYTE end = SearchContext->Result + SearchContext->MemoryBlockSize - SearchContext->PatternSize ;
231+ while (SearchContext->Result <= end) {
232+ if (RtlCompareMemory (SearchContext->SearchPattern , SearchContext->Result , SearchContext->PatternSize ) == SearchContext->PatternSize ) {
233+ __leave;
215234 }
235+
236+ ++SearchContext->Result ;
237+ --SearchContext->MemoryBlockSize ;
216238 }
239+
240+ //
241+ // if the search fails, clear the output parameters
242+ //
243+
244+ SearchContext->Result = nullptr ;
245+ SearchContext->MemoryBlockSize = 0 ;
246+ status = STATUS_NOT_FOUND;
217247 }
218248 __except (EXCEPTION_EXECUTE_HANDLER) {
219249 status = GetExceptionCode ();
220250 }
221251
222- SearchContext->OutBufferPtr = 0 ;
223- SearchContext->RemainingLength = 0 ;
224252 return status;
225253}
226254
0 commit comments