From b34cc846946e18d462694aec3f14b72454dca3ee Mon Sep 17 00:00:00 2001 From: Yimura Date: Wed, 19 May 2021 00:41:55 +0200 Subject: [PATCH] feat(Protections): Added freemode_script_recovery --- BigBaseV2/src/function_types.hpp | 3 ++ BigBaseV2/src/hooking.cpp | 10 +++- BigBaseV2/src/hooking.hpp | 7 +++ .../src/hooks/freemode_script_recovery.cpp | 51 +++++++++++++++++++ BigBaseV2/src/pointers.cpp | 10 ++++ BigBaseV2/src/pointers.hpp | 3 ++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 BigBaseV2/src/hooks/freemode_script_recovery.cpp diff --git a/BigBaseV2/src/function_types.hpp b/BigBaseV2/src/function_types.hpp index 05771398..c5b73779 100644 --- a/BigBaseV2/src/function_types.hpp +++ b/BigBaseV2/src/function_types.hpp @@ -8,4 +8,7 @@ namespace big::functions using run_script_threads_t = bool(*)(std::uint32_t ops_to_execute); using get_native_handler_t = rage::scrNativeHandler(*)(rage::scrNativeRegistrationTable*, rage::scrNativeHash); using fix_vectors_t = void(*)(rage::scrNativeCallContext*); + + using gta_thread_tick = __int64(GtaThread* a1, unsigned int a2); + using gta_thread_kill = __int64(GtaThread* a1); } diff --git a/BigBaseV2/src/hooking.cpp b/BigBaseV2/src/hooking.cpp index 1874ccd8..ce21c1b4 100644 --- a/BigBaseV2/src/hooking.cpp +++ b/BigBaseV2/src/hooking.cpp @@ -37,8 +37,10 @@ namespace big m_set_cursor_pos_hook("SetCursorPos", memory::module("user32.dll").get_export("SetCursorPos").as(), &hooks::set_cursor_pos), m_run_script_threads_hook("Script hook", g_pointers->m_run_script_threads, &hooks::run_script_threads), - m_convert_thread_to_fiber_hook("ConvertThreadToFiber", memory::module("kernel32.dll").get_export("ConvertThreadToFiber").as(), &hooks::convert_thread_to_fiber) + m_convert_thread_to_fiber_hook("ConvertThreadToFiber", memory::module("kernel32.dll").get_export("ConvertThreadToFiber").as(), &hooks::convert_thread_to_fiber), + m_gta_thread_tick_hook("GTA Thread Tick", g_pointers->m_gta_thread_tick, &hooks::gta_thread_tick), + m_gta_thread_kill_hook("GTA Thread Kill", g_pointers->m_gta_thread_kill, &hooks::gta_thread_kill) { m_swapchain_hook.hook(hooks::swapchain_present_index, &hooks::swapchain_present); m_swapchain_hook.hook(hooks::swapchain_resizebuffers_index, &hooks::swapchain_resizebuffers); @@ -63,6 +65,9 @@ namespace big m_run_script_threads_hook.enable(); m_convert_thread_to_fiber_hook.enable(); + m_gta_thread_kill_hook.enable(); + m_gta_thread_tick_hook.enable(); + m_enabled = true; } @@ -70,6 +75,9 @@ namespace big { m_enabled = false; + m_gta_thread_tick_hook.disable(); + m_gta_thread_kill_hook.disable(); + m_convert_thread_to_fiber_hook.disable(); m_run_script_threads_hook.disable(); diff --git a/BigBaseV2/src/hooking.hpp b/BigBaseV2/src/hooking.hpp index 80833a0a..b1233f29 100644 --- a/BigBaseV2/src/hooking.hpp +++ b/BigBaseV2/src/hooking.hpp @@ -2,6 +2,7 @@ #include "common.hpp" #include "detour_hook.hpp" #include "gta/fwddec.hpp" +#include "gta/script_thread.hpp" #include "script_hook.hpp" #include "vmt_hook.hpp" @@ -20,6 +21,9 @@ namespace big static LRESULT wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); static BOOL set_cursor_pos(int x, int y); + + static rage::eThreadState gta_thread_tick(GtaThread* a1, unsigned int a2); + static rage::eThreadState gta_thread_kill(GtaThread* thread); }; struct minhook_keepalive @@ -48,6 +52,9 @@ namespace big detour_hook m_run_script_threads_hook; detour_hook m_convert_thread_to_fiber_hook; + + detour_hook m_gta_thread_tick_hook; + detour_hook m_gta_thread_kill_hook; }; inline hooking *g_hooking{}; diff --git a/BigBaseV2/src/hooks/freemode_script_recovery.cpp b/BigBaseV2/src/hooks/freemode_script_recovery.cpp new file mode 100644 index 00000000..4bd34c64 --- /dev/null +++ b/BigBaseV2/src/hooks/freemode_script_recovery.cpp @@ -0,0 +1,51 @@ +#include "hooking.hpp" +#include "gta/script_thread.hpp" + +namespace big +{ + using namespace rage; + + static char struct_backup[sizeof(GtaThread)]; + static char stack_buffer[0xFFFF]; + static bool freemode_terminated = false; + + eThreadState hooks::gta_thread_tick(GtaThread* thread, unsigned int a2) + { + eThreadState state = thread->m_context.m_state; + + if (thread->m_script_hash == RAGE_JOAAT("freemode") && state == eThreadState::running && !freemode_terminated) + { + memcpy(struct_backup, (void*)thread, sizeof(GtaThread)); + if (thread->m_stack) + memcpy(stack_buffer, thread->m_stack, thread->m_context.m_stack_size); + } + + eThreadState result = g_hooking->m_gta_thread_tick_hook.get_original()(thread, a2); + + if (thread->m_script_hash == RAGE_JOAAT("freemode")) + { + if (result == eThreadState::killed && state == eThreadState::running && !freemode_terminated) + { + result = eThreadState::running; + + memcpy(thread, struct_backup, sizeof(GtaThread)); + if (thread->m_stack) + memcpy(thread->m_stack, stack_buffer, thread->m_context.m_stack_size); + } + + if (freemode_terminated) freemode_terminated = !(result == eThreadState::running); + } + + return result; + } + + eThreadState hooks::gta_thread_kill(GtaThread* thread) + { + eThreadState result = g_hooking->m_gta_thread_kill_hook.get_original()(thread); + + if (thread->m_script_hash == RAGE_JOAAT("freemode")) + freemode_terminated = !(result == eThreadState::running); + + return result; + } +} \ No newline at end of file diff --git a/BigBaseV2/src/pointers.cpp b/BigBaseV2/src/pointers.cpp index 2b2e0ebf..8469ed64 100644 --- a/BigBaseV2/src/pointers.cpp +++ b/BigBaseV2/src/pointers.cpp @@ -112,6 +112,16 @@ namespace big memset(incompatible_version, 0x90, 0x1E); }); + + main_batch.add("Thread Tick", "48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 20 80 B9 ? ? ? ? ? 8B FA 48 8B D9 74 05", [this](memory::handle ptr) + { + m_gta_thread_tick = ptr.as(); + }); + + main_batch.add("Thread Kill", "48 89 5C 24 ? 57 48 83 EC 20 48 83 B9 ? ? ? ? ? 48 8B D9 74 14", [this](memory::handle ptr) + { + m_gta_thread_kill = ptr.as(); + }); main_batch.run(memory::module(nullptr)); diff --git a/BigBaseV2/src/pointers.hpp b/BigBaseV2/src/pointers.hpp index b2b4b126..1ad258a5 100644 --- a/BigBaseV2/src/pointers.hpp +++ b/BigBaseV2/src/pointers.hpp @@ -40,6 +40,9 @@ namespace big std::vector m_event_ptr; unsigned char m_event_restore[event_count]; char* m_event_register; + + functions::gta_thread_tick* m_gta_thread_tick{}; + functions::gta_thread_kill* m_gta_thread_kill{}; }; inline pointers *g_pointers{};