Skip to content

Instantly share code, notes, and snippets.

@Little-Ki
Last active October 25, 2020 07:26
Show Gist options
  • Save Little-Ki/ca9d88101f59e27b0a5feed73d6f748c to your computer and use it in GitHub Desktop.
Save Little-Ki/ca9d88101f59e27b0a5feed73d6f748c to your computer and use it in GitHub Desktop.
[Code] [Kernel] IRQL, memory protection and memory modify
// To write other's memory in kernel, there has other rules, IRQL and memory protection.
// If you try to write memory when memory protection is enabled, you will get BSOD.
// In order to write other's memory, we should disable memory protection first and upgrade IRQL to level 2 (normally is 0).
// The sign of memory protection state is in CR0 register. So just modify it can change state.
// To change IRQL level, use KeRaiseIrqlToDpcLevel and KeLowerIrql, in 64 bits system, IRQL state stored in CR8 register, in 32 bits mode, it's in KPCR.
// Code:
KIRQL WPOFFx64()
{
KIRQL irql = KeRaiseIrqlToDpcLevel();
UINT64 cr0 = __readcr0();
cr0 &= 0xfffffffffffeffff;
__writecr0( cr0 );
_disable();
return(irql);
}
void WPONx64( KIRQL irql )
{
UINT64 cr0 = __readcr0();
cr0 |= 0x10000;
_enable();
__writecr0( cr0 );
KeLowerIrql( irql );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment