dtod
驱动牛犊
驱动牛犊
  • 注册日期2006-07-20
  • 最后登录2013-09-09
  • 粉丝0
  • 关注0
  • 积分418分
  • 威望355点
  • 贡献值0点
  • 好评度54点
  • 原创分0分
  • 专家分0分
阅读:2295回复:2

请教各位关于2.6.29下保护文件的问题?

楼主#
更多 发布于:2009-11-26 13:49
在学习LINUX 内核的 想实现个对指定文件名的文件不能打开就是保护指定文件,现在是在2.6.29内核下,想HOOK SYS_OPEN的可是好像没有2.6.29下导出,是不是可以HOOK filp_open实现保护指定文件的,可是filp_open该怎么HOOK的,请教各位,最好能有示例代码和资料的 谢谢各位

我试了下 hook do_filp_open

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#include <linux/namei.h>

#define dpri(fmt, args...)      printk("%s:%d: " fmt, __func__, __LINE__, ##args)
long i;
static struct file *do_filp_open_hook(int dfd, const char *filename, int flags,
                                      int mode)
{
        printk("[ %d ][TIME %s ] -----  %s, 0x%x, 0%o \n",i,__TIME__, filename, flags, mode);        
i++;
        jprobe_return();
        return NULL;
}


#define RpFunc(func) \
static int func##_rhook(struct kretprobe_instance *ri, struct pt_regs *regs) \
{ \
        LKTRTrace("eax %ld\n", regs->eax); \
        return 0; \
}

//RpFunc(do_filp_open);

/* ---------------------------------------------------------------------- */

#define HookJp(hook)    {.entry = JPROBE_ENTRY(hook)}
#define Hook(func)      {.funcname = #func, .jp = HookJp(func##_hook)}
static struct hook {
        char *funcname;
        struct jprobe jp;
} hooks[] = {
        Hook(do_filp_open),
        {.funcname = NULL}
};

#define HookRp(hook)    {.handler = hook}
#define RHook(func)     {.funcname = #func, .rp = HookRp(func##_rhook)}


static struct rhook {
        char *funcname;
        struct kretprobe rp;
} rhooks[] = {
        //RHook(do_filp_open),
        {.funcname = NULL}
};

static int __init probe_init(void)
{
        int err;
  i=0;
        struct hook *p;
        struct rhook *q;

        p = hooks;
        while (p->funcname) {
                p->jp.kp.symbol_name = p->funcname;
                p++;
        }
        q = rhooks;
        while (q->funcname) {
                q->rp.kp.symbol_name = q->funcname;
                q++;
        }

        err = 0;
        p = hooks;
        while (p->funcname && !err) {
                err = register_jprobe(&p->jp);
                p++;
        }
        if (!err) {
                q = rhooks;
                while (q->funcname && !err) {
                        err = register_kretprobe(&q->rp);
                        q++;
                }
                if (!err)
                        return 0;
                while (--q != rhooks)
                        unregister_kretprobe(&q->rp);
        }

        while (--p != hooks)
                unregister_jprobe(&p->jp);
        return err;
}

static void __exit probe_exit(void)
{
        struct hook *p;
        struct rhook *q;

        p = hooks;
        while (p->funcname) {
                unregister_jprobe(&p->jp);
                p++;
        }
        q = rhooks;
        while (q->funcname) {
                unregister_kretprobe(&q->rp);
                q++;
        }
}

module_init(probe_init);
module_exit(probe_exit);
MODULE_LICENSE("GPL");

可是为什么打印出来的是一些别的信息 如
[ 328 ][TIME 16:23:26 ] -----  /dev/bus/usb/002/001, 0x2, 01
[ 329 ][TIME 16:23:26 ] -----  /dev/bus/usb/001, 0x18800, 026777010434
[ 330 ][TIME 16:23:26 ] -----  /dev/bus/usb/001/001, 0x2, 01
[ 331 ][TIME 16:23:26 ] -----  /dev/bus/usb/001/001, 0x2, 01
[ 332 ][TIME 16:23:26 ] -----  /etc/ld.so.cache, 0x0, 00
[ 333 ][TIME 16:23:26 ] -----  /lib/libc.so.6, 0x0, 027770503450
[ 334 ][TIME 16:23:26 ] -----  /usr/lib/locale/locale-archive, 0x8000, 01


比如我用GEDIT打开一个文件可是在打印里看不到打开这个文件的?
cyliu
论坛版主
论坛版主
  • 注册日期2003-06-13
  • 最后登录2014-04-11
  • 粉丝5
  • 关注0
  • 积分1238分
  • 威望2531点
  • 贡献值0点
  • 好评度577点
  • 原创分14分
  • 专家分10分
沙发#
发布于:2009-11-28 18:54
1 文件安全思路就是错误的
2 就算按您现在的思路,使用的方法也是错误的
建议搞清linux文件系统原理在做方案设计,否则就是白花时间。
走走看看开源好 Solaris vs Linux
dtod
驱动牛犊
驱动牛犊
  • 注册日期2006-07-20
  • 最后登录2013-09-09
  • 粉丝0
  • 关注0
  • 积分418分
  • 威望355点
  • 贡献值0点
  • 好评度54点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2009-11-28 20:59
引用第1楼cyliu于2009-11-28 18:54发表的  :
1 文件安全思路就是错误的
2 就算按您现在的思路,使用的方法也是错误的
建议搞清linux文件系统原理在做方案设计,否则就是白花时间。

谢谢cyliu, 文件安全思路就是错误的 这个不太理解的,请您稍微给点建议的关于文件安全 谢谢您的
游客

返回顶部