From 0289576677dd22f1d7953a897106daa98f609805 Mon Sep 17 00:00:00 2001 From: Andreas Maerten <24669514+Yimura@users.noreply.github.com> Date: Mon, 24 Jul 2023 16:13:09 +0200 Subject: [PATCH] fix(byte_patch): sometimes the page protections get modified between `apply` and `restore`: make sure the protections are fine for us to memcpy. (#1831) * feat(BytePatch): add is_active member function * fix(byte_patch): sometimes the page protections get modified between the apply and restore of the byte_patch, make sure the protections are fine for us to memcpy Co-authored-by: Quentin E. / iDeath --- src/backend/commands/system/window_hook.cpp | 10 +++------- src/memory/byte_patch.cpp | 7 ++++++- src/memory/byte_patch.hpp | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/system/window_hook.cpp b/src/backend/commands/system/window_hook.cpp index 2015d8d2..664dbbcf 100644 --- a/src/backend/commands/system/window_hook.cpp +++ b/src/backend/commands/system/window_hook.cpp @@ -8,23 +8,19 @@ namespace big { using bool_command::bool_command; - memory::byte_patch* m_window_hook_patch; - virtual void refresh() override { - static auto patch = (m_window_hook_patch = memory::byte_patch::make(g_pointers->m_gta.m_window_hook.as(), std::to_array({0xC3, 0x90, 0x90, 0x90})) - .get(), - true); + static auto& window_hook_patch = memory::byte_patch::make(g_pointers->m_gta.m_window_hook.as(), std::to_array({0xC3, 0x90, 0x90, 0x90})); if (m_toggle) { - m_window_hook_patch->apply(); + window_hook_patch->apply(); UnhookWindowsHookEx(*g_pointers->m_gta.m_window_hook.add(45).rip().as()); } else { SetWindowsHookExA(13, g_pointers->m_gta.m_window_hook.add(18).rip().as(), GetModuleHandleA("GTA5.exe"), 0); - m_window_hook_patch->restore(); + window_hook_patch->restore(); } } }; diff --git a/src/memory/byte_patch.cpp b/src/memory/byte_patch.cpp index cb3a1363..158281c4 100644 --- a/src/memory/byte_patch.cpp +++ b/src/memory/byte_patch.cpp @@ -9,15 +9,20 @@ namespace memory void byte_patch::apply() const { + DWORD temp; + VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&m_old_protect); memcpy(m_address, m_value.get(), m_size); + VirtualProtect(m_address, m_size, m_old_protect, &temp); } void byte_patch::restore() const { DWORD temp; - VirtualProtect(m_address, m_size, m_old_protect, &temp); + + VirtualProtect(m_address, m_size, PAGE_EXECUTE_READWRITE, (PDWORD)&temp); memcpy(m_address, m_original_bytes.get(), m_size); + VirtualProtect(m_address, m_size, m_old_protect, &temp); } void byte_patch::remove() const diff --git a/src/memory/byte_patch.hpp b/src/memory/byte_patch.hpp index 2af66790..8565d0f3 100644 --- a/src/memory/byte_patch.hpp +++ b/src/memory/byte_patch.hpp @@ -12,7 +12,6 @@ namespace memory virtual ~byte_patch(); void apply() const; - void restore() const; void remove() const;