Initial import of binr/r2agent

This commit is contained in:
pancake 2013-05-11 01:58:05 +02:00
parent 09f6cc9973
commit f6a8d7b3b8
8 changed files with 160 additions and 1 deletions

View File

@ -7,7 +7,7 @@ PFX=${DESTDIR}/${PREFIX}
BFX=${DESTDIR}/${BINDIR}
LFX=${DESTDIR}/${LIBDIR}
BINS=rax2 rasm2 rabin2 rahash2 radiff2 radare2 rafind2 rarun2 ragg2
BINS=rax2 rasm2 rabin2 rahash2 radiff2 radare2 rafind2 rarun2 ragg2 r2agent
all:
@for a in ${BINS} ; do (cd $$a && ${MAKE} all) || exit 1; done

4
binr/r2agent/Makefile Normal file
View File

@ -0,0 +1,4 @@
BIN=r2agent
BINDEPS=r_socket r_util r_cons
include ../rules.mk

21
binr/r2agent/README.md Normal file
View File

@ -0,0 +1,21 @@
r2agent
=======
BUGS
- not working on windows
- if agent crashes, children processes are still using the listen port
- files
- debug
- open
- push
- pull
- list
- delete
- chmod
- processes
- attach
- kill
- sessions
- list
- close

27
binr/r2agent/index.h Normal file
View File

@ -0,0 +1,27 @@
static const char *page_index =
"<html>"
"<head>"
"<title>r2agent</title>"
"<script>"
"function file_open() {"
" var file = window.prompt ('path to file?');"
" window.open ('/file/open/'+file);"
"}"
"</script>"
"</head>"
"<body>"
" <h2>r2agent</h2>"
" <hr size=1 />"
"File<br />"
" - <a href='javascript:file_open()'>open</a><br />"
" - <a href='/file/list'>list</a><br />"
" - push<br />"
" - pull<br />"
" - delete<br />"
" - chmod<br />"
"Processes<br />"
" - list<br />"
"Sessions<br />"
" - list<br />"
"</body>"
"</html>";

92
binr/r2agent/r2agent.c Normal file
View File

@ -0,0 +1,92 @@
/* radare - LGPL - Copyright 2013 - pancake */
#include <getopt.c>
#include <r_core.h>
#include <signal.h>
#include "index.h"
static int usage (int v) {
printf ("Usage: r2agent [-dh] [-p port]\n"
" -d run in daemon mode (background\n"
" -h show this help message\n"
" -p 8392 specify listening port (defaults to 8080)\n");
return !!!v;
}
int main(int argc, char **argv) {
RSocket *s;
RSocketHTTPRequest *rs;
int c, timeout = 3, dodaemon = 0;
const char *port = "8080";
// TODO: add flag to specify if listen in local or 0.0.0.0
while ((c = getopt (argc, argv, "hp:d")) != -1) {
switch (c) {
case 'd':
dodaemon = 1;
break;
case 'h':
return usage (1);
case 'p':
port = optarg;
break;
}
}
if (dodaemon) {
int pid = fork ();
if (pid >0) {
printf ("%d\n", pid);
return 0;
}
}
s = r_socket_new (R_FALSE);
s->local = 1; // by default
if (!r_socket_listen (s, port, NULL)) {
eprintf ("Cannot listen on http.port\n");
return 1;
}
while (!r_cons_singleton ()->breaked) {
char *result_heap = NULL;
const char *result = page_index;
rs = r_socket_http_accept (s, timeout);
if (!rs) continue;
if (!strcmp (rs->method, "GET")) {
if (!memcmp (rs->path, "/proc/kill/", 11)) {
// TODO: show page here?
int pid = atoi (rs->path+11);
if (pid>0) kill (pid, 9);
} else
if (!memcmp (rs->path, "/file/open/", 11)) {
int pid;
int session_port = 3000 + r_num_rand (1024);
char *filename = rs->path +11;
int filename_len = strlen (filename);
char *cmd;
cmd = malloc (filename_len+40);
sprintf (cmd, "r2 -q -e http.port=%d -c=h %s",
session_port, filename);
// TODO: use r_sys api to get pid when running in bg
pid = r_sys_cmdbg (cmd);
free (cmd);
result = result_heap = malloc (80+filename_len);
sprintf (result_heap,
"<html><body>"
"<a href='/'>back</a><hr size=1/>"
" - <a target='_blank' href='http://localhost:%d/'>open</a><br />"
" - <a href='/proc/kill/%d'>kill</a><br />"
"</body></html>", session_port, pid);
eprintf ("\nchild pid %d\n\n", pid);
}
}
r_socket_http_response (rs, 200, result, 0, NULL);
r_socket_http_close (rs);
free (result_heap);
}
r_socket_free (s);
return 0;
}

View File

@ -1,4 +1,5 @@
/* radare - LGPL - Copyright 2011-2013 - pancake */
#include <r_egg.h>
#include <r_bin.h>
#include <getopt.c>

View File

@ -453,6 +453,7 @@ R_API char *r_sys_cmd_str_w32(const char *cmd);
#endif
R_API int r_sys_truncate(const char *file, int sz);
R_API int r_sys_cmd(const char *cmd);
R_API int r_sys_cmdbg(const char *cmd);
R_API int r_sys_cmdf (const char *fmt, ...);
R_API char *r_sys_cmd_str(const char *cmd, const char *input, int *len);
R_API char *r_sys_cmd_strf(const char *cmd, ...);

View File

@ -356,6 +356,19 @@ R_API int r_sys_cmdf (const char *fmt, ...) {
return ret;
}
R_API int r_sys_cmdbg (const char *str) {
#if __UNIX__
int ret, pid = fork ();
if (pid == -1) return -1;
if (pid) return pid;
ret = r_sandbox_system (str, 0);
eprintf ("{exit: %d, pid: %d, cmd: \"%s\"}", ret, pid, str);
exit (0);
#else
#warning r_sys_cmdbg is not implemented for this platform
#endif
}
R_API int r_sys_cmd (const char *str) {
#if __FreeBSD__
/* freebsd system() is broken */