2016-01-04 14:58:00 -05:00
|
|
|
#include "STDInclude.hpp"
|
2015-12-23 08:45:53 -05:00
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
Hook::~Hook()
|
|
|
|
{
|
|
|
|
if (Hook::Initialized)
|
|
|
|
{
|
|
|
|
Hook::Uninstall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 08:08:46 -05:00
|
|
|
Hook* Hook::Initialize(DWORD place, void(*stub)(), bool useJump)
|
|
|
|
{
|
2016-01-24 06:19:34 -05:00
|
|
|
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)
|
|
|
|
{
|
2016-01-24 06:19:34 -05:00
|
|
|
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;
|
|
|
|
|
2016-01-24 06:19:34 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hook* Hook::Install()
|
|
|
|
{
|
|
|
|
Hook::StateMutex.lock();
|
|
|
|
|
|
|
|
if (!Hook::Initialized || Hook::Installed)
|
|
|
|
{
|
|
|
|
Hook::StateMutex.unlock();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hook::Installed = true;
|
|
|
|
|
2015-12-30 16:22:24 -05:00
|
|
|
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));
|
|
|
|
|
2016-01-24 06:19:34 -05:00
|
|
|
char* code = static_cast<char*>(Hook::Place);
|
2015-12-23 08:45:53 -05:00
|
|
|
|
2016-01-24 06:19:34 -05:00
|
|
|
*code = static_cast<char>(Hook::UseJump ? 0xE9 : 0xE8);
|
2015-12-23 08:45:53 -05:00
|
|
|
|
2016-01-24 06:19:34 -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
|
|
|
|
2015-12-30 16:22:24 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Hook* Hook::Uninstall()
|
|
|
|
{
|
|
|
|
Hook::StateMutex.lock();
|
|
|
|
|
|
|
|
if (!Hook::Initialized || !Hook::Installed)
|
|
|
|
{
|
|
|
|
Hook::StateMutex.unlock();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hook::Installed = false;
|
|
|
|
|
2015-12-30 16:22:24 -05:00
|
|
|
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));
|
|
|
|
|
2015-12-30 16:22:24 -05:00
|
|
|
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)
|
|
|
|
{
|
2016-01-24 06:19:34 -05:00
|
|
|
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-01-24 06:19:34 -05:00
|
|
|
strncpy(static_cast<char*>(place), string, length);
|
2015-12-30 16:22:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hook::SetString(DWORD place, const char* string, size_t length)
|
|
|
|
{
|
2016-01-24 06:19:34 -05:00
|
|
|
Hook::SetString(reinterpret_cast<void*>(place), string, length);
|
2015-12-30 16:22:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hook::SetString(void* place, const char* string)
|
|
|
|
{
|
2016-01-24 06:19:34 -05:00
|
|
|
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)
|
|
|
|
{
|
2016-01-24 06:19:34 -05:00
|
|
|
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
|
|
|
}
|