Skip to content

Commit

Permalink
Refactored load process in wsh.
Browse files Browse the repository at this point in the history
  • Loading branch information
endrazine committed Sep 1, 2016
1 parent 1a7a00c commit ef6e1b0
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 363 deletions.
2 changes: 1 addition & 1 deletion src/wsh/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

CC := gcc

CFLAGS := -masm=intel -rdynamic -W -Wall -Wextra -O0 -mpreferred-stack-boundary=12 -mstackrealign -ggdb -g3 -Wno-unused-but-set-variable -Wno-unused-parameter -I./include/sflib/ -I./include -I../../include/ -rdynamic -Wl,-E -Wl,-z,now #--sanitize=address
CFLAGS := -masm=intel -rdynamic -W -Wno-unused-function -O0 -mpreferred-stack-boundary=12 -mstackrealign -ggdb -g3 -Wno-unused-but-set-variable -Wno-unused-parameter -I./include/sflib/ -I./include -I../../include/ -rdynamic -Wl,-E -Wl,-z,now #--sanitize=address
OBJLIB := ./lua/src/liblua.a ./openlibm/libopenlibm.a


Expand Down
24 changes: 6 additions & 18 deletions src/wsh/Makefile-i386
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,24 @@
#

CC := gcc-4.8
#CC := "gcc-4.8 -m32" # trick to compile liblua.a in 32bits mode

CFLAGS := -masm=intel -W -Wall -Wextra -O0 -mpreferred-stack-boundary=12 -mstackrealign -ggdb -g3 -Wno-unused-but-set-variable -Wno-unused-parameter -I./include/sflib/ -I./include -I../../include/ -rdynamic -Wl,-E -Wl,-z,now -m32 #--sanitize=address

all::
# cd openlibm && make CFLAGS="-pie -fpie -fPIC -m32" ARCH=i386
# cd lua && make linux CFLAGS="-pie -fpie -fPIC -m32 -static-libgcc -lgcc" CC=$(CC)
cd openlibm && make CFLAGS="-pie -fpie -fPIC -m32" ARCH=i386
cd lua && make linux CFLAGS="-march=i386 -m32" CC='/usr/bin/gcc-4.8 -m32'
$(CC) $(CFLAGS) wsh.c -o wsh.o -c -ldl -lreadline -pie -fpie -fPIC
$(CC) $(CFLAGS) wshmain.c -o wshmain.o -c -ldl -lreadline -pie -fpie -fPIC
$(CC) $(CFLAGS) helper.c -o helper.o -c -pie -fpie -fPIC
$(CC) $(CFLAGS) linenoise/linenoise.c -o linenoise.o -c -pie -fpie -fPIC
$(CC) $(CFLAGS) wsh.o helper.o linenoise.o -shared -fPIC -o libwitch.so
ar cr libwitch.a wsh.o helper.o linenoise.o
$(CC) $(CFLAGS) wsh.o helper.o linenoise.o wshmain.o -o wsh-i386 ./lua/src/liblua32.a -lgsl -lgslcblas /usr/lib32/libc.so -lm -ldl /usr/lib/i386-linux-gnu/libiberty.a

#../../../../musl//lib/libc.a

# ../musl/lib/libc.a
# -static
# -ldl
#-static
# -llua5.2
#./liblua.a

# $(CC) $(CFLAGS) wsh2.c -o wsh2.o -c -ldl
# $(CC) $(CFLAGS) wsh2.o -o wsh2 -T script.lds ./liblua.a -liberty -ldl -lm


$(CC) $(CFLAGS) wsh.o helper.o linenoise.o wshmain.o -o wsh-i386 ./lua/src/liblua.a -lgsl -lgslcblas /usr/lib32/libc.so -lm -ldl /usr/lib/i386-linux-gnu/libiberty.a
cp wsh-i386 ../../bin/

dep::
sudo apt-get install libreadline6:i386 libreadline6-dev:i386 --reinstall

clean::
rm wsh-i386 helper.o wsh.o wshmain.o libwitch.so libwitch.a linenoise.o -f
cd openlibm && make clean
Expand Down
221 changes: 124 additions & 97 deletions src/wsh/include/libwitch/wsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,17 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <linenoise.h>
//#include <functions_list.h>
#include "helper.h"
#include <colors.h>
//#include <sigs.h>
#include <ucontext.h>
#include <config.h>
#include <utlist.h>
#include <uthash.h>

