-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added partial memory api for windows
- Loading branch information
Showing
3 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* ---------------------------------- | ||
* | libmem - by rdbo | | ||
* | Memory Hacking Library | | ||
* ---------------------------------- | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2023 Rdbo | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License version 3 | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <libmem/libmem.h> | ||
#include <windows.h> | ||
|
||
LM_API lm_size_t LM_CALL | ||
LM_ReadMemoryEx(const lm_process_t *process, | ||
lm_address_t source, | ||
lm_byte_t *dest, | ||
lm_size_t size) | ||
{ | ||
HANDLE hproc; | ||
SIZE_T bytes_read; | ||
|
||
if (!process || source == LM_ADDRESS_BAD || !dest || size == 0) | ||
return 0; | ||
|
||
hproc = open_process(process->pid, PROCESS_VM_READ); | ||
if (!hproc) | ||
return 0; | ||
|
||
if (!ReadProcessMemory(hproc, source, dest, size, &bytes_read)) | ||
bytes_read = 0; | ||
|
||
close_handle(hproc); | ||
return (lm_size_t)bytes_read; | ||
} | ||
|
||
/********************************/ | ||
|
||
LM_API lm_size_t LM_CALL | ||
LM_WriteMemoryEx(const lm_process_t *process, | ||
lm_address_t dest, | ||
lm_bytearray_t source, | ||
lm_size_t size) | ||
{ | ||
HANDLE hproc; | ||
SIZE_T bytes_written; | ||
|
||
if (!process || dest == LM_ADDRESS_BAD || !source || size == 0) | ||
return 0; | ||
|
||
hproc = open_process(process->pid, PROCESS_VM_WRITE | PROCESS_VM_OPERATION); | ||
if (!hproc) | ||
return 0; | ||
|
||
if (!WriteProcessMemory(hproc, dest, source, size, &bytes_written)) | ||
bytes_written = 0; | ||
|
||
close_handle(hproc); | ||
return (lm_size_t)bytes_written; | ||
} | ||
|
||
/********************************/ | ||
|
||
LM_API lm_bool_t LM_CALL | ||
LM_ProtMemory(lm_address_t address, | ||
lm_size_t size, | ||
lm_prot_t prot, | ||
lm_prot_t *oldprot_out) | ||
{ | ||
DWORD osprot; | ||
DWORD old_osprot; | ||
|
||
if (address == LM_ADDRESS_BAD || !LM_CHECK_PROT(prot)) | ||
return LM_FALSE; | ||
|
||
osprot = get_os_prot(prot); | ||
if (!VirtualProtect(address, size, osprot, &old_osprot)) | ||
return LM_FALSE; | ||
|
||
if (oldprot_out) | ||
*oldprot_out = get_prot(old_osprot); | ||
|
||
return LM_TRUE; | ||
} | ||
|
||
/********************************/ | ||
|
||
LM_API lm_address_t LM_CALL | ||
LM_AllocMemory(lm_size_t size, | ||
lm_prot_t prot) | ||
{ | ||
DWORD osprot; | ||
LPVOID alloc; | ||
|
||
if (!LM_CHECK_PROT(prot)) | ||
return LM_ADDRESS_BAD; | ||
|
||
osprot = get_os_prot(prot); | ||
alloc = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, osprot); | ||
if (!alloc) | ||
return LM_ADDRESS_BAD; | ||
|
||
return (lm_address_t)alloc; | ||
} | ||
|
||
/********************************/ | ||
|
||
LM_API lm_bool_t LM_CALL | ||
LM_FreeMemory(lm_address_t alloc, | ||
lm_size_t size) | ||
{ | ||
/* | ||
* From https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree: | ||
* | ||
* "If the dwFreeType parameter is MEM_RELEASE, this parameter must be 0 (zero). | ||
* The function frees the entire region that is reserved in the initial | ||
* allocation call to VirtualAlloc." | ||
*/ | ||
|
||
size = 0; | ||
return VirtualFree(alloc, size, MEM_RELEASE) ? LM_TRUE : LM_FALSE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* ---------------------------------- | ||
* | libmem - by rdbo | | ||
* | Memory Hacking Library | | ||
* ---------------------------------- | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2023 Rdbo | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License version 3 | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "osprot.h" | ||
#include <sys/mman.h> | ||
|
||
DWORD | ||
get_os_prot(lm_prot_t prot) | ||
{ | ||
DWORD osprot = 0; | ||
|
||
switch (prot) { | ||
case LM_PROT_X: osprot = PAGE_EXECUTE; | ||
case LM_PROT_R: osprot = PAGE_READONLY; | ||
case LM_PROT_W: osprot = PAGE_WRITECOPY; | ||
case LM_PROT_XR: osprot = PAGE_EXECUTE_READ; | ||
case LM_PROT_XW: osprot = PAGE_EXECUTE_WRITECOPY; | ||
case LM_PROT_RW: osprot = PAGE_READWRITE; | ||
case LM_PROT_XRW: osprot = PAGE_EXECUTE_READWRITE; | ||
} | ||
|
||
return osprot; | ||
} | ||
|
||
lm_prot_t | ||
get_prot(DWORD osprot) | ||
{ | ||
lm_prot_t prot = LM_PROT_NONE; | ||
|
||
switch (osprot) { | ||
case PAGE_EXECUTE: prot = LM_PROT_X; | ||
case PAGE_READONLY: prot = LM_PROT_R; | ||
case PAGE_WRITECOPY: prot = LM_PROT_W; | ||
case PAGE_EXECUTE_READ: prot = LM_PROT_XR; | ||
case PAGE_EXECUTE_WRITECOPY: prot = LM_PROT_XW; | ||
case PAGE_READWRITE: prot = LM_PROT_RW; | ||
case PAGE_EXECUTE_READWRITE: prot = LM_PROT_XRW; | ||
} | ||
|
||
return prot; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* ---------------------------------- | ||
* | libmem - by rdbo | | ||
* | Memory Hacking Library | | ||
* ---------------------------------- | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2023 Rdbo | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License version 3 | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/* TODO: Merge 'osprot.h' and 'osprot.c' for Linux and FreeBSD */ | ||
|
||
#ifndef OSPROT_H | ||
#define OSPROT_H | ||
|
||
#include <libmem/libmem.h> | ||
#include <windows.h> | ||
|
||
DWORD | ||
get_os_prot(lm_prot_t prot); | ||
|
||
lm_prot_t | ||
get_prot(DWORD osprot); | ||
|
||
#endif |