From 8fe475a50f7fe2b567bd02f723d75a0e542d84e2 Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 5 Jun 2011 01:33:28 +0200 Subject: [PATCH] * Added rarun2 program --- binr/rarun2/Makefile | 10 ++++ binr/rarun2/main.c | 126 +++++++++++++++++++++++++++++++++++++++++++ binr/rarun2/x | 7 +++ binr/rarun2/x.input | 3 ++ 4 files changed, 146 insertions(+) create mode 100644 binr/rarun2/Makefile create mode 100644 binr/rarun2/main.c create mode 100755 binr/rarun2/x create mode 100644 binr/rarun2/x.input diff --git a/binr/rarun2/Makefile b/binr/rarun2/Makefile new file mode 100644 index 0000000000..ee7da13527 --- /dev/null +++ b/binr/rarun2/Makefile @@ -0,0 +1,10 @@ +BIN=rarun2 +OBJ=main.o + +all: ${OBJ} + ${CC} -o ${BIN} ${OBJ} + +clean: + rm -f ${BIN} ${OBJ} + +.PHONY: all clean diff --git a/binr/rarun2/main.c b/binr/rarun2/main.c new file mode 100644 index 0000000000..361fb752d6 --- /dev/null +++ b/binr/rarun2/main.c @@ -0,0 +1,126 @@ +/* radare2 - Copyleft 2011 - pancake */ +#include +#include +#include +#include + +static char *_arg0 = NULL; +static char *_arg1 = NULL; +static char *_arg2 = NULL; +static char *_arg3 = NULL; +static char *_program = NULL; +static char *_stdin = NULL; +static char *_stdout = NULL; +static char *_stderr = NULL; +static char *_chdir = NULL; +static char *_chroot = NULL; +static char *_preload = NULL; +static char *_setuid = NULL; +static char *_seteuid = NULL; +static char *_setgid = NULL; +static char *_setegid = NULL; +static char *_input = NULL; + +static void parseline (char *b) { + char *e = strchr (b, '='); + if (!e) return; + if (*b=='#') return; + *e++=0; + 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, "chroot")) _chroot = strdup (e); + else if (!strcmp (b, "preload")) _preload = strdup (e); + else if (!strcmp (b, "setuid")) _setuid = strdup (e); + else if (!strcmp (b, "seteuid")) _seteuid = strdup (e); + else if (!strcmp (b, "setgid")) _setgid = strdup (e); + else if (!strcmp (b, "setegid")) _setegid = strdup (e); + else if (!strcmp (b, "arg0")) _arg0 = strdup (e); + else if (!strcmp (b, "arg1")) _arg1 = strdup (e); + else if (!strcmp (b, "arg2")) _arg2 = strdup (e); + else if (!strcmp (b, "arg3")) _arg3 = strdup (e); + else if (!strcmp (b, "setenv")) { + char *v = strchr (e, '='); + if (v) { + *v++=0; + setenv (e, v, 1); + } + } +} + +static void parseinput (char *s) { + if (!*s) return; + while (*s++) { + if (s[0]=='\\' && s[1]=='n') { + *s = '\n'; + strcpy (s+1, s+2); + } + } +} + +static int runfile () { + int ret; + if (!_program) { + printf ("No program rule defined\n"); + return 1; + } + if (_stdin) { + int f = open (_stdin, O_RDONLY); + close (0); + dup2 (f, 0); + } + if (_stdout) { + int f = open (_stdout, O_RDONLY); + close (1); + dup2 (f, 1); + } + if (_stderr) { + int f = open (_stderr, O_RDONLY); + close (2); + dup2 (f, 2); + } + if (_chdir) chdir (_chdir); + if (_chroot) chdir (_chroot); + if (_setuid) setuid (atoi (_setuid)); + if (_seteuid) seteuid (atoi (_seteuid)); + if (_setgid) setgid (atoi (_setgid)); + if (_input) { + int f2[2]; + pipe (f2); + close (0); + dup2 (f2[0], 0); + parseinput (_input); + write (f2[1], _input, strlen (_input)); + } + if (_preload) { +#if __APPLE__ + setenv ("DYLD_PRELOAD", _preload, 1); +#else + setenv ("LD_PRELOAD", _preload, 1); +#endif + } + ret = execl (_program, _program, _arg0, NULL); + printf ("RETURN VALUE = %d\n", ret); + return 0; +} + +int main(int argc, char **argv) { + FILE *fd; + char *file, buf[1024]; + if (argc==1) { + printf ("Usage: rarun2 script\n"); + return 1; + } + file = argv[1]; + fd = fopen (file, "r"); + for (;;) { + fgets (buf, sizeof (buf)-1, fd); + if (feof (fd)) break; + buf[strlen (buf)-1] = 0; + parseline (buf); + } + fclose (fd); + return runfile (); +} diff --git a/binr/rarun2/x b/binr/rarun2/x new file mode 100755 index 0000000000..82da20904c --- /dev/null +++ b/binr/rarun2/x @@ -0,0 +1,7 @@ +#!./rarun2 +program=./pp400 +arg0=10 +#stdin=x.input +input=10\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n +chdir=/tmp +chroot=. diff --git a/binr/rarun2/x.input b/binr/rarun2/x.input new file mode 100644 index 0000000000..5359cf31d2 --- /dev/null +++ b/binr/rarun2/x.input @@ -0,0 +1,3 @@ +10 +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +