/
EnfusionProcess.h
197 lines (161 loc) · 4.87 KB
/
EnfusionProcess.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
by unitx 12.22.2018
*/
#pragma once
#include "Engine.h"
enum Analysis
{
Easy,
Normal,
Deep
};
typedef BOOL(WINAPI* tReadProcessMemory)(
HANDLE hProcess,
LPCVOID lpBaseAddress,
LPVOID lpBuffer,
SIZE_T nSize,
SIZE_T *lpNumberOfBytesRead);
typedef BOOL(WINAPI* tWriteProcessMemory)(
HANDLE hProcess,
LPVOID lpBaseAddress,
LPCVOID lpBuffer,
SIZE_T nSize,
SIZE_T *lpNumberOfBytesWritten);
class EnfusionProcess
{
public:
static VOID AutoGameSelection(Analysis methods);
static HANDLE GetProcessHandleByName(const CHAR *ProcessName);
static uint64_t GetModuleBase();
static uint64_t ModuleBase;
static const char* GameName;
template <typename T>
static T ReadData(uint64_t address)
{
if (!address)
return T();
T buffer;
return ZwCopyMemory(address, &buffer, sizeof(T), false) ? buffer : T();
};
template <typename T>
static void WriteData(uint64_t address, T data)
{
if (!address)
return;
ZwCopyMemory(address, &data, sizeof(T), true);
};
static std::string ReadString(uint64_t address, size_t size)
{
if (!address || size > 1024)
return "";
char string[1024] = "";
return ZwCopyMemory(address, string, size, false) ? std::string(string) : "";
};
static bool WriteString(uint64_t address, char* string, size_t size)
{
if (!address)
return false;
return ZwCopyMemory(address, string, size, true) ? true : false;
};
private:
static HANDLE m_hProcess;
static tReadProcessMemory m_fReadMemory;
static tWriteProcessMemory m_fWriteMemory;
static __forceinline bool ZwCopyMemory(uint64_t address, PVOID buffer, uint64_t size, BOOL write = false)
{
if (m_hProcess == NULL)
return false;
SIZE_T bytes = 0;
if (!write)
{
if (!m_fReadMemory(m_hProcess, (LPVOID)address, buffer, size, (SIZE_T*)&bytes))
return false;
if (bytes != size)
return false;
}
else
{
if (!m_fWriteMemory(m_hProcess, (LPVOID)address, buffer, size, (SIZE_T*)&bytes))
return false;
if (bytes != size)
return false;
}
return true;
};
};
const CHAR* EnfusionProcess::GameName = "DayZ_x64.exe";
HANDLE EnfusionProcess::m_hProcess = NULL;
uint64_t EnfusionProcess::ModuleBase = NULL;
tReadProcessMemory EnfusionProcess::m_fReadMemory = NULL;
tWriteProcessMemory EnfusionProcess::m_fWriteMemory = NULL;
uint64_t EnfusionProcess::GetModuleBase()
{
uint64_t baseAddress = 0;
HANDLE hHandle = EnfusionProcess::GetProcessHandleByName(
EnfusionProcess::GameName);
HMODULE *moduleArray;
LPBYTE moduleArrayBytes;
DWORD bytesRequired;
if (hHandle) {
if (EnumProcessModules(hHandle, NULL, 0, &bytesRequired)) {
if (bytesRequired) {
moduleArrayBytes = (LPBYTE)LocalAlloc(LPTR, bytesRequired);
if (moduleArrayBytes) {
unsigned int moduleCount;
moduleCount = bytesRequired / sizeof(HMODULE);
moduleArray = (HMODULE *)moduleArrayBytes;
if (EnumProcessModules(hHandle, moduleArray, bytesRequired, &bytesRequired)) {
baseAddress = (DWORD_PTR)moduleArray[0];
}
LocalFree(moduleArrayBytes);
}
}
}
CloseHandle(hHandle);
}
return baseAddress;
}
HANDLE EnfusionProcess::GetProcessHandleByName(const CHAR *ProcessName)
{
HANDLE hProcess = NULL;
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes) {
if (strcmp(pEntry.szExeFile, ProcessName) == 0) {
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, (uint32_t)pEntry.th32ProcessID);
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
return hProcess;
}
VOID EnfusionProcess::AutoGameSelection(Analysis methods)
{
if (methods == Analysis::Easy) {
m_hProcess = EnfusionProcess::GetProcessHandleByName(EnfusionProcess::GameName);
}
if (methods == Analysis::Normal) {
m_hProcess = EnfusionProcess::GetProcessHandleByName(EnfusionProcess::GameName);
}
if (methods == Analysis::Deep) {
m_hProcess = GetProcessHandleByName(EnfusionProcess::GameName);
if (m_hProcess == NULL) {
HWND DayZTitleWindow = FindWindow(NULL, "DayZ");
HWND DayZClassName = FindWindow("DayZ", NULL);
DWORD DayZPid = NULL;
if (DayZTitleWindow != NULL) {
GetWindowThreadProcessId(DayZTitleWindow, &DayZPid);
m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, DayZPid);
}
else if (DayZClassName != NULL) {
GetWindowThreadProcessId(DayZClassName, &DayZPid);
m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, DayZPid);
}
}
ModuleBase = EnfusionProcess::GetModuleBase();
m_fReadMemory = (tReadProcessMemory)GetProcAddress(GetModuleHandle("kernel32.dll"), "ReadProcessMemory");
m_fWriteMemory = (tWriteProcessMemory)GetProcAddress(GetModuleHandle("kernel32.dll"), "WriteProcessMemory");
}
}