Linux.ChinaUnix.net
ChinaUnix | Linux首页 | 新闻 | 博客 | 文章 | 专栏 | 新手 | 方案 | 图书 | 下载 | 人才 | 手册 | wiki | 搜索     
Linux论坛
  会员: 密码: 免费注册 | 忘记密码 | 会员登录 | 搜索



精彩推荐帖子 使用kprobes,截获execve系统调用
首页 » CU论坛 » Linux » 汇总贴列表 » 内核源码 »  
[打印] [订阅] [收藏] [本帖文本页] [推荐此主题给朋友,立即获积分]
albcamus (百無一用書生)
法王
[知否興風狂嘯者,回眸時看 ...



CU编号: 140189
注册:2004-3-7
最后登录: 2009-07-04
帖子:12176
精华:22

可用积分:11608 (大富大贵)
信誉积分:110
专家积分:71 (本版:0)
空间积分:0
推广积分:0

来自:嫏嬛閣
状态:...保密...

[个人空间] [短信] [博客]


1楼 发表于 2007-9-20 18:27 
关于截获execve等系统调用,很久以来存在一个问题:新函数不能直接调旧函数,
否则导致stack不平衡,出错。

曾经有高人用一串的汇编代码去平衡堆栈, 但对于偶们这些汇编菜鸟来说, 连阅读都很困难。
而且, 好像gcc4.x下不支持 它使用的一种寻址方式了。

这次使用kprobes来直接找do_execve,直接照搬sys_execve的实现代码。
至于LICENSE的问题,谁用谁负责,我只是从技术角度上说可以这样做。(我的代码是GPL的)

只截获了open和execve作为示例,其它的syscall都是一样的道理。



README:
Can work at i386, linux 2.6.22.

We only intercepted  1) sys_open, and 2) sys_execve,  for an illustrating purpose.


New Feautres:

        ->  Check and verify the WP bit of CR0.
        ->  Intercept execve with the help of kprobes. Likewise, fork/vfork/clone
            can be intercepted in the same way.




源代码:

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/user.h>
#include <linux/errno.h>
#include <linux/cpu.h>
#include <asm/uaccess.h>
#include <asm/fcntl.h>
#include <asm/unistd.h>

MODULE_DESCRIPTION("Intercept the system call table in Linux");
MODULE_AUTHOR("alert7 (alert7@xfocus.org) \n\t\talbcamus <albcamus@gmail.com>");
MODULE_LICENSE("GPL");


/* comment the following line to shut me up */
#define INTERCEPT_DEBUG

