-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbackup.cpp
626 lines (470 loc) · 15.1 KB
/
backup.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
Patchdiff2
Portions (C) 2011 Alexander Pick
Portions (C) 2010 Nicolas Pouvesle
Portions (C) 2007 - 2009 Tenable Network Security, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ida.hpp>
#include "sig.hpp"
#include "diff.hpp"
#include "options.hpp"
#include "backup.hpp"
void buffer_serialize_data(char * buf, size_t blen, size_t * pos, void * data, size_t dsize)
{
if ( (*pos + dsize) > blen)
{
*pos = blen;
return;
}
memcpy(buf+*pos, data, dsize);
*pos += dsize;
}
void buffer_unserialize_data(char * buf, size_t blen, size_t * pos, void * data, size_t dsize)
{
if ( (*pos + dsize) > blen)
{
*pos = blen;
return;
}
memcpy(data, buf+*pos, dsize);
*pos += dsize;
}
void buffer_serialize_ea(char * buf, size_t blen, size_t * pos, ea_t ea)
{
buffer_serialize_data(buf, blen, pos, &ea, sizeof(ea));
}
void buffer_serialize_bool(char * buf, size_t blen, size_t * pos, bool b)
{
buffer_serialize_data(buf, blen, pos, &b, sizeof(b));
}
void buffer_serialize_char(char * buf, size_t blen, size_t * pos, char c)
{
buffer_serialize_data(buf, blen, pos, &c, sizeof(c));
}
void buffer_serialize_int(char * buf, size_t blen, size_t * pos, int i)
{
buffer_serialize_data(buf, blen, pos, &i, sizeof(i));
}
void buffer_serialize_long(char * buf, size_t blen, size_t * pos, unsigned long l)
{
buffer_serialize_data(buf, blen, pos, &l, sizeof(l));
}
void buffer_serialize_size(char * buf, size_t blen, size_t * pos, size_t size)
{
buffer_serialize_data(buf, blen, pos, &size, sizeof(size));
}
void buffer_unserialize_ea(char * buf, size_t blen, size_t * pos, ea_t * ea)
{
buffer_unserialize_data(buf, blen, pos, ea, sizeof(*ea));
}
void buffer_unserialize_bool(char * buf, size_t blen, size_t * pos, bool * b)
{
buffer_unserialize_data(buf, blen, pos, b, sizeof(*b));
}
void buffer_unserialize_char(char * buf, size_t blen, size_t * pos, char * c)
{
buffer_unserialize_data(buf, blen, pos, c, sizeof(*c));
}
void buffer_unserialize_int(char * buf, size_t blen, size_t * pos, int * i)
{
buffer_unserialize_data(buf, blen, pos, i, sizeof(*i));
}
void buffer_unserialize_long(char * buf, size_t blen, size_t * pos, unsigned long * l)
{
buffer_unserialize_data(buf, blen, pos, l, sizeof(*l));
}
void buffer_unserialize_size(char * buf, size_t blen, size_t * pos, size_t * size)
{
buffer_unserialize_data(buf, blen, pos, size, sizeof(*size));
}
/*------------------------------------------------*/
/* function : buffer_serialize_string */
/* description: Serializes a string */
/*------------------------------------------------*/
void buffer_serialize_string(char * buf, size_t blen, size_t * pos, char * s)
{
size_t len;
len = strlen(s) + 1;
if ( (*pos + len) > blen)
{
*pos = blen;
return;
}
memcpy(buf+*pos, s, len);
*pos += len;
}
/*------------------------------------------------*/
/* function : buffer_unserialize_string */
/* description: Unserializes a string */
/*------------------------------------------------*/
void buffer_unserialize_string(char * buf, size_t blen, size_t * pos, char ** s)
{
*s = qstrdup(buf+*pos);
*pos += strlen(*s) + 1;
}
/*------------------------------------------------*/
/* function : singleton_serialize */
/* description: Serializes a signature */
/*------------------------------------------------*/
size_t singleton_serialize(char * buf, size_t blen, psig_t * s, int nfile)
{
size_t pos = 0;
fref_t * fref = NULL;
int num, i;
buffer_serialize_ea(buf, blen, &pos, s->startEA);
buffer_serialize_int(buf, blen, &pos, s->mtype);
buffer_serialize_int(buf, blen, &pos, s->id_crc);
buffer_serialize_int(buf, blen, &pos, nfile);
buffer_serialize_long(buf, blen, &pos, s->sig);
buffer_serialize_long(buf, blen, &pos, s->hash);
buffer_serialize_long(buf, blen, &pos, s->crc_hash);
buffer_serialize_long(buf, blen, &pos, s->str_hash);
buffer_serialize_int(buf, blen, &pos, s->flag);
buffer_serialize_long(buf, blen, &pos, s->lines);
num= 0;
if (s->srefs)
{
fref = s->srefs->list;
num = s->srefs->num;
}
buffer_serialize_int(buf, blen, &pos, num);
for (i=0; i<num; i++)
{
buffer_serialize_ea(buf, blen, &pos, fref->ea);
buffer_serialize_int(buf, blen, &pos, fref->type);
fref = fref->next;
}
if (nfile == 2)
{
if (!strncmp(s->name, "sub_", 4))
{
buffer_serialize_char(buf, blen, &pos, 0);
}
else
{
buffer_serialize_char(buf, blen, &pos, 1);
buffer_serialize_string(buf, blen, &pos, s->name);
}
}
return pos;
}
/*------------------------------------------------*/
/* function : singleton_unserialize */
/* description: Serializes a signature */
/*------------------------------------------------*/
size_t singleton_unserialize(char * buf, size_t blen, psig_t ** s, int version)
{
char tmp[512];
size_t pos = 0;
char c;
int num, i;
ea_t ea;
int type;
*s = sig_init();
if (!(*s)) return 0;
buffer_unserialize_ea(buf, blen, &pos, &(*s)->startEA);
buffer_unserialize_int(buf, blen, &pos, &(*s)->mtype);
buffer_unserialize_int(buf, blen, &pos, &(*s)->id_crc);
buffer_unserialize_int(buf, blen, &pos, &(*s)->nfile);
buffer_unserialize_long(buf, blen, &pos, &(*s)->sig);
buffer_unserialize_long(buf, blen, &pos, &(*s)->hash);
buffer_unserialize_long(buf, blen, &pos, &(*s)->crc_hash);
if (version >= 2)
buffer_unserialize_long(buf, blen, &pos, &(*s)->str_hash);
if (version >= 3)
buffer_unserialize_int(buf, blen, &pos, &(*s)->flag);
buffer_unserialize_long(buf, blen, &pos, &(*s)->lines);
buffer_unserialize_int(buf, blen, &pos, &num);
for (i=0; i<num; i++)
{
buffer_unserialize_ea(buf, blen, &pos, &ea);
buffer_unserialize_int(buf, blen, &pos, &type);
sig_add_sref(*s, ea, type, CHECK_REF);
}
if ((*s)->nfile == 2)
{
buffer_unserialize_char(buf, blen, &pos, &c);
if (c == 1)
buffer_unserialize_string(buf, blen, &pos, &(*s)->name);
else
{
qsnprintf(tmp, sizeof(tmp), "sub_%lu", (unsigned long)(*s)->startEA);
sig_set_name((*s), tmp);
}
}
else
{
pget_func_name((*s)->startEA, tmp, sizeof(tmp));
sig_set_name(*s, tmp);
}
return pos;
}
/*------------------------------------------------*/
/* function : pair_serialize */
/* description: Serializes a signature and the */
/* matched signature */
/*------------------------------------------------*/
size_t pair_serialize(char * buf, size_t blen, psig_t * s)
{
size_t len;
len = singleton_serialize(buf, blen, s, 1);
len += singleton_serialize(buf+len, blen-len, s->msig, 2);
return len;
}
/*------------------------------------------------*/
/* function : pair_unserialize */
/* description: Unserializes a signature and the */
/* matched signature */
/*------------------------------------------------*/
size_t pair_unserialize(char * buf, size_t blen, psig_t ** s, int version)
{
size_t len;
len = singleton_unserialize(buf, blen, s, version);
len += singleton_unserialize(buf+len, blen-len, &(*s)->msig, version);
(*s)->msig->msig = (*s);
return len;
}
/*------------------------------------------------*/
/* function : backup_save_list */
/* description: Backups result list inside a */
/* netnode */
/*------------------------------------------------*/
void backup_save_list(char * node_name, slist_t * sl)
{
char buf[5000];
size_t i;
size_t len;
netnode node;
if (!sl) return;
node.create(node_name);
for (i=0; i<sl->num; i++)
{
nodeidx_t nidx = node.altval(i);
if (nidx != 0)
{
msg("backup failed: netnode already exists !!\n");
return;
}
netnode n(nidx);
n.create();
node.altset(i, n);
if (sl->sigs[i]->msig != NULL)
len = pair_serialize(buf, sizeof(buf), sl->sigs[i]);
else
len = singleton_serialize(buf, sizeof(buf), sl->sigs[i], sl->sigs[i]->nfile);
n.setblob(buf, len, 0, 'P');
}
}
/*------------------------------------------------*/
/* function : backup_load_list */
/* description: Loads result list from a netnode */
/*------------------------------------------------*/
bool backup_load_list(char * node_name, slist_t * sl, int type, int version)
{
char buf[5000];
size_t i;
size_t len;
netnode node;
nodeidx_t nidx;
if (!sl) return true;
node.create(node_name);
for (i=0; i<sl->org_num; i++)
{
nidx = node.altval(i);
if (nidx == BADNODE)
{
msg("backup failed: netnode does not exist !!\n");
return false;
}
netnode n(nidx);
len = sizeof(buf);
if (!n.getblob(buf, &len, 0, 'P'))
{
msg("backup failed: netnode blob does not exist !!\n");
return false;
}
if (type)
pair_unserialize(buf, len, &sl->sigs[i], version);
else
singleton_unserialize(buf, len, &sl->sigs[i], version);
}
sl->num = sl->org_num;
return true;
}
/*------------------------------------------------*/
/* function : backup_free_node */
/* description: Removes node from the IDB */
/*------------------------------------------------*/
void backup_free_node(char * node_name, size_t size)
{
netnode node;
size_t i;
node.create(node_name);
for (i=0; i<size; i++)
{
nodeidx_t nidx = node.altval(i);
netnode n(nidx);
n.delblob(0, 'P');
n.kill();
}
node.kill();
}
/*------------------------------------------------*/
/* function : backup_cleanup */
/* description: Removes results from the IDB */
/*------------------------------------------------*/
void backup_cleanup(deng_t * eng)
{
backup_free_node((char *)"$ pdiff2_matched", eng->mlist ? eng->mlist->org_num : 0);
backup_free_node((char *)"$ pdiff2_identical", eng->ilist ? eng->ilist->org_num : 0);
backup_free_node((char *)"$ pdiff2_unmatched", eng->ulist ? eng->ulist->org_num : 0);
backup_free_node((char *)"$ pdiff2_eng", 1);
}
/*------------------------------------------------*/
/* function : backup_save_eng */
/* description:Saves engine data inside a netnode */
/*------------------------------------------------*/
void backup_save_eng(char * node_name, deng_t * eng)
{
char buf[1000];
char * file;
size_t pos = 0;
netnode node;
size_t msize, isize, usize;
node.create(node_name);
nodeidx_t nidx = node.altval(0);
if (nidx != 0)
{
backup_cleanup(eng);
node.create(node_name);
nidx = node.altval(0);
if (nidx != 0)
{
msg("backup eng failed: netnode already exists !!\n");
return;
}
}
netnode n(nidx);
n.create();
node.altset(0, n);
msize = isize = usize = 0;
if (eng->mlist)
{
file = eng->mlist->file;
msize = eng->mlist->num;
}
if (eng->ilist)
{
file = eng->ilist->file;
isize = eng->ilist->num;
}
if (eng->ulist)
{
file = eng->ulist->file;
usize = eng->ulist->num;
}
buffer_serialize_int(buf, sizeof(buf), &pos, PDIFF_BACKUP_VERSION);
buffer_serialize_string(buf, sizeof(buf), &pos, file);
buffer_serialize_size(buf, sizeof(buf), &pos, msize);
buffer_serialize_size(buf, sizeof(buf), &pos, isize);
buffer_serialize_size(buf, sizeof(buf), &pos, usize);
buffer_serialize_bool(buf, sizeof(buf), &pos, eng->opt->ipc);
n.setblob(buf, pos, 0, 'P');
}
/*------------------------------------------------*/
/* function : backup_load_file */
/* description:Loads engine data inside a netnode */
/*------------------------------------------------*/
deng_t * backup_load_eng(char * node_name, options_t * opt, int * version)
{
char buf[1000];
deng_t * eng;
char * file = NULL;
size_t blen, pos = 0;
netnode node;
size_t msize, isize, usize;
node.create(node_name);
nodeidx_t nidx = node.altval(0);
if (nidx == 0)
return NULL;
netnode n(nidx);
blen = sizeof(buf);
n.getblob(buf, &blen, 0, 'P');
if (!blen)
return NULL;
eng = (deng_t *)qalloc(sizeof(*eng));
if (!eng)
return NULL;
eng->magic = 0x0BADF00D;
eng->wnum = 0;
eng->opt = opt;
msize = isize = usize = 0;
buffer_unserialize_int(buf, sizeof(buf), &pos, version);
buffer_unserialize_string(buf, sizeof(buf), &pos, &file);
buffer_unserialize_size(buf, sizeof(buf), &pos, &msize);
buffer_unserialize_size(buf, sizeof(buf), &pos, &isize);
buffer_unserialize_size(buf, sizeof(buf), &pos, &usize);
buffer_unserialize_bool(buf, sizeof(buf), &pos, &eng->opt->ipc);
// no need to save that in the IDB
eng->opt->save_db = true;
eng->mlist = siglist_init(msize, file);
eng->ilist = siglist_init(isize, file);
eng->ulist = siglist_init(usize, file);
return eng;
}
/*------------------------------------------------*/
/* function : backup_save_results */
/* description: Saves diff results inside the */
/* IDB */
/*------------------------------------------------*/
void backup_save_results(deng_t * eng)
{
backup_save_eng((char *)"$ pdiff2_eng", eng);
backup_save_list((char *)"$ pdiff2_matched", eng->mlist);
backup_save_list((char *)"$ pdiff2_identical", eng->ilist);
backup_save_list((char *)"$ pdiff2_unmatched", eng->ulist);
}
/*------------------------------------------------*/
/* function : backup_load_results */
/* description: Loads diff results from the IDB */
/*------------------------------------------------*/
int backup_load_results(deng_t ** eng, options_t * opt)
{
int ret;
int version;
if (*eng)
{
ret = askbuttons_c("Reuse", "Refresh", "Cancel", 1,
"Previous diff results have been found. Please specify the action to perform.");
if (ret == 0)
{
diff_engine_free(*eng);
*eng = NULL;
}
return ret;
}
*eng = backup_load_eng((char *)"$ pdiff2_eng", opt, &version);
if (!(*eng)) return 0;
ret = askbuttons_c("Reuse", "Refresh", "Cancel", 1,
"Previous diff results have been found. Please specify the action to perform.");
if (ret != 1) goto error;
msg("Loading backup results... ");
if (!backup_load_list((char *)"$ pdiff2_matched", (*eng)->mlist, 1, version)) goto error;
if (!backup_load_list((char *)"$ pdiff2_identical", (*eng)->ilist, 1, version)) goto error;
if (!backup_load_list((char *)"$ pdiff2_unmatched", (*eng)->ulist, 0, version)) goto error;
msg("done.\n");
return ret;
error:
diff_engine_free(*eng);
*eng = NULL;
return ret;
}