Skip to content

Instantly share code, notes, and snippets.

@Akdeniz
Created November 16, 2017 13:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akdeniz/bed92f7efc01e35407130c567dc479ee to your computer and use it in GitHub Desktop.
Save Akdeniz/bed92f7efc01e35407130c567dc479ee to your computer and use it in GitHub Desktop.
hook getenv function with LD_PRELOAD
// 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