jonomango / hv

Lightweight Intel VT-x Hypervisor.
MIT License
363 stars 77 forks source link

Can you ept hook usermode? #11

Closed thewolfram closed 1 year ago

thewolfram commented 1 year ago

hey, is it possible to do an ept hook in usermode with the current state of hv?

jonomango commented 1 year ago

Theoretically, it should be able to EPT hook in usermode with the current implementation, however, there are some limitations that apply to kernel mode as well. The issue is that the EPT hooking works with physical addresses instead of virtual addresses, so if the memory gets paged out while it is hooked the hook will now be invalidated and bad stuff occurs. Use at your own risk.

thewolfram commented 1 year ago

Theoretically, it should be able to EPT hook in usermode with the current implementation, however, there are some limitations that apply to kernel mode as well. The issue is that the EPT hooking works with physical addresses instead of virtual addresses, so if the memory gets paged out while it is hooked the hook will now be invalidated and bad stuff occurs. Use at your own risk.

I managed to ept hook in kernel and now trying to do so in usermode. I guess you first need to be in process context and then place a hook?

jonomango commented 1 year ago

You dont even need to be in the process context. Since the hypercall_install_ept_hook hypercall uses physical addresses, you just need to translate the usermode virtual address somehow (use the process's CR3).

jonomango commented 1 year ago

Also, make sure you are executing the hypercall on every processor, or else the hook will only apply on the processor that ran the hypercall :)

jonomango commented 1 year ago

Here is an old example of using the EPT hooks to intercept syscalls that should still be applicable.

thewolfram commented 1 year ago

Here is an old example of using the EPT hooks to intercept syscalls that should still be applicable.

Thanks, I actually used this example to do my hook in kernel. Now I need to figure out translating process VA to physical one.

thewolfram commented 1 year ago

So I managed to successfully install ept hook in usermode, but I have a problem when hook doesn't get activated right away, I need to call orig function few times and only then exec page gets executed

jonomango commented 1 year ago

What do you mean that you need to call the original function in the program you are hooking? Do you have an example of what you're doing so I can better understand?

thewolfram commented 1 year ago

Sorry for that long answer, my usermode application is just crashing

thewolfram commented 1 year ago

And btw, its crashing when I execute OriginalFunction() in my main for a second time, but HookedFunction() never gets called.

jonomango commented 1 year ago

Have you tried printing out the results for MmGetPhysicalAddress()? I see that you're locking the buffer that you allocate, but that doesn't prevent OriginalFunction() from being paged out (I'm not even sure if VirtualLock() ensures that the PFN stays the same).

thewolfram commented 1 year ago

Have you tried printing out the results for MmGetPhysicalAddress()? I see that you're locking the buffer that you allocate, but that doesn't prevent OriginalFunction() from being paged out (I'm not even sure if VirtualLock() ensures that the PFN stays the same).

Yeah, I tried to print them they have normal addresses. Can it be that my OriginalFunction and HookedFunction are very close to each other (in the same page)?

thewolfram commented 1 year ago

Thats what he said photo_2023-01-30_08-28-01

chiefmasterR commented 12 months ago

edit: reopened issue (https://github.com/jonomango/hv/issues/29)