-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.hpp
114 lines (93 loc) · 3.29 KB
/
memory.hpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <dlfcn.h>
#include <sys/mman.h>
#define MAX_PATH 4096
#include <memory>
#include <string>
#include <vector>
namespace Memory {
struct ModuleInfo {
char name[MAX_PATH];
uintptr_t base;
uintptr_t size;
char path[MAX_PATH];
};
extern std::vector<ModuleInfo> moduleList;
bool TryGetModule(const char* moduleName, ModuleInfo* info);
const char* GetModulePath(const char* moduleName);
void* GetModuleHandleByName(const char* moduleName);
void CloseModuleHandle(void* moduleHandle);
std::string GetProcessName();
uintptr_t FindAddress(const uintptr_t start, const uintptr_t end, const char* target);
uintptr_t Scan(const char* moduleName, const char* pattern, int offset = 0);
std::vector<uintptr_t> MultiScan(const char* moduleName, const char* pattern, int offset = 0);
struct Pattern {
const char* signature;
std::vector<int> offsets;
};
typedef std::vector<int> Offset;
typedef std::vector<const Pattern*> Patterns;
#define PATTERN(name, sig, ...) Memory::Pattern name { sig, Memory::Offset({__VA_ARGS__}) }
#define PATTERNS(name, ...) Memory::Patterns name({__VA_ARGS__})
std::vector<uintptr_t> Scan(const char* moduleName, const Pattern* pattern);
std::vector<std::vector<uintptr_t>> MultiScan(const char* moduleName, const Patterns* patterns);
template <typename T = uintptr_t>
T Absolute(const char* moduleName, int relative) {
auto info = Memory::ModuleInfo();
return (Memory::TryGetModule(moduleName, &info)) ? (T)(info.base + relative) : (T)0;
}
template <typename T = void*>
T GetSymbolAddress(void* moduleHandle, const char* symbolName) {
return (T)dlsym(moduleHandle, symbolName);
}
template <typename T = void*>
inline T VMT(void* ptr, int index) {
return reinterpret_cast<T>((*((void***)ptr))[index]);
}
template <typename T = uintptr_t>
inline T Read(uintptr_t source) {
auto rel = *reinterpret_cast<int*>(source);
return (T)(source + rel + sizeof(rel));
}
template <typename T = uintptr_t>
void Read(uintptr_t source, T* destination) {
auto rel = *reinterpret_cast<int*>(source);
*destination = (T)(source + rel + sizeof(rel));
}
template <typename T = void*>
inline T Deref(uintptr_t source) {
return *reinterpret_cast<T*>(source);
}
template <typename T = void*>
void Deref(uintptr_t source, T* destination) {
*destination = *reinterpret_cast<T*>(source);
}
template <typename T = void*>
inline T DerefDeref(uintptr_t source) {
return **reinterpret_cast<T**>(source);
}
template <typename T = void*>
void DerefDeref(uintptr_t source, T* destination) {
*destination = **reinterpret_cast<T**>(source);
}
template <typename T = uintptr_t>
T Scan(const char* moduleName, const char* pattern, int offset = 0) {
uintptr_t result = 0;
auto info = Memory::ModuleInfo();
if (Memory::TryGetModule(moduleName, &info)) {
auto start = uintptr_t(info.base);
auto end = start + info.size;
result = Memory::FindAddress(start, end, pattern);
if (result) {
result += offset;;
}
}
return reinterpret_cast<T>(result);
}
inline void UnProtect(void* addr, size_t len) {
uintptr_t startPage = (uintptr_t)addr & 0xFFFFF000;
uintptr_t endPage = ((uintptr_t)addr + len) & 0xFFFFF000;
uintptr_t pageLen = endPage - startPage + 0x1000;
mprotect((void*)startPage, pageLen, PROT_READ | PROT_WRITE | PROT_EXEC);
}
}