-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
cmd_search.c
3863 lines (3734 loc) · 107 KB
/
cmd_search.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
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-3.0-only
#include <ht_uu.h>
#include <rz_core.h>
#include <rz_hash.h>
#include "rz_io.h"
#include "rz_list.h"
#include "rz_types_base.h"
#include "cmd_search_rop.c"
#include "core_private.h"
#define USE_EMULATION 0
#define AES_SEARCH_LENGTH 40
#define PRIVATE_KEY_SEARCH_LENGTH 11
static const char *help_msg_search_esil[] = {
"/E", " [esil-expr]", "search offsets matching a specific esil expression",
"/Ej", " [esil-expr]", "same as above but using the given magic file",
"/E?", " ", "show this help",
"\nExamples:", "", "",
"", "/E $$,0x100001060,-,!", "hit when address is 0x100001060",
NULL
};
static const char *help_msg_slash_m[] = {
"/m", "", "search for known magic patterns",
"/m", " [file]", "same as above but using the given magic file",
"/mb", "", "search recognized RzBin headers",
NULL
};
static const char *help_msg_slash[] = {
"Usage:", "/[!bf] [arg]", "Search stuff (see 'e??search' for options)\n"
"|Use io.va for searching in non virtual addressing spaces",
"/", " foo\\x00", "search for string 'foo\\0'",
"/j", " foo\\x00", "search for string 'foo\\0' (json output)",
"/!", " ff", "search for first occurrence not matching, command modifier",
"/!x", " 00", "inverse hexa search (find first byte != 0x00)",
"/+", " /bin/sh", "construct the string with chunks",
"//", "", "repeat last search",
"/a", "[?][1aoditfmsltf] jmp eax", "assemble opcode and search its bytes",
"/b", "", "search backwards, command modifier, followed by other command",
"/c", "[?][adr]", "search for crypto materials",
"/d", " 101112", "search for a deltified sequence of bytes",
"/e", " /E.F/i", "match regular expression",
"/E", " esil-expr", "offset matching given esil expressions $$ = here",
"/f", "", "search forwards, (command modifier)",
"/F", " file [off] [sz]", "search contents of file with offset and size",
// TODO: add subcommands to find paths between functions and filter only function names instead of offsets, etc
"/g", "[g] [from]", "find all graph paths A to B (/gg follow jumps, see search.count and analysis.depth)",
"/h", "[t] [hash] [len]", "find block matching this hash. See ph",
"/i", " foo", "search for string 'foo' ignoring case",
"/m", "[?][ebm] magicfile", "search for magic, filesystems or binary headers",
"/o", " [n]", "show offset of n instructions backward",
"/O", " [n]", "same as /o, but with a different fallback if analysis cannot be used",
"/p", " patternsize", "search for pattern of given size",
"/P", " patternsize", "search similar blocks",
"/s", "[*] [threshold]", "find sections by grouping blocks with similar entropy",
"/r[erwx]", "[?] sym.printf", "analyze opcode reference an offset (/re for esil)",
"/R", " [grepopcode]", "search for matching ROP gadgets, semicolon-separated",
// moved into /as "/s", "", "search for all syscalls in a region (EXPERIMENTAL)",
"/v", "[1248] value", "look for an `cfg.bigendian` 32bit value",
"/V", "[1248] min max", "look for an `cfg.bigendian` 32bit value in range",
"/w", " foo", "search for wide string 'f\\0o\\0o\\0'",
"/wi", " foo", "search for wide string ignoring case 'f\\0o\\0o\\0'",
"/x", " ff..33", "search for hex string ignoring some nibbles",
"/x", " ff0033", "search for hex string",
"/x", " ff43:ffd0", "search for hexpair with mask",
"/z", " min max", "search for strings of given size",
"/*", " [comment string]", "add multiline comment, end it with '*/'",
#if 0
"\nConfiguration:", "", " (type `e??search.` for a complete list)",
"e", " cmd.hit = x", "command to execute on every search hit",
"e", " search.in = ?", "specify where to search stuff (depends on .from/.to)",
"e", " search.align = 4", "only catch aligned search hits",
"e", " search.from = 0", "start address",
"e", " search.to = 0", "end address",
"e", " search.flags = true", "if enabled store flags on keyword hits",
#endif
NULL
};
static const char *help_msg_slash_a[] = {
"Usage:", "/a[?] [arg]", "Search for assembly instructions matching given properties",
"/a", " push rbp", "Assemble given instruction and search the bytes",
"/a1", " [number]", "Find valid assembly generated by changing only the nth byte",
"/aI", "", "Search for infinite loop instructions (jmp $$)",
"/aa", " mov eax", "Linearly find aproximated assembly (case insensitive strstr)",
"/ac", " mov eax", "Same as /aa, but case-sensitive",
"/ad", "[/*j] push;mov", "Match ins1 followed by ins2 in linear disasm",
"/ad/", " ins1;ins2", "Search for regex instruction 'ins1' followed by regex 'ins2'",
"/ad/a", " instr", "Search for every byte instruction that matches regexp 'instr'",
"/ae", " esil", "Search for esil expressions matching substring",
"/af", "[l] family", "Search for instruction of specific family (afl=list",
"/ai", "[j] 0x300 [0x500]", "Find all the instructions using that immediate (in range)",
"/al", "", "Same as aoml, list all opcodes",
"/am", " opcode", "Search for specific instructions of specific mnemonic",
"/ao", " instr", "Search for instruction 'instr' (in all offsets)",
"/as", "[l] ([type])", "Search for syscalls (See /at swi and /af priv)",
"/at", "[l] ([type])", "Search for instructions of given type",
NULL
};
static const char *help_msg_slash_c[] = {
"Usage: /c", "", "Search for crypto materials",
"/ca", "", "Search for AES keys expanded in memory",
"/cc", "[algo] [digest]", "Find collisions (bruteforce block length values until given checksum is found)",
"/cd", "", "Search for ASN1/DER certificates",
"/cr", "", "Search for ASN1/DER private keys (RSA and ECC)",
NULL
};
static const char *help_msg_slash_r[] = {
"Usage:", "/r[acerwx] [address]", " search references to this specific address",
"/r", " [addr]", "search references to this specific address",
"/ra", "", "search all references",
"/rc", "", "search for call references",
"/re", " [addr]", "search references using esil",
"/rr", "", "Find read references",
"/rw", "", "Find write references",
"/rx", "", "Find exec references",
NULL
};
static const char *help_msg_slash_R[] = {
"Usage: /R", "", "Search for ROP gadgets",
"/R", " [filter-by-string]", "Show gadgets",
"/R/", " [filter-by-regexp]", "Show gadgets [regular expression]",
"/R/j", " [filter-by-regexp]", "JSON output [regular expression]",
"/R/q", " [filter-by-regexp]", "Show gadgets in a quiet manner [regular expression]",
"/Rj", " [filter-by-string]", "JSON output",
"/Rk", " [select-by-class]", "Query stored ROP gadgets",
"/Rq", " [filter-by-string]", "Show gadgets in a quiet manner",
NULL
};
static const char *help_msg_slash_Rk[] = {
"Usage: /Rk", "", "Query stored ROP gadgets",
"/Rk", " [nop|mov|const|arithm|arithm_ct]", "Show gadgets",
"/Rkj", "", "JSON output",
"/Rkq", "", "List Gadgets offsets",
NULL
};
static const char *help_msg_slash_x[] = {
"Usage:", "/x [hexpairs]:[binmask]", "Search in memory",
"/x ", "9090cd80", "search for those bytes",
"/x ", "9090cd80:ffff7ff0", "search with binary mask",
NULL
};
static int preludecnt = 0;
static int searchflags = 0;
static int searchshow = 0;
static const char *searchprefix = NULL;
struct search_parameters {
RzCore *core;
RzList *boundaries;
const char *mode;
const char *cmd_hit;
PJ *pj;
int outmode; // 0 or RZ_MODE_RIZINCMD or RZ_MODE_JSON
bool inverse;
bool aes_search;
bool privkey_search;
};
struct endlist_pair {
int instr_offset;
int delay_size;
};
static int search_hash(RzCore *core, const char *hashname, const char *hashstr, ut32 minlen, ut32 maxlen, struct search_parameters *param) {
RzIOMap *map;
ut8 *buf;
int i, j;
RzListIter *iter;
if (!minlen || minlen == UT32_MAX) {
minlen = core->blocksize;
}
if (!maxlen || maxlen == UT32_MAX) {
maxlen = minlen;
}
rz_cons_break_push(NULL, NULL);
for (j = minlen; j <= maxlen; j++) {
ut32 len = j;
eprintf("Searching %s for %d byte length.\n", hashname, j);
rz_list_foreach (param->boundaries, iter, map) {
if (rz_cons_is_breaked()) {
break;
}
ut64 from = map->itv.addr, to = rz_itv_end(map->itv);
st64 bufsz;
bufsz = to - from;
if (len > bufsz) {
eprintf("Hash length is bigger than range 0x%" PFMT64x "\n", from);
continue;
}
buf = malloc(bufsz);
if (!buf) {
eprintf("Cannot allocate %" PFMT64d " bytes\n", bufsz);
goto fail;
}
eprintf("Search in range 0x%08" PFMT64x " and 0x%08" PFMT64x "\n", from, to);
int blocks = (int)(to - from - len);
eprintf("Carving %d blocks...\n", blocks);
(void)rz_io_read_at(core->io, from, buf, bufsz);
for (i = 0; (from + i + len) < to; i++) {
if (rz_cons_is_breaked()) {
break;
}
char *s = rz_hash_to_string(NULL, hashname, buf + i, len);
if (!(i % 5)) {
eprintf("%d\r", i);
}
if (!s) {
eprintf("Hash fail\n");
break;
}
// eprintf ("0x%08"PFMT64x" %s\n", from+i, s);
if (!strcmp(s, hashstr)) {
eprintf("Found at 0x%" PFMT64x "\n", from + i);
rz_cons_printf("f hash.%s.%s = 0x%" PFMT64x "\n",
hashname, hashstr, from + i);
free(s);
free(buf);
return 1;
}
free(s);
}
free(buf);
}
}
rz_cons_break_pop();
eprintf("No hashes found\n");
return 0;
fail:
return -1;
}
static void cmd_search_bin(RzCore *core, RzInterval itv) {
ut64 from = itv.addr, to = rz_itv_end(itv);
int size; // , sz = sizeof (buf);
int fd = core->file->fd;
RzBuffer *b = rz_buf_new_with_io(&core->analysis->iob, fd);
rz_cons_break_push(NULL, NULL);
while (from < to) {
if (rz_cons_is_breaked()) {
break;
}
RzBuffer *ref = rz_buf_new_slice(b, from, to);
RzBinPlugin *plug = rz_bin_get_binplugin_by_buffer(core->bin, ref);
if (plug) {
rz_cons_printf("0x%08" PFMT64x " %s\n", from, plug->name);
if (plug->size) {
RzBinOptions opt = {
.pluginname = plug->name,
.baseaddr = 0,
.loadaddr = 0,
.sz = 4096,
.xtr_idx = 0,
.rawstr = core->bin->rawstr,
.fd = fd,
};
rz_bin_open_io(core->bin, &opt);
size = plug->size(core->bin->cur);
if (size > 0) {
rz_cons_printf("size %d\n", size);
}
}
}
rz_buf_free(ref);
from++;
}
rz_buf_free(b);
rz_cons_break_pop();
}
static int __prelude_cb_hit(RzSearchKeyword *kw, void *user, ut64 addr) {
RzCore *core = (RzCore *)user;
int depth = rz_config_get_i(core->config, "analysis.depth");
// eprintf ("ap: Found function prelude %d at 0x%08"PFMT64x"\n", preludecnt, addr);
rz_core_analysis_fcn(core, addr, -1, RZ_ANALYSIS_REF_TYPE_NULL, depth);
preludecnt++;
return 1;
}
RZ_API int rz_core_search_prelude(RzCore *core, ut64 from, ut64 to, const ut8 *buf, int blen, const ut8 *mask, int mlen) {
ut64 at;
ut8 *b = (ut8 *)malloc(core->blocksize);
if (!b) {
return 0;
}
// TODO: handle sections ?
if (from >= to) {
eprintf("aap: Invalid search range 0x%08" PFMT64x " - 0x%08" PFMT64x "\n", from, to);
free(b);
return 0;
}
rz_search_reset(core->search, RZ_SEARCH_KEYWORD);
rz_search_kw_add(core->search, rz_search_keyword_new(buf, blen, mask, mlen, NULL));
rz_search_begin(core->search);
rz_search_set_callback(core->search, &__prelude_cb_hit, core);
preludecnt = 0;
for (at = from; at < to; at += core->blocksize) {
if (rz_cons_is_breaked()) {
break;
}
if (!rz_io_is_valid_offset(core->io, at, 0)) {
break;
}
(void)rz_io_read_at(core->io, at, b, core->blocksize);
if (rz_search_update(core->search, at, b, core->blocksize) == -1) {
eprintf("search: update read error at 0x%08" PFMT64x "\n", at);
break;
}
}
// rz_search_reset might also benifet from having an if(s->data) RZ_FREE(s->data), but im not sure.
//add a commit that puts it in there to this PR if it wouldn't break anything. (don't have to worry about this happening again, since all searches start by resetting core->search)
//For now we will just use rz_search_kw_reset
rz_search_kw_reset(core->search);
free(b);
return preludecnt;
}
static int count_functions(RzCore *core) {
return rz_list_length(core->analysis->fcns);
}
RZ_API int rz_core_search_preludes(RzCore *core, bool log) {
int ret = -1;
const char *prelude = rz_config_get(core->config, "analysis.prelude");
ut64 from = UT64_MAX;
ut64 to = UT64_MAX;
const char *where = rz_config_get(core->config, "analysis.in");
RzList *list = rz_core_get_boundaries_prot(core, RZ_PERM_X, where, "search");
RzListIter *iter;
RzIOMap *p;
if (!list) {
return -1;
}
int fc0 = count_functions(core);
rz_list_foreach (list, iter, p) {
if (log) {
eprintf("\r[>] Scanning %s 0x%" PFMT64x " - 0x%" PFMT64x " ",
rz_str_rwx_i(p->perm), p->itv.addr, rz_itv_end(p->itv));
if (!(p->perm & RZ_PERM_X)) {
eprintf("skip\n");
continue;
}
}
from = p->itv.addr;
to = rz_itv_end(p->itv);
if (prelude && *prelude) {
ut8 *kw = malloc(strlen(prelude) + 1);
int kwlen = rz_hex_str2bin(prelude, kw);
ret = rz_core_search_prelude(core, from, to, kw, kwlen, NULL, 0);
free(kw);
} else {
RzList *preds = rz_analysis_preludes(core->analysis);
if (preds) {
RzListIter *iter;
RzSearchKeyword *kw;
rz_list_foreach (preds, iter, kw) {
ret = rz_core_search_prelude(core, from, to,
kw->bin_keyword, kw->keyword_length,
kw->bin_binmask, kw->binmask_length);
}
} else {
if (log) {
eprintf("ap: Unsupported asm.arch and asm.bits\n");
}
}
}
if (log) {
eprintf("done\n");
}
}
int fc1 = count_functions(core);
if (log) {
if (list) {
eprintf("Analyzed %d functions based on preludes\n", fc1 - fc0);
} else {
eprintf("No executable section found, cannot analyze anything. Use 'S' to change or define permissions of sections\n");
}
}
rz_list_free(list);
return ret;
}
/* TODO: maybe move into util/str */
static char *getstring(char *b, int l) {
char *r, *res = malloc(l + 1);
int i;
if (!res) {
return NULL;
}
for (i = 0, r = res; i < l; b++, i++) {
if (IS_PRINTABLE(*b)) {
*r++ = *b;
}
}
*r = 0;
return res;
}
static int _cb_hit(RzSearchKeyword *kw, void *user, ut64 addr) {
struct search_parameters *param = user;
RzCore *core = param->core;
const RzSearch *search = core->search;
ut64 base_addr = 0;
bool use_color = core->print->flags & RZ_PRINT_FLAGS_COLOR;
int keyword_len = kw ? kw->keyword_length + (search->mode == RZ_SEARCH_DELTAKEY) : 0;
if (searchshow && kw && kw->keyword_length > 0) {
int len, i, extra, mallocsize;
char *s = NULL, *str = NULL, *p = NULL;
extra = (param->outmode == RZ_MODE_JSON) ? 3 : 1;
const char *type = "hexpair";
ut8 *buf = malloc(keyword_len);
if (!buf) {
return 0;
}
switch (kw->type) {
case RZ_SEARCH_KEYWORD_TYPE_STRING: {
const int ctx = 16;
const int prectx = addr > 16 ? ctx : addr;
char *pre, *pos, *wrd;
const int len = keyword_len;
char *buf = calloc(1, len + 32 + ctx * 2);
type = "string";
rz_io_read_at(core->io, addr - prectx, (ut8 *)buf, len + (ctx * 2));
pre = getstring(buf, prectx);
pos = getstring(buf + prectx + len, ctx);
if (!pos) {
pos = strdup("");
}
if (param->outmode == RZ_MODE_JSON) {
wrd = getstring(buf + prectx, len);
s = rz_str_newf("%s%s%s", pre, wrd, pos);
} else {
wrd = rz_str_utf16_encode(buf + prectx, len);
s = rz_str_newf(use_color ? ".%s" Color_YELLOW "%s" Color_RESET "%s."
: "\"%s%s%s\"",
pre, wrd, pos);
}
free(buf);
free(pre);
free(wrd);
free(pos);
}
free(p);
break;
default:
len = keyword_len; // 8 byte context
mallocsize = (len * 2) + extra;
str = (len > 0xffff) ? NULL : malloc(mallocsize);
if (str) {
p = str;
memset(str, 0, len);
rz_io_read_at(core->io, base_addr + addr, buf, keyword_len);
if (param->outmode == RZ_MODE_JSON) {
p = str;
}
const int bytes = (len > 40) ? 40 : len;
for (i = 0; i < bytes; i++) {
sprintf(p, "%02x", buf[i]);
p += 2;
}
if (bytes != len) {
strcpy(p, "...");
p += 3;
}
*p = 0;
} else {
eprintf("Cannot allocate %d\n", mallocsize);
}
s = str;
str = NULL;
break;
}
if (param->outmode == RZ_MODE_JSON) {
pj_o(param->pj);
pj_kN(param->pj, "offset", base_addr + addr);
pj_ks(param->pj, "type", type);
pj_ks(param->pj, "data", s);
pj_end(param->pj);
} else {
rz_cons_printf("0x%08" PFMT64x " %s%d_%d %s\n",
base_addr + addr, searchprefix, kw->kwidx, kw->count, s);
}
free(s);
free(buf);
free(str);
} else if (kw) {
if (param->outmode == RZ_MODE_JSON) {
pj_o(param->pj);
pj_kN(param->pj, "offset", base_addr + addr);
pj_ki(param->pj, "len", keyword_len);
pj_end(param->pj);
} else {
if (searchflags) {
rz_cons_printf("%s%d_%d\n", searchprefix, kw->kwidx, kw->count);
} else {
rz_cons_printf("f %s%d_%d %d 0x%08" PFMT64x "\n", searchprefix,
kw->kwidx, kw->count, keyword_len, base_addr + addr);
}
}
}
if (searchflags && kw) {
const char *flag = sdb_fmt("%s%d_%d", searchprefix, kw->kwidx, kw->count);
rz_flag_set(core->flags, flag, base_addr + addr, keyword_len);
}
if (*param->cmd_hit) {
ut64 here = core->offset;
rz_core_seek(core, base_addr + addr, true);
rz_core_cmd(core, param->cmd_hit, 0);
rz_core_seek(core, here, true);
}
return true;
}
static int c = 0;
static inline void print_search_progress(ut64 at, ut64 to, int n, struct search_parameters *param) {
if ((++c % 64) || (param->outmode == RZ_MODE_JSON)) {
return;
}
if (rz_cons_singleton()->columns < 50) {
eprintf("\r[ ] 0x%08" PFMT64x " hits = %d \r%s",
at, n, (c % 2) ? "[ #]" : "[# ]");
} else {
eprintf("\r[ ] 0x%08" PFMT64x " < 0x%08" PFMT64x " hits = %d \r%s",
at, to, n, (c % 2) ? "[ #]" : "[# ]");
}
}
static void append_bound(RzList *list, RzIO *io, RzInterval search_itv, ut64 from, ut64 size, int perms) {
RzIOMap *map = RZ_NEW0(RzIOMap);
if (!map) {
return;
}
if (io && io->desc) {
map->fd = rz_io_fd_get_current(io);
}
map->perm = perms;
RzInterval itv = { from, size };
if (size == -1) {
eprintf("Warning: Invalid range. Use different search.in=? or analysis.in=dbg.maps.x\n");
free(map);
return;
}
// TODO UT64_MAX is a valid address. search.from and search.to are not specified
if (search_itv.addr == UT64_MAX && !search_itv.size) {
map->itv = itv;
rz_list_append(list, map);
} else if (rz_itv_overlap(itv, search_itv)) {
map->itv = rz_itv_intersect(itv, search_itv);
if (map->itv.size) {
rz_list_append(list, map);
} else {
free(map);
}
} else {
free(map);
}
}
static bool maskMatches(int perm, int mask, bool only) {
if (mask) {
if (only) {
return ((perm & 7) != mask);
}
return (perm & mask) != mask;
}
return false;
}
RZ_API RzList *rz_core_get_boundaries_prot(RzCore *core, int perm, const char *mode, const char *prefix) {
rz_return_val_if_fail(core, NULL);
RzList *list = rz_list_newf(free); // XXX rz_io_map_free);
if (!list) {
return NULL;
}
char bound_in[32];
char bound_from[32];
char bound_to[32];
snprintf(bound_in, sizeof(bound_in), "%s.%s", prefix, "in");
snprintf(bound_from, sizeof(bound_from), "%s.%s", prefix, "from");
snprintf(bound_to, sizeof(bound_to), "%s.%s", prefix, "to");
const ut64 search_from = rz_config_get_i(core->config, bound_from),
search_to = rz_config_get_i(core->config, bound_to);
const RzInterval search_itv = { search_from, search_to - search_from };
if (!mode) {
mode = rz_config_get(core->config, bound_in);
}
if (!rz_config_get_i(core->config, "cfg.debug") && !core->io->va) {
append_bound(list, core->io, search_itv, 0, rz_io_size(core->io), 7);
} else if (!strcmp(mode, "file")) {
append_bound(list, core->io, search_itv, 0, rz_io_size(core->io), 7);
} else if (!strcmp(mode, "block")) {
append_bound(list, core->io, search_itv, core->offset, core->blocksize, 7);
} else if (!strcmp(mode, "io.map")) {
RzIOMap *m = rz_io_map_get(core->io, core->offset);
if (m) {
append_bound(list, core->io, search_itv, m->itv.addr, m->itv.size, m->perm);
}
} else if (!strcmp(mode, "io.maps")) { // Non-overlapping RzIOMap parts not overridden by others (skyline)
ut64 begin = UT64_MAX;
ut64 end = UT64_MAX;
#define USE_SKYLINE 0
#if USE_SKYLINE
const RzPVector *skyline = &core->io->map_skyline;
size_t i;
for (i = 0; i < rz_pvector_len(skyline); i++) {
const RzIOMapSkyline *part = rz_pvector_at(skyline, i);
ut64 from = rz_itv_begin(part->itv);
ut64 to = rz_itv_end(part->itv);
// XXX skyline's fake map perms are wrong
RzIOMap *m = rz_io_map_get(core->io, from);
int rwx = m ? m->perm : part->map->perm;
#else
void **it;
rz_pvector_foreach (&core->io->maps, it) {
RzIOMap *map = *it;
ut64 from = rz_itv_begin(map->itv);
ut64 to = rz_itv_end(map->itv);
int rwx = map->perm;
#endif
// eprintf ("--------- %llx %llx (%llx %llx)\n", from, to, begin, end);
if (begin == UT64_MAX) {
begin = from;
}
if (end == UT64_MAX) {
end = to;
} else {
if (end == from) {
end = to;
} else {
append_bound(list, NULL, search_itv,
begin, end - begin, rwx);
begin = from;
end = to;
}
}
}
if (end != UT64_MAX) {
append_bound(list, NULL, search_itv, begin, end - begin, 7);
}
} else if (rz_str_startswith(mode, "io.maps.")) {
int len = strlen("io.maps.");
int mask = (mode[len - 1] == '.') ? rz_str_rwx(mode + len) : 0;
// bool only = (bool)(size_t)strstr (mode, ".only");
void **it;
rz_pvector_foreach (&core->io->maps, it) {
RzIOMap *map = *it;
ut64 from = rz_itv_begin(map->itv);
//ut64 to = rz_itv_end (map->itv);
int rwx = map->perm;
if ((rwx & mask) != mask) {
continue;
}
append_bound(list, core->io, search_itv, from, rz_itv_size(map->itv), rwx);
}
} else if (rz_str_startswith(mode, "io.sky.")) {
int len = strlen("io.sky.");
int mask = (mode[len - 1] == '.') ? rz_str_rwx(mode + len) : 0;
bool only = (bool)(size_t)strstr(mode, ".only");
RzVector *skyline = &core->io->map_skyline.v;
ut64 begin = UT64_MAX;
ut64 end = UT64_MAX;
size_t i;
for (i = 0; i < rz_vector_len(skyline); i++) {
const RzSkylineItem *part = rz_vector_index_ptr(skyline, i);
ut64 from = part->itv.addr;
ut64 to = part->itv.addr + part->itv.size;
int perm = ((RzIOMap *)part->user)->perm;
if (maskMatches(perm, mask, only)) {
continue;
}
// eprintf ("--------- %llx %llx (%llx %llx)\n", from, to, begin, end);
if (begin == UT64_MAX) {
begin = from;
}
if (end == UT64_MAX) {
end = to;
} else {
if (end == from) {
end = to;
} else {
// eprintf ("[%llx - %llx]\n", begin, end);
append_bound(list, NULL, search_itv, begin, end - begin, perm);
begin = from;
end = to;
}
}
}
if (end != UT64_MAX) {
append_bound(list, NULL, search_itv, begin, end - begin, 7);
}
} else if (rz_str_startswith(mode, "bin.segments")) {
int len = strlen("bin.segments.");
int mask = (mode[len - 1] == '.') ? rz_str_rwx(mode + len) : 0;
bool only = (bool)(size_t)strstr(mode, ".only");
RzBinObject *obj = rz_bin_cur_object(core->bin);
if (obj) {
RzBinSection *s;
RzListIter *iter;
rz_list_foreach (obj->sections, iter, s) {
if (!s->is_segment) {
continue;
}
if (maskMatches(s->perm, mask, only)) {
continue;
}
ut64 addr = core->io->va ? s->vaddr : s->paddr;
ut64 size = core->io->va ? s->vsize : s->size;
append_bound(list, core->io, search_itv, addr, size, s->perm);
}
}
} else if (rz_str_startswith(mode, "code")) {
RzBinObject *obj = rz_bin_cur_object(core->bin);
if (obj) {
ut64 from = UT64_MAX;
ut64 to = 0;
RzBinSection *s;
RzListIter *iter;
rz_list_foreach (obj->sections, iter, s) {
if (s->is_segment) {
continue;
}
if (maskMatches(s->perm, 1, false)) {
continue;
}
ut64 addr = core->io->va ? s->vaddr : s->paddr;
ut64 size = core->io->va ? s->vsize : s->size;
from = RZ_MIN(addr, from);
to = RZ_MAX(to, addr + size);
}
if (from == UT64_MAX) {
int mask = 1;
void **it;
rz_pvector_foreach (&core->io->maps, it) {
RzIOMap *map = *it;
ut64 from = rz_itv_begin(map->itv);
ut64 size = rz_itv_size(map->itv);
int rwx = map->perm;
if ((rwx & mask) != mask) {
continue;
}
append_bound(list, core->io, search_itv, from, size, rwx);
}
}
append_bound(list, core->io, search_itv, from, to - from, 1);
}
} else if (rz_str_startswith(mode, "bin.sections")) {
int len = strlen("bin.sections.");
int mask = (mode[len - 1] == '.') ? rz_str_rwx(mode + len) : 0;
bool only = (bool)(size_t)strstr(mode, ".only");
RzBinObject *obj = rz_bin_cur_object(core->bin);
if (obj) {
RzBinSection *s;
RzListIter *iter;
rz_list_foreach (obj->sections, iter, s) {
if (s->is_segment) {
continue;
}
if (maskMatches(s->perm, mask, only)) {
continue;
}
ut64 addr = core->io->va ? s->vaddr : s->paddr;
ut64 size = core->io->va ? s->vsize : s->size;
append_bound(list, core->io, search_itv, addr, size, s->perm);
}
}
} else if (!strcmp(mode, "bin.segment")) {
RzBinObject *obj = rz_bin_cur_object(core->bin);
if (obj) {
RzBinSection *s;
RzListIter *iter;
rz_list_foreach (obj->sections, iter, s) {
if (!s->is_segment) {
continue;
}
ut64 addr = core->io->va ? s->vaddr : s->paddr;
ut64 size = core->io->va ? s->vsize : s->size;
if (RZ_BETWEEN(addr, core->offset, addr + size)) {
append_bound(list, core->io, search_itv, addr, size, s->perm);
}
}
}
} else if (!strcmp(mode, "bin.section")) {
RzBinObject *obj = rz_bin_cur_object(core->bin);
if (obj) {
RzBinSection *s;
RzListIter *iter;
rz_list_foreach (obj->sections, iter, s) {
if (s->is_segment) {
continue;
}
ut64 addr = core->io->va ? s->vaddr : s->paddr;
ut64 size = core->io->va ? s->vsize : s->size;
if (RZ_BETWEEN(addr, core->offset, addr + size)) {
append_bound(list, core->io, search_itv, addr, size, s->perm);
}
}
}
} else if (!strcmp(mode, "analysis.fcn") || !strcmp(mode, "analysis.bb")) {
RzAnalysisFunction *f = rz_analysis_get_fcn_in(core->analysis, core->offset,
RZ_ANALYSIS_FCN_TYPE_FCN | RZ_ANALYSIS_FCN_TYPE_SYM);
if (f) {
ut64 from = f->addr, size = rz_analysis_function_size_from_entry(f);
/* Search only inside the basic block */
if (!strcmp(mode, "analysis.bb")) {
RzListIter *iter;
RzAnalysisBlock *bb;
rz_list_foreach (f->bbs, iter, bb) {
ut64 at = core->offset;
if ((at >= bb->addr) && (at < (bb->addr + bb->size))) {
from = bb->addr;
size = bb->size;
break;
}
}
}
append_bound(list, core->io, search_itv, from, size, 5);
} else {
eprintf("WARNING: search.in = ( analysis.bb | analysis.fcn )"
"requires to seek into a valid function\n");
append_bound(list, core->io, search_itv, core->offset, 1, 5);
}
} else if (!strncmp(mode, "dbg.", 4)) {
if (core->bin->is_debugger) {
int mask = 0;
int add = 0;
bool heap = false;
bool stack = false;
bool all = false;
bool first = false;
RzListIter *iter;
RzDebugMap *map;
rz_debug_map_sync(core->dbg);
if (!strcmp(mode, "dbg.map")) {
int perm = 0;
ut64 from = core->offset;
ut64 to = core->offset;
rz_list_foreach (core->dbg->maps, iter, map) {
if (from >= map->addr && from < map->addr_end) {
from = map->addr;
to = map->addr_end;
perm = map->perm;
break;
}
}
if (perm) {
RzIOMap *nmap = RZ_NEW0(RzIOMap);
if (nmap) {
// nmap->fd = core->io->desc->fd;
nmap->itv.addr = from;
nmap->itv.size = to - from;
nmap->perm = perm;
nmap->delta = 0;
rz_list_append(list, nmap);
}
}
} else {
bool only = false;
mask = 0;
if (!strcmp(mode, "dbg.program")) {
first = true;
mask = RZ_PERM_X;
} else if (!strcmp(mode, "dbg.maps")) {
all = true;
} else if (rz_str_startswith(mode, "dbg.maps.")) {
mask = rz_str_rwx(mode + 9);
only = (bool)(size_t)strstr(mode, ".only");
} else if (!strcmp(mode, "dbg.heap")) {
heap = true;
} else if (!strcmp(mode, "dbg.stack")) {
stack = true;
}
ut64 from = UT64_MAX;
ut64 to = 0;
rz_list_foreach (core->dbg->maps, iter, map) {
if (!all && maskMatches(map->perm, mask, only)) {
continue;
}
add = (stack && strstr(map->name, "stack")) ? 1 : 0;
if (!add && (heap && (map->perm & RZ_PERM_W)) && strstr(map->name, "heap")) {
add = 1;
}
if ((mask && (map->perm & mask)) || add || all) {
if (!list) {
list = rz_list_newf(free);
}
RzIOMap *nmap = RZ_NEW0(RzIOMap);
if (!nmap) {
break;
}
nmap->itv.addr = map->addr;
nmap->itv.size = map->addr_end - map->addr;
if (nmap->itv.addr) {
from = RZ_MIN(from, nmap->itv.addr);
to = RZ_MAX(to - 1, rz_itv_end(nmap->itv) - 1) + 1;
}
nmap->perm = map->perm;
nmap->delta = 0;
rz_list_append(list, nmap);
if (first) {
break;
}
}
}
}
}
} else {
/* obey temporary seek if defined '/x 8080 @ addr:len' */
if (core->tmpseek) {
append_bound(list, core->io, search_itv, core->offset, core->blocksize, 5);
} else {
// TODO: repeat last search doesnt works for /a
ut64 from = rz_config_get_i(core->config, bound_from);
if (from == UT64_MAX) {
from = core->offset;
}
ut64 to = rz_config_get_i(core->config, bound_to);
if (to == UT64_MAX) {
if (core->io->va) {
/* TODO: section size? */
} else {
if (core->file) {
to = rz_io_fd_size(core->io, core->file->fd);
}
}
}
append_bound(list, core->io, search_itv, from, to - from, 5);
}
}
return list;
}
static bool is_end_gadget(const RzAnalysisOp *aop, const ut8 crop) {
if (aop->family == RZ_ANALYSIS_OP_FAMILY_SECURITY) {
return false;
}
switch (aop->type) {
case RZ_ANALYSIS_OP_TYPE_TRAP:
case RZ_ANALYSIS_OP_TYPE_RET:
case RZ_ANALYSIS_OP_TYPE_UCALL:
case RZ_ANALYSIS_OP_TYPE_RCALL:
case RZ_ANALYSIS_OP_TYPE_ICALL:
case RZ_ANALYSIS_OP_TYPE_IRCALL:
case RZ_ANALYSIS_OP_TYPE_UJMP:
case RZ_ANALYSIS_OP_TYPE_RJMP:
case RZ_ANALYSIS_OP_TYPE_IJMP:
case RZ_ANALYSIS_OP_TYPE_IRJMP:
case RZ_ANALYSIS_OP_TYPE_JMP:
case RZ_ANALYSIS_OP_TYPE_CALL:
return true;
}
if (crop) { // if conditional jumps, calls and returns should be used for the gadget-search too
switch (aop->type) {
case RZ_ANALYSIS_OP_TYPE_CJMP:
case RZ_ANALYSIS_OP_TYPE_UCJMP:
case RZ_ANALYSIS_OP_TYPE_CCALL:
case RZ_ANALYSIS_OP_TYPE_UCCALL:
case RZ_ANALYSIS_OP_TYPE_CRET:
return true;
}
}
return false;
}
static bool insert_into(void *user, const ut64 k, const ut64 v) {
HtUU *ht = (HtUU *)user;
ht_uu_insert(ht, k, v);
return true;
}
// TODO: follow unconditional jumps
static RzList *construct_rop_gadget(RzCore *core, ut64 addr, ut8 *buf, int buflen, int idx, const char *grep, int regex, RzList *rx_list, struct endlist_pair *end_gadget, HtUU *badstart) {
int endaddr = end_gadget->instr_offset;