+ -
当前位置:首页 → 问答吧 → 怎么把截获的系统调用实时地记录到文件???

怎么把截获的系统调用实时地记录到文件???

时间:2003-04-08

来源:互联网

大家来想想吧

作者: cuicn_21   发布时间: 2003-04-08

作者: cuicn_21   发布时间: 2003-04-08

做个新的系统调用,创建一个内核模块,拦截所有系统调用,然后用printk打印出来。编写应用程序执行这个调用,然后将这个程序的输出重定向到某个文件不就行了。

作者: owl   发布时间: 2003-04-15

请教楼上的,printk打印出来的东西能重定向吗?

作者: keenor   发布时间: 2003-04-16

我写了下面代码,实现了open()系统调用对文件操作(创建文件)。其他调用你自然就明白了:

代码:
/*************************************
 *
 * kopen.c by keenor [email protected]
 * gcc -O2 -I/usr/src/linux/include -c -o kopen.o kopen.c
 * 4/16/2003
 *
 * ***********************************
 */

#ifndef __KERNEL__
        #define __KERNEL__
#endif

#ifndef MODULE
        #define MODULE
#endif

#include <linux/modversions.h>
#include <sys/syscall.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm-i386/uaccess.h>
#include <asm-i386/segment.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <linux/fs.h>

extern void *sys_call_table[];

int (*open)(char *, int, int);

int kread_init(){

        open = sys_call_table[SYS_open];
//      printk("<1>%s%x\n", "open=", (unsigned long)open);

        mm_segment_t old_fs_value=get_fs();

        set_fs(get_ds()); 
        int res = open("/aaa", O_CREAT|O_RDWR|O_EXCL, 0640);
//      printk("<1>%s%d\n", "res of open = ", res);
        set_fs(old_fs_value);
        
        return 0;
}

void kread_cleanup(){
        //
}

module_init(kread_init);
module_exit(kread_cleanup);
MODULE_LICENSE("GPL");
编译通过,我的内核为2.4.20,老内核你再改改吧:)。执行insmod kopen.o 后会在产生 /aaa 文件,权限为640。
注意:redhat等某些dist的某些版本内核并不输出 sys_call_table 符号,这种情况下你可以考虑升级内核:P,或者把原来的内核原码的sys_call_table输出(还是升级内核好)。

作者: keenor   发布时间: 2003-04-16

请帮忙想想截获read,write的方法!,谢谢

作者: cuicn_21   发布时间: 2003-04-22

引用:
最初由 owl 发表
做个新的系统调用,创建一个内核模块,拦截所有系统调用,然后用printk打印出来。编写应用程序执行这个调用,然后将这个程序的输出重定向到某个文件不就行了。

请问你有没有实现过截获所有的系统调用,我这么做了,但是执行的时候总是出错,请指教!谢谢!

作者: bhlyy1976   发布时间: 2003-05-01

呵呵,这位兄台真是说笑, 你的code不但不能截获 而且可能导致overflow,况且你所调用head file也不对路。


你的初始化里面的code只能算个定义。

但结果不是最终的。

作者: ZeroC   发布时间: 2003-05-09

您说的是。
我写那个代码的时候确实太有点无知和狂妄了。这样的帖子留着只能让人笑话,教人误入歧途。后来总想把这个帖删了,可是已经超过了可以删除的时间。希望斑竹看见的话把我那个code删掉,多谢!

作者: keenor   发布时间: 2003-05-09

那也不必, 我们可以加工一下的,

呵呵

作者: ZeroC   发布时间: 2003-05-09

那就请您帮我加工一下吧,以期指点迷津
还有,我不懂什么叫做overflow,请指教
多谢!

作者: keenor   发布时间: 2003-05-10

overflow=溢出。

因为你的point的指向, 所以这么说的!

作者: ZeroC   发布时间: 2003-05-10

我的point在哪里啊?大哥?
还有,溢出我也不懂,请教什么是溢出呢?是什么溢出呢?
多谢!

作者: keenor   发布时间: 2003-05-10

int (*open)(char *, int, int);

int kread_init(){

open = sys_call_table[SYS_open];
// printk("<1>%s%x\n", "open=", (unsigned long)open);

mm_segment_t old_fs_value=get_fs();

set_fs(get_ds());
int res = open("/aaa", O_CREAT|O_RDWR|O_EXCL, 0640);
// printk("<1>%s%d\n", "res of open = ", res);
set_fs(old_fs_value);

return 0;
}

void kread_cleanup(){
//
}
-------------------------------------------------------------------

overflow 你的gdb 吧, 不谈。
-----------------------------------------------------------------
我想该写写看

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

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif
/* SAME TO U

extern void* sys_call_table[];  
              
  
int (*orig_system)(const char *path);

int find_system(const char *path)
{
return 0;            
              
    
}
int init_module(void)        

{
orig_system=sys_call_table[SYS_system];
sys_call_table[SYS_system]=find_system;
return 0;
}
void cleanup_module(void)      

{
sys_call_table[SYS_system]=orig_system;

}

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

大多方法都类似,也许是学习老外吧。呵呵

参照LKM_HACKER方法。

作者: ZeroC   发布时间: 2003-05-10

hehe

作者: keenor   发布时间: 2003-05-10

谢谢指出不对的地方,以后小心发帖子, 谢谢这位朋友, 我收回我发的帖子, sorry,

呵呵。。。。

作者: ZeroC   发布时间: 2003-05-10

呵呵,朋友 也不用这样啊, 叫他离开这里就可以了。

作者: l33t   发布时间: 2003-05-10