Skip to content

Instantly share code, notes, and snippets.

@antonioCoco
Created July 12, 2020 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save antonioCoco/6593d85295c64d77848fab412c49c7f6 to your computer and use it in GitHub Desktop.
Save antonioCoco/6593d85295c64d77848fab412c49c7f6 to your computer and use it in GitHub Desktop.
LPVOID MappingInjectionAlloc(HANDLE hProc, char* buffer, SIZE_T bufferSize, DWORD protectionType) {
pMapViewOfFile3 MapViewOfFile3 = (pMapViewOfFile3)GetProcAddress(GetModuleHandleW(L"kernelbase.dll"), "MapViewOfFile3");
HANDLE hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, (DWORD)bufferSize, NULL);
if (hFileMap == NULL)
{
printf("CreateFileMapping failed with error: %d\n", GetLastError());
exit(-1);
}
LPVOID lpMapAddress = MapViewOfFile3(hFileMap, GetCurrentProcess(), NULL, 0, 0, 0, PAGE_READWRITE, NULL, 0);
if (lpMapAddress == NULL)
{
printf("MapViewOfFile failed with error: %d\n", GetLastError());
exit(-1);
}
memcpy((PVOID)lpMapAddress, buffer, bufferSize);
LPVOID lpMapAddressRemote = MapViewOfFile3(hFileMap, hProc, NULL, 0, 0, 0, protectionType, NULL, 0);
if (lpMapAddressRemote == NULL)
{
printf("\nMapViewOfFile3 failed with error: %d\n", GetLastError());
exit(-1);
}
UnmapViewOfFile(hFileMap);
CloseHandle(hFileMap);
return lpMapAddressRemote;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment