From fb672f079b68fd6ed88961dda3611b769ea22648 Mon Sep 17 00:00:00 2001 From: Silent Date: Mon, 18 Sep 2017 12:52:35 +0200 Subject: [PATCH] Simplified ScopedUnprotect selection Made SA use safer ScopedUnprotect --- SilentPatch/MemoryMgr.h | 38 +++++++++++++++++++++---------- SilentPatchIII/SilentPatchIII.cpp | 10 +------- SilentPatchSA/SilentPatchSA.cpp | 22 ++++++++++-------- SilentPatchVC/SilentPatchVC.cpp | 10 +------- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/SilentPatch/MemoryMgr.h b/SilentPatch/MemoryMgr.h index 5439f14..2929d2e 100644 --- a/SilentPatch/MemoryMgr.h +++ b/SilentPatch/MemoryMgr.h @@ -625,12 +625,25 @@ namespace Memory #include #include +#include namespace ScopedUnprotect { - class unprotect + class Unprotect { + public: + ~Unprotect() + { + for ( auto& it : m_queriedProtects ) + { + DWORD dwOldProtect; + VirtualProtect( std::get<0>(it), std::get<1>(it), std::get<2>(it), &dwOldProtect ); + } + } + protected: + Unprotect() = default; + void UnprotectRange( DWORD_PTR BaseAddress, SIZE_T Size ) { SIZE_T QueriedSize = 0; @@ -650,20 +663,11 @@ namespace ScopedUnprotect } } - ~unprotect() - { - for ( auto& it : m_queriedProtects ) - { - DWORD dwOldProtect; - VirtualProtect( std::get<0>(it), std::get<1>(it), std::get<2>(it), &dwOldProtect ); - } - } - private: std::forward_list< std::tuple< LPVOID, SIZE_T, DWORD > > m_queriedProtects; }; - class Section : protected unprotect + class Section : public Unprotect { public: Section( HINSTANCE hInstance, const char* name ) @@ -696,7 +700,7 @@ namespace ScopedUnprotect bool m_locatedSection = false; }; - class FullModule : protected unprotect + class FullModule : public Unprotect { public: FullModule( HINSTANCE hInstance ) @@ -705,6 +709,16 @@ namespace ScopedUnprotect UnprotectRange( (DWORD_PTR)hInstance, ntHeader->OptionalHeader.SizeOfImage ); } }; + + inline std::unique_ptr UnprotectSectionOrFullModule( HINSTANCE hInstance, const char* name ) + { + std::unique_ptr
section = std::make_unique
( hInstance, name ); + if ( !section->SectionLocated() ) + { + return std::make_unique( hInstance ); + } + return section; + } }; #endif diff --git a/SilentPatchIII/SilentPatchIII.cpp b/SilentPatchIII/SilentPatchIII.cpp index edaf990..2db9c0e 100644 --- a/SilentPatchIII/SilentPatchIII.cpp +++ b/SilentPatchIII/SilentPatchIII.cpp @@ -890,15 +890,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) GetWindowRect(GetDesktopWindow(), &desktop); sprintf_s(aNoDesktopMode, "Cannot find %dx%dx32 video mode", desktop.right, desktop.bottom); - const HINSTANCE hModule = GetModuleHandle( nullptr ); - std::unique_ptr SectionProtect = std::make_unique( hModule, ".text" ); - std::unique_ptr ModuleProtect = nullptr; - if ( !SectionProtect->SectionLocated() ) - { - SectionProtect = nullptr; - ModuleProtect = std::make_unique( hModule ); - } - + std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( GetModuleHandle( nullptr ), ".text" ); if (*(DWORD*)0x5C1E75 == 0xB85548EC) Patch_III_10(desktop); else if (*(DWORD*)0x5C2135 == 0xB85548EC) Patch_III_11(desktop); diff --git a/SilentPatchSA/SilentPatchSA.cpp b/SilentPatchSA/SilentPatchSA.cpp index 3d79d67..016a812 100644 --- a/SilentPatchSA/SilentPatchSA.cpp +++ b/SilentPatchSA/SilentPatchSA.cpp @@ -2295,8 +2295,10 @@ BOOL InjectDelayedPatches_10() if ( !IsAlreadyRunning() ) { using namespace Memory; - ScopedUnprotect::Section Protect( (HINSTANCE)0x400000, ".text" ); - ScopedUnprotect::Section Protect2( (HINSTANCE)0x400000, ".rdata" ); + + const HINSTANCE hInstance = GetModuleHandle( nullptr ); + std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( hInstance, ".text" ); + ScopedUnprotect::Section Protect2( hInstance, ".rdata" ); // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; @@ -2553,8 +2555,9 @@ BOOL InjectDelayedPatches_11() if ( !IsAlreadyRunning() ) { using namespace Memory; - ScopedUnprotect::Section Protect( (HINSTANCE)0x400000, ".text" ); - ScopedUnprotect::Section Protect2( (HINSTANCE)0x400000, ".rdata" ); + const HINSTANCE hInstance = GetModuleHandle( nullptr ); + std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( hInstance, ".text" ); + ScopedUnprotect::Section Protect2( hInstance, ".rdata" ); // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; @@ -2785,8 +2788,9 @@ BOOL InjectDelayedPatches_Steam() if ( !IsAlreadyRunning() ) { using namespace Memory; - ScopedUnprotect::Section Protect( (HINSTANCE)0x400000, ".text" ); - ScopedUnprotect::Section Protect2( (HINSTANCE)0x400000, ".rdata" ); + const HINSTANCE hInstance = GetModuleHandle( nullptr ); + std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( hInstance, ".text" ); + ScopedUnprotect::Section Protect2( hInstance, ".rdata" ); // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; @@ -4763,9 +4767,9 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { hDLLModule = hinstDLL; - HINSTANCE hGameHandle = GetModuleHandle( nullptr ); - ScopedUnprotect::Section Protect( hGameHandle, ".text" ); - ScopedUnprotect::Section Protect2( hGameHandle, ".rdata" ); + const HINSTANCE hInstance = GetModuleHandle( nullptr ); + std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( hInstance, ".text" ); + ScopedUnprotect::Section Protect2( hInstance, ".rdata" ); if (*(DWORD*)DynBaseAddress(0x82457C) == 0x94BF || *(DWORD*)DynBaseAddress(0x8245BC) == 0x94BF) Patch_SA_10(); else if (*(DWORD*)DynBaseAddress(0x8252FC) == 0x94BF || *(DWORD*)DynBaseAddress(0x82533C) == 0x94BF) Patch_SA_11(), MessageBoxW( nullptr, L"You're using a 1.01 executable which is no longer supported by SilentPatch!\n\nI have no idea if anyone was still using it, so if you do - shoot me an e-mail!", L"SilentPatch", MB_OK | MB_ICONWARNING ); diff --git a/SilentPatchVC/SilentPatchVC.cpp b/SilentPatchVC/SilentPatchVC.cpp index 185d768..c758188 100644 --- a/SilentPatchVC/SilentPatchVC.cpp +++ b/SilentPatchVC/SilentPatchVC.cpp @@ -708,15 +708,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) GetWindowRect(GetDesktopWindow(), &desktop); sprintf_s(aNoDesktopMode, "Cannot find %dx%dx32 video mode", desktop.right, desktop.bottom); - const HINSTANCE hModule = GetModuleHandle( nullptr ); - std::unique_ptr SectionProtect = std::make_unique( hModule, ".text" ); - std::unique_ptr ModuleProtect = nullptr; - if ( !SectionProtect->SectionLocated() ) - { - SectionProtect = nullptr; - ModuleProtect = std::make_unique( hModule ); - } - + std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( GetModuleHandle( nullptr ), ".text" ); if(*(DWORD*)0x667BF5 == 0xB85548EC) Patch_VC_10(desktop); else if(*(DWORD*)0x667C45 == 0xB85548EC) Patch_VC_11(desktop);