-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpwuid_nodb.c
37 lines (33 loc) · 888 Bytes
/
getpwuid_nodb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* gcc -o getpwuid_nodb.so -fPIC -shared getpwuid_nodb.c -ldl
* export LD_PRELOAD=$PWD/getpwuid_nodb.so
* FAKE_USER="Dennis_Ritchie" whoami
* docker run -u 1000:1000 -v $(pwd)/getpwuid_nodb.so:/tmp/getpwuid_nodb.so -e LD_PRELOAD=/tmp/getpwuid_nodb.so -e FAKE_USER="Dennis_Ritchie" alpine whoami
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
struct passwd *getpwuid(uid_t uid)
{
static void * (*func)() = NULL;
static char *username = NULL;
struct passwd *p = NULL;
if(!func)
{
func = (void *(*)()) dlsym(RTLD_NEXT, "getpwuid");
username = getenv("FAKE_USER");
if(username==NULL)
username = "user";
}
p=func(0);
if(p){
p->pw_uid = getuid();
p->pw_gid = getgid();
p->pw_dir = "/tmp";
p->pw_gecos = username;
p->pw_name = username;
}
return(p);
}