-
Notifications
You must be signed in to change notification settings - Fork 54
/
memhlp.cpp
418 lines (393 loc) · 8.89 KB
/
memhlp.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
#include "memhlp.hpp"
#include "hashes.h"
#include <sstream>
#include <cstring>
#include <iostream>
#include <openssl/evp.h>
void memhlp::set_segments(std::map<uint64_t,
std::tuple<uint8_t *, uint64_t, int>>* seg){
segs = *seg;
}
std::string memhlp::get_sym_name(enum symbl sym) {
std::stringstream ret;
switch (sym) {
case SYM_ON:
ret << "demov_on";
break;
case SYM_SEL_ON:
ret << "demov_sel_on";
break;
case SYM_M_LOOP:
ret << "demov_master_loop";
break;
case SYM_ENTRYP:
ret << "demov_entrypoint";
break;
case SYM_ALU_EQ:
ret << "demov_equal";
break;
case SYM_ALU_ADD:
ret << "demov_add";
break;
case SYM_BIT_SET:
ret << "demov_bit_set";
break;
case SYM_BIT_CLR:
ret << "demov_bit_clear";
break;
case SYM_IMP_BAND:
case SYM_ALU_AND:
ret << "demov_and";
break;
case SYM_ALU_OR:
ret << "demov_or";
break;
case SYM_ALU_XOR:
ret << "demov_xor";
break;
case SYM_ALU_SHL:
ret << "demov_shl";
break;
case SYM_ALU_SHR:
ret << "demov_shr";
break;
case SYM_ALU_SARI:
ret << "demov_sari";
break;
case SYM_ALU_MULL:
ret << "demov_mul_l";
break;
case SYM_ALU_MULH:
ret << "demov_mul_h";
break;
case SYM_BOOL_OR:
ret << "demov_bool_or";
break;
case SYM_BOOL_XOR:
ret << "demov_bool_xor";
break;
case SYM_BOOL_XNOR:
ret << "demov_bool_xnor";
break;
case SYM_BOOL_AND:
ret << "demov_bool_and";
break;
case SYM_TARGET:
ret << "demov_target_reg";
break;
case SYM_SEL_TARGET:
ret << "demov_sel_target";
break;
case SYM_SP:
ret << "demov_esp";
break;
case SYM_END:
ret << "demov_end";
break;
case SYM_ALU_TRUE:
ret << "demov_SYM_ALU_TRUE";
break;
case SYM_ALU_FALSE:
ret << "demov_SYM_ALU_FALSE";
break;
case SYM_ALU_B0:
ret << "demov_SYM_ALU_B0";
break;
case SYM_ALU_B1:
ret << "demov_SYM_ALU_B1";
break;
case SYM_ALU_B2:
ret << "demov_SYM_ALU_B2";
break;
case SYM_ALU_B3:
ret << "demov_SYM_ALU_B3";
break;
case SYM_ALU_B4:
ret << "demov_SYM_ALU_B4";
break;
case SYM_ALU_B5:
ret << "demov_SYM_ALU_B5";
break;
case SYM_ALU_B6:
ret << "demov_SYM_ALU_B6";
break;
case SYM_ALU_B7:
ret << "demov_SYM_ALU_B7";
break;
case SYM_ALU_ADD8L:
ret << "demov_SYM_ALU_ADD8L";
break;
case SYM_ALU_ADD8H:
ret << "demov_SYM_ALU_ADD8H";
break;
case SYM_ALU_INV8:
ret << "demov_SYM_ALU_INV8";
break;
case SYM_ALU_INV16:
ret << "demov_SYM_ALU_INV16";
break;
case SYM_ALU_CLAMP32:
ret << "demov_SYM_ALU_CLAMP32";
break;
case SYM_ALU_MUL_SUM8L:
ret << "demov_SYM_ALU_MUL_SUM8L";
break;
case SYM_ALU_MUL_SUM8H:
ret << "demov_SYM_ALU_MUL_SUM8H";
break;
case SYM_ALU_MUL_SHL2:
ret << "demov_SYM_ALU_MUL_SHL2";
break;
case SYM_ALU_MUL_SUMS:
ret << "demov_SYM_ALU_MUL_SUMS";
break;
case SYM_ALU_DIV_SHL1_8_C_D:
ret << "demov_SYM_ALU_DIV_SHL1_8_C_D";
break;
case SYM_ALU_DIV_SHL1_8_D:
ret << "demov_SYM_ALU_DIV_SHL1_8_D";
break;
case SYM_ALU_DIV_SHL2_8_D:
ret << "demov_SYM_ALU_DIV_SHL2_8_D";
break;
case SYM_ALU_DIV_SHL3_8_D:
ret << "demov_SYM_ALU_DIV_SHL3_8_D";
break;
case SYM_ALU_SEX8:
ret << "demov_SYM_ALU_SEX8";
break;
case SYM_DATA:
ret << "demov_SYM_DATA";
break;
case SYM_SEL_DATA:
ret << "demov_SYM_SEL_DATA";
break;
case SYM_INVALID:
ret << "demov_invalid";
break;
case SYM_STP_ADD4:
ret << "demov_STACK_ADD4(pop)";
break;
case SYM_STP_SUB4:
ret << "demov_STACK_SUB4(push)";
break;
case SYM_DISCARD:
ret << "demov_DISCARD";
break;
case SYM_DISPATCH:
ret << "demov_DISPATCH";
break;
case SYM_FAULT:
ret << "demov_FAULT";
break;
default:
ret << "demov_unrecognized";
break;
}
return ret.str();
}
std::string memhlp::dump_syms() {
std::stringstream ret;
ret << std::hex;
for (auto &x: symbol) {
ret << get_sym_name(x.second) << "@" << x.first << std::endl;
}
return ret.str();
}
std::string memhlp::dump_syms_idc() {
std::stringstream ret;
ret << std::hex;
for (auto &x: symbol) {
if (x.second == SYM_STP_SUB4 || x.second == SYM_STP_ADD4)
continue;
ret << "\tMakeName(0x";
ret << x.first << ", \"" << get_sym_name(x.second);
ret << "\");" << std::endl;
}
return ret.str();
}
uint8_t* memhlp::get_ptr(uint64_t addr) {
size_t off;
uint8_t *st;
auto *seg = this->get_segment(addr);
if (seg == NULL) return NULL;
off = addr - seg->first;
return (st = std::get<0>(seg->second)) ? st + off : NULL;
}
size_t memhlp::space(uint64_t addr) {
auto *seg = this->get_segment(addr);
if (seg == NULL) return 0;
return (std::get<1>(seg->second)) - (addr - seg->first);
}
int memhlp::is_X(uint64_t addr) {
auto *seg = get_segment(addr);
return (seg && ((std::get<2>(seg->second)) & PF_X))? 1 : 0;
}
std::pair<const uint64_t, std::tuple<uint8_t *, uint64_t, int>>*
memhlp::get_segment(uint64_t addr) {
for (auto &i: segs) {
if (i.first <= addr && (i.first + std::get<1>(i.second)) >= addr)
return &i;
}
return NULL;
}
symbl memhlp::analyse_table(uint64_t addr, int dim) {
if (has_sym_to(addr)) return get_sym(addr);
uint8_t *ptr = get_ptr(addr);
if (!ptr) return SYM_INVALID;
symbl ret = SYM_INVALID;
if (dim == 2) {
uint32_t *ind = (uint32_t *) ptr;
uint8_t *elem;
if (get_ptr(*ind) == NULL) return SYM_INVALID;
/* indexed 1D table looks like 2D */
if ((*ind) + 16 == *(ind + 4) && get_ptr(*ind)[4] == 1) {
add_sym(addr, SYM_ALU_ADD);
return SYM_ALU_ADD;
}
/* boolean operations are very small 2D tables */
if (get_ptr((uint64_t) ind[1]) && !get_ptr((uint64_t) ind[2])) {
uint32_t *target = (uint32_t*) get_ptr((uint64_t) ind[0]);
int tmp = target[0] | (target[1] << 1);
target = (uint32_t*) get_ptr((uint64_t) ind[1]);
tmp |= (target[0] << 2) | (target[1] << 3);
switch (tmp) {
case 0xE:
ret = SYM_BOOL_OR;
break;
case 0x8:
ret = SYM_BOOL_AND;
break;
case 0x6:
ret = SYM_BOOL_XOR;
break;
case 0x9:
ret = SYM_BOOL_XNOR;
break;
default:
std::cerr << "unrecognized boolean op: " << tmp << std::endl;
}
if (ret != SYM_INVALID)
add_sym(addr, ret);
return ret;
}
if (!(elem = get_ptr((uint64_t) ind[7]))) return SYM_INVALID;
/* mad magic here */
switch (elem[0xCB]) {
case 0xCB:
ret = SYM_BIT_SET;
break;
case 0x4B:
ret = SYM_BIT_CLR;
break;
case 0:
if (elem[0xC8] == 0x80)
ret = SYM_ALU_SHL;
else if(!elem[0xC8] && elem[0x7] == 1)
ret = SYM_ALU_EQ;
else
ret = SYM_INVALID;
break;
case 3:
ret = SYM_ALU_AND;
break;
case 0xCF:
ret = SYM_ALU_OR;
break;
case 0xCC:
ret = SYM_ALU_XOR;
break;
case 0x01:
ret = SYM_ALU_SHR;
break;
case 0xFF:
ret = SYM_ALU_SARI;
break;
case 0x8D:
ret = SYM_ALU_MULL;
break;
case 0x5:
ret = SYM_ALU_MULH;
break;
default:
ret = SYM_INVALID;
break;
}
/*else
std::cerr << "table not recognized" << std::endl; */
}
if (dim == 1 && space(addr) > 256) {
EVP_MD_CTX *ctx;
uint64_t diggest[256 >> 6];
size_t i;
do {
ctx = EVP_MD_CTX_create();
if (ctx == 0) break;
if (!EVP_DigestInit_ex(ctx, EVP_sha256(), NULL)) break;
if (!EVP_DigestUpdate(ctx, (const void*) ptr, 256)) break;
if (!EVP_DigestFinal_ex(ctx, (uint8_t *) diggest, NULL)) break;
EVP_MD_CTX_destroy(ctx);
for (i = 0; i < (sizeof(hashes) / sizeof(uint64_t*)); i++) {
if (memcmp(hashes[i],diggest, 256 >> 3) == 0) {
ret = (enum symbl) (((unsigned int)SYM_ALU_TRUE) + i);
break;
}
}
if (ret == SYM_ALU_B6) {
uint32_t *tbl = (uint32_t *) ptr;
int st = 0;
ret = SYM_INVALID;
if (space(addr) < 4 * 256) return ret;
for (i = 64; i < 256; i++) {
st |= (i & 0xC0) ^ ((tbl[i] << 7) | (tbl[i] << 6));
if (tbl[i] & (~1)) {
if (tbl[i] == 0x1010101)
st = 0x20;
else
st = 0;
break;
}
}
if (st == 0x80)
ret = SYM_ALU_B6;
if (st == 0x40)
ret = SYM_ALU_B7;
if (st == 0x20)
ret = SYM_ALU_MUL_SUM8H;
}
} while(0);
}
if (ret != SYM_INVALID) {
std::cout << get_sym_name(ret) << "@0x" << std::hex << addr;
std::cout << std::dec << std::endl;
add_sym(addr, ret);
}
/*std::cerr << "1D tables not implemented yet" << std::endl;*/
return ret;
}
int memhlp::add_sym(uint64_t addr, symbl sym) {
if (sym != SYM_INVALID)
return symbol.emplace(addr, sym).second ? 0 : -1;
else
return -1;
}
symbl memhlp::get_sym (uint64_t addr) {
auto res = symbol.find(addr);
return ((res == symbol.end()) ? SYM_INVALID : res->second);
}
bool memhlp::has_sym (symbl sym) {
for (auto &i : symbol)
if (i.second == sym) return true;
return true;
}
uint64_t memhlp::get_sym_addr (symbl sym) {
for (auto &i : symbol)
if (i.second == sym) return i.first;
return 0;
}
bool memhlp::has_sym_to (uint64_t sym) {
return symbol.count(sym);
}
int memhlp::rem_sym (uint64_t addr) {
return symbol.erase(addr) -1;
}