* Fix segfault in r_cons_memcat

* Add yp command and document yt
* Add p6d and p6e to decode and encode base64 strings
* Remove vm.* config vars
* Fix RNum evaulation for $ variables
* Implement r_fs_posix
This commit is contained in:
pancake 2011-02-23 02:10:28 +01:00
parent 130d088a2c
commit dc3d11d7d5
11 changed files with 153 additions and 82 deletions

15
TODO
View File

@ -20,13 +20,12 @@ Random stuff
SYSENTER: Direct system calls. Commonly, used as antiemulation tricks. SYSENTER: Direct system calls. Commonly, used as antiemulation tricks.
* implement aoe = anal op exec * implement aoe = anal op exec
- sync regs or what? - sync regs or what?
* Search for wide strings /Z or so? /w maybe?
pancake pancake
------- -------
* mount in / results problematic sometimes.. :/
* if console width > X place comments there (ash) * if console width > X place comments there (ash)
* Implement BLOCK in r_core_sysenv_begin|end () * Implement BLOCK in r_core_sysenv_begin|end ()
* comparisions doesnt works (RAnalCond)
* Fix iterators for r_macro (test only?) * Fix iterators for r_macro (test only?)
* Add support for STATIC_PLUGINS in r_lang * Add support for STATIC_PLUGINS in r_lang
- r_lang_define is implemented in lang.c, but requires the collaboration - r_lang_define is implemented in lang.c, but requires the collaboration
@ -59,18 +58,10 @@ earada
nibble nibble
------ ------
* scrollup by bwdisasm not yet implemented * comparisions doesnt works (RAnalCond)
* '+' key in visual cursor mode only increments lower nibble!
- only in debugger mode :/
* register renaming (per-instruction or ranges) * register renaming (per-instruction or ranges)
- r_parser fun? a specific asm.parser plugin that does all this tricks? - r_parser fun? a specific asm.parser plugin that does all this tricks?
- fix instruction navigation
- do not allow to disassemble unaligned addresses (toggle)
- use 'jk' with bwdisasm to go up to previous opcode.
- r_asm can reduce cpu without disasm on fixed size ops archs.
* Display getsym() stuff in rabin2, not only legit syms * Display getsym() stuff in rabin2, not only legit syms
* Check if python plugin works from inside
- write tuto, how to call py code from shell or r2
* dmi command must read from memory if no file path provided * dmi command must read from memory if no file path provided
- rabin from memory ftw, to get libnames of dll, so.. - rabin from memory ftw, to get libnames of dll, so..
* add support for sign/unsigned registers..or at least a way to cast them * add support for sign/unsigned registers..or at least a way to cast them
@ -130,6 +121,7 @@ nibble
* Create radare2-testsuite project * Create radare2-testsuite project
- tests for ired, rax2, radare2, rabin2 ... - tests for ired, rax2, radare2, rabin2 ...
* Is RCore->block and blocksize a RBuf ? refactor!11 * Is RCore->block and blocksize a RBuf ? refactor!11
BINARY INFORMATION BINARY INFORMATION
================== ==================
dwarf, pdb, def, lib dwarf, pdb, def, lib
@ -169,7 +161,6 @@ pancake
* fork/clone child . inject code to create new threads or pids * fork/clone child . inject code to create new threads or pids
* Functions in r_util to get lil/big ut8,16,32 from ut8* * Functions in r_util to get lil/big ut8,16,32 from ut8*
- already done..must find better names probably - already done..must find better names probably
* merge asm.arch vm.arch
* support for macro scripting * support for macro scripting
* better debugger support for OSX and iOS * better debugger support for OSX and iOS
* rarc2 allows to compile invalid code like calling puts() out of context * rarc2 allows to compile invalid code like calling puts() out of context

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */ /* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
#include <r_cmd.h> #include <r_cmd.h>
#include <r_util.h> #include <r_util.h>

View File

@ -261,9 +261,11 @@ R_API void r_cons_printf(const char *format, ...) {
/* final entrypoint for adding stuff in the buffer screen */ /* final entrypoint for adding stuff in the buffer screen */
R_API void r_cons_memcat(const char *str, int len) { R_API void r_cons_memcat(const char *str, int len) {
palloc (len+1); if (len>0) {
memcpy (I.buffer+I.buffer_len, str, len+1); palloc (len+1);
I.buffer_len += len; memcpy (I.buffer+I.buffer_len, str, len+1);
I.buffer_len += len;
}
} }
R_API void r_cons_strcat(const char *str) { R_API void r_cons_strcat(const char *str) {

View File

@ -83,8 +83,6 @@ static void printoffset(ut64 off, int show_color) {
else r_cons_printf ("0x%08"PFMT64x" ", off); else r_cons_printf ("0x%08"PFMT64x" ", off);
} }
/* TODO: move to print/disasm.c */ /* TODO: move to print/disasm.c */
static void r_print_disasm(RPrint *p, RCore *core, ut64 addr, ut8 *buf, int len, int l) { static void r_print_disasm(RPrint *p, RCore *core, ut64 addr, ut8 *buf, int len, int l) {
RAnalCC cc = {0}; RAnalCC cc = {0};
@ -1081,6 +1079,7 @@ static int cmd_mount(void *data, const char *_input) {
} }
static int cmd_yank(void *data, const char *input) { static int cmd_yank(void *data, const char *input) {
int i;
RCore *core = (RCore *)data; RCore *core = (RCore *)data;
switch (input[0]) { switch (input[0]) {
case ' ': case ' ':
@ -1089,6 +1088,10 @@ static int cmd_yank(void *data, const char *input) {
case 'y': case 'y':
r_core_yank_paste (core, r_num_math(core->num, input+2), 0); r_core_yank_paste (core, r_num_math(core->num, input+2), 0);
break; break;
case 'p':
r_cons_memcat (core->yank, core->yank_len);
r_cons_newline ();
break;
case 't': case 't':
{ /* hacky implementation */ { /* hacky implementation */
char *arg = strdup(input+1); char *arg = strdup(input+1);
@ -1098,10 +1101,9 @@ static int cmd_yank(void *data, const char *input) {
break; break;
case '\0': case '\0':
if (core->yank) { if (core->yank) {
int i;
r_cons_printf ("0x%08"PFMT64x" %d ", r_cons_printf ("0x%08"PFMT64x" %d ",
core->yank_off, core->yank_len); core->yank_off, core->yank_len);
for (i=0;i<core->yank_len;i++) for (i=0; i<core->yank_len; i++)
r_cons_printf ("%02x", core->yank[i]); r_cons_printf ("%02x", core->yank[i]);
r_cons_newline (); r_cons_newline ();
} else eprintf ("No buffer yanked already\n"); } else eprintf ("No buffer yanked already\n");
@ -1113,6 +1115,8 @@ static int cmd_yank(void *data, const char *input) {
" y 16 ; copy 16 bytes into clipboard\n" " y 16 ; copy 16 bytes into clipboard\n"
" y 16 0x200 ; copy 16 bytes into clipboard from 0x200\n" " y 16 0x200 ; copy 16 bytes into clipboard from 0x200\n"
" y 16 @ 0x200 ; copy 16 bytes into clipboard from 0x200\n" " y 16 @ 0x200 ; copy 16 bytes into clipboard from 0x200\n"
" yp ; print contents of clipboard\n"
" yt 0x200 ; paste clipboard to 0x200\n"
" yy 0x3344 ; paste clipboard\n"); " yy 0x3344 ; paste clipboard\n");
break; break;
} }
@ -1752,6 +1756,28 @@ static int cmd_print(void *data, const char *input) {
case 'x': case 'x':
r_print_hexdump (core->print, core->offset, core->block, len, 16, 1); //, 78, !(input[1]=='-')); r_print_hexdump (core->print, core->offset, core->block, len, 16, 1); //, 78, !(input[1]=='-'));
break; break;
case '6':
{
int malen = (core->blocksize*4)+1;
ut8 *buf = malloc (malen);
memset (buf, 0, malen);
switch (input[1]) {
case 'e':
r_base64_encode (buf, core->block, core->blocksize);
printf ("%s\n", buf);
break;
case 'd':
if (r_base64_decode (buf, core->block, core->blocksize))
printf ("%s\n", buf);
else eprintf ("r_base64_decode: invalid stream\n");
break;
default:
eprintf ("Usage: p6[ed] [len] ; base 64 encode/decode\n");
break;
}
free (buf);
}
break;
case '8': case '8':
r_print_bytes (core->print, core->block, len, "%02x"); r_print_bytes (core->print, core->block, len, "%02x");
break; break;
@ -1778,16 +1804,16 @@ static int cmd_print(void *data, const char *input) {
switch (input[1]) { switch (input[1]) {
case ' ': case ' ':
case '\0': case '\0':
for (l=0; l<len; l+=sizeof(time_t)) for (l=0; l<len; l+=sizeof (time_t))
r_print_date_unix (core->print, core->block+l, sizeof(time_t)); r_print_date_unix (core->print, core->block+l, sizeof (time_t));
break; break;
case 'd': case 'd':
for (l=0; l<len; l+=4) for (l=0; l<len; l+=4)
r_print_date_dos (core->print, core->block+l, 4); r_print_date_dos (core->print, core->block+l, 4);
break; break;
case 'n': case 'n':
for (l=0; l<len; l+=sizeof(ut64)) for (l=0; l<len; l+=sizeof (ut64))
r_print_date_w32 (core->print, core->block+l, sizeof(ut64)); r_print_date_w32 (core->print, core->block+l, sizeof (ut64));
break; break;
case '?': case '?':
r_cons_printf ( r_cons_printf (
@ -1812,20 +1838,22 @@ static int cmd_print(void *data, const char *input) {
default: default:
r_cons_printf ( r_cons_printf (
"Usage: p[fmt] [len]\n" "Usage: p[fmt] [len]\n"
" p8 [len] 8bit hexpair list of bytes\n" " p6[de] [len] base64 decode/encode\n"
" px [len] hexdump of N bytes\n" " p8 [len] 8bit hexpair list of bytes\n"
" po [len] octal dump of N bytes\n" " pb [len] bitstream of N bytes\n"
" pc [len] output C format\n" " pd [len] disassemble N opcodes\n"
" ps [len] print string\n" " pD [len] disassemble N bytes\n"
" pm [fmt] print formatted memory\n" // TODO: rename to pf?? " po [len] octal dump of N bytes\n"
" pS [len] print wide string\n" " pc [len] output C format\n"
" pt [len] print diferent timestamps\n" " pm [fmt] print formatted memory\n" // TODO: rename to pf??
" pd [len] disassemble N opcodes\n" " ps [len] print string\n"
" pD [len] disassemble N bytes\n" " pS [len] print wide string\n"
" pr [len] print N raw bytes\n" " pt [len] print diferent timestamps\n"
" pu [len] print N url encoded bytes\n" " pr [len] print N raw bytes\n"
" pU [len] print N wide url encoded bytes\n", " pu [len] print N url encoded bytes\n"
" pZ [len] print zoom view\n"); " pU [len] print N wide url encoded bytes\n",
" px [len] hexdump of N bytes\n"
" pZ [len] print zoom view\n");
break; break;
} }
if (tbs != core->blocksize) if (tbs != core->blocksize)

View File

@ -1,6 +1,7 @@
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */ /* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
#include <r_core.h> #include <r_core.h>
#define HAVE_VM 0
static int config_scrcols_callback(void *user, void *data) { static int config_scrcols_callback(void *user, void *data) {
int c = R_MIN (128, R_MAX (((RConfigNode*)data)->i_value, 0)); int c = R_MIN (128, R_MAX (((RConfigNode*)data)->i_value, 0));
@ -255,12 +256,14 @@ static int config_asmarch_callback(void *user, void *data) {
return R_TRUE; return R_TRUE;
} }
#if HAVE_VM
static int config_vmarch_callback(void *user, void *data) { static int config_vmarch_callback(void *user, void *data) {
RCore *core = (RCore *) user; RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data; RConfigNode *node = (RConfigNode *) data;
r_vm_set_arch (core->vm, node->value, core->assembler->bits); r_vm_set_arch (core->vm, node->value, core->assembler->bits);
return R_TRUE; return R_TRUE;
} }
#endif
static int config_asmparser_callback(void *user, void *data) { static int config_asmparser_callback(void *user, void *data) {
RCore *core = (RCore*) user; RCore *core = (RCore*) user;
@ -287,8 +290,10 @@ static int config_asmbits_callback(void *user, void *data) {
} }
if (!r_anal_set_bits (core->anal, node->i_value)) if (!r_anal_set_bits (core->anal, node->i_value))
eprintf ("asm.arch: Cannot setup '%i' bits analysis engine\n", (int)node->i_value); eprintf ("asm.arch: Cannot setup '%i' bits analysis engine\n", (int)node->i_value);
#if HAVE_VM
if (core->assembler->cur) if (core->assembler->cur)
r_vm_set_arch (core->vm, core->assembler->cur->name, node->i_value); r_vm_set_arch (core->vm, core->assembler->cur->name, node->i_value);
#endif
// TODO: change debugger backend bit profile here // TODO: change debugger backend bit profile here
return ret; return ret;
} }
@ -387,7 +392,9 @@ R_API int r_core_config_init(RCore *core) {
r_config_set (cfg, "file.type", ""); r_config_set (cfg, "file.type", "");
r_config_set (cfg, "rap.loop", "true"); r_config_set (cfg, "rap.loop", "true");
/* vm */ /* vm */
#if HAVE_VM
r_config_set_cb (cfg, "vm.arch", "x86", &config_vmarch_callback); r_config_set_cb (cfg, "vm.arch", "x86", &config_vmarch_callback);
#endif
/* zoom */ /* zoom */
r_config_set_i (cfg, "zoom.from", 0); r_config_set_i (cfg, "zoom.from", 0);
r_config_set_i (cfg, "zoom.to", 0); r_config_set_i (cfg, "zoom.to", 0);

View File

@ -10,32 +10,17 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
RCore *core = (RCore *)userptr; // XXX ? RCore *core = (RCore *)userptr; // XXX ?
RFlagItem *flag; RFlagItem *flag;
RAnalOp aop; RAnalOp aop;
ut64 ret; ut64 ret = 0;
*ok = 0;
if (str[0]=='$') { if (str[0]=='$') {
/* analyze opcode */ *ok = 1;
switch (str[1]) { r_anal_aop (core->anal, &aop, core->offset,
case '$': core->block, core->blocksize);
if (str[2]=='$') {
r_anal_aop (core->anal, &aop, core->offset,
core->block, core->blocksize);
return aop.length;
}
return core->offset;
case 'e':
case 'j':
case 'f':
case 'r':
r_anal_aop (core->anal, &aop, core->offset,
core->block, core->blocksize);
break;
}
/* return value */
switch (str[1]) { switch (str[1]) {
case '{': case '{':
{ {
char *ptr, *bptr = strdup(str+2); char *ptr, *bptr = strdup (str+2);
ptr = strchr(bptr, '}'); ptr = strchr (bptr, '}');
if (ptr != NULL) { if (ptr != NULL) {
ut64 ret; ut64 ret;
ptr[0]='\0'; ptr[0]='\0';
@ -52,12 +37,15 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
case 'b': return core->blocksize; case 'b': return core->blocksize;
case 's': return core->file->size; case 's': return core->file->size;
case '?': return core->num->value; case '?': return core->num->value;
case '$': return core->offset;
} }
} }
if ((flag = r_flag_get (core->flags, str))) { if (str[0]>'A') {
ret = flag->offset; if ((flag = r_flag_get (core->flags, str))) {
*ok = R_TRUE; ret = flag->offset;
} else *ok = ret = 0; *ok = R_TRUE;
} else *ok = ret = 0;
}
return ret; return ret;
} }
@ -92,7 +80,8 @@ static const char *radare_argv[] = {
"/", "//", "/a", "/c", "/m", "/x", "/v", "/", "//", "/a", "/c", "/m", "/x", "/v",
"y", "yy", "y?", "y", "yy", "y?",
"wx", "ww", "wf", "w?", "wx", "ww", "wf", "w?",
"pc", "pd", "pD", "px", "pX", "po", "pm", "pr", "pt", "ps", "pz", "pr >", "pu", "pU", "p?", "p6d", "p6e", "p8", "pb", "pc", "pd", "pD", "px", "pX", "po",
"pm", "pr", "pt", "ps", "pz", "pr >", "pu", "pU", "p?",
NULL NULL
}; };

View File

@ -4,29 +4,25 @@
#include <dirent.h> #include <dirent.h>
static RFSFile* fs_posix_open(RFSRoot *root, const char *path) { static RFSFile* fs_posix_open(RFSRoot *root, const char *path) {
#if 0 FILE *fd;
RFSFile *file = r_fs_file_new (root, path); RFSFile *file = r_fs_file_new (root, path);
GrubFS *gfs = grubfs_new (&FSIPTR, &root->iob); file->ptr = NULL;
file->ptr = gfs;
file->p = root->p; file->p = root->p;
if (gfs->file->fs->open (gfs->file, path)) { fd = fopen (path, "r");
if (fd) {
fseek (fd, 0, SEEK_END);
file->size = ftell (fd);
fclose (fd);
} else {
r_fs_file_free (file); r_fs_file_free (file);
grubfs_free (gfs);
file = NULL; file = NULL;
} else file->size = gfs->file->size; }
return file; return file;
#endif
eprintf ("TODO: fs_posix_open\n");
return NULL;
} }
static boolt fs_posix_read(RFSFile *file, ut64 addr, int len) { static boolt fs_posix_read(RFSFile *file, ut64 addr, int len) {
file->data = malloc (len); free (file->data);
if (file->data) { file->data = r_file_slurp_range (file->name, 0, len, NULL);
eprintf ("TODO: fs_posix_read\n");
free (file->data);
file->data = NULL;
}
return R_FALSE; return R_FALSE;
} }

View File

@ -213,6 +213,8 @@ R_API int r_num_to_bits(char *out, ut64 num);
x==','||x==';'||x==':'||x=='['||x==']'||x=='('||x==')'||x=='{'||x=='}') x==','||x==';'||x==':'||x=='['||x==']'||x=='('||x==')'||x=='{'||x=='}')
#define ishexchar(x) ((x>='0'&&x<='9') || (x>='a'&&x<='f') || (x>='A'&&x<='F')) { #define ishexchar(x) ((x>='0'&&x<='9') || (x>='a'&&x<='f') || (x>='A'&&x<='F')) {
R_API void r_base64_encode(ut8 *bout, const ut8 *bin, int len);
R_API int r_base64_decode(ut8 *bout, const ut8 *bin, int len);
/* strings */ /* strings */
#define r_str_write(x,y) write (x, y, strlen(y)) #define r_str_write(x,y) write (x, y, strlen(y))
R_API int r_str_bits (char *strout, const ut8 *buf, int len, const char *bitz); R_API int r_str_bits (char *strout, const ut8 *buf, int len, const char *bitz);

View File

@ -2,7 +2,7 @@ include ../config.mk
NAME=r_util NAME=r_util
OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o range.o log.o OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o range.o log.o
OBJ+=prof.o cache.o sys.o btree.o buf.o list.o flist.o w32-sys.o OBJ+=prof.o cache.o sys.o btree.o buf.o list.o flist.o w32-sys.o base64.o
ifeq (${HAVE_LIB_GMP},1) ifeq (${HAVE_LIB_GMP},1)
OBJ+=big-gmp.o OBJ+=big-gmp.o
else else

54
libr/util/base64.c Normal file
View File

@ -0,0 +1,54 @@
/* Original code from:
* dmc - dynamic mail client -- author: pancake
* See LICENSE file for copyright and license details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <r_util.h>
#define SZ 1024
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
static void b64_encode(const ut8 in[3], ut8 out[4], int len) {
out[0] = cb64[ in[0] >> 2 ];
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
out[2] = (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
out[3] = (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
}
static int b64_decode(const ut8 in[4], ut8 out[3]) {
ut8 len = 3, i, v[4];
for (i=0;i<4;i++) {
if (in[i]<43||in[i]>122)
return -1;
v[i] = cd64[in[i]-43];
if (v[i]!='$') v[i]-=62;
else { len = i-1; break; }
}
out[0] = v[0] << 2 | v[1] >> 4;
out[1] = v[1] << 4 | v[2] >> 2;
out[2] = ((v[2] << 6) & 0xc0) | v[3];
return len;
}
R_API int r_base64_decode(ut8 *bout, const ut8 *bin, int len) {
int in, out, ret;
for (in=out=0; in<len-1; in+=4) {
ret = b64_decode (bin+in, bout+out);
if (ret == -1)
break;
out += ret;
}
return (in != out);
}
R_API void r_base64_encode(ut8 *bout, const ut8 *bin, int len) {
int in, out;
for (in=out=0; in<len; in+=3,out+=4)
b64_encode (bin+in, bout+out, len-in>3?3:len-in);
}

View File

@ -124,7 +124,9 @@ R_API char *r_file_slurp_range(const char *str, ut64 off, int sz, int *osz) {
fseek (fd, off, SEEK_SET); fseek (fd, off, SEEK_SET);
ret = (char *)malloc (sz+1); ret = (char *)malloc (sz+1);
if (ret != NULL) { if (ret != NULL) {
*osz = (int)(size_t)fread (ret, 1, sz, fd); if (osz)
*osz = (int)(size_t)fread (ret, 1, sz, fd);
else fread (ret, 1, sz, fd);
ret[sz] = '\0'; ret[sz] = '\0';
} }
fclose (fd); fclose (fd);