Skip to content

Commit

Permalink
Fix iS hash outputs (#16044) ##bin
Browse files Browse the repository at this point in the history
* Refactor bin_sections function
* Add filter_hash_string function
  • Loading branch information
abcSup committed Feb 24, 2020
1 parent e8cbbc9 commit 97498c3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 85 deletions.
96 changes: 62 additions & 34 deletions libr/core/cbin.c
Expand Up @@ -2351,37 +2351,50 @@ static int bin_symbols(RCore *r, int mode, ut64 laddr, int va, ut64 at, const ch

static char *build_hash_string(int mode, const char *chksum, ut8 *data, ut32 datalen) {
char *chkstr = NULL, *aux, *ret = NULL;
const char *ptr = chksum;
char tmp[128];
int i;
do {
for (i = 0; *ptr && *ptr != ',' && i < sizeof (tmp) -1; i++) {
tmp[i] = *ptr++;
}
tmp[i] = '\0';
r_str_trim_head_tail (tmp);
chkstr = r_hash_to_string (NULL, tmp, data, datalen);
RList *hashlist = r_str_split_duplist (chksum, ",");
RListIter *iter;
char *hashname;
r_list_foreach (hashlist, iter, hashname) {
chkstr = r_hash_to_string (NULL, hashname, data, datalen);
if (!chkstr) {
if (*ptr && *ptr == ',') {
ptr++;
}
continue;
}
if (IS_MODE_SIMPLE (mode)) {
aux = r_str_newf ("%s ", chkstr);
if (IS_MODE_SIMPLE (mode) || IS_MODE_NORMAL (mode)) {
aux = r_str_newf (iter->n? "%s " : "%s", chkstr);
} else if (IS_MODE_JSON (mode)) {
aux = r_str_newf ("\"%s\":\"%s\",", tmp, chkstr);
aux = r_str_newf ("\"%s\":\"%s\",", hashname, chkstr);
} else {
aux = r_str_newf ("%s=%s ", tmp, chkstr);
aux = r_str_newf ("%s=%s ", hashname, chkstr);
}
ret = r_str_append (ret, aux);
free (chkstr);
free (aux);
if (*ptr && *ptr == ',') {
ptr++;
}
} while (*ptr);
}
r_list_free (hashlist);
return ret;
}

static char *filter_hash_string(const char *chksum) {
if (!chksum) {
return NULL;
}

char *aux, *ret = NULL;
bool isFirst = true;
RList *hashlist = r_str_split_duplist (chksum, ",");
RListIter *iter;
char *hashname;
r_list_foreach (hashlist, iter, hashname) {
if (r_hash_name_to_bits (hashname)) {
aux = r_str_newf (isFirst? "%s" : ", %s", hashname);
ret = r_str_append (ret, aux);
free (aux);
if (isFirst) {
isFirst = false;
}
}
}
r_list_free (hashlist);
return ret;
}

Expand Down Expand Up @@ -2519,7 +2532,9 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c

if (chksum && *chksum == '.') {
printHere = true;
chksum++;
}
char *hashtypes = filter_hash_string (chksum);
if (IS_MODE_EQUAL (mode)) {
int cols = r_cons_get_size (NULL);
RList *list = r_list_newf ((RListFree) r_listinfo_free);
Expand Down Expand Up @@ -2565,8 +2580,13 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
r_flag_space_set (r->flags, print_segments? R_FLAGS_FS_SEGMENTS: R_FLAGS_FS_SECTIONS);
}
if (IS_MODE_NORMAL (mode)) {
r_table_set_columnsf (table, "dXxXxss",
"nth", "paddr", "size", "vaddr", "vsize", "perm", "name");
if (hashtypes) {
r_table_set_columnsf (table, "dXxXxsss",
"nth", "paddr", "size", "vaddr", "vsize", "perm", hashtypes, "name");
} else {
r_table_set_columnsf (table, "dXxXxss",
"nth", "paddr", "size", "vaddr", "vsize", "perm", "name");
}
// r_table_align (table, 0, R_TABLE_ALIGN_CENTER);
r_table_align (table, 2, R_TABLE_ALIGN_RIGHT);
r_table_align (table, 4, R_TABLE_ALIGN_RIGHT);
Expand Down Expand Up @@ -2713,14 +2733,14 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
}
} else if (IS_MODE_SIMPLE (mode)) {
char *hashstr = NULL;
if (chksum) {
if (hashtypes) {
ut8 *data = malloc (section->size);
if (!data) {
goto out;
}
ut32 datalen = section->size;
r_io_pread_at (r->io, section->paddr, data, datalen);
hashstr = build_hash_string (mode, chksum, data, datalen);
hashstr = build_hash_string (mode, hashtypes, data, datalen);
free (data);
}
r_cons_printf ("0x%"PFMT64x" 0x%"PFMT64x" %s %s%s%s\n",
Expand All @@ -2732,15 +2752,15 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
free (hashstr);
} else if (IS_MODE_JSON (mode)) {
char *hashstr = NULL;
if (chksum && section->size > 0) {
if (hashtypes && section->size > 0) {
ut8 *data = malloc (section->size);
if (!data) {
goto out;
}
ut32 datalen = section->size;
r_io_pread_at (r->io, section->paddr, data, datalen);
hashstr = build_hash_string (mode, chksum,
data, datalen);
hashstr = build_hash_string (mode, hashtypes,
data, datalen);
free (data);
}
r_cons_printf ("%s{\"name\":\"%s\","
Expand All @@ -2761,7 +2781,7 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
free (hashstr);
} else {
char *hashstr = NULL, str[128];
if (chksum) {
if (hashtypes) {
ut8 *data = malloc (section->size);
if (!data) {
goto out;
Expand All @@ -2771,7 +2791,7 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
if (datalen > 0) {
r_io_pread_at (r->io, section->paddr, data, datalen);
}
hashstr = build_hash_string (mode, chksum, data, datalen);
hashstr = build_hash_string (mode, hashtypes, data, datalen);
free (data);
}
if (section->arch || section->bits) {
Expand All @@ -2785,10 +2805,17 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
: section->name;
// seems like asm.bits is a bitmask that seems to be always 32,64
// const char *asmbits = r_str_sysbits (bits);
r_table_add_rowf (table, "dXxXxss", i,
(ut64)section->paddr, (ut64)section->size,
(ut64)addr, (ut64)section->vsize,
perms, name);
if (hashtypes) {
r_table_add_rowf (table, "dXxXxsss", i,
(ut64)section->paddr, (ut64)section->size,
(ut64)addr, (ut64)section->vsize,
perms, hashstr, name);
} else {
r_table_add_rowf (table, "dXxXxss", i,
(ut64)section->paddr, (ut64)section->size,
(ut64)addr, (ut64)section->vsize,
perms, name);
}
free (hashstr);
}
i++;
Expand Down Expand Up @@ -2823,6 +2850,7 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
r_cons_printf ("\n%s\n", s);
free (s);
}
free (hashtypes);
r_table_free (table);
ht_pp_free (dup_chk_ht);
return ret;
Expand Down
1 change: 0 additions & 1 deletion libr/core/cmd_info.c
Expand Up @@ -701,7 +701,6 @@ static int cmd_info(void *data, const char *input) {
name = "segments";
input++;
action = R_CORE_BIN_ACC_SEGMENTS;
param_shift = 1;
}
// case for iS=
if (input[1] == '=') {
Expand Down
86 changes: 43 additions & 43 deletions test/new/db/cmd/cmd_i
Expand Up @@ -3893,52 +3893,52 @@ EOF
EXPECT=<<EOF
[Sections]

nth paddr size vaddr vsize perm name
-------------------------------------------------
0 0x00000000 0x0 0x00000000 0x0 ----
1 0x00000154 0x13 0x08048154 0x13 -r-- .interp
2 0x00000168 0x20 0x08048168 0x20 -r-- .note.ABI_tag
3 0x00000188 0x38 0x08048188 0x38 -r-- .hash
4 0x000001c0 0x20 0x080481c0 0x20 -r-- .gnu.hash
5 0x000001e0 0x90 0x080481e0 0x90 -r-- .dynsym
6 0x00000270 0x5f 0x08048270 0x5f -r-- .dynstr
7 0x000002d0 0x12 0x080482d0 0x12 -r-- .gnu.version
8 0x000002e4 0x20 0x080482e4 0x20 -r-- .gnu.version_r
9 0x00000304 0x8 0x08048304 0x8 -r-- .rel.dyn
10 0x0000030c 0x30 0x0804830c 0x30 -r-- .rel.plt
11 0x0000033c 0x17 0x0804833c 0x17 -r-x .init
12 0x00000354 0x70 0x08048354 0x70 -r-x .plt
13 0x000003d0 0x274 0x080483d0 0x274 -r-x .text
14 0x00000644 0x1a 0x08048644 0x1a -r-x .fini
15 0x00000660 0x55 0x08048660 0x55 -r-- .rodata
16 0x000006b8 0x4 0x080486b8 0x4 -r-- .eh_frame
17 0x00000f0c 0x8 0x08049f0c 0x8 -rw- .ctors
18 0x00000f14 0x8 0x08049f14 0x8 -rw- .dtors
19 0x00000f1c 0x4 0x08049f1c 0x4 -rw- .jcr
20 0x00000f20 0xd0 0x08049f20 0xd0 -rw- .dynamic
21 0x00000ff0 0x4 0x08049ff0 0x4 -rw- .got
22 0x00000ff4 0x24 0x08049ff4 0x24 -rw- .got.plt
23 0x00001018 0xc 0x0804a018 0xc -rw- .data
24 0x00001024 0x0 0x0804a024 0x4 -rw- .bss
25 0x00001024 0x1b9 0x00000000 0x1b9 ---- .comment
26 0x000011dd 0xdb 0x00000000 0xdb ---- .shstrtab
27 0x00001740 0x460 0x00000000 0x460 ---- .symtab
28 0x00001ba0 0x248 0x00000000 0x248 ---- .strtab
nth paddr size vaddr vsize perm entropy name
------------------------------------------------------------
0 0x00000000 0x0 0x00000000 0x0 ---- 0.00000000
1 0x00000154 0x13 0x08048154 0x13 -r-- 3.68188080 .interp
2 0x00000168 0x20 0x08048168 0x20 -r-- 1.74868984 .note.ABI_tag
3 0x00000188 0x38 0x08048188 0x38 -r-- 1.23442903 .hash
4 0x000001c0 0x20 0x080481c0 0x20 -r-- 1.99254169 .gnu.hash
5 0x000001e0 0x90 0x080481e0 0x90 -r-- 1.56755604 .dynsym
6 0x00000270 0x5f 0x08048270 0x5f -r-- 4.38707564 .dynstr
7 0x000002d0 0x12 0x080482d0 0x12 -r-- 1.19417373 .gnu.version
8 0x000002e4 0x20 0x080482e4 0x20 -r-- 1.66055053 .gnu.version_r
9 0x00000304 0x8 0x08048304 0x8 -r-- 2.75000000 .rel.dyn
10 0x0000030c 0x30 0x0804830c 0x30 -r-- 2.94082791 .rel.plt
11 0x0000033c 0x17 0x0804833c 0x17 -r-x 3.46241535 .init
12 0x00000354 0x70 0x08048354 0x70 -r-x 3.51876869 .plt
13 0x000003d0 0x274 0x080483d0 0x274 -r-x 5.76534324 .text
14 0x00000644 0x1a 0x08048644 0x1a -r-x 3.79621760 .fini
15 0x00000660 0x55 0x08048660 0x55 -r-- 4.59175306 .rodata
16 0x000006b8 0x4 0x080486b8 0x4 -r-- 0.00000000 .eh_frame
17 0x00000f0c 0x8 0x08049f0c 0x8 -rw- 1.00000000 .ctors
18 0x00000f14 0x8 0x08049f14 0x8 -rw- 1.00000000 .dtors
19 0x00000f1c 0x4 0x08049f1c 0x4 -rw- 0.00000000 .jcr
20 0x00000f20 0xd0 0x08049f20 0xd0 -rw- 2.82161069 .dynamic
21 0x00000ff0 0x4 0x08049ff0 0x4 -rw- 0.00000000 .got
22 0x00000ff4 0x24 0x08049ff4 0x24 -rw- 2.98068211 .got.plt
23 0x00001018 0xc 0x0804a018 0xc -rw- 1.58496250 .data
24 0x00001024 0x0 0x0804a024 0x4 -rw- 0.00000000 .bss
25 0x00001024 0x1b9 0x00000000 0x1b9 ---- 4.50015849 .comment
26 0x000011dd 0xdb 0x00000000 0xdb ---- 4.21638420 .shstrtab
27 0x00001740 0x460 0x00000000 0x460 ---- 3.06854806 .symtab
28 0x00001ba0 0x248 0x00000000 0x248 ---- 4.85613827 .strtab

[Segments]

nth paddr size vaddr vsize perm name
-------------------------------------------------
0 0x00000034 0x120 0x08048034 0x120 -r-x PHDR
1 0x00000154 0x13 0x08048154 0x13 -r-- INTERP
2 0x00000000 0x6bc 0x08048000 0x6bc -r-x LOAD0
3 0x00000f0c 0x118 0x08049f0c 0x11c -rw- LOAD1
4 0x00000f20 0xd0 0x08049f20 0xd0 -rw- DYNAMIC
5 0x00000168 0x20 0x08048168 0x20 -r-- NOTE
6 0x00000000 0x0 0x00000000 0x0 -rw- GNU_STACK
7 0x00000f0c 0xf4 0x08049f0c 0xf4 -r-- GNU_RELRO
8 0x00000000 0x0 0x00000000 0x0 ---- NONE
9 0x00000000 0x34 0x08048000 0x34 -rw- ehdr
nth paddr size vaddr vsize perm entropy name
------------------------------------------------------------
0 0x00000034 0x120 0x08048034 0x120 -r-x 2.77947373 PHDR
1 0x00000154 0x13 0x08048154 0x13 -r-- 3.68188080 INTERP
2 0x00000000 0x6bc 0x08048000 0x6bc -r-x 5.13144413 LOAD0
3 0x00000f0c 0x118 0x08049f0c 0x11c -rw- 2.96573278 LOAD1
4 0x00000f20 0xd0 0x08049f20 0xd0 -rw- 2.82161069 DYNAMIC
5 0x00000168 0x20 0x08048168 0x20 -r-- 1.74868984 NOTE
6 0x00000000 0x0 0x00000000 0x0 -rw- 0.00000000 GNU_STACK
7 0x00000f0c 0xf4 0x08049f0c 0xf4 -r-- 2.71815661 GNU_RELRO
8 0x00000000 0x0 0x00000000 0x0 ---- 0.00000000 NONE
9 0x00000000 0x34 0x08048000 0x34 -rw- 2.79887339 ehdr

EOF
RUN
Expand Down
14 changes: 7 additions & 7 deletions test/new/db/tools/rabin2
Expand Up @@ -61,13 +61,13 @@ CMDS=!rabin2 -K md5 -S ${R2_FILE}
EXPECT=<<EOF
[Sections]

nth paddr size vaddr vsize perm name
--------------------------------------------------
0 0x00000400 0xa00 0x00401000 0x1000 -r-x .text
1 0x00000e00 0x200 0x00402000 0x1000 -rw- .data
2 0x00001000 0x200 0x00403000 0x1000 -r-- .rdata
3 0x00000000 0x0 0x00404000 0x1000 -rw- .bss
4 0x00001200 0x400 0x00405000 0x1000 -rw- .idata
nth paddr size vaddr vsize perm md5 name
-----------------------------------------------------------------------------------
0 0x00000400 0xa00 0x00401000 0x1000 -r-x 3d6a5b8dc5e41b09659638ad5735e5b5 .text
1 0x00000e00 0x200 0x00402000 0x1000 -rw- b3c542ba67f2d1a949fa1a25ce22f7e3 .data
2 0x00001000 0x200 0x00403000 0x1000 -r-- c9a877a4e945b087b8323830442c88d6 .rdata
3 0x00000000 0x0 0x00404000 0x1000 -rw- d41d8cd98f00b204e9800998ecf8427e .bss
4 0x00001200 0x400 0x00405000 0x1000 -rw- 963212dc58a74f32e0f6ffce84dcf7ea .idata

EOF
RUN
Expand Down

0 comments on commit 97498c3

Please sign in to comment.