Implement rabin2 -zzz (dump strings to stdout, works on huge files)

This commit is contained in:
pancake 2014-10-24 18:25:27 +02:00
parent e0b20554ec
commit 131b9092c2
3 changed files with 52 additions and 22 deletions

View File

@ -83,6 +83,7 @@ static int rabin_show_help(int v) {
" -x extract bins contained in file\n"
" -z strings (from data section)\n"
" -zz strings (from raw bins [e bin.rawstr=1])\n"
" -zzz dump raw strings to stdout (for huge files)\n"
" -Z guess size of binary program\n"
);
return 1;
@ -379,7 +380,8 @@ int main(int argc, char **argv) {
r_lib_opendir (l, LIBDIR"/radare2/"R2_VERSION);
#define is_active(x) (action&x)
#define set_action(x) actions++; action |=x
#define set_action(x) actions++; action |= x
#define unset_action(x) action &= ~x
while ((c = getopt (argc, argv, "jgqAf:a:B:b:c:Ck:K:dMm:n:N:@:isSIHelRwO:o:pPrvLhxzZ")) != -1) {
switch (c) {
case 'g':
@ -425,7 +427,13 @@ int main(int argc, char **argv) {
case 'S': set_action (ACTION_SECTIONS); break;
case 'z':
if (is_active (ACTION_STRINGS)) {
rawstr = R_TRUE;
if (rawstr) {
/* rawstr mode 2 means that we are not going */
/* to store them just dump'm all to stdout */
rawstr = 2;
} else {
rawstr = R_TRUE;
}
} else set_action (ACTION_STRINGS);
break;
case 'Z': set_action (ACTION_SIZE); break;
@ -550,6 +558,9 @@ int main(int argc, char **argv) {
r_core_fini (&core);
return 0;
}
if (rawstr == 2) {
unset_action (ACTION_STRINGS);
}
r_config_set_i (core.config, "bin.rawstr", rawstr);
cf = r_core_file_open (&core, file, R_IO_READ, 0);
fd = cf ? r_core_file_cur_fd (&core) : -1;
@ -566,6 +577,11 @@ int main(int argc, char **argv) {
return 1;
}
}
if (rawstr == 2) {
rawstr = R_FALSE;
bin->minstrlen = r_config_get_i (core.config, "bin.minstr");
r_bin_dump_strings (core.bin->cur, bin->minstrlen);
}
if (query) {
if (rad) {

View File

@ -22,7 +22,7 @@ static RBinPlugin *bin_static_plugins[] = { R_BIN_STATIC_PLUGINS };
static RBinXtrPlugin *bin_xtr_static_plugins[] = { R_BIN_XTR_STATIC_PLUGINS };
static int is_data_section(RBinFile *a, RBinSection *s);
static RList* get_strings(RBinFile *a, int min);
static RList* get_strings(RBinFile *a, int min, int dump);
static void r_bin_object_delete_items (RBinObject *o);
static void r_bin_object_free (void /*RBinObject*/ *o_);
static int r_bin_object_set_items(RBinFile *binfile, RBinObject *o);
@ -116,7 +116,7 @@ static int string_scan_range (RList *list, const ut8 *buf, int min, const ut64 f
if (type == -1)
type = R_STRING_TYPE_DETECT;
if (!list || !buf || !min)
if (!buf || !min)
return -1;
while (needle < to) {
@ -179,6 +179,7 @@ static int string_scan_range (RList *list, const ut8 *buf, int min, const ut64 f
tmp[i++] = '\0';
if (runes >= min) {
if (list) {
RBinString *new = R_NEW0 (RBinString);
new->type = str_type;
new->length = runes;
@ -188,6 +189,10 @@ static int string_scan_range (RList *list, const ut8 *buf, int min, const ut64 f
if (i < sizeof (new->string))
memcpy (new->string, tmp, i);
r_list_append (list, new);
} else {
// DUMP TO STDOUT. raw dumping for rabin2 -zzz
printf ("0x%08"PFMT64x" %s\n", str_start, tmp);
}
}
}
@ -208,7 +213,7 @@ static void get_strings_range(RBinFile *arch, RList *list, int min, ut64 from, u
if (min == 0)
min = plugin? plugin->minstrlen: 4;
if (arch->rawstr)
if (arch->rawstr == R_TRUE)
min = 1;
/* Some plugins return zero, fix it up */
@ -219,9 +224,13 @@ static void get_strings_range(RBinFile *arch, RList *list, int min, ut64 from, u
if (!to || to > arch->buf->length)
to = arch->buf->length;
if (to != 0 && to > 0xf00000) {
eprintf ("WARNING: bin_strings buffer is too big at 0x%08"PFMT64x"\n", from);
return;
if (arch->rawstr != 2) {
// in case of dump ignore here
if (to != 0 && to > 0xf00000) {
eprintf ("WARNING: bin_strings buffer is too big (0x%08"PFMT64x")\n", to-from);
return;
}
}
if (string_scan_range (list, arch->buf->buf, min, from, to, -1) < 0)
@ -229,8 +238,7 @@ static void get_strings_range(RBinFile *arch, RList *list, int min, ut64 from, u
r_list_foreach (list, it, ptr) {
RBinSection *s = r_bin_get_section_at (arch->o, ptr->paddr, R_FALSE);
if (s)
ptr->vaddr = s->vaddr + (ptr->paddr - s->paddr);
if (s) ptr->vaddr = s->vaddr + (ptr->paddr - s->paddr);
}
}
@ -251,20 +259,19 @@ static int is_data_section(RBinFile *a, RBinSection *s) {
return 0;
}
static RList* get_strings(RBinFile *a, int min) {
static RList* get_strings(RBinFile *a, int min, int dump) {
RListIter *iter;
RBinSection *section;
RBinObject *o = a? a->o : NULL;
RList *ret = r_list_new ();
if (!ret) {
eprintf ("Error allocating array\n");
return NULL;
} else if (!o) {
eprintf ("Error bin object unitialized\n");
return ret;
RList *ret;
if (!o) return NULL;
if (dump) {
/* dump to stdout, not stored in list */
ret = NULL;
} else {
ret = r_list_newf (free);
if (!ret) return NULL;
}
ret->free = free;
if (o->sections && !r_list_empty (o->sections) && !a->rawstr) {
r_list_foreach (o->sections, iter, section) {
@ -280,6 +287,11 @@ static RList* get_strings(RBinFile *a, int min) {
return ret;
}
R_API int r_bin_dump_strings(RBinFile *a, int min) {
get_strings (a, min, 1);
return 0;
}
R_API int r_bin_load_languages(RBinFile *binfile) {
if (r_bin_lang_objc (binfile))
return R_BIN_NM_OBJC;
@ -355,7 +367,7 @@ static int r_bin_object_set_items(RBinFile *binfile, RBinObject *o) {
if (cp->relocs) o->relocs = cp->relocs (binfile);
if (cp->sections) o->sections = cp->sections (binfile);
if (cp->strings) o->strings = cp->strings (binfile);
else o->strings = get_strings (binfile, minlen);
else o->strings = get_strings (binfile, minlen, 0);
if (cp->symbols) o->symbols = cp->symbols (binfile);
if (cp->classes) o->classes = cp->classes (binfile);
if (cp->lines) o->lines = cp->lines (binfile);
@ -1122,7 +1134,7 @@ R_API RList* r_bin_reset_strings(RBin *bin) {
if (plugin && plugin->strings)
o->strings = plugin->strings (a);
else o->strings = get_strings (a, bin->minstrlen);
else o->strings = get_strings (a, bin->minstrlen, 0);
return o->strings;
}

View File

@ -361,6 +361,8 @@ R_API void r_bin_bind(RBin *b, RBinBind *bnd);
R_API int r_bin_add(RBin *bin, RBinPlugin *foo);
R_API int r_bin_xtr_add(RBin *bin, RBinXtrPlugin *foo);
R_API void* r_bin_free(RBin *bin);
R_API int r_bin_load_languages(RBinFile *binfile);
R_API int r_bin_dump_strings(RBinFile *a, int min);
// ref
R_API int r_bin_file_deref_by_bind (RBinBind * binb);
R_API int r_bin_file_deref (RBin *bin, RBinFile * a);