Legal disclaimer: this shit is only for educational purposes i am not held responsible for any misuse god forbid
Hello romdump community
Today we are looking at the chinese MonProcessControl.sys vulnerable driver a typical playbook driver for BYOVD abuse and usermode process termination we will dive into reverse engineering the vulnerable function and replicate the finding we will recover the ioctl value and the vulnerable handler purely by static analysis, as if we had no prior knowledge of them or any specifics to train you on how to look for valid candidates for byovd with that said let us dive in
What are we looking at?
what is monprocesscontrol driver? let us have some context:
the driver is relatively very small only 50~ kb in size i was able to reverse engineer the majority of the driver just enough to collect information to map out the core functionality and purpose of this driver
the driver is developed by Honor for its PC software suite, it belongs to HONOR's system utilities specifically tied to its C:\HONOR\MagicAnimation\MagicSpaceTurbo.exe software to monitor and manage system processes and performance on honor laptops here is a list of related apis used by it:
- PsGetCurrentProcessId
- PsSetCreateProcessNotifyRoutineEx
- RtlCreateSecurityDescriptor
- RtlAbsoluteToSelfRelativeSD
- RtlGetDaclSecurityDescriptor
- registry manipulation apis
- and so on ...
Reverse engineering using binary ninja:
when we open the driver in binary ninja the program instantly recognizes the DriverEntry
sub_14000b000() is the driver entry where we pass the DriverObject
judging by this there is no signs of calls to WdfIoQueueCreate or other WDF (KMDF/UMDF) frameworks APIs so this driver is a typical simple WDM driver which means we won't go through the torture and witch work we were doing in the previous writeup.
WDM drivers handle IRPs directly through DriverObject -> MajorFunction dispatch routines instead.
in the driver entry the driver's code populate the the driverobject with pointers to some functions we are interested specifically at the offset 0xe0 why? this offset. At this offset we expect the driver to store the pointer to the driver's IRP_MJ_DEVICE_CONTROL dispatch routine MajorFunction[14] so this is the address of the ioctl handler the routine responsible for processing DeviceIoControl requests sent from user-mode applications.
MajorFunction starts at DRIVER_OBJECT+0x70 on x64, and IRP_MJ_DEVICE_CONTROL is index 14 so 0x70 + 14*8 = 0xE0.
so this is our next move looking inside sub_14000813c()
we can see that the ioctl dispatcher looks for the return value from calling this function sub_1400082a4() if the returned NTSTATUS is negative (a failure code)it calls IoCompleteRequest which will signal the I/O manager that the device driver has finished processing the Irp package and returning it hmmm so by guessing this is the access control function right? because because it gates the dispatch routine before any real work happens in the dispatch routine this is the driver way of saying
so let us inspect the function!!
the function is pretty simple and dangerous it will grant us ability to perform Ring 0 operations if we passed its test let us see:
we first intialize two vars
int32_t var_18 = 0x540052 void* const var_10 = u"\\HONOR\\MagicAnimation\\MagicSpaceTurbo.exe"
0x52 = 82 bytes = 41 UTF-16 code units, exactly the byte length of the string \HONOR\MagicAnimation\MagicSpaceTurbo.exe &var_18 acts as the base address of this unicode string struct (Length, Maxlength and buffer pointer in var_10)
if ((arg2 != 0x224004 && arg2 != 0x22400c && arg2 != 0x224030 && arg2 != 0x224034 && arg2 != 0x22403c && arg2 != 0x224048) || *(arg1 + 0x40) == 0) return 0
it then checks if the incoming ioctl matches one of the six specific control codes AND (arg1 + 0x40) == 0 checks if the enforcement flag is disabled if both succeeds it rejects
now here is where it begins to get interesting the function call PsGetCurrentProcessId() to get the pid of the user-mode caller which is us it then store it in rax_1 and passes it to sub_1400012cc with a pointer to uninitialized memory now we need a trust me boss moment here to keep this article digestable and short the function calls another function which dereferences a function pointer inside a function pointer table in .rdata. resolved dynamically via MmGetSystemRoutineAddressand calls it this function resolves the memory address of the function ZwQuerySystemInformation and calls it on our process to get our own process's ProcessImageFileName and then returns that result to the function which will then allocate a UNICODE_STRING buffer later stored in P which we passed in the function we are in
now let us complete to see what our function does with our processimagefilename our function checks if the pointer to the P struct is null and its internal Buffer is not empty before reading them to avoid null deref nice but it makes a disaster logic bug, the function then calls sub_14000178c() with P_1 (the pointer to the P struct that contains our process image file name and it passes var_18 which contains the length of var_10 ) and passes 1 before cleanup routine and returning
so we can already smell that this function will compare both UNICODE_STRING structures let us verify that and check if that is the only thing it does there might be another checks
so all the driver does for caller verification is checking if the suffix of our processimagefilename matches \HONOR\MagicAnimation\MagicSpaceTurbo.exe THAT IS IT REALLY
The vulnerable handler
now let us get back to the ioctl dispatcher and inspect the first ioctl code handler function sub_1400022a4() for 0x22400c to see wut it does
as you can see the driver can terminate any process thanks to sub_1400022a4 which calls sub_1400016bc with pid specified by us in arg_8 which calls sub_1400013f8 internally that returns the handle to the process specified pid and then it calls ZwTerminateProcess(handle to the process we passed its pid) and then our ioctl handler function stores the result in rax_3 if zero it prints the clear message of failing to terminate the process otherwise it returns rax_3 to the ioctl dispatcher which then eventually goes through
label_14000821d: arg2->IoStatus..Status = rbx_9 IofCompleteRequest(Irp: arg2, PriorityBoost: 0)
and that is it !!
Arbitrary process termination by a kernel driver which its caller authorization relies on matching an expected directory against the caller image directory so what do we do, there is no signature, no ACL, no session check. we just spoof it (directory + filename)
here is driver and the poc i made in rust ofc main.rs make sure to place the driver MonProcessControl.sys
in same dir as the poc
and dont forget to do
sc create MonProcessControl binPath= "C:\Users\***\Desktop\china\MonProcessControl.sys" type= kernel
sc start MonProcessControl
why does this work on even ppl protected processes?
Because PPL is designed to restrict user mode callers while the kernel trusts itself and operates above those access checks.
The PreviousMode check taken from the calling thread is skipped in kernel mode ppl enforcement in routines like ntopenprocess applies when PreviousMode == UserMode but when kernel mode calls routines like zwopenprocess as we saw previously in the driver code PreviousMode is KernelMode so the kernel skips the ppl trust comparsion the driver passing OBJ_KERNEL_HANDLE in the OBJECT_ATTRIBUTES (and/or opening from a system context), which tells the object manager to skip the access/PPL checks. So PPL protect EDR processes can get terminated and here is the demo of kicking windows defender out of orbit using the poc
Quick note tho: as you saw that we went from driver entry all way to the ioctl handler chain but this is because we manually inspected the first ioctl handler in the switch statement in the dispatcher and funny enough we were lucky that the first ioctl handler is actually the one related to the process termination. In a typical hunt for the vulnerable handlers we dont inspect each ioctl code one by one we grep the import table and .rdata for interesting routine names like ZwTerminateProcess, MmCopyMemory, MmMapIoSpace and xref upward all way up to the subcommand or ioctl handler because there might be dozens or even hundreds of ioctl codes as we did before in the writeup that you should give a read named suicide i only did this to get you familiar with the DriverEntry to ioctl handler chain here is also how we could have traced this using this trick
as you can see i simply searched in the imports for something like ZwTerminateProcess and found the match and i then xreffed it and found a call inside sub_1400016bc() function which is called by the ioctl handler for 0x22400c ioctl code which we just discussed simple and gets the job done
Thx for reading ❤️