forked from MCJack123/DashBot-3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHAPIH.cpp
439 lines (362 loc) · 13.5 KB
/
HAPIH.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "HAPIH.h"
HandleIH::~HandleIH() {
if(MainHandle) CloseHandle(MainHandle);
}
HandleIH::HandleIH() {
MainHandle = 0;
}
HandleIH::HandleIH(DWORD ProcessID) {
MainHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD, FALSE, ProcessID);
if (!MainHandle) status = GetLastError();
return;
}
HandleIH::HandleIH(HANDLE CustomHandle) {
if (CustomHandle) MainHandle = CustomHandle;
return;
}
HandleIH & HandleIH::operator=(DWORD ProcessID) {
if (MainHandle) CloseHandle(MainHandle);
MainHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD, FALSE, ProcessID);
if (!MainHandle) status = GetLastError();
return *this;
}
HandleIH & HandleIH::operator=(HANDLE CustomHandle) {
if (MainHandle == CustomHandle) return *this;
if (MainHandle) CloseHandle(MainHandle);
MainHandle = CustomHandle;
return *this;
}
const unsigned HandleIH::GetStatus() const {
return status;
}
HandleIH::operator HANDLE() const {
if (status) return 0;
else return MainHandle;
}
HandleIH::operator bool() const {
return static_cast<bool>(operator HANDLE());
}
void swap(PointerIH & First, PointerIH & Second) {
using std::swap;
swap(First.BaseAddr, Second.BaseAddr);
swap(First.Offsets, Second.Offsets);
swap(First.Addend, Second.Addend);
}
unsigned DJBHash(const std::vector<unsigned char> & vec){
unsigned Hash = 5381;
for (auto & c : vec) {
Hash = ((Hash << 5) + Hash) + c;
}
return Hash;
}
PointerIH & PointerIH::operator<<(std::size_t Off){
Offsets.push_back(static_cast<std::size_t>(Off));
return *this;
}
PointerIH & PointerIH::operator=(PointerIH rhs) {
swap(*this, rhs);
return *this;
}
PointerIH & PointerIH::operator=(PointerIH && rhs)
{
swap(*this, rhs);
return *this;
}
PointerIH::PointerIH(const PointerIH & rhs){
BaseAddr = rhs.BaseAddr;
Addend = rhs.Addend;
Offsets = rhs.Offsets;
}
PointerIH::PointerIH(PointerIH && rhs) {
PointerIH();
swap(*this, rhs);
}
PointerIH & PointerIH::operator+=(const size_t & rhs){
Addend += rhs;
return *this;
}
PointerIH & PointerIH::operator-=(const size_t & rhs){
Addend -= rhs;
return *this;
}
void HackIH::WriteLog(const std::string & Output) const{
if (LogStream) {
std::time_t time = std::time(0);
std::tm TimeStruct = { 0 };
if (localtime_s(&TimeStruct, &time) == 0) *LogStream << '[' << std::setw(2) << std::setfill('0') << std::to_string(TimeStruct.tm_hour) << ':' << std::setw(2) << std::setfill('0') << std::to_string(TimeStruct.tm_min) << ':' << std::setw(2) << std::setfill('0') << std::to_string(TimeStruct.tm_sec) << ']' << ' ' << Output << '\n';
else *LogStream << Output << '\n';
}
}
void HackIH::GetProcessesInfo() {
Processes.clear();
PROCESSENTRY32 pe32;
HandleIH hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
WriteLog("Failed to get process info, CreateToolhelp32Snapshot returned invalid handle with error: " + std::to_string(GetLastError()));
return;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcessSnap, &pe32)) {
WriteLog("Failed to get process info, Process32First returned invalid handle with error: " + std::to_string(GetLastError()));
return;
}
do {
Processes.push_back(std::make_tuple(pe32.th32ProcessID, pe32.szExeFile));
//std::cout << pe32.th32ProcessID <<"\t\t"<< pe32.szExeFile << std::endl;
} while (Process32Next(hProcessSnap, &pe32));
std::sort(Processes.begin(), Processes.end(), [](
std::tuple<unsigned /*ProcessID*/, std::string /*ProcName*/> a,
std::tuple<unsigned /*ProcessID*/, std::string /*ProcName*/> b
) {
return std::get<0>(a) < std::get<0>(b);
});
}
void HackIH::GetModulesInfo(DWORD PID) {
Modules.clear();
MODULEENTRY32 me32;
HandleIH hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, PID);
if (hModuleSnap == INVALID_HANDLE_VALUE) {
WriteLog("Failed to get modules info, CreateToolHelp32Snapshot returned invalid handle with error: " + std::to_string(GetLastError()));
return;
}
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hModuleSnap, &me32)) {
WriteLog("Failed to get modules info, Module32First returned invalid handle with error: " + std::to_string(GetLastError()));
return;
}
do {
Modules.push_back(std::make_tuple(me32.modBaseAddr, me32.modBaseSize, me32.szExePath));
} while (Module32Next(hModuleSnap, &me32));
std::sort(Modules.begin(), Modules.end(), [](
std::tuple<void* /*Address*/, std::size_t /*Size*/, std::string /*ProcName*/> a,
std::tuple<void* /*Address*/, std::size_t /*Size*/, std::string /*ProcName*/> b
) {
return std::get<0>(a) < std::get<0>(b);
});
}
HackIH::HackIH() {
GetProcessesInfo();
}
HackIH::~HackIH() {
}
void HackIH::WriteProcesses(std::ostream & out) const {
for (auto c : Processes) {
out << std::get<0>(c) << "\t\t" << std::get<1>(c) << std::endl;
}
}
void HackIH::WriteModules(std::ostream & out) const {
if (isBound) for (auto c : Modules) {
out << std::get<0>(c) << "\t" << std::get<1>(c) << "\t" << std::get<2>(c) << std::endl;
}
}
const std::string HackIH::GetProcessName(DWORD PID) const {
auto it = std::find_if(Processes.begin(), Processes.end(), [PID](
std::tuple<unsigned /*ProcessID*/,
std::string /*ProcName*/> x) {
return std::get<0>(x) == PID;
});
if (it == Processes.end()) {
WriteLog("Couldn't find process with process ID: " + std::to_string(PID));
return "";
}
else return std::get<1>(*it);
}
const DWORD HackIH::GetProcessPID(std::string ProcessName) const {
auto it = std::find_if(Processes.begin(), Processes.end(), [ProcessName](
std::tuple<unsigned /*ProcessID*/,
std::string /*ProcName*/> x) {
return std::get<1>(x) == ProcessName;
});
if (it == Processes.end()) {
WriteLog("Couldn't find process with process name: " + ProcessName);
return 0;
}
return std::get<0>(*it);
}
void HackIH::SetDebugOutput(std::ostream & OutputStream){
LogStream = &OutputStream;
}
void HackIH::DisableLog(){
LogStream = nullptr;
}
bool HackIH::bind(DWORD PID) {
GetModulesInfo(PID);
BaseAddress = GetModuleAddress(GetProcessName(PID));
if (!BaseAddress) {
isBound = 0;
WriteLog("Couldn't bind to process. (" + GetProcessName(PID) + ' ' + std::to_string(PID)+')');
GetProcessesInfo();
return 0;
}
ProcHandle = PID;
if (!ProcHandle) WriteLog("OpenProcess failed with error " + std::to_string(ProcHandle.GetStatus()));
else WriteLog("Bound successfully to process. (" + GetProcessName(PID) + ' ' + std::to_string(PID) + ')');
isBound = static_cast<bool>(ProcHandle);
if (isBound) ProcID = PID;
return isBound;
}
bool HackIH::bind(std::string ProcessName) {
return bind(GetProcessPID(ProcessName));
}
void* HackIH::GetModuleAddress(const std::string & ModuleName) const {
auto it = std::find_if(Modules.begin(), Modules.end(), [ModuleName](
std::tuple<void* /*Address*/, std::size_t /*Size*/, std::string /*ProcName*/> x
) {
auto n = std::get<2>(x).rfind('\\');
return ModuleName == std::get<2>(x).substr(n + 1);
});
if (it == Modules.end()) {
WriteLog("Couldn't find " + ModuleName + " module.");
return 0;
}
return std::get<0>(*it);
}
void * HackIH::GetPointerAddress(const PointerIH & Pointer) const{
if(!isBound) return nullptr;
std::size_t Addr = reinterpret_cast<std::size_t>(Pointer.GetBase());
if (Pointer.size()) Addr += Pointer[0];
for (int i = 1; i < Pointer.size(); i++) {
std::size_t ReadAddr = Addr;
if (!ReadProcessMemory(ProcHandle, reinterpret_cast<void*>(Addr), &ReadAddr, sizeof(std::size_t), 0)) {
std::stringstream stream;
stream << "0x" << reinterpret_cast<void*>(Addr);
WriteLog("Failed reading memory (Error: "+std::to_string(GetLastError())+") at address: " + stream.str());
return nullptr;
}
Addr = ReadAddr;
Addr += Pointer[i];
}
Addr += Pointer.GetAddend();
std::stringstream stream;
stream << "0x" << reinterpret_cast<void*>(Addr);
WriteLog("Pointer Read successfully, final address: " + stream.str());
return reinterpret_cast<void*>(Addr);
}
std::string HackIH::GetPointerOffset(const PointerIH & Pointer) const
{
if (!isBound) return std::string();
auto Addr = GetPointerAddress(Pointer);
if (!Addr) return std::string();
for (auto & M : Modules) {
if (Addr >= (std::get<0>(M)) && reinterpret_cast<std::size_t>(Addr) < (reinterpret_cast<std::size_t>(std::get<0>(M)) + std::get<1>(M))) {
auto n = std::get<2>(M).rfind('\\');
;
std::stringstream stream;
stream << std::get<2>(M).substr(n + 1);
stream << " + 0x";
stream << std::setw(8) << std::setfill('0') << std::hex << reinterpret_cast<std::size_t>(std::get<0>(M)) - reinterpret_cast<std::size_t>(Addr);
return stream.str();
}
}
return std::string();
}
bool HackIH::WriteRaw(const PointerIH & Pointer, const void * Buffer, std::size_t BufSize) const{
auto Addr = GetPointerAddress(Pointer);
if (!Addr) return false;
std::stringstream stream;
stream << "0x" << Addr;
if (!WriteProcessMemory(ProcHandle, Addr, Buffer, BufSize, 0)) {
WriteLog("Error in writing memory (Error code: " + std::to_string(GetLastError()) + ") on Address: " + stream.str());
return false;
}
WriteLog("Successfull memory writing on Address: " + stream.str());
return true;
}
bool HackIH::ReadRaw(const PointerIH & Pointer, void * OutBuffer, std::size_t ReadSize) const{
auto Addr = GetPointerAddress(Pointer);
if (!Addr) return false;
std::stringstream stream;
stream << "0x" << Addr;
if (!ReadProcessMemory(ProcHandle, Addr, OutBuffer, ReadSize, 0)) {
WriteLog("Error in reading memory (Error code: " + std::to_string(GetLastError()) + ") on Address: " + stream.str());
return false;
}
WriteLog("Successfull memory reading on Address: " + stream.str());
return true;
}
void* HackIH::AllocateRaw(std::size_t Size,DWORD Protection) const {
auto Addr = VirtualAllocEx(ProcHandle, nullptr, Size, MEM_COMMIT | MEM_RESERVE, Protection);
if (!Addr) WriteLog("Error in allocating new memory, error No: " + std::to_string(GetLastError()));
return Addr;
}
HANDLE HackIH::CreateThread(const PointerIH & Pointer,void* Parameter,bool Suspended) const {
auto Addr = GetPointerAddress(Pointer);
if (!Addr) return nullptr;
HANDLE Handle = CreateRemoteThread(ProcHandle, NULL, NULL, static_cast<LPTHREAD_START_ROUTINE>(Addr), Parameter, Suspended?CREATE_SUSPENDED:0, NULL);
if(!Handle) WriteLog("Error in creating new thread, error No: "+std::to_string(GetLastError()));
std::stringstream stream;
stream << "0x" << Addr;
WriteLog("Thread Started at address: " + stream.str());
return Handle;
}
bool HackIH::WriteBytes(const PointerIH & Pointer, const std::string & str) const {
return WriteRaw(Pointer, str.c_str(), str.size());
}
bool HackIH::WriteBytes(const PointerIH & Pointer, const std::vector<unsigned char> & Vec) const {
return WriteRaw(Pointer, Vec.data(), Vec.size());
}
std::vector<unsigned char> HackIH::ReadBytes(const PointerIH & Pointer, std::size_t ReadSize) const {
std::vector<unsigned char> Vec(ReadSize);
if (!ReadRaw(Pointer, Vec.data(), ReadSize)) return std::vector<unsigned char>();
else return Vec;
}
void * HackIH::AllocateString(const std::string & str) const {
auto Addr = AllocateRaw(str.size(),PAGE_READWRITE);
if (!Addr) return nullptr;
if (!WriteBytes(Addr, str)) return nullptr;
return Addr;
}
bool HackIH::DllInject(const std::string & FileName,bool UnloadAfterInjection) {
if (!isBound) return false;
auto FilePtr = AllocateString(FileName);
if (!FilePtr) return false;
HandleIH Thread = CreateThread(reinterpret_cast<void*>(LoadLibraryA), FilePtr,1);
if (!Thread) return false;
if (ResumeThread(Thread) == -1) {
WriteLog("Couldn't resume thread, Error: "+ std::to_string(GetLastError()));
return false;
}
GetModulesInfo(ProcID);
auto n = FileName.rfind('\\');
auto InjectedAddr = GetModuleAddress(FileName.substr(n + 1));
if (!InjectedAddr) {
WriteLog("DLL isn't loaded inside process (It could have unloaded itself, unlikely)");
return false;
}
if (!UnloadAfterInjection) return true;
auto WaitCode = WaitForSingleObject(Thread, INFINITE);
bool Finished = 0;
DWORD ThreadStatus = 0;
switch (WaitCode) {
case 0x80:
WriteLog("Wait Abandoned for thread.");
return false;
case 0x0:
WriteLog("Thread finished Injector routine.");
if (!GetExitCodeThread(Thread, &ThreadStatus)) WriteLog("Couldn't get status of the thread, improbable error, possibly the injection has succedeed?");
WriteLog("DLL Injection exited with code: " + std::to_string(ThreadStatus));
Finished = 1;
break;
case 0x102:
WriteLog("Wait routine has timed out, still running, can't unload DLL.");
break;
case 0xFFFFFFFF:
WriteLog("The function has failed, Error: "+std::to_string(GetLastError()));
return false;
}
if (!Finished) return true;
return DllEject(FileName);
}
bool HackIH::DllEject(const std::string & FileName){
GetModulesInfo(ProcID);
auto n = FileName.rfind('\\');
auto InjectedAddr = GetModuleAddress(FileName.substr(n + 1));
HandleIH Thread = CreateThread(reinterpret_cast<void*>(FreeLibrary), InjectedAddr, 1);
if (!Thread) return false;
if (ResumeThread(Thread) == -1) {
WriteLog("Couldn't resume thread, Error: " + std::to_string(GetLastError()));
return false;
}
WriteLog("DLL Unloaded");
return true;
}