1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
class Memory
{
public:
Memory() {
DWORD dwModuleBase = (DWORD)GetModuleHandle(NULL);
if (dwModuleBase == 0x00) {
//logger.log_windows_error("Memory::GetModuleHandle failed");
}
}
template <typename T>
inline T Read(LPVOID lpAddress) const {
return *(T*)lpAddress;
}
//memory.Write<BYTE, FALSE>(lpAddress, 0xE8);
template<typename T, bool EnableVirtual = true>
inline void Write(LPVOID lpAddress, T _tValue) const {
if constexpr (EnableVirtual) {
//if (EnableVirtual) {
DWORD dwOldProtect = 0;
VirtualProtect(lpAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &dwOldProtect);
*(T*)lpAddress = _tValue;
VirtualProtect(lpAddress, sizeof(T), dwOldProtect, &dwOldProtect);
} else {
*(T*)lpAddress = _tValue;
}
}
};
Memory memory;
void CallPatch(LPVOID lpAddress, LPVOID lpTarget) {
DWORD dwOldProtect = 0;
auto PatchSize = 5;
VirtualProtect(lpAddress, PatchSize, PAGE_EXECUTE_READWRITE, &dwOldProtect);
memory.Write<BYTE, false>(lpAddress, 0xE8);
auto lpCallPatchAddress = reinterpret_cast<LPVOID>((DWORD)lpAddress + 1);
auto dwRelativePatchAddress = (DWORD)lpTarget - (DWORD)lpAddress - 5;
memory.Write<DWORD>(lpCallPatchAddress, dwRelativePatchAddress);
VirtualProtect(lpAddress, PatchSize, dwOldProtect, &dwOldProtect);
}
// FOR COMPILE TEST ONLY
int main() {
char a[20], b[20];
CallPatch(a, b);
}
|