33
44PMMP_GLOBAL_DATA MmpGlobalDataPtr;
55
6- BOOLEAN MmpBuildSectionName (_Out_ PUNICODE_STRING SectionName) {
7- WCHAR buffer[128 ];
8-
9- swprintf_s (buffer, L" \\ Sessions\\ %d\\ BaseNamedObjects\\ MMPP*%08X" , NtCurrentPeb ()->SessionId , (unsigned int )(ULONG_PTR)NtCurrentProcessId ());
10- return RtlCreateUnicodeString (SectionName, buffer);
11- }
12-
136PRTL_RB_TREE FindLdrpModuleBaseAddressIndex () {
147 PRTL_RB_TREE LdrpModuleBaseAddressIndex = nullptr ;
158 PLDR_DATA_TABLE_ENTRY_WIN10 nt10 = decltype (nt10)(MmpGlobalDataPtr->MmpBaseAddressIndex ->NtdllLdrEntry );
@@ -297,60 +290,41 @@ VOID InitializeWindowsVersion() {
297290}
298291
299292NTSTATUS MmpAllocateGlobalData () {
300- NTSTATUS status = STATUS_UNSUCCESSFUL ;
293+ NTSTATUS status;
301294 HANDLE hSection = nullptr ;
302295 OBJECT_ATTRIBUTES oa;
303296 LARGE_INTEGER li;
304297 UNICODE_STRING us{};
298+ PVOID BaseAddress = 0 ;
299+ SIZE_T ViewSize = 0 ;
300+ WCHAR buffer[128 ];
305301
306- li.QuadPart = 0x1000 ;
307-
308- do {
309-
310- if (!MmpBuildSectionName (&us))break ;
311-
312- InitializeObjectAttributes (&oa, &us, 0 , nullptr , nullptr );
313-
314- status = NtCreateSection (
315- &hSection,
316- SECTION_ALL_ACCESS,
317- &oa,
318- &li,
319- PAGE_READWRITE,
320- SEC_COMMIT | SEC_BASED,
321- nullptr
322- );
323- if (!NT_SUCCESS (status)) {
324- if (status != STATUS_OBJECT_NAME_COLLISION) break ;
302+ swprintf_s (
303+ buffer,
304+ L" \\ Sessions\\ %d\\ BaseNamedObjects\\ MMPP*%08X" ,
305+ NtCurrentPeb ()->SessionId ,
306+ (unsigned int )(ULONG_PTR)NtCurrentProcessId ()
307+ );
325308
326- HANDLE hSection2;
327- status = NtOpenSection (
328- &hSection2,
329- SECTION_ALL_ACCESS,
330- &oa
331- );
332- if (!NT_SUCCESS (status))break ;
333-
334- SECTION_BASIC_INFORMATION sbi{};
335- status = NtQuerySection (
336- hSection2,
337- SECTION_INFORMATION_CLASS::SectionBasicInformation,
338- &sbi,
339- sizeof (sbi),
340- nullptr
341- );
309+ RtlInitUnicodeString (&us, buffer);
310+ InitializeObjectAttributes (&oa, &us, 0 , nullptr , nullptr );
342311
343- NtClose (hSection2);
344- MmpGlobalDataPtr = (PMMP_GLOBAL_DATA)sbi.BaseAddress ;
345- break ;
346- }
312+ li.QuadPart = 0x1000 ;
347313
348- PVOID BaseAddress = 0 ;
349- SIZE_T ViewSize = 0 ;
314+ status = NtCreateSection (
315+ &hSection,
316+ SECTION_ALL_ACCESS,
317+ &oa,
318+ &li,
319+ PAGE_READWRITE,
320+ SEC_COMMIT,
321+ nullptr
322+ );
323+ if (NT_SUCCESS (status)) {
350324 status = NtMapViewOfSection (
351325 hSection,
352326 NtCurrentProcess (),
353- &BaseAddress ,
327+ (PVOID*)&MmpGlobalDataPtr ,
354328 0 ,
355329 0 ,
356330 nullptr ,
@@ -359,19 +333,44 @@ NTSTATUS MmpAllocateGlobalData() {
359333 0 ,
360334 PAGE_READWRITE
361335 );
362- if (!NT_SUCCESS (status))break ;
363-
364- MmpGlobalDataPtr = (PMMP_GLOBAL_DATA)BaseAddress;
365336
366- } while (false );
367-
368- RtlFreeUnicodeString (&us);
369-
370- if (NT_SUCCESS (status)) {
371- status = hSection ? status : STATUS_ALREADY_INITIALIZED;
337+ if (!NT_SUCCESS (status)) {
338+ NtClose (hSection);
339+ }
372340 }
373341 else {
374- if (hSection)NtClose (hSection);
342+ if (status == STATUS_OBJECT_NAME_COLLISION) {
343+ status = NtOpenSection (
344+ &hSection,
345+ SECTION_ALL_ACCESS,
346+ &oa
347+ );
348+
349+ if (NT_SUCCESS (status)) {
350+ status = NtMapViewOfSection (
351+ hSection,
352+ NtCurrentProcess (),
353+ &BaseAddress,
354+ 0 ,
355+ 0 ,
356+ nullptr ,
357+ &ViewSize,
358+ ViewUnmap,
359+ 0 ,
360+ PAGE_READONLY
361+ );
362+
363+ NtClose (hSection);
364+
365+ if (NT_SUCCESS (status)) {
366+ MmpGlobalDataPtr = (PMMP_GLOBAL_DATA)((PMMP_GLOBAL_DATA)BaseAddress)->BaseAddress ;
367+ NtUnmapViewOfSection (NtCurrentProcess (), BaseAddress);
368+
369+ status = STATUS_ALREADY_INITIALIZED;
370+ }
371+
372+ }
373+ }
375374 }
376375
377376 return status;
@@ -399,6 +398,7 @@ NTSTATUS InitializeLockHeld() {
399398
400399 MmpGlobalDataPtr->MajorVersion = MEMORY_MODULE_MAJOR_VERSION;
401400 MmpGlobalDataPtr->MinorVersion = MEMORY_MODULE_MINOR_VERSION;
401+ MmpGlobalDataPtr->BaseAddress = MmpGlobalDataPtr;
402402
403403 GetSystemInfo (&MmpGlobalDataPtr->SystemInfo );
404404
0 commit comments