Implement rafind2 -F to find the contents of the file ##search (#17143)

This commit is contained in:
pancake 2020-06-29 20:11:34 +02:00 committed by GitHub
parent f061b49c14
commit 3acbe60fde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 26 deletions

View file

@ -10,7 +10,7 @@ R_API int r_hex_str2binmask(const char *in, ut8 *out, ut8 *mask);
R_API int r_hex_str2bin(const char *in, ut8 *out);
R_API int r_hex_bin2str(const ut8 *in, int len, char *out);
R_API char *r_hex_bin2strdup(const ut8 *in, int len);
R_API int r_hex_to_byte(ut8 *val, ut8 c);
R_API bool r_hex_to_byte(ut8 *val, ut8 c);
R_API int r_hex_str_is_valid(const char *s);
R_API st64 r_hex_bin_truncate(ut64 in, int n);
R_API char *r_hex_from_c(const char *code);

View file

@ -13,25 +13,25 @@
#include <r_io.h>
// XXX kill those globals
static int showstr = 0;
static int rad = 0;
static int align = 0;
static ut64 from = 0LL, to = -1;
static const char *mask = NULL;
static int nonstop = 0;
static bool showstr = false;
static bool rad = false;
static bool identify = false;
static bool quiet = false;
static bool hexstr = false;
static bool widestr = false;
static bool nonstop = false;
static bool json = false;
static int mode = R_SEARCH_STRING;
static ut64 cur = 0;
static int align = 0;
static ut8 *buf = NULL;
static const char *curfile = NULL;
static ut64 bsize = 4096;
static int hexstr = 0;
static int widestr = 0;
static ut64 from = 0LL, to = -1;
static ut64 cur = 0;
static RPrint *pr = NULL;
static RList *keywords;
static const char *mask = NULL;
static const char *curfile = NULL;
static const char *comma = "";
static bool json = false;
static int hit(RSearchKeyword *kw, void *user, ut64 addr) {
int delta = addr - cur;
@ -71,7 +71,7 @@ static int hit(RSearchKeyword *kw, void *user, ut64 addr) {
}
str[j] = 0;
} else {
int i;
size_t i;
for (i = 0; i < sizeof (_str); i++) {
char ch = buf[delta + i];
if (ch == '"' || ch == '\\') {
@ -85,7 +85,7 @@ static int hit(RSearchKeyword *kw, void *user, ut64 addr) {
str[i] = 0;
}
} else {
int i;
size_t i;
for (i = 0; i < sizeof (_str); i++) {
char ch = buf[delta + i];
if (ch == '"' || ch == '\\') {
@ -128,6 +128,7 @@ static int show_help(const char *argv0, int line) {
" -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"
" -F [file] read the contents of the file and use it as keyword\n"
" -h show this help\n"
" -i identify filetype (r2 -nqcpm file)\n"
" -j output in JSON\n"
@ -331,14 +332,14 @@ R_API int r_main_rafind2(int argc, const char **argv) {
keywords = r_list_newf (NULL);
RGetopt opt;
r_getopt_init (&opt, argc, argv, "a:ie:b:jmM:s:S:x:Xzf:t:E:rqnhvZ");
r_getopt_init (&opt, argc, argv, "a:ie:b:jmM:s:S:x:Xzf:F:t:E:rqnhvZ");
while ((c = r_getopt_next (&opt)) != -1) {
switch (c) {
case 'a':
align = r_num_math (NULL, opt.arg);
break;
case 'r':
rad = 1;
rad = true;
break;
case 'i':
identify = true;
@ -376,12 +377,6 @@ R_API int r_main_rafind2(int argc, const char **argv) {
case 'b':
bsize = r_num_math (NULL, opt.arg);
break;
case 'x':
mode = R_SEARCH_KEYWORD;
hexstr = 1;
widestr = 0;
r_list_append (keywords, (void*)opt.arg);
break;
case 'M':
// XXX should be from hexbin
mask = opt.arg;
@ -389,9 +384,33 @@ R_API int r_main_rafind2(int argc, const char **argv) {
case 'f':
from = r_num_math (NULL, opt.arg);
break;
case 'F':
{
size_t data_size;
char *data = r_file_slurp (opt.arg, &data_size);
if (!data) {
eprintf ("Cannot slurp '%s'\n", opt.arg);
return 1;
}
char *hexdata = r_hex_bin2strdup ((ut8*)data, data_size);
if (hexdata) {
mode = R_SEARCH_KEYWORD;
hexstr = true;
widestr = false;
r_list_append (keywords, (void*)hexdata);
}
free (data);
}
break;
case 't':
to = r_num_math (NULL, opt.arg);
break;
case 'x':
mode = R_SEARCH_KEYWORD;
hexstr = 1;
widestr = 0;
r_list_append (keywords, (void*)opt.arg);
break;
case 'X':
pr = r_print_new ();
break;
@ -406,7 +425,7 @@ R_API int r_main_rafind2(int argc, const char **argv) {
mode = R_SEARCH_STRING;
break;
case 'Z':
showstr = 1;
showstr = true;
break;
default:
return show_help (argv[0], 1);

View file

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2019 - pancake */
/* radare - LGPL - Copyright 2007-2020 - pancake */
#include "r_types.h"
#include "r_util.h"
@ -6,7 +6,7 @@
#include <ctype.h>
/* int c; ret = hex_to_byte(&c, 'c'); */
R_API int r_hex_to_byte(ut8 *val, ut8 c) {
R_API bool r_hex_to_byte(ut8 *val, ut8 c) {
if (IS_DIGIT (c)) {
*val = (ut8)(*val) * 16 + (c - '0');
} else if (c >= 'A' && c <= 'F') {

View file

@ -1,4 +1,4 @@
.Dd Oct 19, 2015
.Dd Jun 24, 2020
.Dt RAFIND2 1
.Sh NAME
.Nm rafind2
@ -8,6 +8,7 @@
.Op Fl ijzZXnrhqv
.Op Fl b Ar size
.Op Fl f Ar from
.Op Fl F Ar file
.Op Fl t Ar to
.Op Fl [m|s|e] Ar str
.Op Fl x Ar hex
@ -39,6 +40,8 @@ Carve for known file-types using the r_magic signatures
Set binary mask to be applied
.It Fl f Ar from
Specify the source adddress
.It Fl F Ar file
Read the keyword to search from the contents of the given file
.It Fl t Ar to
Specify the target adddress
.It Fl X

View file

@ -204,3 +204,24 @@ EXPECT_ERR=<<EOF
Cannot open empty path
EOF
RUN
NAME=rafind2 -F
FILE=-
CMDS=!rafind2 -F scripts/keyword bins/mach0/FileDP
EXPECT=<<EOF
0x4e9
0x6f5
0x6f9
0x706
0x729
0x72d
0x73b
0x75d
0x761
0x76d
0x3473
0x348f
0x34ab
EOF
RUN

1
test/scripts/keyword Normal file
View file

@ -0,0 +1 @@
lib