Skip to content

Instantly share code, notes, and snippets.

@vic511
Last active August 29, 2017 19:49
Show Gist options
  • Select an option

  • Save vic511/9c9a759afb5b1d370287fda32c104f71 to your computer and use it in GitHub Desktop.

Select an option

Save vic511/9c9a759afb5b1d370287fda32c104f71 to your computer and use it in GitHub Desktop.
Dynamic reloading PoC
#include <stdio.h>
#include <stdbool.h>
bool hello(void)
{
if (printf("Hello first\n") < 0)
return false;
return true;
}
CC = gcc
CFLAGS = -Wall -Wextra -std=gnu99
LDFLAGS = -ldl
RM = rm -f
poc: poc.o first.so second.so
first.so: first.c
$(CC) $(CFLAGS) -shared -fPIC -o $@ $^
second.so: second.c
$(CC) $(CFLAGS) -shared -fPIC -o $@ $^
fclean:
$(RM) first.so second.so poc.o poc
re: fclean poc
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <dlfcn.h>
#define ERROR(NAME) ({ perror(NAME); false; })
typedef bool (*t_hellofunc)(void);
static bool callhello(void *handle)
{
t_hellofunc hello = NULL;
hello = dlsym(handle, "hello");
if (hello == NULL)
return ERROR("dlsym");
return hello();
}
static bool poc(const char *library)
{
bool ret = true;
void *handle = NULL;
handle = dlopen(library, RTLD_LAZY);
if (handle == NULL)
return ERROR("dlopen");
ret = callhello(handle);
if (dlclose(handle) != 0)
return ERROR("dlclose");
return ret;
}
int main(void)
{
if (!poc("first.so"))
return EXIT_FAILURE;
if (!poc("second.so"))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdbool.h>
bool hello(void)
{
if (printf("Hello second\n") < 0)
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment