LD_PRELOAD trick

Cheatsheet - LD_PRELOAD trick Link to heading

## Compilation

gcc -o hook.so dlsym.c -ldl -fPIC -shared

/!\ Order matters

Usage : Link to heading

LD_PRELOAD=$PWD/hook.so ./target

Examples Link to heading

Strcmp hook : Link to heading

#define _GNU_SOURCE

#include <stdio.h>
#include <dlfcn.h>

int strcmp(const char *s1, const char *s2){
	
	printf("Hook strcmp : strcmp('%s','%s')\n", s1, s2);

	int (*true_strcmp)(const char*, const char*);
	true_strcmp = dlsym(RTLD_NEXT, "strcmp");

	return true_strcmp(s1,s2);
}

SSL inspector : Link to heading

#define _GNU_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <openssl/ssl.h>

int SSL_write(SSL *context, const void *buffer, int size){

	int (*orig_ssl_write)(SSL *context, const void *buffer, int size);	
	orig_ssl_write = dlysym(RTLD_NEXT,"SSL_write");

	printf("%s\n\n", buffer);
	
	return orig_ssl_write(context, buffer, size);
}