- Added generic string search support
  - Added class format autodetection
  - Minor fixup
This commit is contained in:
Nibble 2009-03-17 00:34:45 +01:00
parent 758b492739
commit f3983d9316
6 changed files with 76 additions and 25 deletions

View file

@ -19,9 +19,52 @@
static struct r_bin_handle_t *bin_static_plugins[] =
{ R_BIN_STATIC_PLUGINS };
static struct r_bin_string_t *get_strings(struct r_bin_t *bin)
static struct r_bin_string_t *get_strings(struct r_bin_t *bin, int min)
{
return NULL;
struct r_bin_string_t *ret = NULL;
u8 *buf = NULL;
u64 len, max_str = 0;
int i, matches = 0, ctr = 0;
char str[R_BIN_SIZEOF_NAMES];
len = lseek(bin->fd, 0, SEEK_END);
max_str = (u64)(len/min);
ret = malloc(max_str*sizeof(struct r_bin_string_t));
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "Error allocating file\n");
return NULL;
}
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, len);
for(i = 0; i < len && ctr < max_str; i++) {
if ((IS_PRINTABLE(buf[i]))) {
str[matches] = buf[i];
if (matches < sizeof(str))
matches++;
} else {
/* check if the length fits on our request */
if (matches >= min) {
str[matches] = '\0';
ret[ctr].offset = i-matches;
ret[ctr].size = matches;
memcpy(ret[ctr].string, str, R_BIN_SIZEOF_NAMES);
ret[ctr].string[R_BIN_SIZEOF_NAMES-1] = '\0';
ret[ctr].last = 0;
ctr++;
}
matches = 0;
}
}
ret[ctr].last = 1;
free(buf);
return ret;
}
struct r_bin_t *r_bin_new(char *file, int rw)
@ -112,7 +155,10 @@ int r_bin_autoset(struct r_bin_t *bin)
} else if (!memcmp(buf, "\x4d\x5a", 2)) {
r_bin_set(bin, "bin_pe");
return R_TRUE;
}
} else if (!memcmp(buf, "\xca\xfe\xba\xbe", 4)) {
r_bin_set(bin, "bin_java");
return R_TRUE;
}
return R_FALSE;
}
@ -185,7 +231,7 @@ struct r_bin_string_t* r_bin_get_strings(struct r_bin_t *bin)
{
if (bin->cur && bin->cur->strings)
return bin->cur->strings(bin);
else return get_strings(bin);
else return get_strings(bin, 5);
return NULL;
}

View file

