iw4x-client/src/Utils/Hooking.cpp

219 lines
5.4 KiB
C++
Raw Normal View History

#include "STDInclude.hpp"
2015-12-23 08:45:53 -05:00
namespace Utils
{
2016-04-08 10:15:01 -04:00
std::map<void*, void*> Hook::Interceptor::IReturn;
std::map<void*, void(*)()> Hook::Interceptor::ICallbacks;
void Hook::Interceptor::Install(void* place, void(*stub)())
{
return Hook::Interceptor::Install(reinterpret_cast<void**>(place), stub);
}
void Hook::Interceptor::Install(void** place, void(*stub)())
{
Hook::Interceptor::IReturn[place] = *place;
Hook::Interceptor::ICallbacks[place] = stub;
*place = Hook::Interceptor::InterceptionStub;
}
void __declspec(naked) Hook::Interceptor::InterceptionStub()
{
__asm
{
sub esp, 4h // Reserve space on the stack for the return address
pushad // Store registers
lea eax, [esp + 20h] // Load initial stack pointer
push eax // Push it onto the stack
call RunCallback // Run the callback based on the given stack pointer
call PopReturn // Get the initial return address according to the stack pointer
add esp, 4h // Clear the stack
mov [esp + 20h], eax // Store the return address at the reserves space
popad // Restore the registers
retn // Return (jump to our return address)
}
}
void Hook::Interceptor::RunCallback(void* place)
{
auto iCallback = Hook::Interceptor::ICallbacks.find(place);
if (iCallback != Hook::Interceptor::ICallbacks.end())
{
iCallback->second();
Hook::Interceptor::ICallbacks.erase(iCallback);
}
}
void* Hook::Interceptor::PopReturn(void* place)
{
void* retVal = nullptr;
auto iReturn = Hook::Interceptor::IReturn.find(place);
if (iReturn != Hook::Interceptor::IReturn.end())
{
retVal = iReturn->second;
Hook::Interceptor::IReturn.erase(iReturn);
}
return retVal;
}
2015-12-23 08:45:53 -05:00
Hook::~Hook()
{
if (Hook::Initialized)
{
Hook::Uninstall();
}
}
2015-12-28 08:08:46 -05:00
Hook* Hook::Initialize(DWORD place, void(*stub)(), bool useJump)
{
return Hook::Initialize(place, reinterpret_cast<void*>(stub), useJump);
2015-12-28 08:08:46 -05:00
}
2015-12-23 08:45:53 -05:00
Hook* Hook::Initialize(DWORD place, void* stub, bool useJump)
{
return Hook::Initialize(reinterpret_cast<void*>(place), stub, useJump);
2015-12-23 08:45:53 -05:00
}
Hook* Hook::Initialize(void* place, void* stub, bool useJump)
{
if (Hook::Initialized) return this;
Hook::Initialized = true;
Hook::UseJump = useJump;
Hook::Place = place;
Hook::Stub = stub;
Hook::Original = static_cast<char*>(Hook::Place) + 5 + *reinterpret_cast<DWORD*>((static_cast<char*>(Hook::Place) + 1));
2015-12-23 11:12:15 -05:00
2015-12-23 08:45:53 -05:00
return this;
}
2016-03-14 16:29:21 -04:00
Hook* Hook::Install(bool unprotect, bool keepUnportected)
2015-12-23 08:45:53 -05:00
{
Hook::StateMutex.lock();
if (!Hook::Initialized || Hook::Installed)
{
Hook::StateMutex.unlock();
return this;
}
Hook::Installed = true;
2016-03-14 16:29:21 -04:00
if (unprotect) VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &this->Protection);
2015-12-23 08:45:53 -05:00
memcpy(Hook::Buffer, Hook::Place, sizeof(Hook::Buffer));
char* code = static_cast<char*>(Hook::Place);
2015-12-23 08:45:53 -05:00
*code = static_cast<char>(Hook::UseJump ? 0xE9 : 0xE8);
2015-12-23 08:45:53 -05:00
*reinterpret_cast<size_t*>(code + 1) = reinterpret_cast<size_t>(Hook::Stub) - (reinterpret_cast<size_t>(Hook::Place) + 5);
2015-12-23 08:45:53 -05:00
2016-03-14 16:29:21 -04:00
if (unprotect && !keepUnportected) VirtualProtect(Hook::Place, sizeof(Hook::Buffer), Hook::Protection, &this->Protection);
2015-12-23 08:45:53 -05:00
FlushInstructionCache(GetCurrentProcess(), Hook::Place, sizeof(Hook::Buffer));
Hook::StateMutex.unlock();
return this;
}
void Hook::Quick()
{
if (Hook::Installed)
{
Hook::Installed = false;
}
}
2016-03-14 16:29:21 -04:00
Hook* Hook::Uninstall(bool unprotect)
2015-12-23 08:45:53 -05:00
{
Hook::StateMutex.lock();
if (!Hook::Initialized || !Hook::Installed)
{
Hook::StateMutex.unlock();
return this;
}
Hook::Installed = false;
2016-03-14 16:29:21 -04:00
if(unprotect) VirtualProtect(Hook::Place, sizeof(Hook::Buffer), PAGE_EXECUTE_READWRITE, &this->Protection);
2015-12-23 08:45:53 -05:00
memcpy(Hook::Place, Hook::Buffer, sizeof(Hook::Buffer));
2016-03-14 16:29:21 -04:00
if (unprotect) VirtualProtect(Hook::Place, sizeof(Hook::Buffer), Hook::Protection, &this->Protection);
2015-12-23 08:45:53 -05:00
FlushInstructionCache(GetCurrentProcess(), Hook::Place, sizeof(Hook::Buffer));
Hook::StateMutex.unlock();
return this;
}
void* Hook::GetAddress()
{
return Hook::Place;
}
void Hook::Nop(void* place, size_t length)
{
2016-02-29 09:49:27 -05:00
DWORD oldProtect;
VirtualProtect(place, length, PAGE_EXECUTE_READWRITE, &oldProtect);
2015-12-23 08:45:53 -05:00
memset(place, 0x90, length);
2016-02-29 09:49:27 -05:00
VirtualProtect(place, length, oldProtect, &oldProtect);
FlushInstructionCache(GetCurrentProcess(), place, length);
2015-12-23 08:45:53 -05:00
}
void Hook::Nop(DWORD place, size_t length)
{
Nop(reinterpret_cast<void*>(place), length);
2015-12-23 08:45:53 -05:00
}
2015-12-30 16:22:24 -05:00
void Hook::SetString(void* place, const char* string, size_t length)
{
2016-03-09 14:38:13 -05:00
DWORD oldProtect;
VirtualProtect(place, length + 1, PAGE_EXECUTE_READWRITE, &oldProtect);
strncpy(static_cast<char*>(place), string, length);
2016-03-09 14:38:13 -05:00
VirtualProtect(place, length + 1, oldProtect, &oldProtect);
2015-12-30 16:22:24 -05:00
}
void Hook::SetString(DWORD place, const char* string, size_t length)
{
Hook::SetString(reinterpret_cast<void*>(place), string, length);
2015-12-30 16:22:24 -05:00
}
void Hook::SetString(void* place, const char* string)
{
Hook::SetString(place, string, strlen(static_cast<char*>(place)));
2015-12-30 16:22:24 -05:00
}
void Hook::SetString(DWORD place, const char* string)
{
Hook::SetString(reinterpret_cast<void*>(place), string);
2015-12-30 16:22:24 -05:00
}
2016-02-11 18:36:52 -05:00
void Hook::RedirectJump(void* place, void* stub)
{
char* operandPtr = static_cast<char*>(place) + 2;
int newOperand = reinterpret_cast<int>(stub) - (reinterpret_cast<int>(place) + 6);
Utils::Hook::Set<int>(operandPtr, newOperand);
}
void Hook::RedirectJump(DWORD place, void* stub)
{
Hook::RedirectJump(reinterpret_cast<void*>(place), stub);
}
2015-12-23 08:45:53 -05:00
}