-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathceserver_interface.cpp
More file actions
363 lines (333 loc) · 10.3 KB
/
ceserver_interface.cpp
File metadata and controls
363 lines (333 loc) · 10.3 KB
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
#include "ceserver_interface.h"
#include "ceserver.h"
#include "ceserver_interface_impl.h"
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <shared_mutex>
#include <stack>
#include <sys/time.h>
#include <unordered_map>
// abstract layer for handles management
namespace ceserver {
uint64_t GetTickCount() {
long long tmp;
struct timeval tv;
gettimeofday(&tv, NULL);
tmp = tv.tv_sec;
tmp = tmp * 1000;
tmp = tmp + (tv.tv_usec / 1000);
return tmp;
}
class Process {
private:
uint64_t pid = 0;
uint64_t ProcessObject = 0;
bool is64bit = false;
public:
explicit Process(uint64_t _pid, uint64_t obj) {
pid = _pid;
ProcessObject = obj;
is64bit = ceserver_impl::Is64BitProcess(obj);
}
uint64_t GetPid() const { return pid; }
uint64_t GetProcessObject() const { return ProcessObject; }
bool Is64Bit() { return is64bit; }
};
class HandleListEntry {
private:
int count;
public:
void *pointer;
handleType type;
explicit HandleListEntry(void *p, handleType t) {
this->pointer = p;
this->type = t;
this->count = 1;
};
void AddRef() { this->count++; }
void DecRef() { this->count--; }
int refCount() { return this->count; }
};
using typeTHSProcessHandle = process_list *;
using typeTHSModuleHandle = module_list *;
using typeProcessHandle = Process *;
std::map<HANDLE, HandleListEntry *> m_handletable;
std::shared_mutex m_handletable_lock;
HANDLE m_latesthandle = 0;
std::stack<HANDLE> m_handletable_freed;
handleType GetHandleType(HANDLE handle) {
m_handletable_lock.lock_shared();
auto it = m_handletable.find(handle);
if (it != m_handletable.end()) {
void *ptr = m_handletable[handle];
if (ptr) {
handleType ty = m_handletable[handle]->type;
m_handletable_lock.unlock_shared();
return ty;
}
}
m_handletable_lock.unlock_shared();
return htEmpty;
}
void *GetPointerFromHandle(HANDLE handle) {
m_handletable_lock.lock_shared();
auto it = m_handletable.find(handle);
if (it != m_handletable.end()) {
void *ptr = m_handletable[handle];
if (ptr) {
auto result = m_handletable[handle]->pointer;
m_handletable_lock.unlock_shared();
return result;
}
}
m_handletable_lock.unlock_shared();
return nullptr;
}
HANDLE CreateHandle(void *pointer, handleType ht) {
m_handletable_lock.lock();
HANDLE hNewHandle;
if (!m_handletable_freed.empty()) {
hNewHandle = m_handletable_freed.top();
m_handletable_freed.pop();
m_handletable.insert({hNewHandle, new HandleListEntry(pointer, ht)});
// m_handletable[hNewHandle]=pointer;
m_handletable_lock.unlock();
return hNewHandle;
}
hNewHandle = ++m_latesthandle;
m_handletable.insert({hNewHandle, new HandleListEntry(pointer, ht)});
m_handletable_lock.unlock();
;
return hNewHandle;
}
void DestoryHandle(HANDLE handle) {
auto it = m_handletable.find(handle);
if (it != m_handletable.end()) {
void *ptr = m_handletable[handle];
handleType ty = m_handletable[handle]->type;
if (ptr) {
auto he = (HandleListEntry *)ptr;
if (ty == htTHSProcess) {
auto obj = (typeTHSProcessHandle)he->pointer;
delete obj;
} else if (ty == htTHSModule) {
auto obj = (typeTHSModuleHandle)he->pointer;
delete obj;
} else if (ty == htProcesHandle) {
auto obj = (typeProcessHandle)he->pointer;
ceserver_impl::CloseProcess(obj->GetProcessObject());
delete obj;
}
delete he;
}
m_handletable_freed.push(it->first);
m_handletable.erase(it);
}
}
void CloseHandle(HANDLE handle) {
m_handletable_lock.lock();
auto it = m_handletable.find(handle);
if (it != m_handletable.end()) {
auto he = m_handletable[handle];
he->DecRef();
if (he->refCount() == 0) {
DestoryHandle(handle);
}
}
m_handletable_lock.unlock();
}
HandleListEntry *QueryHandle(HANDLE handle) {
m_handletable_lock.lock_shared();
auto it = m_handletable.find(handle);
HandleListEntry *result = it != m_handletable.end() ? it->second : nullptr;
m_handletable_lock.unlock_shared();
return result;
}
HANDLE CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID) {
if (dwFlags & TH32CS_SNAPPROCESS) {
auto &&pl = ceserver_impl::TraverseProcess();
process_list *plist = new process_list({pl, pl.begin()});
if (plist != nullptr) {
return CreateHandle(plist, htTHSProcess);
} else {
return 0;
}
} else if (dwFlags & TH32CS_SNAPMODULE) {
auto &&ml = ceserver_impl::TraverseModule(th32ProcessID);
module_list *mlist = new module_list({ml, ml.begin()});
if (mlist != nullptr) {
return CreateHandle(mlist, htTHSModule);
} else {
return 0;
}
}
return 0;
}
BOOL Process32Next(HANDLE hSnapshot, process_list_entry *processentry) {
HandleListEntry *he = QueryHandle(hSnapshot);
if (he && he->type == htTHSProcess) {
process_list *plist = (decltype(plist))he->pointer;
if (plist && !plist->list.empty() && plist->itor != plist->list.end()) {
*processentry = *plist->itor;
plist->itor++;
return TRUE;
}
}
return FALSE;
}
BOOL Process32First(HANDLE hSnapshot, process_list_entry *processentry) {
HandleListEntry *he = QueryHandle(hSnapshot);
if (he && he->type == htTHSProcess) {
process_list *plist = (decltype(plist))he->pointer;
if (plist && !plist->list.empty()) {
plist->itor = plist->list.begin();
//*processentry=*plist->itor;
return Process32Next(hSnapshot, processentry);
}
}
return FALSE;
}
BOOL Module32Next(HANDLE hSnapshot, module_list_entry *moduleentry) {
HandleListEntry *he = QueryHandle(hSnapshot);
if (he && he->type == htTHSModule) {
module_list *plist = (decltype(plist))he->pointer;
if (plist && !plist->list.empty() && plist->itor != plist->list.end()) {
*moduleentry = *plist->itor;
plist->itor++;
return TRUE;
}
}
return FALSE;
}
BOOL Module32First(HANDLE hSnapshot, module_list_entry *moduleentry) {
HandleListEntry *he = QueryHandle(hSnapshot);
if (he && he->type == htTHSModule) {
module_list *plist = (decltype(plist))he->pointer;
if (plist && !plist->list.empty()) {
plist->itor = plist->list.begin();
//*processentry=*plist->itor;
return Module32Next(hSnapshot, moduleentry);
}
}
return FALSE;
}
HANDLE OpenProcess(DWORD pid) {
uint64_t obj = ceserver_impl::OpenProcess(pid);
if (obj == 0)
return 0;
auto p = new Process(pid, obj);
return CreateHandle(p, htProcesHandle);
}
int GetArchitecture(HANDLE hProcess) {
if (GetHandleType(hProcess) == htProcesHandle) {
Process *p = (Process *)GetPointerFromHandle(hProcess);
if (p->Is64Bit())
return 1;
else
return 0;
}
return -1;
}
unsigned char GetPlatformABI() {
// windows=0 linux=1
return ceserver_impl::GetPlatformABI();
}
int ReadProcessMemory(HANDLE hProcess, uint64_t lpAddress, void *buffer,
int size) {
if (GetHandleType(hProcess) == htProcesHandle) {
Process *p = (Process *)GetPointerFromHandle(hProcess);
if (p) {
return ceserver_impl::ReadProcessMemory(p->GetProcessObject(), lpAddress,
buffer, size);
}
}
return 0;
}
int WriteProcessMemory(HANDLE hProcess, uint64_t lpAddress, void *buffer,
int size) {
if (GetHandleType(hProcess) == htProcesHandle) {
Process *p = (Process *)GetPointerFromHandle(hProcess);
if (p) {
return ceserver_impl::WriteProcessMemory(p->GetProcessObject(), lpAddress,
buffer, size);
}
}
return 0;
}
std::list<region_info> TraverseMemoryRegion(uint64_t pid) {
auto result = ceserver_impl::TraverseMemoryRegion(pid);
result.sort(
[](auto a, auto b) -> bool { return a.baseaddress < b.baseaddress; });
std::list<region_info> noaccess{};
region_info prev_region = {0, 0, PAGE_NOACCESS, MEM_PRIVATE};
for (auto &&m : result) {
uint64_t prev_endaddr = prev_region.baseaddress + prev_region.size;
if (m.baseaddress - prev_endaddr > 0) {
noaccess.push_back({.baseaddress = prev_endaddr,
.size = m.baseaddress - prev_endaddr,
.protection = PAGE_NOACCESS,
.type = MEM_PRIVATE});
}
prev_region = m;
}
result.splice(result.begin(), noaccess);
result.sort(
[](auto a, auto b) -> bool { return a.baseaddress < b.baseaddress; });
return std::move(result);
}
std::map<int, std::pair<uint64_t, std::list<region_info>>>
m_VirtualQueryEx_Cache;
std::mutex m_VirtualQueryEx_Cache_lock;
int VirtualQueryExImpl(Process *p, uint64_t lpAddress, region_info *rinfo,
char *mapsline) {
auto pid = p->GetPid();
// processing cache
if (m_VirtualQueryEx_Cache.find(pid) != m_VirtualQueryEx_Cache.end()) {
auto &&[time, region] = m_VirtualQueryEx_Cache[pid];
if (GetTickCount() - time > 1000) {
auto &&new_region = TraverseMemoryRegion(pid);
m_VirtualQueryEx_Cache.erase(pid);
m_VirtualQueryEx_Cache.insert({pid, {GetTickCount(), new_region}});
}
} else {
auto &&new_region = TraverseMemoryRegion(pid);
m_VirtualQueryEx_Cache.insert({pid, {GetTickCount(), new_region}});
}
auto regions = m_VirtualQueryEx_Cache[pid].second;
for (auto &&m : regions) {
if (lpAddress >= m.baseaddress && lpAddress < m.baseaddress + m.size) {
*rinfo = m;
return 1;
}
}
return 0;
}
int VirtualQueryEx(HANDLE hProcess, uint64_t lpAddress, region_info *rinfo,
char *mapsline) {
*rinfo = {0};
if (GetHandleType(hProcess) == htProcesHandle) {
Process *p = (Process *)GetPointerFromHandle(hProcess);
if (p) {
m_VirtualQueryEx_Cache_lock.lock();
int result = VirtualQueryExImpl(p, lpAddress, rinfo, mapsline);
m_VirtualQueryEx_Cache_lock.unlock();
return result;
}
}
return 0;
}
std::shared_ptr<std::list<region_info>> VirtualQueryExFull(HANDLE hProcess,
uint32_t flags) {
if (GetHandleType(hProcess) == htProcesHandle) {
Process *p = (Process *)GetPointerFromHandle(hProcess);
if (p) {
return std::make_shared<std::list<region_info>>(
TraverseMemoryRegion(p->GetPid()));
}
}
return std::make_shared<std::list<region_info>>();
}
} // namespace ceserver