Created
February 11, 2020 07:31
-
-
Save MarisElsins/5e476327640dceb0f1a612267e09d89b to your computer and use it in GitHub Desktop.
An LD_PRELOAD library that fakes the hostname output in `hostname` and `uname commands`
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
// gcc -fPIC -shared -Wl,-soname,libfakehostname.so.1 -ldl -o /usr/lib64/libfakehostname.so.1 fakehostname.c | |
// gcc -m32 -fPIC -shared -Wl,-soname,libfakehostname.so.1 -ldl -o /usr/lib/libfakehostname.so.1 fakehostname.c | |
// export LD_PRELOAD=libfakehostname.so.1 | |
// export FAKEHOSTNAME=myfakehostname.domain.com | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <dlfcn.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/utsname.h> | |
#include <stdbool.h> | |
#ifndef RTLD_NEXT | |
#define RTLD_NEXT ((void *) -1l) | |
#endif | |
int ret; | |
bool error_is_printed = false; | |
typedef int (*uname_t) (struct utsname * buf); | |
typedef int (*gethostname_t)(char *name, size_t len); | |
static void *getLibFunc(const char *funcname) | |
{ | |
void *func; | |
char *error = NULL; | |
func = dlsym(RTLD_NEXT, funcname); | |
if ( func == NULL ) { | |
error = dlerror(); | |
fprintf(stderr, "I can't locate lib function `%s' error: %s", funcname, error); | |
exit(EXIT_FAILURE); | |
} | |
return func; | |
} | |
static void *chkfakehost() | |
{ | |
if ( !error_is_printed && getenv("FAKEHOSTNAME") && strlen(getenv("FAKEHOSTNAME")) > sysconf(_SC_HOST_NAME_MAX)) { | |
fprintf(stderr,"WARNING! FAKEHOSTNAME variable exceeds %d (HOST_NAME_MAX) characters.\n",sysconf(_SC_HOST_NAME_MAX)); | |
error_is_printed = true; | |
} | |
} | |
int gethostname(char *name, size_t len) { | |
gethostname_t old_gethostname = (gethostname_t) getLibFunc("gethostname"); | |
chkfakehost(); | |
ret = old_gethostname(name, len); | |
if (getenv("FAKEHOSTNAME")) | |
strncpy(name, getenv("FAKEHOSTNAME"), len); | |
return ret; | |
} | |
int uname(struct utsname *buf) | |
{ | |
uname_t real_uname = (uname_t) getLibFunc("uname"); | |
chkfakehost(); | |
ret = real_uname((struct utsname *) buf); | |
if (getenv("FAKEHOSTNAME")) { | |
memset(buf->nodename, 0, sizeof(buf->nodename)); | |
strncpy(buf->nodename, getenv("FAKEHOSTNAME"), sizeof(buf->nodename)-1) ; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment