99 lines
2.4 KiB
C++
99 lines
2.4 KiB
C++
#pragma once
|
|
|
|
namespace mh
|
|
{
|
|
__forceinline auto get_minhook_data_for_hook(void* target, void* handler, void** original, uint8_t* buffer)
|
|
{
|
|
MH_STATUS status;
|
|
|
|
if ((status = MH_CreateHook(target, handler, original, 0)) != MH_OK)
|
|
{
|
|
log_info("failed to create minhook request => %i", status);
|
|
return false;
|
|
}
|
|
|
|
if ((status = MH_EnableHook(target, buffer)) != MH_OK)
|
|
{
|
|
log_info("failed to enable minhook request => %i", status);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
__forceinline auto stealth_patch_bytes(uintptr_t target, uint8_t* patch, size_t size)
|
|
{
|
|
if (!hvi::write_virt_memory(win_api::get_current_process_id(), target, patch, size, true))
|
|
{
|
|
log_info_("stealth_patch_bytes() -> failed to write memory. \n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//
|
|
// just a regular stealth patch.
|
|
//
|
|
template < typename T >
|
|
__forceinline auto stealth_patch(uintptr_t target, void* handler, T* original)
|
|
{
|
|
uint8_t patch[64];
|
|
|
|
memset(patch, 0, sizeof(patch));
|
|
|
|
if (!target)
|
|
return false;
|
|
|
|
if (!get_minhook_data_for_hook(mem::addr(target).Ptr(), handler, (void**)original, patch))
|
|
{
|
|
log_info_("stealth_patch_api() -> failed to get data from MH engine. \n");
|
|
return false;
|
|
}
|
|
|
|
if (!hvi::write_virt_memory(win_api::get_current_process_id(), target, patch, 5, true))
|
|
{
|
|
log_info_("stealth_patch_api() -> failed to write memory. \n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//
|
|
// this in addition triggers CoW (copy-on-write) so the page gets it's unique physical address.
|
|
// that's necessary and very handy for shared DLLs which are mapped to the same physical memory,
|
|
// like system DLLs kernel32.dll ntdll.dll etc. we don't want a system wide hook, that'd be fatal.
|
|
// access violations, undefined behavior and insane performance loss as well so yeah no don't try that.
|
|
//
|
|
template < typename T >
|
|
__forceinline auto stealth_patch_api(uintptr_t target, void* handler, T* original)
|
|
{
|
|
uint8_t patch[64];
|
|
|
|
memset(patch, 0, sizeof(patch));
|
|
|
|
if (!target)
|
|
return false;
|
|
|
|
if (!hv::trigger_cow(target))
|
|
{
|
|
//return false;
|
|
}
|
|
|
|
if (!get_minhook_data_for_hook(mem::addr(target).Ptr(), handler, (void**)original, patch))
|
|
{
|
|
log_info_("stealth_patch_api() -> failed to get data from MH engine. \n");
|
|
return false;
|
|
}
|
|
|
|
if (!hvi::write_virt_memory(win_api::get_current_process_id(), target, patch, 5, true))
|
|
{
|
|
log_info_("stealth_patch_api() -> failed to write memory. \n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|