MCPcopy Create free account
hub / github.com/BeneficialCode/WinArk / MapSectionForShellCode

Method MapSectionForShellCode

KernelLibrary/Section.cpp:411–464  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

409}
410
411NTSTATUS Section::MapSectionForShellCode(DllStats* pDllStats,
412 PVOID* pBaseAddr) {
413 ASSERT(pDllStats);
414 NTSTATUS status = STATUS_SUCCESS;
415
416 PVOID baseAddress = nullptr;
417
418 if (!pDllStats->IsVaid()) {
419 status = STATUS_INVALID_PARAMETER_MIX;
420 return status;
421 }
422
423 PVOID reservedAddr = pDllStats->PreferredAddress;
424 SIZE_T regionSize = pDllStats->SizeOfImage;
425
426 status = ZwAllocateVirtualMemory(NtCurrentProcess(),
427 &reservedAddr, 0, &regionSize,
428 MEM_RESERVE, PAGE_NOACCESS);
429
430 if (status != STATUS_SUCCESS &&
431 status != STATUS_CONFLICTING_ADDRESSES) {
432 LogError("Error: (0x%X) MapSectionForShellCode > ZwAllocateVirtualMemory, PID=%u, sectionType=%c, "
433 "addr=0x%p, sz=0x%X\n",
434 status, PsGetCurrentProcessId(),
435 pDllStats->Type, pDllStats->PreferredAddress,
436 pDllStats->SizeOfImage);
437 return status;
438 }
439
440 SIZE_T viewSize = 0;
441 LARGE_INTEGER offset{};
442
443 status = MmMapViewOfSection(pDllStats->Section,
444 IoGetCurrentProcess(), &baseAddress, 0, 0,
445 &offset, &viewSize, ViewUnmap, 0, PAGE_EXECUTE);
446
447 if (!NT_SUCCESS(status)) {
448 LogError("ERROR: (0x%X) MapSectionForShellCode > MmMapViewOfSection, "
449 "PID = % u, sectionType = % c",
450 status,
451 PsGetCurrentProcessId(),
452 pDllStats->Type
453 );
454 return status;
455 }
456
457 regionSize = 0;
458 status = ZwFreeVirtualMemory(NtCurrentProcess(), &reservedAddr, &regionSize, MEM_RELEASE);
459
460 if (pBaseAddr)
461 *pBaseAddr = baseAddress;
462
463 return status;
464}

Callers

nothing calls this directly

Calls 2

LogErrorFunction · 0.85
IsVaidMethod · 0.80

Tested by

no test coverage detected