* Fix make deinstall

* rarun2 and rasc2 now depend on r_util
* Add r_mem_protect() as a wrapper for mprotect/VirtualProtect
* Fix segfault in java class parser

--HG--
rename : binr/rarun2/main.c => binr/rarun2/rarun2.c
This commit is contained in:
pancake 2011-09-19 13:54:57 +02:00
parent 63c62b1df3
commit 279d4e6f5f
10 changed files with 93 additions and 66 deletions

View File

@ -99,7 +99,7 @@ symstall install-symlink: install-man-symlink install-doc-symlink install-pkgcon
deinstall uninstall:
cd libr && ${MAKE} uninstall PARENT=1 PREFIX=${PREFIX} DESTDIR=${DESTDIR}
cd binr && ${MAKE} uninstall PARENT=1 PREFIX=${PREFIX} DESTDIR=${DESTDIR}
cd libr/db/d && ${MAKE} uninstall PARENT=1 PREFIX=${PREFIX} DESTDIR=${DESTDIR}
cd libr/syscall/d && ${MAKE} uninstall PARENT=1 PREFIX=${PREFIX} DESTDIR=${DESTDIR}
@echo
@echo "Run 'make purge' to also remove installed files from previous versions of r2"
@echo

6
TODO
View File

@ -13,12 +13,13 @@
* Remove/deprecate libr/vm
* Merge libr/db inside libr/util ?
* Test r_search_delta()
* Dupped javasm bin/asm -- must merge
------8<-------------------8<--------------------8<-----------------8<----------
====[[ 0.8.2 ]]====
====[[ 0.8.6 ]]====
* rax2 to not change base (always hexa?)
* rax2 -k by default?
* Rename r_hashtable -> r_ht
- Make ht64.c include ht.c
@ -79,7 +80,6 @@ TODO
* Add r_cons_prompt () ... calling set_prompt + fgets -- this api needs cleanup
- set prompt, set line, fgets
- strict width in visual
* Dupped javasm bin/asm
* REFACTORING of disasm loop XDDDDD -1 (r2-0.9 plzz)
- arch dependent anal code must be removed from disasm loop +1

View File

