I have gotten very interested in windows kernel mode development recently and I am trying to write a jmp instruction inside a program in usermode. It is important to note my driver is manual mapped to kernel space via drvmap. I can read/write easily with MmCopyVirtualMemory but unfortunately the part of memory I need to write to is protected. I tried multiple methods to no avail. The first method i tried was using ZwProtectVirtualMemory which is an ntdll.dll undocumented function, I imported the function like so in my driver
extern "C"{__declspec(dllimport) NTSYSAPI NTSTATUS NTAPI ZwProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PULONG NumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG OldAccessProtection);}
and I call the function like so
KAPC_STATE apc;
KeStackAttachProcess(proc, &apc);
auto addy = (void*)in->addr;
unsigned long old_prot;
ZwProtectVirtualMemory(ZwCurrentProcess(), &addy, (PULONG)in->sz, PAGE_EXECUTE_READWRITE, &old_prot);
KeUnstackDetachProcess(&apc);
That BSODS me giving me the unhandled kmode exception message. The second thing I tried was flipping the 16th bit of the cr0 register but that BSOD'ed me aswell. I also tried mapping an mdl from physical memory but that was way out of my league. Any ideas are appreciated. this issue has been bugging me for days.