API fixes for bochs and honor sandbox

This commit is contained in:
pancake 2016-03-27 13:43:51 +02:00
parent 699e3b84d1
commit d092be54e2
5 changed files with 79 additions and 65 deletions

View File

@ -20,7 +20,7 @@ typedef struct {
static libbochs_t *desc = NULL;
static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
char cmd[50];
char cmd[64];
char num[4];
char addr[19];
char bufcmd[100];
@ -31,7 +31,7 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
if (set) {
//eprintf("[set] bochs_breakpoint %016"PFMT64x"\n",bp->addr);
sprintf (cmd, "lb 0x%x", (ut32)bp->addr);
bochs_send_cmd(desc,cmd,true);
bochs_send_cmd (desc, cmd, true);
bCapturaRegs = true;
} else {
//eprintf("[unset] bochs_breakpoint %016"PFMT64x"\n",bp->addr);
@ -62,9 +62,9 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
} while (desc->data[i] != '<' && i<lenRec-4);
}
if (a == bp->addr) {
snprintf (bufcmd,sizeof (bufcmd), "d %i",n);
snprintf (bufcmd, sizeof (bufcmd), "d %i",n);
//eprintf("[unset] Break point localizado indice = %x (%x) %s \n",n,(DWORD)a,bufcmd);
bochs_send_cmd(desc,bufcmd,true);
bochs_send_cmd (desc, bufcmd, true);
}
}
@ -233,7 +233,7 @@ static int r_debug_bochs_continue(RDebug *dbg, int pid, int tid, int sig) {
static void bochs_debug_break(void *u) {
eprintf("bochs_debug_break: Sending break...\n");
CommandStop_ (desc);
bochs_cmd_stop (desc);
bBreak = true;
}
@ -251,7 +251,7 @@ static int r_debug_bochs_wait(RDebug *dbg, int pid) {
r_cons_break (bochs_debug_break, dbg);
i = 500;
do {
WaitForReply_(desc);
bochs_wait (desc);
if (bBreak) {
if (desc->data[0]) {
eprintf("ctrl+c %s\n", desc->data);

View File

@ -759,8 +759,8 @@ R_API const char *r_constr_add (RConstr *c, const char *str);
/* sandbox */
R_API DIR* r_sandbox_opendir (const char *path);
R_API int r_sandbox_enable (int e);
R_API int r_sandbox_disable (int e);
R_API bool r_sandbox_enable (bool e);
R_API bool r_sandbox_disable (bool e);
R_API int r_sandbox_system (const char *x, int fork);
R_API bool r_sandbox_creat (const char *path, int mode);
R_API int r_sandbox_open (const char *path, int mode, int perm);

View File

@ -25,33 +25,41 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
char * archivoBochs;
char * archivoCfg;
int l;
if (!r_sandbox_enable (false)) {
return NULL;
}
if (!__plugin_open (io, file, 0))
return NULL;
if (riobochs) {
return riobochs;
}
archivoBochs = malloc (1024);
if (!archivoBochs) return NULL;
archivoCfg = malloc (1024);
if (!archivoCfg) {
free (archivoBochs);
return NULL;
}
i = strstr (file+8, "#");
if (i) {
l = i - file+8;
strncpy(archivoBochs,file+8,l<1024?l:1024);
l = i - file + 8;
strncpy (archivoBochs,file+8,l<1024?l:1024);
archivoBochs[l]=0;
l=strlen(i+1);
strncpy(archivoCfg,i+1,l<1024?l:1024);
archivoCfg[l]=0;
l = strlen (i+1);
strncpy (archivoCfg,i+1,l<1024?l:1024);
archivoCfg[l] = 0;
} else {
free(archivoBochs);
free(archivoCfg);
lprintf("Error cant find : \n");
free (archivoBochs);
free (archivoCfg);
eprintf ("Error cant find :\n");
return NULL;
}
riob = R_NEW0 (RIOBochs);
// Inicializamos
if (bochs_open_(&riob->desc,archivoBochs,archivoCfg) == true) {
if (bochs_open (&riob->desc,archivoBochs,archivoCfg) == true) {
desc = &riob->desc;
riobochs = r_io_desc_new (&r_io_plugin_bochs, -1, file, rw, mode, riob);
//riogdb = r_io_desc_new (&r_io_plugin_gdb, riog->desc.sock->fd, file, rw, mode, riog);
@ -79,13 +87,13 @@ static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
ut64 addr = io->off;
if (!desc || !desc->data) return -1;
lprintf("io_read ofs= %016"PFMT64x" count= %x\n",io->off,count);
bochs_read_(desc,addr,count,buf);
bochs_read (desc,addr,count,buf);
return count;
}
static int __close(RIODesc *fd) {
lprintf("io_close\n");
bochs_close_(desc);
bochs_close (desc);
return true;
}
@ -100,7 +108,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
io->cb_printf ("%s\n", desc->data);
return 1;
} else if (!strncmp (cmd, "dobreak", 7)) {
CommandStop_ (desc);
bochs_cmd_stop (desc);
io->cb_printf ("%s\n", desc->data);
return 1;
}

View File

@ -1,10 +1,10 @@
/* radare - LGPL - Copyright 2012-2015 - pancake */
/* radare - LGPL - Copyright 2012-2016 - pancake */
#include <r_util.h>
#include <signal.h>
static int enabled = 0;
static int disabled = 0;
static bool enabled = 0;
static bool disabled = 0;
/**
* This function verifies that the given path is allowed. Paths are allowed only if they don't
@ -51,7 +51,7 @@ R_API int r_sandbox_check_path (const char *path) {
return R_TRUE;
}
R_API int r_sandbox_disable (int e) {
R_API bool r_sandbox_disable (bool e) {
if (e) {
disabled = enabled;
enabled = 0;
@ -61,8 +61,8 @@ R_API int r_sandbox_disable (int e) {
return enabled;
}
R_API int r_sandbox_enable (int e) {
if (enabled) return R_TRUE;
R_API bool r_sandbox_enable (bool e) {
if (enabled) return true;
return (enabled = !!e);
}

View File

@ -9,13 +9,13 @@ int sizeSend = 0;
#define SIZE_BUF 0x5800 * 2
int RunRemoteThread_(libbochs_t* b, const ut8 *lpBuffer, ut32 dwSize, int a4, ut32 *lpExitCode) {
#if __WINDOWS__
static int RunRemoteThread_(libbochs_t* b, const ut8 *lpBuffer, ut32 dwSize, int a4, ut32 *lpExitCode) {
LPVOID pProcessMemory;
HANDLE hInjectThread;
int result = 0;
HANDLE hInjectThread;
int result = 0;
signed int tmpResult;
DWORD NumberOfBytesWritten;
DWORD NumberOfBytesWritten;
tmpResult = 0;
pProcessMemory = VirtualAllocEx(b->processInfo.hProcess, 0, dwSize, 0x1000u, 0x40u);
@ -39,28 +39,26 @@ int RunRemoteThread_(libbochs_t* b, const ut8 *lpBuffer, ut32 dwSize, int a4, ut
result = tmpResult;
}
return result;
#else
return 0;
#endif
}
#endif
void ResetBuffer_(libbochs_t* b) {
void bochs_reset_buffer(libbochs_t* b) {
memset (b->data, 0, SIZE_BUF);
b->punteroBuffer = 0;
}
bool CommandStop_(libbochs_t * b) {
bool bochs_cmd_stop(libbochs_t * b) {
#if __WINDOWS__
HMODULE hKernel;
DWORD ExitCode;
DWORD apiOffset = 0;
char buffer[] = {
char buffer[] = {
0x68, 0x00, 0x00, 0x00, 0x00, //push 0
0x68, 0x00, 0x00, 0x00, 0x00, //push 0
0xE8, 0x00, 0x00, 0x00, 0x00, //call $
0x83, 0x04, 0x24, 0x0A, //add [esp], 0A
0x68, 0x30, 0x30, 0x30, 0x30, //push GenerateConsoleCtrlEvent
0xC3, //retn
0xC3, //retn
0xC2, 0x04, 0x00, //retn 4
0xeb, 0xfe //jmp $
};
@ -74,22 +72,22 @@ bool CommandStop_(libbochs_t * b) {
#endif
}
bool WaitForReply_(libbochs_t *b) {
bool bochs_wait(libbochs_t *b) {
#if __WINDOWS__
int times = 0;
DWORD dwRead,aval,leftm;
times = 100; // reintenta durante 10 segundos
ResetBuffer_(b);
bochs_reset_buffer(b);
do {
while (PeekNamedPipe (b->hReadPipeIn, NULL, 0, NULL, &aval, &leftm)) {
if (aval < 0) break;
if (!ReadFile(b->hReadPipeIn, &b->data[b->punteroBuffer], SIZE_BUF, &dwRead, 0)) {
lprintf("WaitForReply_: !!ERROR Leyendo datos del pipe.\n\n");
lprintf("bochs_wait: !!ERROR Leyendo datos del pipe.\n\n");
return false;
}
//lprintf("mythreadlector: %x %x\n",NumberOfBytesRead,punteroBuffer);
if (dwRead)
b->punteroBuffer +=dwRead;
b->punteroBuffer +=dwRead;
}
if (strstr (b->data, "<bochs:")) {
break;
@ -106,18 +104,18 @@ void bochs_send_cmd(libbochs_t* b, const char * comando, bool bWait) {
#if __WINDOWS__
//lprintf("Enviando comando: %s\n",comando);
DWORD dwWritten;
ResetBuffer_(b);
bochs_reset_buffer(b);
ZeroMemory(cmdBuff,128);
sizeSend=sprintf(cmdBuff,"%s\n",comando);
WriteFile(b->hWritePipeOut, cmdBuff, strlen(cmdBuff), &dwWritten, NULL);
if (bWait)
WaitForReply_(b);
bochs_wait(b);
#else
#warning TODO bochs_send_cmd not implemented for this platform
#endif
}
int bochs_read_(libbochs_t* b, ut64 addr, int count, ut8 * buf) {
int bochs_read(libbochs_t* b, ut64 addr, int count, ut8 * buf) {
char buff[128];
int lenRec = 0,i = 0,ini = 0, fin = 0, pbuf = 0, totalread = 0;
totalread = (count >SIZE_BUF / 3)? SIZE_BUF / 3: count;
@ -146,7 +144,7 @@ int bochs_read_(libbochs_t* b, ut64 addr, int count, ut8 * buf) {
return 0;
}
void bochs_close_(libbochs_t* b) {
void bochs_close(libbochs_t* b) {
b->isRunning = false;
#if __WINDOWS__
CloseHandle (b->hReadPipeIn);
@ -161,53 +159,61 @@ void bochs_close_(libbochs_t* b) {
free (cmdBuff);
}
bool bochs_open_(libbochs_t* b, const char * rutaBochs, const char * rutaConfig) {
bool bochs_open(libbochs_t* b, const char * rutaBochs, const char * rutaConfig) {
bool result = false;
#if __WINDOWS__
struct _SECURITY_ATTRIBUTES PipeAttributes;
char commandline[1024];
// alojamos el buffer de datos
b->data = malloc(SIZE_BUF);
lpTmpBuffer = malloc(SIZE_BUF);
cmdBuff = malloc(128);
lprintf("bochs_open: invocado\n");
b->data = malloc (SIZE_BUF);
if (!b->data) return false;
lpTmpBuffer = malloc (SIZE_BUF);
if (!lpTmpBuffer) {
R_FREE (b->data);
return false;
}
cmdBuff = malloc (128);
if (!cmdBuff) {
R_FREE (b->data);
free (lpTmpBuffer);
return false;
}
// creamos los pipes
PipeAttributes.nLength = 12;
PipeAttributes.bInheritHandle = 1;
PipeAttributes.lpSecurityDescriptor = 0;
//
result = FALSE;
if (CreatePipe(&b->hReadPipeIn, &b->hReadPipeOut, &PipeAttributes, SIZE_BUF) &&
CreatePipe(&b->hWritePipeIn, &b->hWritePipeOut, &PipeAttributes, SIZE_BUF)
if (CreatePipe (&b->hReadPipeIn, &b->hReadPipeOut, &PipeAttributes, SIZE_BUF) &&
CreatePipe (&b->hWritePipeIn, &b->hWritePipeOut, &PipeAttributes, SIZE_BUF)
) {
// Inicializamos las estructuras
ZeroMemory(&b->info, sizeof(STARTUPINFO));
ZeroMemory(&b->processInfo, sizeof(PROCESS_INFORMATION));
b->info.cb = sizeof(STARTUPINFO);
memset (&b->info, 0, sizeof (STARTUPINFO));
memset (&b->processInfo, 0, sizeof (PROCESS_INFORMATION));
b->info.cb = sizeof (STARTUPINFO);
// Asignamos los pipes
b->info.hStdError = b->hReadPipeOut;
b->info.hStdOutput = b->hReadPipeOut;
b->info.hStdInput = b->hWritePipeIn;
b->info.dwFlags |= STARTF_USESTDHANDLES;
// Creamos el proceso
sprintf(commandline, "\"%s\" -f \"%s\" -q ",rutaBochs,rutaConfig);
snprintf (commandline, sizeof (commandline), "\"%s\" -f \"%s\" -q ",rutaBochs,rutaConfig);
lprintf("*** Creando proces: %s\n",commandline);
if (CreateProcessA(NULL, commandline, NULL, NULL,TRUE, CREATE_NEW_CONSOLE , NULL, NULL, &b->info, &b->processInfo)) {
lprintf ("Proceso spawneado\n");
if (CreateProcessA (NULL, commandline, NULL, NULL, TRUE, CREATE_NEW_CONSOLE , NULL, NULL, &b->info, &b->processInfo)) {
lprintf ("Process created\n");
WaitForInputIdle (b->processInfo.hProcess, INFINITE);
lprintf ("Entrada inicializada\n");
lprintf ("Initialized input\n");
b->isRunning = true;
//CreateThread(NULL, 0, MyThLector_, b, 0, 0);
//b->ghWriteEvent = CreateEvent(NULL, TRUE, FALSE, TEXT("WriteEvent"));
//CreateThread(NULL, 0, MyThEscritor_, b, 0, 0);
ResetBuffer_(b);
lprintf("Esperando inicializacion de bochs.\n");
if (WaitForReply_(b)) {
lprintf("Inicializacion completa.\n");
bochs_reset_buffer (b);
eprintf ("Waiting for bochs...\n");
if (bochs_wait(b)) {
eprintf ("Ready.\n");
result = true;
} else {
bochs_close_(b);
bochs_close (b);
}
}
}