forked from mlghuskie/NoBastian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABClient.h
178 lines (155 loc) · 4.28 KB
/
ABClient.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
#pragma once
#include "Include.h"
using namespace std;
namespace asmjs
{
class ABClient
{
public:
string PipeName;
HANDLE hPipe;
ABClient(string pipeName = "\\\\.\\pipe\\ABPipe1") : PipeName(pipeName) {};
void Connect()
{
hPipe = CreateFileA
(
PipeName.c_str(),
GENERIC_READ | GENERIC_WRITE,
null,
null,
OPEN_EXISTING,
null,
null
);
if (hPipe == INVALID_HANDLE_VALUE) throw exception("Couldn't connect to pipe: CreateFileA failed");
}
void WritePipeRaw(void* data, uint64_t size)
{
if(!WriteFile(hPipe, data, size, null, null)) throw exception("Couldn't write data to pipe: WriteFile failed");
}
void ReadPipeRaw(void* data, uint64_t size)
{
if (!ReadFile(hPipe, data, size, null, null)) throw exception("Couldn't read data from pipe: ReadFile failed");
}
template <class T> void WritePipe(T& data)
{
WritePipeRaw(&data, sizeof(T));
}
template <class T> void ReadPipe(T& data)
{
ReadPipeRaw(&data, sizeof(T));
}
template <class T_In, class T_Out> void Request(T_In& in, T_Out& out)
{
WritePipe(in);
ReadPipe(out);
}
void Ping()
{
ABRequest req = { C_Ping };
ABResponse res;
Request(req, res);
if (!res.Status) throw exception("Pinging failed");
}
void UnloadModule()
{
ABRequest req = { C_UnloadModule };
WritePipe(req);
}
void AccuireProcessHandle(uint64_t processId, ACCESS_MASK desiredAccess)
{
ReqAccuireProcessHandle req;
req.Command = C_AccuireProcessHandle;
req.DesiredAccess = desiredAccess;
req.ProcessId = processId;
ABResponse res;
Request(req, res);
if (!res.Status) throw exception("Couldn't accuire process handle");
}
void SetProcessHandle(HANDLE hProcess)
{
ReqSetProcessHandle req;
req.Command = C_SetProcessHandle;
req.hProcess = hProcess;
ABResponse res;
Request(req, res);
if (!res.Status) throw exception("Couldn't set process handle");
}
uint64_t GetProcessModuleBase(string module)
{
auto sz = sizeof(ABRequest::Command) + module.length() + 0x1;
byte* buff = new byte[sz];
((ABRequest*)buff)->Command = C_GetModuleBase;
memcpy(buff + sizeof(ABRequest::Command), module.c_str(), module.length() + 0x1);
WritePipeRaw(buff, sz);
delete[] buff;
ResModuleBase res;
ReadPipe(res);
if (!res.Status) throw exception("Couldn't get module base failed");
return res.ModuleBase;
}
void WpmRaw(uint64_t address, void* data, uint64_t size)
{
auto buffSize = offsetof(ReqWpm, Data) + size;
byte* buff = new byte[buffSize];
((ReqWpm*)buff)->Command = C_WPM;
((ReqWpm*)buff)->Address = address;
((ReqWpm*)buff)->DataSize = size;
memcpy((&((ReqWpm*)buff)->Data), data, size);
WritePipeRaw(buff, buffSize);
delete[] buff;
}
void RpmRaw(uint64_t address, void* data, uint64_t size)
{
auto buffSize = offsetof(ResRpm, Data) + size;
auto buff = new byte[buffSize];
ReqRpm req;
req.Command = C_RPM;
req.Address = address;
req.DataSize = size;
WritePipe(req);
ReadPipeRaw(buff, buffSize);
memcpy(data, buff + offsetof(ResRpm, Data), size);
delete[] buff;
}
DWORD VirtualProtect(uint64_t address, uint64_t size, DWORD protection)
{
ReqVp req;
ResVp res;
req.Command = C_VirtualProtect;
req.Address = address;
req.Size = size;
req.NewProtection = protection;
WritePipe(req);
ReadPipe(res);
if (!res.Status) throw exception("VirtualProtect failed");
return res.OldProtection;
}
uint64_t VirtualAlloc(uint64_t address, uint64_t size, DWORD allocationType, DWORD protection)
{
ReqVa req;
ResVa res;
req.Command = C_VirtualAlloc;
req.Address = address;
req.Size = size;
req.AllocationType = allocationType;
req.Protection = protection;
Request(req, res);
if (!res.Status) throw exception("VirtualAlloc failed");
return res.Address;
}
template <class T> void Wpm(uint64_t address, T& data)
{
WpmRaw(address, &data, sizeof(T));
}
template <class T> void Rpm(uint64_t address, T& data)
{
RpmRaw(address, &data, sizeof(T));
}
void Disconnect()
{
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
}
};
}