| 1 | #define _GNU_SOURCE |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/syscall.h> |
| 6 | #include <dlfcn.h> |
| 7 | #include <unistd.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <time.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #define i { srand(23); printf("%s();\n",__func__); } |
| 13 | |
| 14 | //Catch rand() calls etc... |
| 15 | int rand(void) { i; return 23; } |
| 16 | long int random(void) { i; return 23; } |
| 17 | int rand_r(unsigned int *seedp) { i; return 23; } |
| 18 | int random_r(struct random_data *buf, int32_t *result) { i; return 23; } |
| 19 | //Time |
| 20 | time_t time(time_t *tloc) { i; return (time_t)0; } |
| 21 | int clock_gettime(clockid_t clk_id, struct timespec *tp) { i; tp->tv_nsec=0; tp->tv_sec=0; return (time_t)0; } |
| 22 | //OpenSSL (not working i guess...) |
| 23 | int RAND_bytes(unsigned char *buf, int num) { i; memset(buf, 23, (size_t) num); return 23; } |
| 24 | int RAND_pseudo_bytes(unsigned char *buf, int num) { i; memset(buf, 23, (size_t) num); return 23; } |
| 25 | int FIPS_rand_bytes(unsigned char *buf, int num) { i; memset(buf, 23, (size_t) num); return 23; } |
| 26 | int FIPS_rand_pseudo_bytes(unsigned char *buf, int num) { i; memset(buf, 23, (size_t) num); return 23; } |
| 27 | //IDs |
| 28 | pid_t getpid(void) { i; return (pid_t)0; } |
| 29 | uid_t getuid(void) { i; return (uid_t)0; } |
| 30 | gid_t getgid(void) { i; return (gid_t)0; } |
| 31 | uid_t geteuid(void) { i; return (uid_t)0; } |
| 32 | gid_t getegid(void) { i; return (gid_t)0; } |
| 33 | |
| 34 | //Catch open() calls (while redirecting filename): |
| 35 | static const char *redirect_name(const char *name) |
| 36 | { |
| 37 | if( |
| 38 | (strcmp(name,"/dev/random") == 0) || |
| 39 | (strcmp(name,"/dev/urandom") == 0) |
| 40 | ) { |
| 41 | printf("REDIRECT HIT: %s\n", name); |
| 42 | return "/dev/zero"; |
| 43 | } |
| 44 | return name; |
| 45 | } |
| 46 | |
| 47 | int open(const char *filename, int flags, ...) |
| 48 | { |
| 49 | //srand(23); |
| 50 | static int (*open_orig)(const char *, int, mode_t); |
| 51 | int ret; |
| 52 | va_list ap; |
| 53 | mode_t mode; |
| 54 | |
| 55 | if (!open_orig) { |
| 56 | open_orig = dlsym(RTLD_NEXT, "open"); |
| 57 | } |
| 58 | |
| 59 | va_start(ap, flags); |
| 60 | mode = va_arg(ap, mode_t); |
| 61 | va_end(ap); |
| 62 | |
| 63 | ret = open_orig(redirect_name(filename), flags, mode); |
| 64 | //ret = syscall(SYS_open,redirect_name(filename),flags,mode); |
| 65 | |
| 66 | printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); |
| 67 | |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | int open64(const char *filename, int flags, ...) |
| 72 | { |
| 73 | //srand(23); |
| 74 | static int (*open64_orig)(const char *, int, mode_t); |
| 75 | int ret; |
| 76 | va_list ap; |
| 77 | mode_t mode; |
| 78 | |
| 79 | if (!open64_orig) { |
| 80 | open64_orig = dlsym(RTLD_NEXT, "open64"); |
| 81 | } |
| 82 | |
| 83 | va_start(ap, flags); |
| 84 | mode = va_arg(ap, mode_t); |
| 85 | va_end(ap); |
| 86 | |
| 87 | ret = open64_orig(redirect_name(filename), flags, mode); |
| 88 | //ret = syscall(SYS_open64,redirect_name(filename),flags,mode); |
| 89 | |
| 90 | printf("open64(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); |
| 91 | |
| 92 | return ret; |
| 93 | } |