radare2/binr/rafind2/rafind2.c

371 lines
8.0 KiB
C
Raw Normal View History

2018-11-14 13:12:34 +01:00
/* radare - LGPL - Copyright 2009-2018 - pancake */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.c>
#include <r_types.h>
#include <r_search.h>
#include <r_util.h>
2018-11-14 13:12:34 +01:00
#include <r_util/r_print.h>
#include <r_cons.h>
#include <r_lib.h>
#include <r_io.h>
static int showstr = 0;
static int rad = 0;
2015-10-18 23:32:33 +02:00
static int align = 0;
static ut64 from = 0LL, to = -1;
2015-09-07 19:16:24 +02:00
static char *mask = NULL;
static int nonstop = 0;
static bool identify = false;
static bool quiet = false;
static int mode = R_SEARCH_STRING;
static ut64 cur = 0;
static ut8 *buf = NULL;
static char *curfile = NULL;
static ut64 bsize = 4096;
static int hexstr = 0;
static int widestr = 0;
2018-11-14 13:12:34 +01:00
static RPrint *pr = NULL;
static RList *keywords;
static int hit(RSearchKeyword *kw, void *user, ut64 addr) {
int delta = addr - cur;
if (cur > addr && (cur - addr == kw->keyword_length - 1)) {
// This case occurs when there is hit in search left over
delta = cur - addr;
}
2016-08-03 01:17:27 +02:00
if (delta < 0 || delta >= bsize) {
eprintf ("Invalid delta\n");
return 0;
}
if (rad) {
printf ("f hit%d_%d 0x%08"PFMT64x" ; %s\n", 0, kw->count, addr, curfile);
} else {
if (showstr) {
2016-08-03 01:17:27 +02:00
if (widestr) {
char *str = calloc (1, bsize);
int i, j = 0;
for (i = delta; buf[i] && i < bsize; i++) {
if (!IS_PRINTABLE (buf[i])) {
break;
}
str[j++] = buf[i++];
if (j > 80) {
2016-08-03 01:17:27 +02:00
strcpy (str + j, "...");
j += 3;
break;
}
if (buf[i]) {
break;
}
}
str[j] = 0;
printf ("0x%"PFMT64x" %s\n", addr, str);
free (str);
} else {
printf ("0x%"PFMT64x" %s\n", addr, buf + delta);
}
} else {
printf ("0x%"PFMT64x"\n", addr);
if (pr) {
2017-06-06 01:05:02 +00:00
r_print_hexdump (pr, addr, (ut8*)buf + delta, 78, 16, 1, 1);
r_cons_flush ();
}
}
}
return 1;
}
static int show_help(char *argv0, int line) {
printf ("Usage: %s [-mXnzZhqv] [-a align] [-b sz] [-f/t from/to] [-[e|s|S] str] [-x hex] file|dir ..\n", argv0);
if (line) {
return 0;
}
printf (
2015-10-18 23:32:33 +02:00
" -a [align] only accept aligned hits\n"
" -b [size] set block size\n"
" -e [regex] search for regex matches (can be used multiple times)\n"
" -f [from] start searching from address 'from'\n"
2015-10-18 23:32:33 +02:00
" -h show this help\n"
2017-08-07 03:19:15 -07:00
" -i identify filetype (r2 -nqcpm file)\n"
" -m magic search, file-type carver\n"
" -M [str] set a binary mask to be applied on keywords\n"
" -n do not stop on read errors\n"
2015-10-18 23:32:33 +02:00
" -r print using radare commands\n"
" -s [str] search for a specific string (can be used multiple times)\n"
" -S [str] search for a specific wide string (can be used multiple times)\n"
2015-10-18 23:32:33 +02:00
" -t [to] stop search at address 'to'\n"
" -q quiet - do not show headings (filenames) above matching contents (default for searching a single file)\n"
2015-10-18 23:32:33 +02:00
" -v print version and exit\n"
" -x [hex] search for hexpair string (909090) (can be used multiple times)\n"
" -X show hexdump of search results\n"
2015-10-18 23:32:33 +02:00
" -z search for zero-terminated strings\n"
2016-08-03 01:17:27 +02:00
" -Z show string found on each search hit\n"
);
return 0;
}
static int rafind_open(char *file);
static int rafind_open_file(char *file) {
RListIter *iter;
2018-10-15 16:18:51 +02:00
RIO *io = NULL;
RSearch *rs = NULL;
const char *kw;
2015-10-18 23:32:33 +02:00
bool last = false;
2018-10-15 20:48:24 +02:00
int ret, result = 0;
2018-10-15 16:18:51 +02:00
buf = NULL;
if (!quiet) {
printf ("File: %s\n", file);
}
if (identify) {
char *cmd = r_str_newf ("r2 -e search.show=false -e search.maxhits=1 -nqcpm '%s'", file);
r_sandbox_system (cmd, 1);
free (cmd);
return 0;
}
io = r_io_new ();
2018-10-15 16:18:51 +02:00
if (!io) {
return 1;
}
2018-10-15 16:18:51 +02:00
if (!r_io_open_nomap (io, file, R_PERM_R, 0)) {
eprintf ("Cannot open file '%s'\n", file);
result = 1;
goto err;
}
rs = r_search_new (mode);
if (!rs) {
2018-10-15 16:18:51 +02:00
result = 1;
goto err;
}
2018-10-15 16:18:51 +02:00
2015-10-18 23:32:33 +02:00
buf = calloc (1, bsize);
if (!buf) {
eprintf ("Cannot allocate %"PFMT64d" bytes\n", bsize);
2018-10-15 16:18:51 +02:00
result = 1;
goto err;
}
2015-10-18 23:32:33 +02:00
rs->align = align;
r_search_set_callback (rs, &hit, buf);
2016-08-03 01:17:27 +02:00
if (to == -1) {
2018-10-15 16:18:51 +02:00
to = r_io_size (io);
2016-08-03 01:17:27 +02:00
}
2018-10-15 16:18:51 +02:00
if (!r_cons_new ()) {
result = 1;
goto err;
}
if (mode == R_SEARCH_STRING) {
2016-08-03 01:17:27 +02:00
/* TODO: implement using api */
r_sys_cmdf ("rabin2 -qzzz '%s'", file);
2018-10-15 16:18:51 +02:00
goto done;
}
if (mode == R_SEARCH_MAGIC) {
char *tostr = (to && to != UT64_MAX)?
r_str_newf ("-e search.to=%"PFMT64d, to): strdup ("");
char *cmd = r_str_newf ("r2"
" -e search.in=range"
" -e search.align=%d"
" -e search.from=%"PFMT64d
" %s -qnc/m '%s'",
align, from, tostr, file);
r_sandbox_system (cmd, 1);
free (cmd);
free (tostr);
2018-10-15 16:18:51 +02:00
goto done;
}
2018-08-01 22:27:19 +02:00
if (mode == R_SEARCH_ESIL) {
r_list_foreach (keywords, iter, kw) {
2018-10-15 20:48:24 +02:00
char *cmd = r_str_newf ("r2 -qc \"/E %s\" %s", kw, file);
if (cmd) {
r_sandbox_system (cmd, 1);
free (cmd);
}
2018-08-01 22:27:19 +02:00
}
2018-10-15 16:18:51 +02:00
goto done;
2018-08-01 22:27:19 +02:00
}
if (mode == R_SEARCH_KEYWORD) {
r_list_foreach (keywords, iter, kw) {
2015-09-07 19:16:24 +02:00
if (hexstr) {
r_search_kw_add (rs, r_search_keyword_new_hex (kw, mask, NULL));
2015-09-07 19:16:24 +02:00
} else if (widestr) {
r_search_kw_add (rs, r_search_keyword_new_wide (kw, mask, NULL, 0));
2015-09-07 19:16:24 +02:00
} else {
r_search_kw_add (rs, r_search_keyword_new_str (kw, mask, NULL, 0));
2015-09-07 19:16:24 +02:00
}
}
2011-12-02 00:18:35 +01:00
} else if (mode == R_SEARCH_STRING) {
2016-08-03 01:17:27 +02:00
r_search_kw_add (rs, r_search_keyword_new_hexmask ("00", NULL)); //XXX
}
2011-12-02 00:18:35 +01:00
curfile = file;
r_search_begin (rs);
2018-10-15 16:18:51 +02:00
(void)r_io_seek (io, from, R_IO_SEEK_SET);
result = 0;
2011-12-02 00:18:35 +01:00
for (cur = from; !last && cur < to; cur += bsize) {
if ((cur + bsize) > to) {
bsize = to - cur;
2015-10-18 23:32:33 +02:00
last = true;
}
2017-05-03 17:19:49 +02:00
ret = r_io_pread_at (io, cur, buf, bsize);
if (ret == 0) {
2016-08-03 01:17:27 +02:00
if (nonstop) {
continue;
}
2018-10-15 16:18:51 +02:00
result = 1;
break;
}
2015-12-31 16:28:16 +01:00
if (ret != bsize && ret > 0) {
bsize = ret;
2015-12-31 16:28:16 +01:00
}
2011-12-02 00:18:35 +01:00
if (r_search_update (rs, cur, buf, ret) == -1) {
2011-12-02 00:18:35 +01:00
eprintf ("search: update read error at 0x%08"PFMT64x"\n", cur);
break;
}
}
2018-10-15 16:18:51 +02:00
done:
r_cons_free ();
err:
free (buf);
2018-10-15 16:18:51 +02:00
r_search_free (rs);
r_io_free (io);
return result;
}
static int rafind_open_dir(char *dir) {
RList *files;
RListIter *iter;
char *fullpath;
char *fname = NULL;
files = r_sys_dir (dir);
if (files) {
r_list_foreach (files, iter, fname) {
/* Filter-out unwanted entries */
if (*fname == '.') {
continue;
}
fullpath = r_str_newf ("%s"R_SYS_DIR"%s", dir, fname);
rafind_open (fullpath);
free (fullpath);
}
r_list_free (files);
}
return 0;
}
static int rafind_open(char *file) {
if (r_file_is_directory (file)) {
return rafind_open_dir (file);
}
return rafind_open_file (file);
}
int main(int argc, char **argv) {
int c;
keywords = r_list_new ();
2018-08-01 22:27:19 +02:00
while ((c = getopt (argc, argv, "a:ie:b:mM:s:S:x:Xzf:t:E:rqnhvZ")) != -1) {
switch (c) {
2015-10-18 23:32:33 +02:00
case 'a':
align = r_num_math (NULL, optarg);
break;
case 'r':
rad = 1;
break;
case 'i':
identify = true;
break;
case 'n':
nonstop = 1;
break;
case 'm':
mode = R_SEARCH_MAGIC;
break;
case 'e':
mode = R_SEARCH_REGEXP;
hexstr = 0;
r_list_append (keywords, optarg);
break;
2018-08-01 22:27:19 +02:00
case 'E':
mode = R_SEARCH_ESIL;
r_list_append (keywords, optarg);
break;
case 's':
mode = R_SEARCH_KEYWORD;
hexstr = 0;
widestr = 0;
r_list_append (keywords, optarg);
break;
case 'S':
mode = R_SEARCH_KEYWORD;
hexstr = 0;
widestr = 1;
2017-07-12 17:07:47 +02:00
r_list_append (keywords, optarg);
break;
case 'b':
bsize = r_num_math (NULL, optarg);
break;
case 'x':
mode = R_SEARCH_KEYWORD;
hexstr = 1;
widestr = 0;
r_list_append (keywords, optarg);
break;
case 'M':
// XXX should be from hexbin
mask = optarg;
break;
case 'f':
from = r_num_math (NULL, optarg);
break;
case 't':
to = r_num_math (NULL, optarg);
break;
case 'X':
pr = r_print_new ();
break;
case 'q':
quiet = true;
break;
2011-12-02 03:43:08 +01:00
case 'v':
printf ("rafind2 v"R2_VERSION"\n");
return 0;
case 'h':
return show_help(argv[0], 0);
case 'z':
mode = R_SEARCH_STRING;
break;
case 'Z':
showstr = 1;
break;
default:
return show_help (argv[0], 1);
}
}
2016-08-03 01:17:27 +02:00
if (optind == argc) {
return show_help (argv[0], 1);
2016-08-03 01:17:27 +02:00
}
/* Enable quiet mode if searching just a single file */
if (optind + 1 == argc && !r_file_is_directory (argv[optind])) {
quiet = true;
}
2016-08-03 01:17:27 +02:00
for (; optind < argc; optind++) {
rafind_open (argv[optind]);
2016-08-03 01:17:27 +02:00
}
return 0;
}