This repository has been archived on 2026-11-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
my-first-apex-internal/hooks.cpp
T
2026-11-05 02:19:09 +01:00

300 lines
6.7 KiB
C++

#include <include.h>
bool hooks::register_(hooks::data_t& data, bool resolve_jt, bool small_fn)
{
uintptr_t actual_target{};
if (resolve_jt)
{
if (!hde::resolve_jump(data.m_target, actual_target, true))
return false;
}
else
actual_target = mem::addr(data.m_target).Base();
if (!actual_target)
return false;
const auto result =
hook::detour::place(mem::addr(actual_target).Ptr(),
data.m_handler, &data.m_original,
&data.m_backup_length, nullptr,
nullptr, FALSE, small_fn
);
return result;
}
bool hooks::initialize_early()
{
log_call();
if (mh::MH_Initialize() != mh::MH_OK)
{
log_info_("failed to initialize mh.");
return false;
}
if (!setup_detours_api())
{
log_info_("failed to setup API hooks.");
return false;
}
if (!setup_detours_early())
{
log_info_("failed to setup GAME hooks (early).");
return false;
}
return true;
}
bool hooks::initialize()
{
log_call();
if (!setup_vmt() || !setup_detours())
{
log_info_("failed to setup GAME hooks.");
return false;
}
/*if (!setup_detours_ac())
{
log_info_("failed to setup AC hooks.");
return false;
}*/
constexpr auto unprotect_game = false;
if (unprotect_game)
{
const auto& process_info = g::p::context->m_process_info;
if (util::protect_virtual_memory((void*)process_info.base, process_info.length, PAGE_EXECUTE_READWRITE))
{
log_info_("deprotected the process memory for debugging.");
g::unprotected_game = true;
}
}
return true;
}
bool hooks::setup_detours_early()
{
if (!mh::stealth_patch(BASE_OF(VTABLE_FUNC(ix::file_system, HOOK_IDX_LOOKUP_FILE)), &lookup_file, &o::lookup_file))
return false;
return true;
}
bool hooks::setup_detours()
{
if (!mh::stealth_patch(dyn_addr::get_dynamic_address(_XS("fn_write_user_cmd")), &write_user_cmd, &o::write_user_cmd))
return false;
if (!mh::stealth_patch(dyn_addr::get_dynamic_address(_XS("fn_paint_end")), &paint_fn, &o::paint_fn))
return false;
if (!mh::stealth_patch(BASE_OF(VTABLE_FUNC(ix::base_client, HOOK_IDX_FRAME_STAGE_NOTIFY)), &frame_stage_notify, &o::frame_stage_notify))
return false;
if (!mh::stealth_patch(BASE_OF(VTABLE_FUNC(ix::engine_client, HOOK_IDX_GET_PLAYER_INFO)), &get_player_info, &o::get_player_info))
return false;
if (!mh::stealth_patch(BASE_OF(VTABLE_FUNC(ix::input, HOOK_IDX_INPUT_CREATE_MOVE)), &create_move, &o::create_move))
return false;
if (!mh::stealth_patch(dyn_addr::get_dynamic_address(_XS("view_render_start")), &view_render_start, &o::view_render_start))
return false;
if (!mh::stealth_patch(dyn_addr::get_dynamic_address(_XS("cl_move")), &cl_move, &o::cl_move))
return false;
uint8_t patch_silent[] = { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
if (!mh::stealth_patch_bytes(DYN_ADDR("silent1"), patch_silent, sizeof(patch_silent)) ||
!mh::stealth_patch_bytes(DYN_ADDR("silent1") + 0x12, patch_silent, sizeof(patch_silent)) ||
!mh::stealth_patch_bytes(DYN_ADDR("silent1") + 0x1A, patch_silent, sizeof(patch_silent)))
return false;
return true;
}
bool set_callback_user_exception_dispatcher(void* handler)
{
struct patch_t
{
patch_t(uintptr_t dst)
{
pad[0] = 0x90;
pad[1] = 0x90;
bytes[0] = 0x48;
bytes[1] = 0xB8;
memcpy(dest, &dst, sizeof(dest));
}
uint8_t bytes[2];
uint8_t dest[8];
uint8_t pad[2];
} patch( mem::addr(handler).Base() );
auto func_addr = util::find_export<uintptr_t>(_XS("ntdll.dll"), _XS("KiUserExceptionDispatcher"));
if (!func_addr)
return false;
func_addr += 0x1;
if (!hv::trigger_cow(func_addr))
return false;
if (!hvi::write_virt_memory(
win_api::get_current_process_id(),
func_addr, &patch, sizeof(patch),
true
))
return false;
return true;
}
bool hooks::setup_detours_api()
{
if (!set_callback_user_exception_dispatcher(&api::user_exception_dispatcher))
return false;
if (!mh::stealth_patch_api(
util::find_export<uintptr_t>(_XS("kernel32.dll"),
_XS("AddVectoredExceptionHandler")),
&api::add_veh_handler, &o::api_::add_veh_handler
))
return false;
if (!mh::stealth_patch_api(
util::find_export<uintptr_t>(_XS("kernel32.dll"),
_XS("RemoveVectoredExceptionHandler")),
&api::remove_veh_handler, &o::api_::remove_veh_handler))
return false;
if (!mh::stealth_patch_api(
util::find_export<uintptr_t>(_XS("ntdll.dll"),
_XS("NtQueryInformationThread")),
&api::query_info_thread, &o::api_::query_info_thread))
return false;
if (!mh::stealth_patch_api(
util::find_export<uintptr_t>(_XS("kernel32.dll"),
_XS("GetThreadContext")),
&api::get_thread_context, &o::api_::get_thread_context))
return false;
if (!mh::stealth_patch_api(
util::find_export<uintptr_t>(_XS("user32.dll"),
_XS("TranslateMessage")),
&api::translate_message, &o::api_::translate_message))
return false;
return true;
}
bool hooks::setup_detours_ac()
{
auto* const ctx = g::p::acc::eac;
if (ctx == nullptr || !ctx->launched || !ctx->internal_module.found)
return false;
const auto module_base = ctx->internal_module.base;
const auto module_length = ctx->internal_module.length;
if (!module_base || !module_length)
return false;
struct detour_t
{
const char* name;
const wchar_t* pattern;
void* handler;
void** original;
};
auto list = stl::vector<detour_t>();
list.push_back({
_XS("fetch_suspect_strings"),
_XSW(L"48 89 5C 24 ? 48 89 74 24 ? 48 89 7C 24 ? 55 41 56 41 57 48 8B EC 48 83 EC 70 48 8B 5A 08 48 8B"),
&ac::fetch_suspect_strings,
(void**)&o::ac_::fetch_suspect_string,
});
list.push_back({
_XS("fetch_suspect_system_calls"),
_XSW(L"48 89 54 24 ? 48 89 4C 24 ? 53 56 57 41 54 41 55 41 56 41 57 48 81 EC ? ? ? ? 48 8B DA 4C 8B F9"),
&ac::fetch_suspect_strings,
(void**)&o::ac_::fetch_suspect_system_calls
});
/*list.push_back({
_XS("detect_suspect_memory_regions"),
_XSW(L"48 8B C4 48 89 58 10 48 89 70 18 48 89 78 20 55 41 54 41 55 41 56 41 57 48 8D 68 A8 48 81 EC"),
&ac::detect_suspect_memory_regions,
(void**)&o::ac_::detect_suspect_memory_regions
});*/
for (auto i = 0ull; i < list.size(); i++)
{
const auto& info = list[i];
if (!info.pattern || !info.handler || !info.original)
{
log_info("invalid ac_detour data at index %i.", i);
return false;
}
const auto target = shared::sig::FindPattern(
module_base,
module_length,
info.pattern
);
if (!target)
{
log_info("failed ot find ac_detour data at index %i.", i);
return false;
}
log_info("found ac_detour target for index %i at %p.", i, target);
if (!mh::stealth_patch(target, info.handler, info.original))
{
log_info("failed to do ac_detour for index %i and target %p.", i, target);
return false;
}
log_info("performed ac_detour for index %i and target %p.", i, target);
}
return true;
}
bool hooks::setup_vmt()
{
return true;
}
void hooks::shutdown()
{
log_call();
//
// ..
//
}