-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMemory.cpp
127 lines (110 loc) · 3 KB
/
Memory.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Memory.h"
DWORD GetProcId(const wchar_t* procName)
{
DWORD procId = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
if (Process32First(hSnap, &procEntry))
{
do
{
if (!_wcsicmp(procEntry.szExeFile, procName))
{
procId = procEntry.th32ProcessID;
break;
}
} while (Process32Next(hSnap, &procEntry));
}
}
CloseHandle(hSnap);
return procId;
}
uintptr_t GetModuleBaseAddress(DWORD procId, const wchar_t* modName)
{
uintptr_t modBaseAddr = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (Module32First(hSnap, &modEntry))
{
do
{
if (!_wcsicmp(modEntry.szModule, modName))
{
modBaseAddr = (uintptr_t)modEntry.modBaseAddr;
break;
}
} while (Module32Next(hSnap, &modEntry));
}
}
CloseHandle(hSnap);
return modBaseAddr;
}
uintptr_t FindDMAAddress(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets)
{
uintptr_t addr = ptr;
for (unsigned int i = 0; i < offsets.size(); ++i)
{
ReadProcessMemory(hProc, (BYTE*)addr, &addr, sizeof(addr), NULL);
addr += offsets[i];
}
return addr;
}
kernelmode_proc_handler kernelHandler;
bool kernelmode_proc_handler::is_attached() { return handle != INVALID_HANDLE_VALUE; }
bool kernelmode_proc_handler::attach(const wchar_t* proc_name)
{
pid = GetProcId(proc_name);
if (!pid) {
printf("Process not found");
return false;
}
uintptr_t client = GetModuleBaseAddress(pid, L"GameAssembly.dll");
handle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
if (handle == INVALID_HANDLE_VALUE)
{
CloseHandle(handle);
std::cout << "Invalid handle" << std::endl;
return false;
}
return true;
}
uint64_t kernelmode_proc_handler::get_module_base(const wchar_t* module_name)
{
if (handle == INVALID_HANDLE_VALUE) return 0;
uintptr_t modBaseAddr = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (Module32First(hSnap, &modEntry))
{
do
{
if (!_wcsicmp(modEntry.szModule, module_name))
{
modBaseAddr = (uintptr_t)modEntry.modBaseAddr;
break;
}
} while (Module32Next(hSnap, &modEntry));
}
}
CloseHandle(hSnap);
return modBaseAddr;
}
void kernelmode_proc_handler::read_memory(uint64_t src_addr, uint64_t dst_addr, size_t size)
{
if (handle == INVALID_HANDLE_VALUE) return;
ReadProcessMemory(handle, reinterpret_cast<const void*>(src_addr), reinterpret_cast<void*>(dst_addr), size, NULL);
}
void kernelmode_proc_handler::write_memory(uint64_t src_address, uint64_t dst_address, size_t size)
{
if (handle == INVALID_HANDLE_VALUE) return;
WriteProcessMemory(handle, reinterpret_cast<void*>(src_address), reinterpret_cast<void*>(dst_address), size, NULL);
}