@ -1,12 +1,5 @@
BIN=rarun2
BINDEPS=r_util
OBJ=main.o
all: ${BIN}
${BIN}: ${OBJ}
${CC} -o ${BIN} ${OBJ}
mrproper clean:
rm -f ${BIN} ${OBJ}
.PHONY: all clean
include ../binr.mk

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <r_util.h>
static char *_arg0 = NULL;
static char *_arg1 = NULL;
@ -13,7 +14,7 @@ static char *_program = NULL;
static char *_stdin = NULL;
static char *_stdout = NULL;
static char *_stderr = NULL;
static char *_chdir = NULL;
static char *_chgdir = NULL;
static char *_chroot = NULL;
static char *_preload = NULL;
static char *_setuid = NULL;
@ -27,13 +28,13 @@ static void parseline (char *b) {
if (!e) return;
if (*b=='#') return;
*e++=0;
if (*e=='$') e = strdup (getenv (e));
if (*e=='$') e = r_sys_getenv (e);
if (e == NULL) return;
if (!strcmp (b, "program")) _program = strdup (e);
else if (!strcmp (b, "stdout")) _stdout = strdup (e);
else if (!strcmp (b, "stdin")) _stdin = strdup (e);
else if (!strcmp (b, "input")) _input = strdup (e);
else if (!strcmp (b, "chdir")) _chdir = strdup (e);
else if (!strcmp (b, "chdir")) _chgdir = strdup (e);
else if (!strcmp (b, "chroot")) _chroot = strdup (e);
else if (!strcmp (b, "preload")) _preload = strdup (e);
else if (!strcmp (b, "setuid")) _setuid = strdup (e);
@ -48,11 +49,12 @@ static void parseline (char *b) {
char *v = strchr (e, '=');
if (v) {
*v++=0;
setenv (e, v, 1);
r_sys_setenv (e, v);
}
}
}
#if __UNIX__
static void parseinput (char *s) {
if (!*s) return;
while (*s++) {
@ -62,6 +64,7 @@ static void parseinput (char *s) {
}
}
}
#endif
static int runfile () {
int ret;
@ -84,8 +87,9 @@ static int runfile () {
close (2);
dup2 (f, 2);
}
if (_chdir) chdir (_chdir);
if (_chgdir) chdir (_chgdir);
if (_chroot) chdir (_chroot);
#if __UNIX__
if (_setuid) setuid (atoi (_setuid));
if (_seteuid) seteuid (atoi (_seteuid));
if (_setgid) setgid (atoi (_setgid));
@ -97,11 +101,12 @@ static int runfile () {
parseinput (_input);
write (f2[1], _input, strlen (_input));
}
#endif
if (_preload) {
#if __APPLE__
setenv ("DYLD_PRELOAD", _preload, 1);
r_sys_setenv ("DYLD_PRELOAD", _preload);
#else
setenv ("LD_PRELOAD", _preload, 1);
r_sys_setenv ("LD_PRELOAD", _preload);
#endif
}
ret = execl (_program, _program, _arg0, NULL);

View File

@ -1,5 +1,5 @@
BIN=rasc2
DEPS=
BINDEPS=r_util
MYCLEAN=myclean
OBJS=shellcodes.o test.o

View File

@ -9,6 +9,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <r_util.h>
#if __UNIX__
#include <sys/mman.h>
@ -253,21 +254,18 @@ int print_shellcode() {
ut8 *ptr = malloc (4096);
void (*cb)() = (void *)&shellcode;
memcpy (ptr, shellcode, SCSIZE);
#if __UNIX__
mprotect (ptr, 4096, PROT_READ|PROT_EXEC); // rx must be ok
mprotect (ptr, 4096, PROT_READ|PROT_WRITE|PROT_EXEC); // try rwx
#endif
r_mem_protect (ptr, 4096, "rx");
r_mem_protect (ptr, 4096, "rwx"); // try, ignore if fail
cb = (void*)ptr;
cb();
cb ();
free (ptr);
}
break;
case 4:
printf ("\"");
j = 0;
for (i=0;i<SCSIZE;i++) {
for (i=0;i<SCSIZE;i++)
printf ("\\x%02x", output[i]);
}
printf ("\"\n");
break;
}

View File