#define DEFAULT_SCRIPT "/usr/share/wcc/scripts/debug"
#define DEFAULT_SCRIPT_INDEX "/usr/share/wcc/scripts/INDEX"
#define DEFAULT_WSHRC ".wshrc"
#define DEFAULT_WSH_PROFILE ".wsh_profile"
#define PROC_ASLR_PATH "/proc/sys/kernel/randomize_va_space"

#define DEFAULT_LEARN_FILE "./learnwitch.log"
Expand Down Expand Up @@ -339,91 +340,94 @@ extern char *__progname_full;
/**
* Forward prototypes declarations
*/
int do_loadlib(char *libname);
int empty_phdrs(void);
int empty_shdrs(void);
static struct link_map *do_loadlib(char *libname);
static int empty_phdrs(void);
static int wsh_appear(lua_State * L);
static int wsh_hide(lua_State * L);
static int empty_shdrs(void);
//int getarray(lua_State * L);
int getsize(lua_State * L);
int newarray(lua_State * L);
int print_functions(lua_State * L);
int print_libs(lua_State * L);
int print_objects(lua_State * L);
int print_phdrs(void);
int print_shdrs(void);
int entrypoints(lua_State * L);
int print_symbols(lua_State * L);
int print_version(void);
int setarray(lua_State * L);
int usage(char *name);
void set_align_flag(void);
void set_branch_flag(void);
void set_trace_flag(void);
void singlebranch(lua_State * L);
void singlestep(lua_State * L);
void traceunaligned(lua_State * L);
void unset_align_flag(void);
void unset_branch_flag(void);
void unset_trace_flag(void);
void unsinglebranch(lua_State * L);
void unsinglestep(lua_State * L);
void untraceunaligned(lua_State * L);
void unverbosetrace(lua_State * L);
void verbosetrace(lua_State * L);
void xfree(lua_State * L);

void systrace(lua_State * L);
void rtrace(lua_State * L);
void unsystrace(lua_State * L);
void unrtrace(lua_State * L);


int add_symbol(char *symbol, char *libname, char *htype, char *hbind, unsigned long value, unsigned int size, unsigned long int addr);
void segment_add(unsigned long int addr, unsigned long int size, char *perms, char *fname, char *ptype, int flags);

int alloccharbuf(lua_State * L);
int bfmap(lua_State * L);
int breakpoint(lua_State * L);
int execlib(lua_State * L);
int getcharbuf(lua_State * L);
int grep(lua_State * L);
int grepptr(lua_State * L);
int help(lua_State * L);
int hollywood(lua_State * L);
int info(lua_State * L);
int libcall(lua_State * L);
int loadbin(lua_State * L);
int man(lua_State * L);
int map(lua_State * L);
int phdrs(lua_State * L);
int priv_memcpy(lua_State * L);
int priv_strcat(lua_State * L);
int priv_strcpy(lua_State * L);
int rdnum(lua_State * L);
int rdstr(lua_State * L);
int setcharbuf(lua_State * L);
int shdrs(lua_State * L);
int verbose(lua_State * L);
int xalloc(lua_State * L);
int ralloc(lua_State * L);

int headers(lua_State * L);
int prototypes(lua_State * L);
int bsspolute(lua_State * L);

unsigned int ltrace(void);
int procmap_lua(void);
void rescan(void);
void hexdump(uint8_t * data, size_t size, size_t colorstart, size_t color_len);
int disable_aslr(void);
int enable_aslr(void);
void script(char *path);

int enable_core(lua_State * L);
int disable_core(lua_State * L);
int gencore(lua_State * L);

char *signaltoname(int signal);
char *sicode_strerror(int signal, siginfo_t * s);
static int getsize(lua_State * L);
static int newarray(lua_State * L);
static int print_functions(lua_State * L);
static int print_libs(lua_State * L);
static int print_objects(lua_State * L);
static int print_phdrs(void);
static int print_shdrs(void);
static int entrypoints(lua_State * L);
static int print_symbols(lua_State * L);
static int print_version(void);
static int setarray(lua_State * L);
static int usage(char *name);
static void set_align_flag(void);
static void set_branch_flag(void);
static void set_trace_flag(void);
static void singlebranch(lua_State * L);
static void singlestep(lua_State * L);
static void traceunaligned(lua_State * L);
static void unset_align_flag(void);
static void unset_branch_flag(void);
static void unset_trace_flag(void);
static void unsinglebranch(lua_State * L);
static void unsinglestep(lua_State * L);
static void untraceunaligned(lua_State * L);
static void unverbosetrace(lua_State * L);
static void verbosetrace(lua_State * L);
static void xfree(lua_State * L);

