From c8a445c66fa5a5a7f2ac21911c16269188a41b7d Mon Sep 17 00:00:00 2001 From: dankmeme01 <42031238+dankmeme01@users.noreply.github.com> Date: Tue, 27 Aug 2024 21:20:29 +0200 Subject: [PATCH] add virtualprotect for windows rawWriteMemory --- src/target/Windows32Target.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/target/Windows32Target.cpp b/src/target/Windows32Target.cpp index 1436c0f..8b35dd5 100644 --- a/src/target/Windows32Target.cpp +++ b/src/target/Windows32Target.cpp @@ -1,7 +1,6 @@ #include "Windows32Target.hpp" #include -#include using namespace tulip::hook; @@ -53,9 +52,18 @@ Result<> Windows32Target::protectMemory(void* address, size_t size, uint32_t pro } Result<> Windows32Target::rawWriteMemory(void* destination, void const* source, size_t size) { - if (!WriteProcessMemory(GetCurrentProcess(), destination, source, size, nullptr)) { - return Err("Unable to write to memory"); + DWORD oldProtection; + + // protect memory to be writable + if (!VirtualProtect(destination, size, this->getWritableProtection(), &oldProtection)) { + return Err("Unable to protect memory"); } + + std::memcpy(destination, source, size); + + // restore old protection + VirtualProtect(destination, size, oldProtection, &oldProtection); + return Ok(); }