forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceservertest.c
417 lines (302 loc) · 7.83 KB
/
ceservertest.c
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
/*
* ceservertest.c
*
* Created on: Jul 1, 2013
* Author: eric
*
* this is basically a standalone client process, but because i'm lazy it runs in the same context as the server
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "ceserver.h"
#include "api.h" //for debugevent
int pHandle;
int cenet_connect(void)
{
int fd;
int i;
int PORT=52736;
struct sockaddr_in addr;
fd=socket(AF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=htonl(INADDR_LOOPBACK); //0x1600a8c0; //INADDR_LOOPBACK;
printf("calling connect...\n");
i=connect(fd, (struct sockaddr *)&addr, sizeof(addr));
printf("after connect. %d\n", i);
return fd;
}
int cenet_OpenProcess(int fd, int pid)
{
#pragma pack(1)
struct
{
char command;
int pid;
} op;
#pragma pack()
int pHandle;
printf("cenet_OpenProcess(%d,%d)\n", fd, pid);
op.command=CMD_OPENPROCESS;
op.pid=pid;
pHandle=0;
sendall(fd, &op, sizeof(op), 0);
recv(fd, &pHandle, sizeof(pHandle),MSG_WAITALL);
return pHandle;
}
int cenet_startDebugger(int fd, int pHandle)
{
#pragma pack(1)
struct
{
char command;
HANDLE pHandle;
} sd;
#pragma pack()
int result;
sd.command=CMD_STARTDEBUG;
sd.pHandle=pHandle;
sendall(fd, &sd, sizeof(sd), 0);
recv(fd, &result, sizeof(result), MSG_WAITALL);
return result;
}
int cenet_waitForDebugEvent(int fd, int pHandle, DebugEvent* devent, int timeout)
{
#pragma pack(1)
struct
{
char command;
HANDLE pHandle;
int timeout;
} wfd;
#pragma pack()
int result;
wfd.command=CMD_WAITFORDEBUGEVENT;
wfd.pHandle=pHandle;
wfd.timeout=timeout;
sendall(fd, &wfd, sizeof(wfd), 0);
recv(fd, &result, sizeof(result), MSG_WAITALL);
if (result)
recv(fd, devent, sizeof(DebugEvent), MSG_WAITALL);
printf(">>>>>>>>>>>>>>>>>>cenet_waitForDebugEvent returned<<<<<<<<<<<<<<<<\n");
return result;
}
int cenet_continueFromDebugEvent(int fd, int pHandle, int tid, int ignore)
{
#pragma pack(1)
struct
{
char command;
HANDLE pHandle;
int tid;
int ignore;
} cfd;
#pragma pack()
int result;
cfd.command=CMD_CONTINUEFROMDEBUGEVENT;
cfd.pHandle=pHandle;
cfd.tid=tid;
cfd.ignore=ignore;
sendall(fd, &cfd, sizeof(cfd), 0);
recv(fd, &result, sizeof(result), MSG_WAITALL);
return result;
}
int cenet_readProcessMemory(int fd, int pHandle, unsigned long long address, void *dest, int size)
{
#pragma pack(1)
struct
{
char command;
int pHandle;
unsigned long long address;
int size;
} rpm;
#pragma pack()
int result;
printf("cenet_readProcessMemory(%d, %d, %llx, %p, %d)", fd, pHandle, address, dest, size);
rpm.command=CMD_READPROCESSMEMORY;
rpm.pHandle=pHandle;
rpm.address=address;
rpm.size=size;
sendall(fd, &rpm, sizeof(rpm), 0);
recv(fd, &result, sizeof(result), MSG_WAITALL);
printf("result=%d\n", result);
recv(fd, dest, result, MSG_WAITALL);
return result;
}
int cenet_setBreakpoint(int fd, int pHandle, int tid, void *Address, int bptype, int bpsize, int debugreg)
{
#pragma pack(1)
struct
{
char command;
HANDLE hProcess;
int tid;
int debugreg;
uint64_t Address;
int bptype;
int bpsize;
} sb;
#pragma pack()
int result;
printf("cenet_setBreakpoint sizeof(sb)=%d\n", sizeof(sb));
sb.command=CMD_SETBREAKPOINT;
sb.hProcess=pHandle;
sb.tid=tid;
sb.debugreg=debugreg;
sb.Address=(uintptr_t)Address;
sb.bptype=bptype;
sb.bpsize=bpsize;
sendall(fd, &sb, sizeof(sb), 0);
recv(fd, &result, sizeof(result), MSG_WAITALL);
return result;
}
int cenet_removeBreakpoint(int fd, int pHandle, int tid, int debugreg, int waswatchpoint)
{
#pragma pack(1)
struct
{
char command;
HANDLE hProcess;
uint32_t tid;
uint32_t debugreg;
uint32_t waswatchpoint;
} rb;
#pragma pack()
int result;
printf("cenet_removeBreakpoint\n");
rb.command=CMD_REMOVEBREAKPOINT;
rb.hProcess=pHandle;
rb.tid=tid;
rb.debugreg=debugreg;
rb.waswatchpoint=waswatchpoint;
sendall(fd, &rb, sizeof(rb), 0);
recv(fd, &result, sizeof(result), MSG_WAITALL);
return result;
}
#define ARM_DBG_READ(N, M, OP2, VAL) do {\
asm volatile("mrc p14, 0, %0, " #N "," #M ", " #OP2 : "=r" (VAL));\
} while (0)
int cenet_VirtualQueryExFull(int fd, int pHandle, DWORD flags)
{
#pragma pack(1)
struct
{
char command;
HANDLE hProcess;
uint8_t flags;
} vqef;
#pragma pack()
vqef.command=CMD_VIRTUALQUERYEXFULL;
vqef.hProcess=pHandle;
vqef.flags=flags;
sendall(fd, &vqef, sizeof(vqef),0);
}
void *CESERVERTEST_DEBUGGERTHREAD(void *arg)
{
int count=0;
int fd=cenet_connect();
int dscr=0;
#ifdef __arm__
ARM_DBG_READ(c0, c1, 0, dscr);
printf("after: %x\n", dscr);
#endif
if (cenet_startDebugger(fd, pHandle))
{
int i;
DebugEvent devent;
printf("cenet_startDebugger=true\n");
while (1)
{
count++;
printf("count=%d\n", count);
if (count==4)
{
printf("going to set breakpoint\n");
i=cenet_setBreakpoint(fd, pHandle, -1, 0x00ce0000, 3, 4,0);
//cenet_setBreakpoint(fd, pHandle, -1, 0x85e4, 0, 1,2);
//cenet_setBreakpoint(fd, pHandle, -1, 0x85e4, 0, 1,3);
// cenet_setBreakpoint(fd, pHandle, -1, 0x85e4, 0, 1,4);
//cenet_setBreakpoint(fd, pHandle, -1, 0xa000, 3, 4, 0);
printf("cenet_setBreakpoint returned %d\n",i);
}
i=cenet_waitForDebugEvent(fd, pHandle, &devent, 2000);
if (i)
{
if (devent.debugevent==5)
{
printf("TRAP (thread %d)\n", devent.threadid);
// printf("Going to remove breakpoint\n");
//cenet_removeBreakpoint(fd, pHandle, devent.threadid,0,1);
//printf("After removeBreakpoint\n");
cenet_continueFromDebugEvent(fd, pHandle, devent.threadid, 2); //single step
i=cenet_waitForDebugEvent(fd, pHandle, &devent, 2000);
printf("after single step. i=%d\n",i);
printf("devent.debugevent=%d (thread %d)\n", devent.debugevent, devent.threadid);
// cenet_setBreakpoint(fd, pHandle, devent.threadid,0xce0000, 3,4,0);
// cenet_setBreakpoint(fd, pHandle, -1, 0xa000, 3, 4);
// printf("cenet_removeBreakpoint returned\n");
cenet_continueFromDebugEvent(fd, pHandle, devent.threadid, 1); //continue unhandled/single step
}
else
cenet_continueFromDebugEvent(fd, pHandle, devent.threadid, 0);
}
}
}
else
{
printf("Failed to start the debugger\n");
}
return NULL;
}
void *CESERVERTEST_RPMTHREAD(void *arg)
{
int fd=cenet_connect();
int i;
int hp;
while (1)
{
//cenet_readProcessMemory(fd, 0x601048, &hp, 4)
#ifdef __arm__
i=cenet_readProcessMemory(fd, pHandle, 0xa000, &hp, 4);
#else
i=cenet_readProcessMemory(fd, pHandle, 0x601068, &hp, 4);
#endif
printf("CESERVERTEST_RPMTHREAD:");
printf("i=%d\n", i);
printf("hp=%d\n", hp);
sleep(1);
}
}
void *CESERVERTEST(void *argv[])
{
int fd;
int pid=atoi(argv[2]);
pthread_t pth;
printf("CESERVERTEST: running\n");
//sleep(2);
printf("connecting...\n");
fd=cenet_connect();
printf("fd=%d\n", fd);
printf("pid=%d\n", pid);
pHandle=cenet_OpenProcess(fd, pid);
printf("pHandle=%d\n", pHandle);
//cenet_VirtualQueryExFull(fd, pHandle, VQE_DIRTYONLY | VQE_PAGEDONLY);
// CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
//launch the debuggerthread
//pthread_create(&pth, NULL, CESERVERTEST_DEBUGGERTHREAD, NULL);
CESERVERTEST_DEBUGGERTHREAD(NULL);
//launch the rpmthread
// sleep(1);
//pthread_create(&pth, NULL, CESERVERTEST_RPMTHREAD, NULL);
// while (1);
fflush(stdout);
return NULL;
}