static void systrace(lua_State * L);
static void rtrace(lua_State * L);
static void unsystrace(lua_State * L);
static void unrtrace(lua_State * L);


static int add_symbol(char *symbol, char *libname, char *htype, char *hbind, unsigned long value, unsigned int size, unsigned long int addr);
static void segment_add(unsigned long int addr, unsigned long int size, char *perms, char *fname, char *ptype, int flags);

static int alloccharbuf(lua_State * L);
static int bfmap(lua_State * L);
static int teletype(lua_State * L);
static int breakpoint(lua_State * L);
static int execlib(lua_State * L);
static int getcharbuf(lua_State * L);
static int grep(lua_State * L);
static int grepptr(lua_State * L);
static int help(lua_State * L);
static int hollywood(lua_State * L);
static int info(lua_State * L);
static int libcall(lua_State * L);
static int loadbin(lua_State * L);
static int man(lua_State * L);
static int map(lua_State * L);
static int phdrs(lua_State * L);
static int priv_memcpy(lua_State * L);
static int priv_strcat(lua_State * L);
static int priv_strcpy(lua_State * L);
static int rdnum(lua_State * L);
static int rdstr(lua_State * L);
static int setcharbuf(lua_State * L);
static int shdrs(lua_State * L);
static int verbose(lua_State * L);
static int xalloc(lua_State * L);
static int ralloc(lua_State * L);

static int headers(lua_State * L);
static int prototypes(lua_State * L);
static int bsspolute(lua_State * L);

static unsigned int ltrace(void);
static int procmap_lua(void);
static void rescan(void);
static void hexdump(uint8_t * data, size_t size, size_t colorstart, size_t color_len);
static int disable_aslr(void);
static int enable_aslr(void);
static int run_script(char *name);

static int enable_core(lua_State * L);
static int disable_core(lua_State * L);
static int gencore(lua_State * L);

static char *signaltoname(int signal);
static char *sicode_strerror(int signal, siginfo_t * s);

/*
int memmap (lua_State *L);
Expand All @@ -433,12 +437,12 @@ int setmemmap(lua_State * L);
int memmapsize(lua_State * L);
*/

int rawmemread (lua_State *L);
int rawmemwrite (lua_State *L);
int rawmemstr (lua_State *L);
int rawmemusage (lua_State *L);
int rawmemaddr (lua_State *L);
int rawmemstrlen(lua_State *L);
static int rawmemread (lua_State *L);
static int rawmemwrite (lua_State *L);
static int rawmemstr (lua_State *L);
static int rawmemusage (lua_State *L);
static int rawmemaddr (lua_State *L);
static int rawmemstrlen(lua_State *L);

/**
* Internal representation of an ELF
Expand Down Expand Up @@ -547,7 +551,7 @@ typedef struct symbols_t {
} symbols_t;

typedef struct eps_t {
unsigned long int addr;
unsigned long long int addr;
char *name;

struct eps_t *prev; // utlist.h
Expand All @@ -562,18 +566,22 @@ typedef struct wsh_t {

// State
lua_State *L;
FILE *scriptfile;
char *scriptname;
char *luabuff;
unsigned int luabuffsz;

char *selflib;
char *learnlog;
FILE *learnfile;

unsigned long long int mainhandle; // This is really a struct link_map *

unsigned int opt_verbose;
unsigned int opt_quiet;
unsigned int opt_hollywood; // Default = 1;
unsigned int mainhandle;
unsigned int opt_rescan;

unsigned int opt_rescan;
unsigned int opt_verbosetrace; // Display verbose trace
unsigned int opt_appear; // Display ourselves or hide ourselves ?

unsigned int firsterrno;
unsigned int firstsicode;
Expand Down Expand Up @@ -636,11 +644,30 @@ typedef struct wsh_t {

} wsh_t;

/**
* The next structure define
* how prototypes are learned
* by analysing runtime experiences
*/
typedef struct tuple_t{
void *addr;
char *name;
} tuple_t;

typedef struct learn_key_t{

char ttype[10];
char tlib[200];
char tfunction[200];
char targ[20];
char tvalue[200];
}learn_key_t;

typedef struct learn_t{
learn_key_t key;
char toffset[20];
UT_hash_handle hh;
} learn_t;

int wsh_init(void);
int wsh_getopt(wsh_t * wsh1, int argc, char **argv);
Expand Down

0 comments on commit ef6e1b0

Please sign in to comment.