-
Notifications
You must be signed in to change notification settings - Fork 7
/
ProcessManager.hpp
178 lines (147 loc) · 3.94 KB
/
ProcessManager.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
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
#pragma once
#include <iostream>
#include <Windows.h>
#include <vector>
#include <Tlhelp32.h>
#include <atlconv.h>
#define _is_invalid(v) if(v==NULL) return false
#define _is_invalid(v,n) if(v==NULL) return n
enum StatusCode
{
SUCCEED,
FAILE_PROCESSID,
FAILE_HPROCESS,
FAILE_MODULE,
};
class ProcessManager
{
private:
bool Attached = false;
public:
HANDLE hProcess = 0;
DWORD ProcessID = 0;
DWORD64 ModuleAddress = 0;
public:
~ProcessManager()
{
//if (hProcess)
//CloseHandle(hProcess);
}
StatusCode Attach(std::string ProcessName)
{
ProcessID = this->GetProcessID(ProcessName);
_is_invalid(ProcessID, FAILE_PROCESSID);
hProcess = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_CREATE_THREAD, TRUE, ProcessID);
_is_invalid(hProcess, FAILE_HPROCESS);
ModuleAddress = reinterpret_cast<DWORD64>(this->GetProcessModuleHandle(ProcessName));
_is_invalid(ModuleAddress, FAILE_MODULE);
Attached = true;
return SUCCEED;
}
void Detach()
{
if (hProcess)
CloseHandle(hProcess);
hProcess = 0;
ProcessID = 0;
ModuleAddress = 0;
Attached = false;
}
bool IsActive()
{
if (!Attached)
return false;
DWORD ExitCode{};
GetExitCodeProcess(hProcess, &ExitCode);
return ExitCode == STILL_ACTIVE;
}
template <typename ReadType>
bool ReadMemory(DWORD64 Address, ReadType& Value, int Size)
{
_is_invalid(hProcess,false);
_is_invalid(ProcessID, false);
if (ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(Address), &Value, Size, 0))
return true;
return false;
}
template <typename ReadType>
bool ReadMemory(DWORD64 Address, ReadType& Value)
{
_is_invalid(hProcess, false);
_is_invalid(ProcessID, false);
if (ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(Address), &Value, sizeof(ReadType), 0))
return true;
return false;
}
template <typename ReadType>
bool WriteMemory(DWORD64 Address, ReadType& Value, int Size)
{
_is_invalid(hProcess, false);
_is_invalid(ProcessID, false);
if (WriteProcessMemory(hProcess, reinterpret_cast<LPCVOID>(Address), &Value, Size, 0))
return true;
return false;
}
template <typename ReadType>
bool WriteMemory(DWORD64 Address, ReadType& Value)
{
_is_invalid(hProcess, false);
_is_invalid(ProcessID, false);
if (WriteProcessMemory(hProcess, reinterpret_cast<LPVOID>(Address), &Value, sizeof(ReadType), 0))
return true;
return false;
}
std::vector<DWORD64> SearchMemory(std::string Signature, DWORD64 StartAddress, DWORD64 EndAddress);
DWORD64 TraceAddress(DWORD64 BaseAddress, std::vector<DWORD> Offsets)
{
_is_invalid(hProcess,0);
_is_invalid(ProcessID,0);
DWORD64 Address = 0;
if (Offsets.size() == 0)
return BaseAddress;
if (!ReadMemory<DWORD64>(BaseAddress, Address))
return 0;
for (int i = 0; i < Offsets.size() - 1; i++)
{
if (!ReadMemory<DWORD64>(Address + Offsets[i], Address))
return 0;
}
return Address == 0 ? 0 : Address + Offsets[Offsets.size() - 1];
}
public:
DWORD GetProcessID(std::string ProcessName)
{
PROCESSENTRY32 ProcessInfoPE;
ProcessInfoPE.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(15, 0);
Process32First(hSnapshot, &ProcessInfoPE);
USES_CONVERSION;
do {
if (strcmp(W2A(ProcessInfoPE.szExeFile), ProcessName.c_str()) == 0)
{
CloseHandle(hSnapshot);
return ProcessInfoPE.th32ProcessID;
}
} while (Process32Next(hSnapshot, &ProcessInfoPE));
CloseHandle(hSnapshot);
return 0;
}
HMODULE GetProcessModuleHandle(std::string ModuleName)
{
MODULEENTRY32 ModuleInfoPE;
ModuleInfoPE.dwSize = sizeof(MODULEENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, this->ProcessID);
Module32First(hSnapshot, &ModuleInfoPE);
USES_CONVERSION;
do {
if (strcmp(W2A(ModuleInfoPE.szModule), ModuleName.c_str()) == 0)
{
CloseHandle(hSnapshot);
return ModuleInfoPE.hModule;
}
} while (Module32Next(hSnapshot, &ModuleInfoPE));
CloseHandle(hSnapshot);
return 0;
}
};
inline ProcessManager ProcessMgr;