@ -73,7 +73,7 @@ static unsigned short read_short(int fd)
return ntohs(sh);
}
static struct r_bin_java_cp_item_t* get_cp(struct r_bin_java_t *bin, u64 i)
static struct r_bin_java_cp_item_t* get_cp(struct r_bin_java_t *bin, unsigned short i)
{
if (i<0||i>bin->cf.cp_count)
return &cp_null_item;
@ -91,10 +91,8 @@ static int attributes_walk(struct r_bin_java_t *bin, struct r_bin_java_attr_t *a
read(fd, buf, 6);
attr->name_idx = R_BIN_JAVA_USHORT(buf,0);
attr->name = strdup((get_cp(bin, attr->name_idx-1))->value);
IFDBG {
name = (get_cp(bin, attr->name_idx-1))->value;//cp_items[R_BIN_JAVA_USHORT(buf,0)-1].value;
printf(" %2d: Name Index: %d (%s)\n", j, attr->name_idx, name);
}
name = (get_cp(bin, attr->name_idx-1))->value;//cp_items[R_BIN_JAVA_USHORT(buf,0)-1].value;
IFDBG printf(" %2d: Name Index: %d (%s)\n", j, attr->name_idx, name);
// TODO add comment with constant pool index
sz3 = R_BIN_JAVA_UINT(buf, 2);
if (fields) {
@ -230,7 +228,8 @@ static int javasm_init(struct r_bin_java_t *bin)
case 1: // utf 8 string
read(bin->fd, buf, 2);
sz = R_BIN_JAVA_USHORT(buf,0); //(buf[0]<<8)|buf[1];
//bin->cp_items[i].len = sz;
bin->cp_items[i].length = sz;
bin->cp_items[i].off += 3;
read(bin->fd, buf, sz);
buf[sz] = '\0';
break;
@ -421,6 +420,7 @@ int r_bin_java_get_strings(struct r_bin_java_t *bin, struct r_bin_java_str_t *st
if (bin->cp_items[i].tag == 1) {
str[ctr].offset = (u64)bin->cp_items[i].off;
str[ctr].ordinal = (u64)bin->cp_items[i].ord;
str[ctr].size = (u64)bin->cp_items[i].length;
memcpy(str[ctr].str, bin->cp_items[i].value, R_BIN_JAVA_MAXSTR);
ctr++;
}

View file

@ -30,6 +30,7 @@ struct r_bin_java_cp_item_t {
char name[32];
char *value;
u8 bytes[5];
unsigned short length;
unsigned short ord;
unsigned short off;
};
@ -96,6 +97,7 @@ struct r_bin_java_sym_t {
struct r_bin_java_str_t {
u64 offset;
u64 ordinal;
u64 size;
char str[R_BIN_JAVA_MAXSTR];
};

View file

@ -1,6 +1,6 @@
CFLAGS=-I../../include -I../format/ -Wall -fPIC -shared -Wl,-R..
# XXX
CFLAGS+=-DLIL_ENDIAN=1 -D__UNIX__ -g -DR_DEBUG=1
CFLAGS+=-DLIL_ENDIAN=1 -D__UNIX__ -g -DR_DEBUG=0
foo: all

View file

@ -60,7 +60,7 @@ static struct r_bin_symbol_t* symbols(struct r_bin_t *bin)
strncpy(ret[i].name, symbol[i].name, R_BIN_SIZEOF_NAMES);
strncpy(ret[i].forwarder, "NONE", R_BIN_SIZEOF_NAMES);
strncpy(ret[i].bind, "NONE", R_BIN_SIZEOF_NAMES);
strncpy(ret[i].type, "Method", R_BIN_SIZEOF_NAMES);
strncpy(ret[i].type, "FUNC", R_BIN_SIZEOF_NAMES);
ret[i].rva = ret[i].offset = symbol[i].offset;
ret[i].size = symbol[i].size;
ret[i].ordinal = 0;
@ -92,7 +92,7 @@ static struct r_bin_string_t* strings(struct r_bin_t *bin)
for (i = 0; i < strings_count; i++) {
strncpy(ret[i].string, string[i].str, R_BIN_SIZEOF_NAMES);
ret[i].rva = ret[i].offset = string[i].offset;
ret[i].size = 0;
ret[i].size = string[i].size;
ret[i].ordinal = string[i].ordinal;
ret[i].last = 0;
}

View file

@ -48,7 +48,7 @@ static int rabin_show_help()
"Available plugins:\n");
r_bin_list(&bin);
return R_FALSE;
return R_TRUE;
}
static int rabin_show_entrypoint()
@ -158,10 +158,11 @@ static int rabin_show_symbols()
if (!strncmp(symbolsp->type,"OBJECT", 6))
printf("Cd %lli @ 0x%08llx\n",
symbolsp->size, baddr+symbolsp->rva);
printf("b %lli && ", symbolsp->size);
}
printf("f sym.%s @ 0x%08llx\n",
symbolsp->name, baddr+symbolsp->rva);
printf("f sym.%s %lli @ 0x%08llx\n",
symbolsp->name, symbolsp->size,
baddr+symbolsp->rva);
} else printf("f sym.%s @ 0x%08llx\n",
symbolsp->name, baddr+symbolsp->rva);
} else printf("address=0x%08llx offset=0x%08llx ordinal=%03lli "
"forwarder=%s size=%08lli bind=%s type=%s name=%s\n",
baddr+symbolsp->rva, symbolsp->offset,
@ -203,12 +204,14 @@ static int rabin_show_strings()
while (!stringsp->last) {
if (rad) {
r_flag_name_filter(stringsp->string);
printf("f str.%s @ 0x%08llx\n",
stringsp->string, baddr+stringsp->rva);
printf( "f str.%s %lli @ 0x%08llx\n"
"Cs %lli @ 0x%08llx\n",
stringsp->string, stringsp->size, baddr+stringsp->rva,
stringsp->size, baddr+stringsp->rva);
} else printf("address=0x%08llx offset=0x%08llx ordinal=%03lli "
"string=%s\n",
"size=%08lli string=%s\n",
baddr+stringsp->rva, stringsp->offset,
stringsp->ordinal, stringsp->string);
stringsp->ordinal, stringsp->size, stringsp->string);
stringsp++; ctr++;
}
@ -437,13 +440,13 @@ int main(int argc, char **argv)
sprintf(str, "bin_%s", format);
if (!r_bin_set(&bin, str)) {
fprintf(stderr, "Unknown format\n");
return R_FALSE;
return R_TRUE;
}
free (str);
} else {
if (!r_bin_autoset(&bin)) {
fprintf(stderr, "Not supported format\n");
return R_FALSE;
return R_TRUE;
}
}
@ -462,5 +465,5 @@ int main(int argc, char **argv)
if (op != NULL && action&ACTION_OPERATION)
rabin_do_operation(op);
return R_TRUE;
return R_FALSE;
}