Skip to content

Commit

Permalink
add virtualprotect for windows rawWriteMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Aug 27, 2024
1 parent fbf79b4 commit c8a445c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/target/Windows32Target.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "Windows32Target.hpp"

#include <Platform.hpp>
#include <stdexcept>

using namespace tulip::hook;

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit c8a445c

Please sign in to comment.