Created
November 16, 2017 13:39
-
-
Save Akdeniz/bed92f7efc01e35407130c567dc479ee to your computer and use it in GitHub Desktop.
hook getenv function with LD_PRELOAD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ltrace seems to be failing when getenv is called by shared object | |
// gcc -shared -fpic -ldl -o hook_getenv.so hook_getenv.c | |
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <dlfcn.h> | |
#include <limits.h> | |
#include <errno.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
char* (*original_getenv_func)(const char *) = NULL; | |
char* getenv(const char *name) | |
{ | |
if(!original_getenv_func) | |
original_getenv_func = dlsym(RTLD_NEXT, "getenv"); | |
char* result = original_getenv_func(name); | |
if(result==NULL) | |
printf("%s=NULL\n", name); | |
else | |
printf("%s=%s\n", name, result); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment