Many syntax, language, indent, naming fixes for bochs

This commit is contained in:
pancake 2016-03-25 00:21:27 +01:00
parent c45fe4e1e7
commit 434b8251e9
5 changed files with 264 additions and 249 deletions

View File

@ -5,19 +5,19 @@
#include <r_asm.h>
#include <r_debug.h>
#include <libbochs.h>
static bool bCapturaRegs = TRUE;
static bool bStep = FALSE;
static bool bBreak = FALSE;
static bool bAjusta = TRUE;
static char * saveRegs;
static ut64 ripParada=0;
static bool bCapturaRegs = true;
static bool bStep = false;
static bool bBreak = false;
static bool bAjusta = true;
static char *saveRegs;
static ut64 ripStop = 0LL;
typedef struct {
libbochs_t desc;
} RIOBochs;
static libbochs_t *desc = NULL;
#define eprintf(x,y...) \
{ FILE * myfile; myfile=fopen("logio.txt","a"); fprintf(myfile,x,##y);fflush(myfile);fclose(myfile); }
static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
char cmd[50];
@ -26,16 +26,14 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
char bufcmd[100];
ut64 a;
int n,i,lenRec;
eprintf("bochs_breakpoint\n");
eprintf ("bochs_breakpoint\n");
if (!bp) return false;
if (set) {
//eprintf("[set] bochs_breakpoint %016"PFMT64x"\n",bp->addr);
sprintf(cmd,"lb 0x%x",(DWORD)bp->addr);
EnviaComando_(desc,cmd,TRUE);
bCapturaRegs = TRUE;
}
else
{
sprintf (cmd, "lb 0x%x", (ut32)bp->addr);
bochs_send_cmd(desc,cmd,true);
bCapturaRegs = true;
} else {
//eprintf("[unset] bochs_breakpoint %016"PFMT64x"\n",bp->addr);
/*
Num Type Disp Enb Address
@ -43,14 +41,12 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
2 lbreakpoint keep y 0x0000000000007c00
<bochs:39>
*/
EnviaComando_(desc,"blist",TRUE);
bochs_send_cmd(desc,"blist",true);
lenRec = strlen(desc->data);
a=-1;
if (!strncmp(desc->data, "Num Type", 8))
{
if (!strncmp(desc->data, "Num Type", 8)) {
i = 37;
do
{
do {
if (desc->data[i + 24] == 'y') {
strncpy(num, &desc->data[i], 3);
num[3] = 0;
@ -65,11 +61,10 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) {
i += 48;
} while (desc->data[i] != '<' && i<lenRec-4);
}
if (a==bp->addr)
{
sprintf(bufcmd,"d %i",n);
if (a == bp->addr) {
snprintf (bufcmd,sizeof (bufcmd), "d %i",n);
//eprintf("[unset] Break point localizado indice = %x (%x) %s \n",n,(DWORD)a,bufcmd);
EnviaComando_(desc,bufcmd,TRUE);
bochs_send_cmd(desc,bufcmd,true);
}
}
@ -81,23 +76,21 @@ static int r_debug_bochs_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
char regname[4];
char strBase[19];
char strLimit[19];
int i = 0,pos = 0, lenRec = 0, posRIP = 0;;
ut64 val=0, valRIP = 0;
ut16 val1=0;
int i = 0, pos = 0, lenRec = 0;
ut64 val = 0, valRIP = 0, posRIP = 0;
eprintf("bochs_reg_read\n");
if (bCapturaRegs == TRUE) {
EnviaComando_(desc,"regs",TRUE);
eprintf ("bochs_reg_read\n");
if (bCapturaRegs == true) {
bochs_send_cmd(desc, "regs", true);
//r14: 00000000_00000000 r15: 00000000_00000000
//rip: 00000000_0000e07b
//eflags 0x00000046: id vip vif ac vm rf nt IOPL=0 of df if tf sf ZF af PF cf
//<bochs:109>return -1;
pos=0x78;
lenRec = strlen(desc->data);
pos = 0x78;
lenRec = strlen (desc->data);
while (desc->data[i] != 0 && i < lenRec -4 ) {
if ( (desc->data[i] == (BYTE)'r' && desc->data[i + 3] == (BYTE)':')) {
strncpy(regname, &desc->data[i], 3);
if ( (desc->data[i] == (ut8)'r' && desc->data[i + 3] == (ut8)':')) {
strncpy (regname, &desc->data[i], 3);
regname[3] = 0;
strncpy(&strReg[2], &desc->data[i + 5], 8);
strncpy(&strReg[10], &desc->data[i + 14], 8);
@ -109,36 +102,26 @@ static int r_debug_bochs_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
// eprintf("parseado %s = %s valx64 = %016"PFMT64x"\n", regname, strReg,val);
memcpy(&buf[pos],&val,8);
// guardamos la posicion del rip y su valor para ajustarlo al obtener el CS
if (!strncmp(regname,"rip",3))
{
if (!strncmp(regname,"rip",3)) {
posRIP = pos;
valRIP = val;
}
pos+=8;
}
else
i++;
} else i++;
}
EnviaComando_(desc,"info cpu",TRUE);
if (strstr(desc->data,"PC_32"))
{
bAjusta = TRUE;
bochs_send_cmd (desc, "info cpu", true);
if (strstr (desc->data,"PC_32")) {
bAjusta = true;
//eprintf("[modo PC_32]\n");
}
else if (strstr(desc->data,"PC_80"))
{
bAjusta = FALSE;
} else if (strstr (desc->data,"PC_80")) {
bAjusta = false;
//eprintf("[modo PC_80]\n");
}
else if (strstr(desc->data,"PC_64"))
{
bAjusta = FALSE;
} else if (strstr (desc->data,"PC_64")) {
bAjusta = false;
//eprintf("[modo PC_64]\n");
}
else
eprintf("[modo desconocido] \n%s\n",desc->data);
} else eprintf ("[unknown mode]\n%s\n",desc->data);
/*
es:0x0000, dh=0x00009300, dl=0x0000ffff, valid=7
Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
@ -157,44 +140,46 @@ static int r_debug_bochs_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
gdtr:base=0x0000000000000000, limit=0xffff
idtr:base=0x0000000000000000, limit=0xffff
*/
EnviaComando_(desc,"sreg",TRUE);
bochs_send_cmd (desc,"sreg",true);
pos = 0x38;
char * s [] = { "es:0x", "cs:0x","ss:0x","ds:0x","fs:0x","gs:0x",0};
for (int n = 0; s[n] != 0; n++) {
if ((i=strstr(desc->data,s[n]))) {
strncpy(&strReg[0], i+3, 7);
const char *x;
int n;
for (n = 0; s[n] != 0; n++) {
if ((x=strstr(desc->data,s[n]))) {
strncpy (&strReg[0], x+3, 7);
strReg[6] = 0;
val = r_num_get(NULL,strReg);
strncpy(regname,s[n],2);
regname[2]=0;
if ((i = strstr(i, "base="))) {
strncpy(strBase, i + 5, 10);
val = r_num_get (NULL, strReg);
strncpy (regname, s[n], 2);
regname[2] = 0;
if ((x = strstr (x, "base="))) {
strncpy (strBase, x + 5, 10);
strBase[10] = 0;
}
if ((i = strstr(i, "limit="))) {
strncpy(strLimit, i + 6, 10);
if ((x = strstr (x, "limit="))) {
strncpy (strLimit, x + 6, 10);
strLimit[10] = 0;
}
//eprintf("%s localizado %s %04x base = %s limit = %s\n",regname,strReg,(WORD)val,strBase,strLimit);
memcpy(&buf[pos],&val,2);
memcpy (&buf[pos], &val, 2);
pos+= 2;
if (bAjusta) {
if (!strncmp(regname,"cs",2)) {
valRIP+=(val*0x10); // desplazamos CS y lo añadimos a RIP
if (!strncmp (regname,"cs",2)) {
valRIP += (val*0x10); // desplazamos CS y lo añadimos a RIP
//eprintf("%016"PFMT64x"\n",valRIP);
}
}
}
}
// Cheat para evitar traducciones de direcciones
if (ripParada!=0)
memcpy(&buf[0],&ripParada,8);
if (ripStop!=0)
memcpy(&buf[0],&ripStop,8);
else
memcpy(&buf[0],&valRIP,8); // guardamos el valor cs:ip en el registro virtual "vip"
//eprintf("guardando regs procesados%x\n",size);
memcpy(saveRegs,buf,size);
bCapturaRegs = FALSE;
memcpy (saveRegs,buf,size);
bCapturaRegs = false;
//eprintf("bochs_reg_read\n");
} else {
memcpy(buf,saveRegs,size);
@ -216,97 +201,96 @@ void map_free(RDebugMap *map) {
static RList *r_debug_bochs_map_get(RDebug* dbg) { //TODO
eprintf("bochs_map_getdebug:\n");
RDebugMap *mr;
RList *list = r_list_new();
RList *list = r_list_newf((RListFree)map_free);
if (!list)
return NULL;
list->free=(RListFree)map_free;
mr = R_NEW0 (RDebugMap);
mr->name = strdup ("fake");
mr->addr = 0;
mr->addr_end = 0xffffffff;
mr->size = 0xffffffff;
mr->addr_end = UT32_MAX;
mr->size = UT32_MAX;
mr->perm = 0;
mr->user = 0;
if (mr != NULL) r_list_append (list, mr);
if (mr) r_list_append (list, mr);
return list;
}
static int r_debug_bochs_step(RDebug *dbg) {
eprintf("bochs_step\n");
EnviaComando_(desc,"s",TRUE);
bCapturaRegs = TRUE;
bStep = TRUE;
eprintf ("bochs_step\n");
bochs_send_cmd (desc,"s",true);
bCapturaRegs = true;
bStep = true;
return true;
}
static int r_debug_bochs_continue(RDebug *dbg, int pid, int tid, int sig) {
eprintf("bochs_continue:\n");
EnviaComando_(desc,"c",FALSE);
bCapturaRegs = TRUE;
bBreak = FALSE;
eprintf ("bochs_continue:\n");
bochs_send_cmd (desc, "c", false);
bCapturaRegs = true;
bBreak = false;
return true;
}
static void bochs_debug_break(void *u) {
eprintf("bochs_debug_break: Enviando break ...\n");
CommandStop_(desc);
bBreak = TRUE;
eprintf("bochs_debug_break: Sending break...\n");
CommandStop_ (desc);
bBreak = true;
}
static int r_debug_bochs_wait(RDebug *dbg, int pid) {
eprintf("bochs_wait:\n");
char strIP[19];
int ini = 0, fin = 0, i = 0;
int i = 0;
const char *x;
char *ini = 0, *fin = 0;
eprintf ("bochs_wait:\n");
if (bStep) {
bStep = FALSE;
bStep = false;
} else {
r_cons_break (bochs_debug_break, dbg);
i =500;
i = 500;
do {
EsperaRespuesta_(desc);
WaitForReply_(desc);
if (bBreak) {
if (desc->data[0]!=0) {
eprintf("parada por ctrl+c %s\n",desc->data);
bBreak = FALSE;
if (desc->data[0]) {
eprintf("ctrl+c %s\n", desc->data);
bBreak = false;
break;
}
i--;
if (!i) {
bBreak = FALSE;
eprintf("parada por ctrl+c sin respuesta.\n");
bBreak = false;
eprintf ("empty ctrl+c.\n");
break;
}
}
// leer buffer para comprobar si hay parada por breakpoint
else if(desc->data[0]!=0) {
//eprintf("parada por break point %s\n",desc->data);
} else if (desc->data[0]) {
//eprintf("stop on breakpoint%s\n",desc->data);
break;
}
} while(1);
}
eprintf("bochs_wait: fuera while\n");
i=0;
eprintf ("bochs_wait: loop done\n");
i = 0;
// Next at t=394241428
// (0) [0x000000337635] 0020:0000000000337635 (unk. ctxt): add eax, esi ; 03c6
ripParada = 0;
if ( (i=strstr(desc->data,"Next at")))
{
if ((ini=strstr(i,"[0x"))) {
if ((fin=strstr(ini,"]"))) {
strncpy(strIP,ini+1,fin - ini-1);
strIP[fin-ini-1]=0;
ripStop = 0;
if ((x = strstr (desc->data, "Next at"))) {
if ((ini = strstr (x, "[0x"))) {
if ((fin = strstr (ini,"]"))) {
int len = fin - ini - 1;
strncpy (strIP, ini+1, len);
strIP[len] = 0;
//eprintf(" parada EIP = %s\n",strIP);
ripParada = r_num_get(NULL,strIP);
ripStop = r_num_get (NULL, strIP);
}
}
}
desc->data[0]=0;
desc->data[0] = 0;
return true;
}
static int r_debug_bochs_stop(RDebug *dbg) {
eprintf("bochs_stop:\n");
//RIOBdescbg *o = dbg->iob.io->desc->data;
@ -317,20 +301,20 @@ static int r_debug_bochs_stop(RDebug *dbg) {
static int r_debug_bochs_attach(RDebug *dbg, int pid) {
eprintf("bochs_attach:\n");
RIODesc *d = dbg->iob.io->desc;
eprintf ("bochs_attach:\n");
dbg->swstep = false;
if (d && d->plugin && d->plugin->name && d->data) {
if (!strcmp ("bochs", d->plugin->name)) {
RIOBochs *g = d->data;
int arch = r_sys_arch_id (dbg->arch);
int bits = dbg->anal->bits;
//int arch = r_sys_arch_id (dbg->arch);
// int bits = dbg->anal->bits;
if (( desc = &g->desc )) {
eprintf("bochs attach: ok\n");
saveRegs = malloc(1024);
bCapturaRegs = TRUE;
bStep = FALSE;
bBreak = FALSE;
bCapturaRegs = true;
bStep = false;
bBreak = false;
}
}
}
@ -338,8 +322,8 @@ static int r_debug_bochs_attach(RDebug *dbg, int pid) {
}
static int r_debug_bochs_detach(RDebug *dbg, int pid) {
eprintf("bochs_detach:\n");
free(saveRegs);
eprintf ("bochs_detach:\n");
free (saveRegs);
return true;
}
@ -525,8 +509,7 @@ static const char *r_debug_bochs_reg_profile(RDebug *dbg) {
return NULL;
}
struct r_debug_plugin_t r_debug_plugin_bochs = {
RDebugPlugin r_debug_plugin_bochs = {
.name = "bochs",
/* TODO: Add support for more architectures here */
.license = "LGPL3",

View File

@ -5,8 +5,6 @@
#include <r_lib.h>
#include <r_util.h>
#include <libbochs.h>
#define eprintf(x,y...) \
{ FILE * myfile; myfile=fopen("logio.txt","a"); fprintf(myfile,x,##y);fflush(myfile);fclose(myfile); }
typedef struct {
libbochs_t desc;
@ -14,6 +12,7 @@ typedef struct {
static libbochs_t *desc = NULL;
static RIODesc *riobochs = NULL;
extern RIOPlugin r_io_plugin_bochs; // forward declaration
static int __plugin_open(RIO *io, const char *file, ut8 many) {
return !strncmp (file, "bochs://", strlen ("bochs://"));
@ -21,41 +20,38 @@ static int __plugin_open(RIO *io, const char *file, ut8 many) {
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
RIOBochs *riob;
eprintf("io_open\n");
lprintf("io_open\n");
const char *i;
char * archivoBochs;
char * archivoCfg;
int i,l;
int l;
if (!__plugin_open (io, file, 0))
return NULL;
if (riobochs) {
return riobochs;
}
archivoBochs=malloc(1024);
archivoCfg= malloc (1024);
archivoBochs = malloc (1024);
archivoCfg = malloc (1024);
i=strstr(&file[8],"#");
if (i)
{
l=i - (DWORD)&file[8];
strncpy(archivoBochs,&file[8],l<1024?l:1024);
i = strstr (file+8, "#");
if (i) {
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;
}
else
{
} else {
free(archivoBochs);
free(archivoCfg);
eprintf("Error cant find : \n");
lprintf("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);
@ -63,18 +59,18 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
//free(archivoCfg);
return riobochs;
}
eprintf ("bochsio.open: Cannot connect to bochs.\n");
lprintf ("bochsio.open: Cannot connect to bochs.\n");
free (riob);
return NULL;
}
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
eprintf("io_write\n");
lprintf("io_write\n");
return -1;
}
static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
eprintf("io_seek %016"PFMT64x" \n",offset);
lprintf("io_seek %016"PFMT64x" \n",offset);
return offset;
}
@ -82,38 +78,36 @@ static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
memset (buf, 0xff, count);
ut64 addr = io->off;
if (!desc || !desc->data) return -1;
eprintf("io_read ofs= %016"PFMT64x" count= %x\n",io->off,count);
lprintf("io_read ofs= %016"PFMT64x" count= %x\n",io->off,count);
bochs_read_(desc,addr,count,buf);
return count;
}
static int __close(RIODesc *fd) {
eprintf("io_close\n");
lprintf("io_close\n");
bochs_close_(desc);
return true;
}
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
eprintf("system command (%s)\n", cmd);
lprintf ("system command (%s)\n", cmd);
if (!strcmp (cmd, "help")) {
eprintf ("Usage: =!cmd args\n"
lprintf ("Usage: =!cmd args\n"
" =!:<bochscmd> - Send a bochs command.\n"
" =!dobreak - pause bochs.\n");
} else if (!strncmp (cmd, ":", 1)) {
eprintf("io_system: Enviando comando bochs\n");
EnviaComando_(desc,&cmd[1],TRUE);
lprintf ("io_system: Enviando comando bochs\n");
SendCommand_ (desc, &cmd[1], true);
io->cb_printf ("%s\n", desc->data);
return 1;
} else if (!strncmp (cmd, "dobreak", 7)) {
CommandStop_(desc);
CommandStop_ (desc);
io->cb_printf ("%s\n", desc->data);
return 1;
}
return true;
}
RIOPlugin r_io_plugin_bochs = {
RIOPlugin r_io_plugin_bochs = {
.name = "bochs",
.desc = "Attach to a BOCHS debugger",
.license = "LGPL3",
@ -126,3 +120,11 @@ RIOPlugin r_io_plugin_bochs = {
.system = __system,
.isdbg = true
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_bochs,
.version = R2_VERSION
};
#endif

View File

@ -180,10 +180,10 @@ fs.udf
fs.ufs
fs.xfs
io.bfdbg
io.bochs
io.debug
io.default
io.gdb
io.bochs
io.r2pipe
io.gzip
io.http

View File

@ -1,12 +1,18 @@
/*! \file */
#ifndef LIBBOCHS_H
#define LIBBOCHS_H
#include <r_util.h>
#if __WINDOWS__
#include <windows.h>
#endif
typedef struct libbochs_t {
char * data;
char *data;
int punteroBuffer;
int sizeSend;
#if __WINDOWS__
HANDLE hReadPipeIn;
HANDLE hReadPipeOut;
HANDLE hWritePipeIn;
@ -14,19 +20,28 @@ typedef struct libbochs_t {
HANDLE ghWriteEvent;
PROCESS_INFORMATION processInfo;
STARTUPINFO info;
BOOL bEjecuta;
#endif
bool isRunning;
} libbochs_t;
//DWORD WINAPI MyThLector_(LPVOID lpParam)
//DWORD WINAPI MyThEscritor_(LPVOID lpParam)
BOOL EsperaRespuesta_(libbochs_t *b);
int EjecutaThreadRemoto_(libbochs_t* b, LPVOID lpBuffer, DWORD dwSize, int a4, LPDWORD lpExitCode);
bool WaitForReply_(libbochs_t *b);
int RunRemoteThread_(libbochs_t* b, const ut8* lpBuffer, ut32 dwSize, int a4, ut32 *lpExitCode);
void ResetBuffer_(libbochs_t* b);
BOOL CommandStop_(libbochs_t * b);
VOID EnviaComando_(libbochs_t* b, char * comando,BOOL bWait);
int bochs_read_(libbochs_t* b,ut64 addr,int count,ut8 * buf);
bool CommandStop_(libbochs_t * b);
void SendCommand_(libbochs_t* b, const char * comando, bool bWait);
int bochs_read_(libbochs_t* b, ut64 addr, int count, ut8* buf);
void bochs_close_(libbochs_t* b);
BOOL bochs_open_(libbochs_t* b ,char * rutaBochs, char * rutaConfig);
bool bochs_open_(libbochs_t* b, const char *rutaBochs, const char *rutaConfig);
#define ENABLE_DEBUG 0
#if ENABLE_DEBUG
#define lprintf(x,y...) { FILE *fd;fd=fopen("bochs.io.log", "a"); if (fd) {fprintf(fd,x,##y);fflush(fd);fclose(fd); }}
#else
#define lprintf(x,y...) {}
#endif
/*!

View File

@ -1,15 +1,16 @@
/* libbochs - LGPL - Copyright 2016 - SkUaTeR */
/* libbochs - radare2 - LGPL - Copyright 2016 - SkUaTeR */
#include "libbochs.h"
static char * lpTmpBuffer; //[0x2800u];
static char * cmdBuff;//[128];
int sizeSend=0;
static char *lpTmpBuffer; //[0x2800u];
static char *cmdBuff;//[128];
int sizeSend = 0;
#define SIZE_BUF 0x5800 * 2
#define eprintf(x,y...) \
{ FILE * myfile; myfile=fopen("logio.txt","a"); fprintf(myfile,x,##y);fflush(myfile);fclose(myfile); }
int EjecutaThreadRemoto_(libbochs_t* b, LPVOID lpBuffer, DWORD dwSize, int a4, LPDWORD lpExitCode)
{
int RunRemoteThread_(libbochs_t* b, const ut8 *lpBuffer, ut32 dwSize, int a4, ut32 *lpExitCode) {
#if __WINDOWS__
LPVOID pProcessMemory;
HANDLE hInjectThread;
int result = 0;
@ -18,35 +19,38 @@ int EjecutaThreadRemoto_(libbochs_t* b, LPVOID lpBuffer, DWORD dwSize, int a4, L
tmpResult = 0;
pProcessMemory = VirtualAllocEx(b->processInfo.hProcess, 0, dwSize, 0x1000u, 0x40u);
if (pProcessMemory)
{
if (WriteProcessMemory(b->processInfo.hProcess, pProcessMemory, lpBuffer, dwSize, &NumberOfBytesWritten))
{
if (pProcessMemory) {
if (WriteProcessMemory(b->processInfo.hProcess, pProcessMemory, lpBuffer, dwSize, &NumberOfBytesWritten)) {
hInjectThread = CreateRemoteThread(b->processInfo.hProcess, 0, 0, pProcessMemory, 0, 0, 0);
if (hInjectThread)
{
if (hInjectThread) {
if (!WaitForSingleObject(hInjectThread, 0xFFFFFFFF)
&& (!a4 || ReadProcessMemory(b->processInfo.hProcess, pProcessMemory, lpBuffer, dwSize, &NumberOfBytesWritten)))
&& (!a4 || ReadProcessMemory (b->processInfo.hProcess,
pProcessMemory, lpBuffer, dwSize, &NumberOfBytesWritten)))
{
if (lpExitCode)
GetExitCodeThread(hInjectThread, lpExitCode);
GetExitCodeThread (hInjectThread, lpExitCode);
tmpResult = 1;
}
}
}
VirtualFreeEx(b->processInfo.hProcess, pProcessMemory, 0, 0x8000u);
VirtualFreeEx (b->processInfo.hProcess, pProcessMemory, 0, 0x8000u);
if (hInjectThread)
CloseHandle(hInjectThread);
CloseHandle (hInjectThread);
result = tmpResult;
}
return result;
#else
return 0;
#endif
}
void ResetBuffer_(libbochs_t* b)
{
ZeroMemory(b->data, SIZE_BUF);
void ResetBuffer_(libbochs_t* b) {
memset (b->data, 0, SIZE_BUF);
b->punteroBuffer = 0;
}
BOOL CommandStop_(libbochs_t * b) {
bool CommandStop_(libbochs_t * b) {
#if __WINDOWS__
HMODULE hKernel;
DWORD ExitCode;
DWORD apiOffset = 0;
@ -63,58 +67,68 @@ BOOL CommandStop_(libbochs_t * b) {
hKernel = GetModuleHandleA("kernel32");
apiOffset = (DWORD)GetProcAddress(hKernel, "GenerateConsoleCtrlEvent");
*((DWORD *)&buffer[20]) = apiOffset;
ExitCode = EjecutaThreadRemoto_(b, &buffer, 0x1Eu, 0, &ExitCode) && ExitCode;
ExitCode = RunRemoteThread_(b, &buffer, 0x1Eu, 0, &ExitCode) && ExitCode;
return ExitCode;
#else
return 0;
#endif
}
BOOL EsperaRespuesta_(libbochs_t *b) {
int veces = 0;
bool WaitForReply_(libbochs_t *b) {
#if __WINDOWS__
int times = 0;
DWORD dwRead,aval,leftm;
veces = 100; // reintenta durante 10 segundos
times = 100; // reintenta durante 10 segundos
ResetBuffer_(b);
do {
while(PeekNamedPipe(b->hReadPipeIn,NULL,0,NULL,&aval,&leftm)) {
if (aval>0) {
if (!ReadFile(b->hReadPipeIn, &b->data[b->punteroBuffer], SIZE_BUF, &dwRead, 0))
{
eprintf("EsperaRespuesta_: !!ERROR Leyendo datos del pipe.\n\n");
return FALSE;
}
//eprintf("mythreadlector: %x %x\n",NumberOfBytesRead,punteroBuffer);
if (dwRead)
b->punteroBuffer +=dwRead;
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");
return false;
}
else
break;
//lprintf("mythreadlector: %x %x\n",NumberOfBytesRead,punteroBuffer);
if (dwRead)
b->punteroBuffer +=dwRead;
}
if (strstr(b->data, "<bochs:")) {
if (strstr (b->data, "<bochs:")) {
break;
}
Sleep(10);
} while(--veces);
return TRUE;
Sleep (10);
} while (--times);
return true;
#else
return false;
#endif
}
VOID EnviaComando_(libbochs_t* b, char * comando, BOOL bWait) {
//eprintf("Enviando comando: %s\n",comando);
void bochs_send_cmd(libbochs_t* b, const char * comando, bool bWait) {
#if __WINDOWS__
//lprintf("Enviando comando: %s\n",comando);
DWORD dwWritten;
ResetBuffer_(b);
ZeroMemory(cmdBuff,128);
sizeSend=sprintf(cmdBuff,"%s\n",comando);
WriteFile(b->hWritePipeOut, cmdBuff, strlen(cmdBuff), &dwWritten, NULL);
if (bWait)
EsperaRespuesta_(b);
WaitForReply_(b);
#else
#warning TODO
#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;
if (count >SIZE_BUF / 3)
totalread=SIZE_BUF / 3;
else
totalread=count;
sprintf(buff,"xp /%imb 0x%016"PFMT64x"",totalread,addr);
EnviaComando_(b,buff,TRUE);
eprintf("%s\n",b->data);
lenRec=strlen(b->data);
if (!strncmp(b->data, "[bochs]:", 8)) {
totalread = (count >SIZE_BUF / 3)? SIZE_BUF / 3: count;
snprintf(buff, sizeof (buff), "xp /%imb 0x%016"PFMT64x"",totalread,addr);
bochs_send_cmd (b, buff, true);
lprintf ("%s\n", b->data);
lenRec = strlen (b->data);
if (!strncmp (b->data, "[bochs]:", 8)) {
i += 10; // nos sitiamos en la siguiente linea.
do {
while (b->data[i] != 0 && b->data[i] != ':' && i < lenRec) // buscamos los :
@ -125,7 +139,7 @@ int bochs_read_(libbochs_t* b,ut64 addr,int count,ut8 * buf) {
fin = i++;
b->data[fin] = 0;
pbuf+=r_hex_str2bin(&b->data[ini],&buf[pbuf]);
//eprintf("%s\n", &lpBuffer[ini]);
//lprintf("%s\n", &lpBuffer[ini]);
i++; // siguiente linea
} while (b->data[i] != '<' && i < lenRec);
}
@ -133,29 +147,30 @@ int bochs_read_(libbochs_t* b,ut64 addr,int count,ut8 * buf) {
}
void bochs_close_(libbochs_t* b) {
b->bEjecuta=FALSE;
CloseHandle(b->hReadPipeIn);
CloseHandle(b->hReadPipeOut);
CloseHandle(b->hWritePipeIn);
CloseHandle(b->hWritePipeOut);
CloseHandle(b->ghWriteEvent);
TerminateProcess(b->processInfo.hProcess,0);
b->isRunning = false;
#if __WINDOWS__
CloseHandle (b->hReadPipeIn);
CloseHandle (b->hReadPipeOut);
CloseHandle (b->hWritePipeIn);
CloseHandle (b->hWritePipeOut);
CloseHandle (b->ghWriteEvent);
TerminateProcess (b->processInfo.hProcess,0);
#endif
free(b->data);
free(lpTmpBuffer);
free(cmdBuff);
free (lpTmpBuffer);
free (cmdBuff);
}
BOOL bochs_open_(libbochs_t* b ,char * rutaBochs, char * rutaConfig) {
bool bochs_open_(libbochs_t* b, const char * rutaBochs, const char * rutaConfig) {
bool result = false;
#if __WINDOWS__
struct _SECURITY_ATTRIBUTES PipeAttributes;
BOOL result;
char commandline[1024];
// alojamos el buffer de datos
b->data = malloc(SIZE_BUF);
lpTmpBuffer = malloc(SIZE_BUF);
cmdBuff = malloc(128);
eprintf("bochs_open: invocado\n");
lprintf("bochs_open: invocado\n");
// creamos los pipes
PipeAttributes.nLength = 12;
PipeAttributes.bInheritHandle = 1;
@ -176,26 +191,26 @@ BOOL bochs_open_(libbochs_t* b ,char * rutaBochs, char * rutaConfig) {
b->info.dwFlags |= STARTF_USESTDHANDLES;
// Creamos el proceso
sprintf(commandline, "\"%s\" -f \"%s\" -q ",rutaBochs,rutaConfig);
eprintf("*** Creando proces: %s\n",commandline);
lprintf("*** Creando proces: %s\n",commandline);
if (CreateProcessA(NULL, commandline, NULL, NULL,TRUE, CREATE_NEW_CONSOLE , NULL, NULL, &b->info, &b->processInfo)) {
eprintf("Proceso spawneado\n");
WaitForInputIdle(b->processInfo.hProcess, INFINITE);
eprintf("Entrada inicializada\n");
lprintf ("Proceso spawneado\n");
WaitForInputIdle (b->processInfo.hProcess, INFINITE);
lprintf ("Entrada inicializada\n");
b->bEjecuta=TRUE;
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);
eprintf("Esperando inicializacion de bochs.\n");
if (EsperaRespuesta_(b)) {
eprintf("Inicializacion completa.\n");
result = TRUE;
}
else
lprintf("Esperando inicializacion de bochs.\n");
if (WaitForReply_(b)) {
lprintf("Inicializacion completa.\n");
result = true;
} else {
bochs_close_(b);
}
}
}
#endif
return result;
}