gethostname 공유 라이브러리 정보 확인

$ hostname
jongpak.com

$ ltrace hostname
...
gethostname("jongpak.com", 128)       = 0
...

$ nm -D /bin/hostname | grep gethostname
    U gethostname

$ ldd /bin/hostname
    libresolv.so.2 => /lib/tls/i686/cmov/libresolve.so.2 (0xbfc7000)
    libc.so.6 => /lib/tls/cmov/libc.so.6 (0xb7e9100)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7fea000)

$ nm -D /lib/libc.so.6 | grep gethostname
000db70 W gethostname

gethostname 공유 라이브러리 코드 fake

#include <stdlib.h>
#include <string.h>
  
int gethostname(char* name, size_t len)
{
    char *p = getenv("FAKE_HOSTNAME");
  
    if (p == NULL) {
        p = "localhost";
    }
  
    strncpy(name, p, len - 1);
    name[len - 1] = '\\0';
  
    return 0;
}

공유라이브러리 바꿔치기

$ gcc -shared -fPIC -o gethostname.so gethostname.c
  
$ LD_PRELOAD=./gethostname.so hostname
localhost
  
$ FAKE_HOSTNAME=test.com LD_PRELOAD=./hethostname.so hostname
test.com