#ifdef INTERCEPT_DEBUG
    #define dbgprint(format,args...) \
        printk("intercept: function:%s-L%d: "format, __FUNCTION__, __LINE__, ##args);
#else
    #define dbgprint(format,args...)  do {} while(0);
#endif


/**
* the system call table
*/

void **my_table;

unsigned int orig_cr0;

/**
* the original syscall functions
*/

asmlinkage long (*old_open) (char __user *filename, int flags, int mode);
asmlinkage int  (*old_execve) (struct pt_regs regs);



/** do_execve and do_fork */
unsigned int can_exec_fork = 0;
int    (*new_do_execve) (char * filename,
            char __user *__user *argv,
            char __user *__user *envp,
            struct pt_regs * regs);


struct idtr {
    unsigned short limit;
    unsigned int base;
} __attribute__ ((packed));


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



/**
*  check if we can intercept fork/vfork/clone/execve or not
*
*  return : 0 for no, 1 for yes
*/

struct kprobe kp_exec;
unsigned int can_intercept_fork_exec(void)
{
    int ret = 0;

#ifndef CONFIG_KPROBES
    return ret;
#endif

    kp_exec.symbol_name = "do_execve";

    ret = register_kprobe(&kp_exec);
    if (ret != 0 ) {
        dbgprint("cannot find do_execve by kprobe.\n");
        return 0;
    }
    new_do_execve = ( int (*)
             (char *,
              char __user * __user *,
              char __user * __user *,
              struct pt_regs *
             )
            ) kp_exec.addr;

    dbgprint("do_execve at %p\n", (void *)kp_exec.addr);
    unregister_kprobe(&kp_exec);


    return 1;
}



/**
* clear WP bit of CR0, and return the original value
*/

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 20 bit of CR0, a.k.a WP bit */
    cr0 &= 0xfffeffff;

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

/** set CR0 with new value
*
* @val : new value to set in cr0
*/

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


/**
* Return the first appearence of NEEDLE in HAYSTACK.  
* */

static void *memmem(const void *haystack, size_t haystack_len,
            const void *needle, size_t needle_len)
{/*{{{*/
    const char *begin;
    const char *const last_possible
        = (const char *) haystack + haystack_len - needle_len;

    if (needle_len == 0)
        /* The first occurrence of the empty string is deemed to occur at
           the beginning of the string.  */

        return (void *) haystack;

    /* Sanity check, otherwise the loop might search through the whole
       memory.  */

    if (__builtin_expect(haystack_len < needle_len, 0))
        return NULL;

    for (begin = (const char *) haystack; begin <= last_possible;
         ++begin)
        if (begin[0] == ((const char *) needle)[0]
            && !memcmp((const void *) &begin[1],
                   (const void *) ((const char *) needle + 1),
                   needle_len - 1))
            return (void *) begin;

    return NULL;
}/*}}}*/


/**
* Find the location of sys_call_table
*/

static unsigned long get_sys_call_table(void)
{/*{{{*/
/* we'll read first 100 bytes of int $0x80 */
#define OFFSET_SYSCALL 100        

    struct idtr idtr;
    struct idt idt;
    unsigned sys_call_off;
    unsigned retval;
    char sc_asm[OFFSET_SYSCALL], *p;

    /* well, let's read IDTR */
    asm("sidt %0":"=m"(idtr)
             :
             :"memory" );

    dbgprint("idtr base at 0x%X\n", (unsigned int)idtr.base);

    /* Read in IDT for vector 0x80 (syscall) */
    memcpy(&idt, (char *) idtr.base + 8 * 0x80, sizeof(idt));

    sys_call_off = (idt.off2 << 16) | idt.off1;

    dbgprint("idt80: flags=%X sel=%X off=%X\n",
                 (unsigned) idt.flags, (unsigned) idt.sel, sys_call_off);

    /* we have syscall routine address now, look for syscall table
       dispatch (indirect call) */

    memcpy(sc_asm, (void *)sys_call_off, OFFSET_SYSCALL);

    /**
     * Search opcode of `call sys_call_table(,eax,4)'
     */

    p = (char *) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\x85", 3);
    if (p == NULL)
        return 0;

    retval = *(unsigned *) (p + 3);
    if (p) {
        dbgprint("sys_call_table at 0x%x, call dispatch at 0x%x\n",
             retval, (unsigned int) p);
    }
    return retval;
#undef OFFSET_SYSCALL
}/*}}}*/



/**
* new_open - replace the original sys_open when initilazing,
*           as well as be got rid of when removed
*/

asmlinkage long new_open(char *filename, int flags, int mode)
{
    dbgprint("hello\n");
    return old_open (filename, flags, mode);
}


/**
* new_execve - you should change this function whenever the kernel's sys_execve()
* changes
*/

asmlinkage int new_execve(struct pt_regs regs)
{
    int error;
    char *filename;

    dbgprint("Hello\n");

    filename = getname( (char __user *) regs.ebx );
    error = PTR_ERR(filename);
    if ( IS_ERR(filename) )
        goto out;
    dbgprint("file to execve: %s\n", filename);
    error = new_do_execve(filename,
                  (char __user * __user *) regs.ecx,
                  (char __user * __user *) regs.edx,
                  &regs);
    if (error == 0) {
        task_lock(current);
        current->ptrace &= ~PT_DTRACE;
        task_unlock(current);
        set_thread_flag(TIF_IRET);
    }
    putname (filename);
out:
    return error;
}

static int intercept_init(void)
{
    my_table = (void **)get_sys_call_table();
    if (my_table == NULL)
        return -1;

    dbgprint("sys call table address %p\n", (void *) my_table);

#define REPLACE(x) old_##x = my_table[__NR_##x];\
    my_table[__NR_##x] = new_##x

   
    REPLACE(open);
    can_exec_fork = can_intercept_fork_exec();
    if(can_exec_fork == 1)
        REPLACE(execve);


#undef REPLACE
    return 0;
}





static int __init this_init(void)
{
    int ret;
    printk("syscall intercept: Hi, poor linux!\n");

    orig_cr0 = clear_and_return_cr0();   
    ret = intercept_init();
    setback_cr0(orig_cr0);

    return ret;
}

static void __exit this_fini(void)
{
    printk("syscall intercept: bye, poor linux!\n");

#define RESTORE(x) my_table[__NR_##x] = old_##x

    orig_cr0 = clear_and_return_cr0();   
    RESTORE(open);
    if(can_exec_fork == 1)
        RESTORE(execve);
    setback_cr0(orig_cr0);

#undef RESTORE
}

module_init(this_init);
module_exit(this_fini);



您对本贴的看法:鲜花[1] 臭蛋[0]

__________________________________



  •  平生自有千秋在,不向群兒問毀譽!
     
  •  爱国主义是群氓的道德安全套。
     
  •  有些爱国青年想必是为人做了太多blowjob,不然为何张口便吐生殖器?
     
  •  Feel free to ask questions by 站内短信, and I'll feel free to ignore you.


mq110   帅哥
荣誉版主-法师
告别.




CU编号: 224120
注册:2005-2-10
最后登录: 2009-05-07
帖子:6842
精华:2

可用积分:114 (白手起家)
信誉积分:105
专家积分:2 (本版:0)
空间积分:0
推广积分:0

状态:...保密...

[个人空间] [短信] [博客]


2楼 发表于 2007-9-20 19:59 
alert7 (alert7@xfocus.org)
albcamus <albcamus@gmail.com

和alert7一起写的啊。。没签保密协议?



您对本贴的看法:鲜花[0] 臭蛋[0]
圆点坐标   帅哥
精灵使
潜行者


CU奥运火炬传递手2008
CU编号: 276410
注册:2005-6-3
最后登录: 2009-05-06
帖子:3272
精华:0

可用积分:600 (稍有积蓄)
信誉积分:130
专家积分:33 (本版:0)
空间积分:0
推广积分:0

状态:...保密...

[个人空间] [短信] [博客]


3楼 发表于 2007-9-20 20:14 


QUOTE:
原帖由 mq110 于 2007-9-20 19:59 发表
alert7 (alert7@xfocus.org)
albcamus  

这个有什么需要保密的吗?so easy....



您对本贴的看法:鲜花[0] 臭蛋[0]
mq110   帅哥
荣誉版主-法师
告别.




CU编号: 224120
注册:2005-2-10
最后登录: 2009-05-07
帖子:6842
精华:2

可用积分:114 (白手起家)
信誉积分:105
专家积分:2 (本版:0)
空间积分:0
推广积分:0

状态:...保密...

[个人空间] [短信] [博客]


4楼 发表于 2007-9-20 20:42 


QUOTE:
原帖由 圆点坐标 于 2007-9-20 20:14 发表

这个有什么需要保密的吗?so easy....

概念上的错误,难道简单就不保密了吗?
公司简单的代码多了去了,对于你来说没有技术难度的,你都可以称为简单,难道就可以说不保密?



您对本贴的看法:鲜花[0] 臭蛋[0]
albcamus (百無一用書生)
法王
[知否興風狂嘯者,回眸時看 ...



CU编号: 140189
注册:2004-3-7
最后登录: 2009-07-04
帖子:12176
精华:22

可用积分:11608 (大富大贵)
信誉积分:110
专家积分:71 (本版:0)
空间积分:0
推广积分:0

来自:嫏嬛閣
状态:...保密...

[个人空间] [短信] [博客]


5楼 发表于 2007-9-20 22:41 


QUOTE:
原帖由 mq110 于 2007-9-20 19:59 发表
alert7 (alert7@xfocus.org)
albcamus  

没啥啊,这个方法最初是很早的某期phrack上的,他做了优化,然后我补写了kprobe和WP部分。 其实都是开源的



您对本贴的看法:鲜花[0] 臭蛋[0]

__________________________________



  •  平生自有千秋在,不向群兒問毀譽!
     
  •  爱国主义是群氓的道德安全套。
     
  •  有些爱国青年想必是为人做了太多blowjob,不然为何张口便吐生殖器?
     
  •  Feel free to ask questions by 站内短信, and I'll feel free to ignore you.


守夜人   帅哥
风云使者




CU编号: 225261
注册:2005-2-16
最后登录: 2007-12-30
帖子:464
精华:0

可用积分:474 (白手起家)
信誉积分:100
专家积分:0 (本版:0)
空间积分:0
推广积分:0

状态:...离线...

[个人空间] [短信] [博客]


6楼 发表于 2007-9-23 19:56 
在2.4下直接导出sys_call_table截获系统调用是很容易的,但2.4.18以后好像就不行了..
楼主这个方法是不是能弥补2.6.x内核不能导出sys_call_table的问题?????



您对本贴的看法:鲜花[0] 臭蛋[0]
albcamus (百無一用書生)
法王
[知否興風狂嘯者,回眸時看 ...



CU编号: 140189
注册:2004-3-7
最后登录: 2009-07-04
帖子:12176
精华:22

可用积分:11608 (大富大贵)
信誉积分:110
专家积分:71 (本版:0)
空间积分:0
推广积分:0

来自:嫏嬛閣
状态:...保密...

[个人空间] [短信] [博客]


7楼 发表于 2007-9-23 21:51 


QUOTE:
原帖由 守夜人 于 2007-9-23 19:56 发表
在2.4下直接导出sys_call_table截获系统调用是很容易的,但2.4.18以后好像就不行了..
楼主这个方法是不是能弥补2.6.x内核不能导出sys_call_table的问题?????

sure



您对本贴的看法:鲜花[0] 臭蛋[0]

__________________________________



  •  平生自有千秋在,不向群兒問毀譽!
     
  •  爱国主义是群氓的道德安全套。
     
  •  有些爱国青年想必是为人做了太多blowjob,不然为何张口便吐生殖器?
     
  •  Feel free to ask questions by 站内短信, and I'll feel free to ignore you.


bluster
精灵王



CU编号: 573179
注册:2007-6-7
最后登录: 2007-12-29
帖子:371
精华:0

可用积分:460 (白手起家)
信誉积分:100
专家积分:0 (本版:0)
空间积分:0
推广积分:0

状态:...离线...

[个人空间] [短信] [博客]


8楼 发表于 2007-9-24 09:29 
这里边似乎就是用 kallsyms_lookup_name 来找do_execve的地址?
似乎用kallsyms_lookup_name更通用一点,我看2.6.x前面的版本里面似乎register_kprobes没有现在的kallsyms_lookup_name.
有一点疑问,我记得Linux的符号表是自己管理的 kernel/kallsyms.c ,对于没有导出符号,我怎么记得好像在link的时候是放到最后面的,
似乎可能会被覆盖(好像有个内核选择控制这个行为,CONFIG_KALLSYMS?) 。

btw, Linux kernel的下游开发越来越难了,光符号表就折腾死人,api又不稳,这个register_kprobes就是现成的例子。



您对本贴的看法:鲜花[0] 臭蛋[0]

__________________________________



albcamus (百無一用書生)
法王
[知否興風狂嘯者,回眸時看 ...



CU编号: 140189
注册:2004-3-7
最后登录: 2009-07-04
帖子:12176
精华:22

可用积分:11608 (大富大贵)
信誉积分:110
专家积分:71 (本版:0)
空间积分:0
推广积分:0

来自:嫏嬛閣
状态:...保密...

[个人空间] [短信] [博客]


9楼 发表于 2007-9-24 12:42 


QUOTE:
原帖由 bluster 于 2007-9-24 09:29 发表
这里边似乎就是用 kallsyms_lookup_name 来找do_execve的地址?
似乎用kallsyms_lookup_name更通用一点,我看2.6.x前面的版本里面似乎register_kprobes没有现在的kallsyms_lookup_name.
有一点疑问,我记得Li ...

较新版本的kernel中, kallsyms_lookup_name已经不可用了。

没办法, kernel API真是折腾人。举例来说,我装的vmware,每当升级一个内核版本,都要改一堆代码才能跑起来。



您对本贴的看法:鲜花[0] 臭蛋[0]

__________________________________



  •  平生自有千秋在,不向群兒問毀譽!
     
  •  爱国主义是群氓的道德安全套。
     
  •  有些爱国青年想必是为人做了太多blowjob,不然为何张口便吐生殖器?
     
  •  Feel free to ask questions by 站内短信, and I'll feel free to ignore you.


dedodong
侠客




CU编号: 348511
注册:2005-12-12
最后登录: 2009-01-14
帖子:15
精华:0

可用积分:14 (白手起家)
信誉积分:100
专家积分:0 (本版:0)
空间积分:0
推广积分:0

状态:...离线...

[个人空间] [短信] [博客]


10楼 发表于 2007-10-17 10:19 


QUOTE:
原帖由 albcamus 于 2007-9-24 12:42 发表

没办法, kernel API真是折腾人。举例来说,我装的vmware,每当升级一个内核版本,都要改一堆代码才能跑起来。

我装的vmware,每当升级一个内核版本,都要改一堆代码才能跑起来。   

牛....
albcamus大师不知道最近空闲不? 能收我做徒弟不??

嘿嘿



您对本贴的看法:鲜花[0] 臭蛋[0]

首页 » CU论坛 » Linux » 汇总贴列表 » 内核源码 »

 


Copyright © 2001-2008 ChinaUnix.net All Rights Reserved     联系我们:

感谢所有关心和支持过ChinaUnix的朋友们    转载本站内容请注明原作者名及出处

京ICP证041476号


清除 Cookies - Linux时代 - Archiver - WAP - TOP

Processed in 0.225534 second(s), 5 queries , Gzip enabled