@ -84,21 +84,21 @@ static int java_resolve(int idx, char *str) {
if (idx<0||idx>cf.cp_count)
return 1;
if (cp_items) {
if((!strcmp (cp_items[idx].name, "MethodRef"))
|| (!strcmp (cp_items[idx].name, "FieldRef"))) {
int class = USHORT (get_cp(idx)->bytes,0);
//int namet = USHORT(get_cp(idx)->bytes,2);
char *class_str = get_cp(USHORT(get_cp(class)->bytes,0)-1)->value;
char *namet_str = get_cp(USHORT(get_cp(class)->bytes,2)-1)->value;
//char *namet_str = get_cp(namet)->value;
sprintf (str, "%s %s", class_str, namet_str);
} else
if (!strcmp (cp_items[idx].name, "String")) {
sprintf(str, "\"%s\"", get_cp(USHORT(get_cp(idx)->bytes,0)-1)->value);
} else
if (!strcmp(cp_items[idx].name, "Utf8")) {
sprintf (str, "\"%s\"", get_cp(idx)->value);
} else sprintf (str, "0x%04x", USHORT(get_cp(idx)->bytes,0));
if ((!strcmp (cp_items[idx].name, "MethodRef"))
|| (!strcmp (cp_items[idx].name, "FieldRef"))) {
int class = USHORT (get_cp(idx)->bytes,0);
//int namet = USHORT(get_cp(idx)->bytes,2);
char *class_str = get_cp(USHORT(get_cp(class)->bytes,0)-1)->value;
char *namet_str = get_cp(USHORT(get_cp(class)->bytes,2)-1)->value;
//char *namet_str = get_cp(namet)->value;
sprintf (str, "%s %s", class_str, namet_str);
} else
if (!strcmp (cp_items[idx].name, "String")) {
sprintf(str, "\"%s\"", get_cp(USHORT(get_cp(idx)->bytes,0)-1)->value);
} else
if (!strcmp(cp_items[idx].name, "Utf8")) {
sprintf (str, "\"%s\"", get_cp(idx)->value);
} else sprintf (str, "0x%04x", USHORT(get_cp(idx)->bytes,0));
} else strcpy (str, "(null)");
return 0;
}
@ -292,9 +292,9 @@ int java_classdump(const char *file, int verbose) {
javasm_init();
/* start parsing */
fread(&cf, 10, 1, fd); //sizeof(struct classfile), 1, fd);
if (memcmp(cf.cafebabe, "\xCA\xFE\xBA\xBE", 4)) {
fprintf(stderr, "Invalid header\n");
fread (&cf, 10, 1, fd); //sizeof(struct classfile), 1, fd);
if (memcmp (cf.cafebabe, "\xCA\xFE\xBA\xBE", 4)) {
fprintf(stderr, "java_classdump: Invalid header\n");
return -1;
}

View File

@ -1,6 +1,6 @@
// XXX this is dupped in r_asm and r_bin :O
/*
* Copyright (C) 2007, 2008, 2009, 2010-2011
* Copyright (C) 2007-2011
* pancake <youterm.com>, nibble <develsec.org>
*/
@ -54,7 +54,7 @@ static int attributes_walk(RBinJavaObj *bin, struct r_bin_java_attr_t *attr, int
int j=0,k;
char *name;
for (j=0;j<sz2;j++) {
for (j=0; j<sz2; j++) {
if (r_buf_read_at (bin->b, R_BUF_CUR, (ut8*)buf, 6) != 6) {
eprintf ("Cannot read 6 bytes in class file\n");
return R_FALSE;
@ -77,7 +77,7 @@ static int attributes_walk(RBinJavaObj *bin, struct r_bin_java_attr_t *attr, int
IFDBG printf ("**ERROR ** Cannot identify attribute name into constant pool\n");
continue;
}
if (!strcmp(name, "Code")) {
if (!strcmp (name, "Code")) {
attr->type = R_BIN_JAVA_TYPE_CODE;
r_buf_read_at (bin->b, R_BUF_CUR, (ut8*)buf, 8);
@ -171,7 +171,7 @@ eprintf ("local.%d.%d.name=%s\n", bin->midx, i, name);
static int javasm_init(RBinJavaObj *bin) {
unsigned short sz, sz2;
char buf[0x9999];
char buf[0x4096];
int i, j;
/* Initialize structs */
@ -187,7 +187,9 @@ static int javasm_init(RBinJavaObj *bin) {
/* start parsing */
r_buf_read_at (bin->b, R_BUF_CUR, (ut8*)&bin->cf, 10); //sizeof(struct r_bin_java_classfile_t), 1, bin->fd);
if (memcmp (bin->cf.cafebabe, "\xCA\xFE\xBA\xBE", 4)) {
fprintf(stderr, "Invalid header\n");
eprintf ("javasm_init: Invalid header (%02x %02x %02x %02x)\n",
bin->cf.cafebabe[0], bin->cf.cafebabe[1],
bin->cf.cafebabe[2], bin->cf.cafebabe[3]);
return R_FALSE;
}
@ -233,9 +235,13 @@ static int javasm_init(RBinJavaObj *bin) {
sz = R_BIN_JAVA_USHORT (buf, 0);
bin->cp_items[i].length = sz;
bin->cp_items[i].off += 3;
if (sz > 0)
if (sz>=0 && sz<sizeof (buf)) {
r_buf_read_at (bin->b, R_BUF_CUR, (ut8*)buf, sz);
buf[sz] = '\0';
buf[sz] = '\0';
} else {
eprintf ("Invalid utf8 length %d\n", sz);
buf[0] = 0;
}
break;
default:
r_buf_read_at (bin->b, R_BUF_CUR, (ut8*)buf, c->len);
@ -254,7 +260,7 @@ static int javasm_init(RBinJavaObj *bin) {
i += 2;
break;
case 7:
IFDBG printf("%d\n", R_BIN_JAVA_USHORT(buf,0));
IFDBG eprintf ("%d\n", R_BIN_JAVA_USHORT (buf,0));
break;
case 8:
IFDBG printf("string ptr %d\n", R_BIN_JAVA_USHORT(buf, 0));
@ -441,30 +447,28 @@ void* r_bin_java_free(RBinJavaObj* bin) {
}
RBinJavaObj* r_bin_java_new(const char* file) {
RBinJavaObj *bin;
ut8 *buf;
if (!(bin = malloc(sizeof(RBinJavaObj))))
return NULL;
memset (bin, 0, sizeof (RBinJavaObj));
RBinJavaObj *bin = R_NEW0 (RBinJavaObj);
bin->file = file;
if (!(buf = (ut8*)r_file_slurp(file, &bin->size)))
return r_bin_java_free(bin);
if (!(buf = (ut8*)r_file_slurp (file, &bin->size)))
return r_bin_java_free (bin);
bin->b = r_buf_new ();
if (!r_buf_set_bytes(bin->b, buf, bin->size))
return r_bin_java_free(bin);
if (!r_buf_set_bytes (bin->b, buf, bin->size))
return r_bin_java_free (bin);
free (buf);
if (!javasm_init (bin))
return r_bin_java_free(bin);
return r_bin_java_free (bin);
return bin;
}
RBinJavaObj* r_bin_java_new_buf(struct r_buf_t *buf) {
RBinJavaObj* r_bin_java_new_buf(RBuffer *buf) {
RBinJavaObj *bin = R_NEW0 (RBinJavaObj);
if (!bin) return NULL;
bin->b = buf;
bin->size = buf->length;
// seek backward
buf->cur = 0;
if (!javasm_init (bin))
return r_bin_java_free(bin);
return r_bin_java_free (bin);
return bin;
}

View File

@ -244,6 +244,7 @@ R_API int r_cache_invalidate(struct r_cache_t *c, ut64 from, ut64 to);
R_API void r_prof_start(struct r_prof_t *p);
R_API double r_prof_end(struct r_prof_t *p);
R_API int r_mem_protect(void *ptr, int size, const char *prot);
R_API int r_mem_set_num (ut8 *dest, int dest_size, ut64 num, int endian);
R_API int r_mem_eq(ut8 *a, ut8 *b, int len);
R_API void r_mem_copybits(ut8 *dst, const ut8 *src, int bits);

View File

@ -205,3 +205,29 @@ R_API int r_mem_unpack(const ut8 *buf) {
// TODO: copy this from r_buf??
return R_TRUE;
}
R_API int r_mem_protect(void *ptr, int size, const char *prot) {
#if __UNIX__
int p = 0;
if (strchr (prot, 'x')) p |= PROT_EXEC;
if (strchr (prot, 'r')) p |= PROT_READ;
if (strchr (prot, 'w')) p |= PROT_WRITE;
if (mprotect (ptr, size, p)==-1)
return R_FALSE;
#elif __WINDOWS__
int r, w, x;
DWORD p = PAGE_NOACCESS;
r = strchr (prot, 'r')? 1: 0;
w = strchr (prot, 'w')? 1: 0;
x = strchr (prot, 'x')? 1: 0;;
if (w && x) return R_FALSE;
if (x) p = PAGE_EXECUTE_READ;
else if (w) p = PAGE_READWRITE;
else if (r) p = PAGE_READONLY;
if (!VirtualProtect (ptr, size, p, NULL))
return R_FALSE;
#else
#warning Unknown platform
#endif
return R_TRUE;
}