-
Notifications
You must be signed in to change notification settings - Fork 2
/
MemProc.c
379 lines (308 loc) · 7.02 KB
/
MemProc.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
#include "MemProc.h"
#include <string.h>
#define __DEBUG_OBJECT__ "MemProc"
#include "dbg/dbg.h"
MemProc *
memproc_new (char *process_name, char *window_name)
{
MemProc *mp;
if ((mp = calloc (sizeof (MemProc), 1)) == NULL)
return NULL;
mp->memchunks = NULL;
mp->stype = SEARCH_TYPE_BYTES;
mp->pid = 0;
mp->process_name = strdup(process_name);
mp->window_name = window_name;
mp->base_addr = 0;
mp->default_baseaddr = 0;
return mp;
}
bool
memproc_detected (MemProc *mp)
{
return (mp->proc != 0);
}
int
memproc_is_dumped (MemProc *mp)
{
return mp->memchunks != NULL;
}
void
memproc_dump_details (MemProc *mp, int start, int end, int nbSections, int (*boolean_function) (MEMORY_BASIC_INFORMATION *, void *), void *arg)
{
MEMORY_BASIC_INFORMATION meminfo;
int addr = start;
int curNbSections = 0;
if (!mp->proc)
{
warn ("Process pid=%d not found", mp->pid);
return;
}
mp->memchunks = bb_queue_new ();
while (1)
{
if (addr >= end && end != -1)
break;
if (nbSections != -1 && curNbSections >= nbSections) {
break;
}
if (VirtualQueryEx (mp->proc, (void *) addr, &meminfo, sizeof (meminfo)) == 0) {
dbg ("VirtualQueryEx returned 0");
break;
}
if (boolean_function (&meminfo, arg))
{
MemChunk *mc = memchunk_new (mp->proc, &meminfo);
switch (meminfo.Type)
{
case MEM_IMAGE: mc->type = MEM_TYPE_IMAGE; break;
case MEM_MAPPED: mc->type = MEM_TYPE_MAPPED; break;
case MEM_PRIVATE: mc->type = MEM_TYPE_PRIVATE; break;
}
console_set_col (0x07);
bb_queue_add (mp->memchunks, mc);
}
addr = ((unsigned int) meminfo.BaseAddress + (unsigned int) meminfo.RegionSize);
curNbSections++;
}
}
static int
memproc_dump_helper (MEMORY_BASIC_INFORMATION *meminfo, void *arg)
{
(void) meminfo;
(void) arg;
return 1;
}
void
memproc_dump (MemProc *mp, int start, int end)
{
memproc_dump_details (mp, start, end, -1, memproc_dump_helper, NULL);
}
void
memproc_dump_sections (MemProc *mp, int start, int nbSections)
{
memproc_dump_details (mp, start, -1, nbSections, memproc_dump_helper, NULL);
}
void
memproc_set_default_baseaddr (MemProc *mp, int default_baseaddr)
{
mp->default_baseaddr = default_baseaddr;
}
bool
memproc_refresh_handle (MemProc *mp)
{
if (mp == NULL)
return false;
// Get the PID
if ((mp->pid = get_pid_by_name (mp->process_name)) == 0)
{
// Process not active
mp->proc = NULL;
warn ("Process \"%s\" not found.", mp->process_name);
return false;
}
// Get the process handle
if ((mp->proc = OpenProcess (PROCESS_ALL_ACCESS, false, mp->pid)) == 0)
{
warn ("Process is unable to be opened with all access.");
return false;
}
// Get the base address
if ((mp->base_addr = get_baseaddr (mp->process_name)) == 0)
{
dbg ("Process <%s> not found.", mp->process_name);
mp->base_addr = mp->default_baseaddr;
}
// Get the window handle
if (mp->window_name != NULL)
{
mp->hwnd = get_hwnd_from_title (mp->window_name);
if (!mp->hwnd) {
warn ("Cannot find the window \"%s\".", mp->window_name);
}
}
return true;
}
void
memproc_debug (MemProc *mp)
{
bb_queue_debug_custom_data (mp->memchunks, memchunk_debug);
}
void
memproc_full_debug (MemProc *mp)
{
bb_queue_debug_custom_data (mp->memchunks, memchunk_full_debug);
}
void
memproc_search_float (MemProc *mp, float value, void (*pre_search) (MemChunk *, float prct))
{
char *mask = "xxxx";
unsigned char pattern[4] = {0, 0, 0, 0};
memcpy (pattern, &value, 4);
memproc_search (mp, pattern, mask, pre_search, SEARCH_TYPE_FLOAT);
}
void
memproc_search_integer (MemProc *mp, int value, void (*pre_search) (MemChunk *, float prct))
{
char *mask = "xxxx";
unsigned char pattern[4] = {0, 0, 0, 0};
memcpy (pattern, &value, 4);
memproc_search (mp, pattern, mask, pre_search, SEARCH_TYPE_INTEGER);
}
void
memproc_search_text (MemProc *mp, char *text, char *mask, void (*pre_search) (MemChunk *, float prct))
{
int len = strlen (text);
if (mask == NULL)
{
mask = malloc (len + 1);
memset (mask, 'x', len);
mask[len] = '\0';
}
memproc_search (mp, (unsigned char *) text, mask, pre_search, SEARCH_TYPE_STRING);
}
void
memproc_update (MemProc *mp, BbQueue *memblocks)
{
MemBlock *mb;
foreach_bbqueue_item (memblocks, mb)
{
memblock_read_from_memory (mp, mb);
// TODO : impact de l'update dans le buffer des mc
}
}
BbQueue *
memblock_get_change (MemProc *mp, BbQueue *res)
{
// TODO
(void) mp;
(void) res;
return NULL;
}
DWORD
mem_search (DWORD start, DWORD size, unsigned char *pattern, char *mask)
{
int offset = 0;
unsigned char * buffer = (char *) start;
do {
if ((offset = find_pattern (buffer, size, pattern, mask)) != -1) {
return (DWORD) buffer + offset;
}
} while (offset != -1);
return 0;
}
void
memproc_search (MemProc *mp, unsigned char *pattern, char *mask, void (*pre_search) (MemChunk *, float prct), SearchType stype)
{
MemChunk *mc;
int offset;
int total;
int loop = 1;
if (mp->memchunks == NULL)
{
warn ("No memchunks stored, you must call %s () first", STRINGIFY (memproc_dump));
return;
}
mp->mask_len = strlen (mask);
foreach_bbqueue_item (mp->memchunks, mc)
{
offset = 0;
total = 0;
if (pre_search != NULL)
pre_search (mc, (float) loop / (float) bb_queue_get_length (mp->memchunks));
do
{
if ((offset = find_pattern (mc->buffer + total, mc->size - total, pattern, mask)) != -1)
{
bb_queue_add_raw (mc->matches, total + offset);
total += offset + strlen (mask);
}
} while (offset != -1);
loop++;
}
mp->stype = stype;
}
BbQueue *
memproc_get_res (MemProc *mp)
{
int offset;
void *data = NULL;
MemBlock *b;
MemChunk *mc;
BbQueue *q = bb_queue_new ();
foreach_bbqueue_item (mp->memchunks, mc)
{
while (bb_queue_get_length (mc->matches))
{
offset = (int) bb_queue_pop (mc->matches);
switch (mp->stype)
{
case SEARCH_TYPE_BYTES:
case SEARCH_TYPE_FLOAT:
case SEARCH_TYPE_INTEGER:
data = malloc (mp->mask_len);
break;
case SEARCH_TYPE_STRING:
data = malloc (mp->mask_len + 1);
((unsigned char *) data) [mp->mask_len] = '\0';
break;
}
memcpy (data, &mc->buffer[(int) offset], mp->mask_len);
b = memblock_new (data, mc->addr + offset, mp->mask_len, mc->type);
bb_queue_add (q, b);
}
}
return q;
}
MemBlock *
memblock_new (void *data, DWORD addr, int size, MemType type)
{
MemBlock *r;
if ((r = malloc (sizeof (MemBlock)) ) == NULL)
return NULL;
r->data = data;
r->addr = addr;
r->size = size;
r->type = type;
return r;
}
void
memblock_debug (MemBlock *mb)
{
printf("addr = %x - size = %x", (int) mb->addr, mb->size);
}
void
memproc_set_absolute_addr (MemProc *mp, DWORD *addr)
{
(*addr) = (*addr) + mp->base_addr;
};
inline void
memblock_read_from_memory (MemProc *mp, MemBlock *mem)
{
read_from_memory (mp->proc, mem->data, mem->addr, mem->size);
}
void
memproc_free (MemProc *memproc)
{
if (memproc != NULL)
{
memproc_clear (memproc);
free (memproc);
}
}
void
memproc_clear (MemProc *memproc)
{
if (memproc != NULL)
{
bb_queue_free_all (memproc->memchunks, memchunk_free);
}
}
void
memblock_free (MemBlock *m)
{
if (m != NULL)
{
free (m);
}
}