Wednesday, June 20, 2007

Debug your applications without recompilation

Leave a Comment
Scenario:
You wanna debug an application but you dont have the code for it?
You suspect some functions that are failing, you dont feel like recompiling whole stuff in debug mode or put in print statements.

Solution:
Hook the APIs using LD_PRELOAD. (refer to my earlier post on hooking Linux)

Here is the code that I compiled into a .so which help me debug an issue in a binary. I wanted to get debug prints of a function that copies files to a directory. I needed to put print statements in the function, rather than that I used this method to get all copy commands being executed.

#include
#include
#include

#if defined(RTLD_NEXT)
#define REAL_LIBC RTLD_NEXT
#else
#define REAL_LIBC ((void *) -1L)
#endif

int system(char * command){
printf("Vij: system called - hacked\n");
static int (*o_dlconnect) ( char *command )=0;
o_dlconnect = (int(*)( char * )) dlsym(REAL_LIBC,"system"); printf("\n%s",command);
return (*o_dlconnect)( command );
}

0 comments: