From 34fd7ba6ccc4368f87bcfc8e4ef702496e4c5655 Mon Sep 17 00:00:00 2001 From: Kevin Haubris Date: Mon, 16 May 2022 12:48:34 -0500 Subject: [PATCH] Added changes and example of building as .so file, a runner.c file, and how to run it for #1 --- Makefile | 7 ++++++ README.md | 8 ++++++ includes/ELFRunner_include.h | 8 ++++++ runner.c | 48 ++++++++++++++++++++++++++++++++++++ src/ELFLoader.c | 8 ++++-- src/beacon_compatibility.c | 3 +++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 includes/ELFRunner_include.h create mode 100644 runner.c diff --git a/Makefile b/Makefile index 5a4bc0a..ea2b312 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,12 @@ x86_64: x86_64D: gcc -g -DDEBUG -Wall -I ./includes/ -DTESTING_MAIN $(SOURCES) -ldl -o ELFLoaderD.out +x86_64so: + gcc -shared -fPIC -fvisibility=hidden -DLIBRARY -Wall -I ./includes/ -DTESTING_MAIN $(SOURCES) -ldl -o libELFLoader.so + +x86_64runner: + gcc -I ./includes/ runner.c -o runner.out -L . -lELFLoader + x86_64_bsd: gcc -Wall -I ./includes/ -DTESTING_MAIN $(SOURCES) -o ELFLoader_bsd.out @@ -124,4 +130,5 @@ clean: rm -f ./SA/src/*.o rm -f testobjects/*.o rm -f ELFLoader_public + rm -f libELFLoader.so .PHONY: x86_64 x86_64D test2_dup test2 test clean diff --git a/README.md b/README.md index a4da826..32f6320 100644 --- a/README.md +++ b/README.md @@ -115,3 +115,11 @@ This is the generic example that doesn't take any arguments. ``` ./ELFLoader.out SA/src/uname.o ``` + +### Runner and Library Example + +``` +make x86_64so +make x86_64runner +LD_LIBRARY_PATH=. ./runner.out ./SA/src/env.o +``` diff --git a/includes/ELFRunner_include.h b/includes/ELFRunner_include.h new file mode 100644 index 0000000..b273279 --- /dev/null +++ b/includes/ELFRunner_include.h @@ -0,0 +1,8 @@ +#ifndef ELFLOADER_INCLUDE_H_ +#define ELFLOADER_INCLUDE_H_ + +int ELFRunner(char* functionName, unsigned char* elfObjectData, unsigned int size, unsigned char* argumentdata, int argumentSize); +char* BeaconGetOutputData(int *outsize); +unsigned char* unhexlify(unsigned char* value, int *outlen); + +#endif diff --git a/runner.c b/runner.c new file mode 100644 index 0000000..861a157 --- /dev/null +++ b/runner.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +int main(int argc, char** argv, char** envp) +{ + //int (*ptr)(char*, int*); + unsigned char* buf = NULL; + int size = 0; + FILE* elf = NULL; + int checkcode = 0; + char* outputdata = NULL; + char* argumentdata = NULL; + int argumentdatalen = 0; + int outputdataLen = 0; + + if (argc < 2){ + printf("%s ./path/to/objectfile.o\n", argv[0]); + return 0; + } + elf = fopen(argv[1], "rb"); + if (elf == NULL){ + printf("ERROR: File doesn't exist\n"); + return 0; + } + fseek(elf, 0, SEEK_END); + size = ftell(elf); + fseek(elf, 0, SEEK_SET); + buf = calloc(size, 1); + if (buf == NULL){ + return 0; + } + (void)fread(buf, 1, size, elf); + argumentdata = (char*)unhexlify((unsigned char*)argv[2], &argumentdatalen); + checkcode = ELFRunner("go", buf, size, (unsigned char*)argumentdata, argumentdatalen ); + if (checkcode == 0){ + outputdata = BeaconGetOutputData(&outputdataLen); + printf("Output data : %s\n", outputdata); + free(outputdata); + } + + + free(buf); + fclose(elf); + return 0; +} + diff --git a/src/ELFLoader.c b/src/ELFLoader.c index f39c954..65ac0e4 100644 --- a/src/ELFLoader.c +++ b/src/ELFLoader.c @@ -14,7 +14,9 @@ #include "ELFLoader.h" #include "beacon_compatibility.h" - +#ifdef LIBRARY +__attribute__ ((visibility ("default"))) +#endif unsigned char* unhexlify(unsigned char* value, int *outlen){ unsigned char* retval = NULL; char byteval[3] = {0}; @@ -48,7 +50,9 @@ errcase: return retval; } - +#ifdef LIBRARY +__attribute__ ((visibility ("default"))) +#endif int ELFRunner(char* functionName, unsigned char* elfObjectData, unsigned int size, unsigned char* argumentdata, int argumentSize){ #if defined(__amd64__) || defined(__x86_64__) || defined(__i386__) || (defined(DEBUG) && defined(ELFRUNNERTEST)) ELFInfo_t elfinfo; diff --git a/src/beacon_compatibility.c b/src/beacon_compatibility.c index 93fc1b0..a7cc43e 100644 --- a/src/beacon_compatibility.c +++ b/src/beacon_compatibility.c @@ -318,6 +318,9 @@ int BeaconIsAdmin(void){ return 0; } +#ifdef LIBRARY +__attribute__ ((visibility ("default"))) +#endif char* BeaconGetOutputData(int *outsize){ char* outdata = beacon_compatibility_output; *outsize = beacon_compatibility_size;