This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MemMan.cpp
75 lines (61 loc) · 2 KB
/
MemMan.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "MemMan.h"
cMemMan::cMemMan()
{
#ifdef WIN32
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
#endif
}
cMemMan::~cMemMan()
{
#ifdef WIN32
CloseHandle(hProcess);
#endif
}
bool cMemMan::ReadMemory(const void* lpBaseAddress, const void *lpBuffer, const int _nSize)
{
bool bResult;
#ifdef WIN32
bResult = (ReadProcessMemory(hProcess, lpBaseAddress, (LPVOID)lpBuffer, _nSize, NULL) != 0);
#else
bResult = true;
bResult &= UnProtect(lpBaseAddress, _nSize);
unsigned int p_size = sysconf(_SC_PAGESIZE);
char* all_adr = (char*)(((unsigned int)lpBaseAddress) & ~(p_size - 1));
unsigned int size = (unsigned int)lpBaseAddress - (unsigned int)all_adr + _nSize;
bResult &= (mlock(all_adr, size) == 0);
memcpy((void*)lpBuffer, lpBaseAddress, _nSize);
bResult &= (munlock(all_adr, size) == 0);
#endif
return bResult;
}
bool cMemMan::WriteMemory(const void* lpBaseAddress, const void *lpBuffer, const int _nSize)
{
bool bResult;
#ifdef WIN32
bResult = (WriteProcessMemory(hProcess, (LPVOID)lpBaseAddress, lpBuffer, _nSize, NULL) != false);
#else
bResult = true;
bResult &=UnProtect(lpBaseAddress, _nSize);
unsigned int p_size = sysconf(_SC_PAGESIZE);
char* all_adr = (char*)(((unsigned int)lpBaseAddress) & ~(p_size - 1));
unsigned int size = (unsigned int)lpBaseAddress - (unsigned int)all_adr + _nSize;
bResult &= (mlock(all_adr, size) == 0);
memcpy((void*)lpBaseAddress, lpBuffer, _nSize);
bResult &= (munlock(all_adr, size) == 0);
#endif
return bResult;
}
bool cMemMan::UnProtect(const void*lpBaseAddress, const int _nSize)
{
bool bResult = false;
#ifdef WIN32
DWORD oldprv;
bResult = (VirtualProtect((void*)lpBaseAddress, _nSize, PAGE_EXECUTE_READWRITE, &oldprv) != false);
#else
unsigned int p_size = sysconf(_SC_PAGESIZE);
char* all_adr = (char*)(((unsigned int)lpBaseAddress) & ~(p_size - 1));
unsigned int size = (unsigned int)lpBaseAddress - (unsigned int)all_adr + _nSize;
bResult = (mprotect(all_adr, size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0);
#endif
return bResult;
}