Add checksum correction hints for DEX

This commit is contained in:
pancake 2013-04-11 01:25:25 +02:00
parent 469db755bf
commit 032860782d
7 changed files with 98 additions and 20 deletions

View File

@ -5,6 +5,8 @@
#include <r_lib.h>
#include <r_bin.h>
#include "dex/dex.h"
#define r_hash_adler32 __adler32
#include "../../hash/adler32.c"
static int load(RBinArch *arch) {
if(!(arch->bin_obj = r_bin_dex_new_buf (arch->buf)))
@ -37,8 +39,9 @@ static int check(RBinArch *arch) {
return R_FALSE;
}
static RBinInfo * info(RBinArch *arch) {
static RBinInfo *info(RBinArch *arch) {
char *version;
RBinHash *h;
RBinInfo *ret = R_NEW0 (RBinInfo);
if (!ret) return NULL;
strncpy (ret->file, arch->file, R_BIN_SIZEOF_STRINGS);
@ -53,6 +56,34 @@ static RBinInfo * info(RBinArch *arch) {
strncpy (ret->subsystem, "any", R_BIN_SIZEOF_STRINGS);
strncpy (ret->machine, "Dalvik VM", R_BIN_SIZEOF_STRINGS);
strncpy (ret->arch, "dalvik", R_BIN_SIZEOF_STRINGS);
h = &ret->sum[0];
h->type = "sha1";
h->len = 20;
h->addr = 12;
h->from = 12;
h->to = arch->buf->length-32;
memcpy (h->buf, arch->buf->buf+12, 20);
h = &ret->sum[1];
h->type = "adler32";
h->len = 4;
h->addr = 0x8;
h->from = 12;
h->to = arch->buf->length-h->from;
memcpy (h->buf, arch->buf->buf+8, 4);
{
ut32 *fc = (ut32 *)(arch->buf->buf + 8);
ut32 cc = __adler32 (arch->buf->buf + h->from, h->to);
ut8 *fb = (ut8*)fc, *cb = (ut8*)&cc;
if (*fc != cc) {
eprintf ("wx %02x%02x%02x%02x @ 0x8 "
"# Fix %02x%02x%02x%02x adler32 checksum\n",
cb[0], cb[1], cb[2], cb[3],
fb[0], fb[1], fb[2], fb[3]);
}
}
ret->lang = "java";
ret->bits = 32;
ret->big_endian = 0;
@ -334,7 +365,7 @@ static RList* sections(RBinArch *arch) {
if (arch->buf->length > ptr->rva) {
ptr->size = ptr->vsize = arch->buf->length - ptr->rva;
} else {
ptr->size = ptr->vsize = ptr->rva - arch->buf->length ;
ptr->size = ptr->vsize = ptr->rva - arch->buf->length;
// hacky workaround
eprintf ("Hack\n");
//ptr->size = ptr->vsize = 1024;

View File

@ -87,11 +87,10 @@ static int bin_strings (RCore *r, int mode, ut64 baddr, int va) {
}
static int bin_info (RCore *r, int mode) {
int i, j;
char str[R_FLAG_NAME_SIZE];
RBinInfo *info;
if ((info = r_bin_get_info (r->bin)) == NULL)
return R_FALSE;
RBinInfo *info = r_bin_get_info (r->bin);
if (!info) return R_FALSE;
if (mode & R_CORE_BIN_JSON) {
r_cons_printf ("{\"type\":\"%s\","
@ -172,7 +171,7 @@ static int bin_info (RCore *r, int mode) {
}
} else {
// if type is 'fs' show something different?
r_cons_printf ("# File info\n");
//r_cons_printf ("# File info\n");
r_cons_printf ("file\t%s\n"
"type\t%s\n"
"pic\t%s\n"
@ -203,6 +202,23 @@ static int bin_info (RCore *r, int mode) {
r_str_bool (R_BIN_DBG_SYMS (info->dbg_info)),
r_str_bool (R_BIN_DBG_RELOCS (info->dbg_info)),
info->rpath);
for (i=0; info->sum[i].type; i++) {
int len, hashchk = 1;
//ut8 *sum = &info; // XXX
RBinHash *h = &info->sum[i];
ut64 hash = r_hash_name_to_bits (h->type);
RHash *rh = r_hash_new (R_TRUE, hash);
len = r_hash_calculate (rh, hash, (const ut8*)r->bin->cur.buf+h->from, h->to);
//ut8 *p = r->bin->cur.buf+h->addr;
if (len<1) eprintf ("Invaild wtf\n");
hashchk = (!memcmp (rh->digest, h->buf, h->len));
r_hash_free (rh);
r_cons_printf ("%s\t%d-%dc\t", h->type, h->from, h->to+h->from);
for (j=0; j<h->len; j++)
r_cons_printf ("%02x", h->buf[j]);
r_cons_newline ();
}
}
}
return R_TRUE;
@ -785,15 +801,13 @@ static int bin_classes (RCore *r, int mode) {
static int bin_size (RCore *r, int mode) {
int size = r_bin_get_size (r->bin);
//if (mode & R_CORE_BIN_SET)
if ((mode & R_CORE_BIN_SIMPLE) || mode&R_CORE_BIN_JSON) {
r_cons_printf ("%d\n", size);
} else if ((mode & R_CORE_BIN_RADARE)) {
r_cons_printf ("f bin_size @ %d\n", size);
} else if ((mode & R_CORE_BIN_SET)) {
} else {
if ((mode & R_CORE_BIN_SIMPLE) || mode&R_CORE_BIN_JSON)
r_cons_printf ("%d\n", size);
}
else if ((mode & R_CORE_BIN_RADARE))
r_cons_printf ("f bin_size @ %d\n", size);
else if ((mode & R_CORE_BIN_SET))
r_core_cmdf (r, "f bin_size @ %d\n", size);
else r_cons_printf ("%d\n", size);
return R_TRUE;
}

View File

@ -1334,6 +1334,17 @@ R_API char *r_core_cmd_str_pipe(RCore *core, const char *cmd) {
return NULL;
}
R_API char *r_core_cmd_strf(RCore *core, const char *fmt, ...) {
char string[4096];
char *ret;
va_list ap;
va_start (ap, fmt);
vsnprintf (string, sizeof (string), fmt, ap);
ret = r_core_cmd_str (core, string);
va_end (ap);
return ret;
}
/* return: pointer to a buffer with the output of the command */
R_API char *r_core_cmd_str(RCore *core, const char *cmd) {
const char *static_str;

View File

@ -21,7 +21,7 @@ static void algolist(int mode) {
static int cmd_hash(void *data, const char *input) {
char *p, algo[32];
RCore *core = (RCore *)data;
ut32 i, len = core->blocksize;
ut32 i, osize, len = core->blocksize;
const char *ptr;
if (input[0]==' ') return 0;
@ -59,7 +59,11 @@ static int cmd_hash(void *data, const char *input) {
if (ptr != NULL) {
int nlen = r_num_math (core->num, ptr+1);
if (nlen>0) len = nlen;
}
osize = core->blocksize;
if (nlen>core->blocksize) {
r_core_block_size (core, nlen);
}
} else osize =0;
/* TODO: Simplify this spaguetti monster */
if (!r_str_ccmp (input, "md4", ' ')) {
RHash *ctx = r_hash_new (R_TRUE, R_HASH_MD4);
@ -68,6 +72,11 @@ static int cmd_hash(void *data, const char *input) {
r_cons_newline ();
r_hash_free (ctx);
} else
if (!r_str_ccmp (input, "adler32", ' ')) {
ut32 hn = r_hash_adler32 (core->block, len);
ut8 *b = &hn;
r_cons_printf ("%02x%02x%02x%02x\n", b[0], b[1], b[2], b[3]);
} else
if (!r_str_ccmp (input, "md5", ' ')) {
RHash *ctx = r_hash_new (R_TRUE, R_HASH_MD5);
const ut8 *c = r_hash_do_md5 (ctx, core->block, len);
@ -131,6 +140,7 @@ static int cmd_hash(void *data, const char *input) {
"Comments:\n"
" # this is a comment note the space after the sharp sign\n");
}
if (osize)
r_core_block_size (core, osize);
return 0;
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011-2012 - pancake */
/* radare - LGPL - Copyright 2011-2013 - pancake */
#include <r_fs.h>
#include "../config.h"

View File

@ -50,6 +50,16 @@ typedef struct r_bin_addr_t {
ut64 offset;
} RBinAddr;
typedef struct r_bin_hash_t {
const char *type;
ut64 addr;
int len;
ut64 from;
ut64 to;
ut8 buf[32];
const char *cmd;
} RBinHash;
typedef struct r_bin_info_t {
char file[R_BIN_SIZEOF_STRINGS];
char type[R_BIN_SIZEOF_STRINGS];
@ -66,6 +76,7 @@ typedef struct r_bin_info_t {
int has_pi; // pic/pie
int big_endian;
ut64 dbg_info;
RBinHash sum[2];
} RBinInfo;
// XXX: this is a copy of RBinObject

View File

@ -165,8 +165,9 @@ R_API int r_core_cmdf(void *user, const char *fmt, ...);
R_API int r_core_flush(void *user, const char *cmd);
R_API int r_core_cmd0(void *user, const char *cmd);
R_API void r_core_cmd_init(RCore *core);
R_API char *r_core_cmd_str(RCore *core, const char *cmd);
R_API int r_core_cmd_pipe(RCore *core, char *radare_cmd, char *shell_cmd);
R_API char *r_core_cmd_str(RCore *core, const char *cmd);
R_API char *r_core_cmd_strf(RCore *core, const char *fmt, ...);
R_API char *r_core_cmd_str_pipe(RCore *core, const char *cmd);
R_API int r_core_cmd_file(RCore *core, const char *file);
R_API int r_core_cmd_command(RCore *core, const char *command);