Simplified ScopedUnprotect selection

Made SA use safer ScopedUnprotect
This commit is contained in:
Silent 2017-09-18 12:52:35 +02:00
parent 29cea98c70
commit fb672f079b
4 changed files with 41 additions and 39 deletions

View file

@ -625,12 +625,25 @@ namespace Memory
#include <forward_list>
#include <tuple>
#include <memory>
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<Unprotect> UnprotectSectionOrFullModule( HINSTANCE hInstance, const char* name )
{
std::unique_ptr<Section> section = std::make_unique<Section>( hInstance, name );
if ( !section->SectionLocated() )
{
return std::make_unique<FullModule>( hInstance );
}
return section;
}
};
#endif

View file

@ -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<ScopedUnprotect::Section> SectionProtect = std::make_unique<ScopedUnprotect::Section>( hModule, ".text" );
std::unique_ptr<ScopedUnprotect::FullModule> ModuleProtect = nullptr;
if ( !SectionProtect->SectionLocated() )
{
SectionProtect = nullptr;
ModuleProtect = std::make_unique<ScopedUnprotect::FullModule>( hModule );
}
std::unique_ptr<ScopedUnprotect::Unprotect> Protect = ScopedUnprotect::UnprotectSectionOrFullModule( GetModuleHandle( nullptr ), ".text" );
if (*(DWORD*)0x5C1E75 == 0xB85548EC) Patch_III_10(desktop);
else if (*(DWORD*)0x5C2135 == 0xB85548EC) Patch_III_11(desktop);

View file

@ -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<ScopedUnprotect::Unprotect> 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<ScopedUnprotect::Unprotect> 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<ScopedUnprotect::Unprotect> 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<ScopedUnprotect::Unprotect> 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 );

View file

@ -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<ScopedUnprotect::Section> SectionProtect = std::make_unique<ScopedUnprotect::Section>( hModule, ".text" );
std::unique_ptr<ScopedUnprotect::FullModule> ModuleProtect = nullptr;
if ( !SectionProtect->SectionLocated() )
{
SectionProtect = nullptr;
ModuleProtect = std::make_unique<ScopedUnprotect::FullModule>( hModule );
}
std::unique_ptr<ScopedUnprotect::Unprotect> Protect = ScopedUnprotect::UnprotectSectionOrFullModule( GetModuleHandle( nullptr ), ".text" );
if(*(DWORD*)0x667BF5 == 0xB85548EC) Patch_VC_10(desktop);
else if(*(DWORD*)0x667C45 == 0xB85548EC) Patch_VC_11(desktop);