rz-find: add -R option to execute rizin commands (#5981)

`-E` option now executes shell commands instead
This commit is contained in:
Ashish Kumar 2026-03-21 11:19:58 +05:30 committed by GitHub
parent 004653cc87
commit 882ca0c6fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 77 additions and 7 deletions

View file

@ -39,7 +39,9 @@ Set block size
.It Fl e Ar regex
Search for regex matches (can be used multiple times)
.It Fl E Ar cmd
Execute command for each file found
Execute shell command for each file found.
.It Fl R Ar cmd
Execute Rizin command for each search hit.
.It Fl f Ar from
Start searching from address 'from'
.It Fl F Ar file

View file

@ -43,6 +43,7 @@ typedef struct {
const char *curfile;
const char *comma;
const char *exec_command;
const char *rizin_command;
} RzfindOptions;
static void rzfind_options_fini(RzfindOptions *ro) {
@ -57,6 +58,7 @@ static void rzfind_options_init(RzfindOptions *ro) {
ro->to = UT64_MAX;
ro->keywords = rz_list_newf(NULL);
ro->exec_command = NULL;
ro->rizin_command = NULL;
}
static int rzfind_open(RzfindOptions *ro, const char *file);
@ -64,6 +66,7 @@ static int rzfind_open(RzfindOptions *ro, const char *file);
typedef struct {
RzfindOptions *opt;
const char *filename;
RzCore *core;
} RzfindContext;
static int hit(RzSearchKeyword *kw, void *user, ut64 addr) {
@ -164,6 +167,15 @@ static int hit(RzSearchKeyword *kw, void *user, ut64 addr) {
free(command);
return 1;
}
if (ro->rizin_command && ctx->core) {
rz_core_seek(ctx->core, addr, true);
char *output = rz_core_cmd_str(ctx->core, ro->rizin_command);
if (output) {
printf("%s", output);
free(output);
}
return 1;
}
return 1;
}
@ -210,7 +222,7 @@ static void print_bin_string(RzBinFile *bf, RzBinString *string, RzfindOptions *
static int show_help(const char *argv0, int line) {
printf("%s%s%s", Color_CYAN, "Usage: ", Color_RESET);
printf("rz-find [-mXnzZhqvV] [-a align] [-b sz] [-f/t from/to] [-[e|s|w|S|I] str] [-x hex] -|file|dir ..\n");
printf("rz-find [-mXnzZhqvV] [-a align] [-b sz] [-f/t from/to] [-[e|s|w|S|I] str] [-x hex] [-R cmd] -|file|dir ..\n");
if (line) {
return 0;
}
@ -219,7 +231,8 @@ static int show_help(const char *argv0, int line) {
"-a", "align", "Only accept aligned hits",
"-b", "size", "Set block size",
"-e", "regex", "Search for regex matches (can be used multiple times)",
"-E", "cmd", "Execute command for each file found",
"-E", "cmd", "Execute shell command for each file found.",
"-R", "cmd", "Execute Rizin command for each search hit.",
"-f", "from", "Start searching from address 'from'",
"-F", "file", "Read the contents of the file and use it as keyword",
"-h", "", "Show this help",
@ -483,7 +496,29 @@ static int rzfind_open_file(RzfindOptions *ro, const char *file, const ut8 *data
goto err;
}
rs->align = ro->align;
RzfindContext ctx = { .opt = ro, .filename = file };
RzCore *core = NULL;
if (ro->rizin_command) {
core = rz_core_new();
if (!core) {
eprintf("Cannot allocate core for rizin command execution\n");
result = 1;
goto err;
}
rz_core_loadlibs(core, RZ_CORE_LOADLIBS_ALL);
rz_config_set_b(core->config, "scr.interactive", false);
rz_config_set_b(core->config, "scr.prompt", false);
rz_config_set_b(core->config, "cfg.debug", false);
RzCoreFile *cfile = rz_core_file_open(core, file, RZ_PERM_R, 0);
if (!cfile) {
eprintf("Cannot open file '%s' in core for rizin command\n", file);
rz_core_free(core);
core = NULL;
result = 1;
goto err;
}
rz_core_bin_load(core, NULL, UT64_MAX);
}
RzfindContext ctx = { .opt = ro, .filename = file, .core = core };
rz_search_set_callback(rs, &hit, &ctx);
ut64 to = ro->to;
if (to == -1) {
@ -753,7 +788,7 @@ RZ_API int rz_main_rz_find(int argc, const char **argv) {
}
RzGetopt opt;
rz_getopt_init(&opt, argc, argv, "a:ie:b:jmM:s:w:S:I:x:Xzf:F:t:E:rqnhvVZ");
rz_getopt_init(&opt, argc, argv, "a:ie:b:jmM:s:w:S:I:x:Xzf:F:t:E:R:rqnhvVZ");
while ((c = rz_getopt_next(&opt)) != -1) {
switch (c) {
case 'a':
@ -787,6 +822,10 @@ RZ_API int rz_main_rz_find(int argc, const char **argv) {
ro.quiet = true;
ro.exec_command = opt.arg;
break;
case 'R':
ro.quiet = true;
ro.rizin_command = opt.arg;
break;
case 's':
ro.mode = RZ_SEARCH_KEYWORD;
ro.hexstr = false;

View file

@ -521,11 +521,12 @@ NAME=rz-find -h
TOOL=rz-find
ARGS=-h
EXPECT=<<EOF
Usage: rz-find [-mXnzZhqvV] [-a align] [-b sz] [-f/t from/to] [-[e|s|w|S|I] str] [-x hex] -|file|dir ..
Usage: rz-find [-mXnzZhqvV] [-a align] [-b sz] [-f/t from/to] [-[e|s|w|S|I] str] [-x hex] [-R cmd] -|file|dir ..
-a align Only accept aligned hits
-b size  Set block size
-e regex Search for regex matches (can be used multiple times)
-E cmd  Execute command for each file found
-E cmd  Execute shell command for each file found.
-R cmd  Execute Rizin command for each search hit.
-f from  Start searching from address 'from'
-F file  Read the contents of the file and use it as keyword
-h Show this help

View file

@ -134,6 +134,34 @@ EXPECT=<<EOF
EOF
RUN
NAME=rz-find -R
FILE==
CMDS=!rz-find -s "GCC" -R "e scr.color=0 ; px 1" bins/elf/ioli/crackme0x00
EXPECT=<<EOF
0x101d
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x0000101d ff .
0x105c
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x0000105c ff .
0x109b
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x0000109b ff .
0x10da
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x000010da ff .
0x1119
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x00001119 ff .
0x1158
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x00001158 ff .
0x1197
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x00001197 ff .
EOF
RUN
NAME=rz-find -b 0xa
TOOL=rz-find
ARGS=-s 250382 -b 0xa bins/elf/ioli/crackme0x00