proc文件系统下文件隐藏

来源:互联网 发布:搜狗拼音输入法 mac 编辑:程序博客网 时间:2024/04/27 23:27
0.
  初读vfs,大体思路都有了,仔细分析了proc中,写个文件隐藏深入了解proc目录项的添加与创建。
 
1.yy原先的隐藏方法
  原先的方法是考虑sys_getdents64这个系统调用,有变更内部执行流,也有hook filldir64
这个函数的。
  我想写个通用的方法,就是切断要隐藏的文件与整个kernel的关系,说白了就是把文件相关结构
脱离kernel联系(比如一些文件相关链表等),具体实行起来把inode/dentry/file等等结构都
考虑到,未免太繁琐了。况且这里只是实现欺骗ls的小功能。
 
2.
  具体实现起来也要涉及sys_getdents64的流程(ls就用此函数)
----------
  sys_getdents64 
   ==> vfs_readdir 
    ==> file->f_op->readdir(对于proc根目录是proc_root_readdir/proc内部子目录是proc_readdir)
     ==> proc_readdir:
   ...
   do {
       if (filldir(dirent, de->name, de->namelen, filp->f_pos, de->low_ino, de->mode >> 12) < 0)
           goto out;
       filp->f_pos++;
       de = de->next;
   } while (de);
   ...
----------
  从代码里面可以看到,最后获取的de(proc_dir_entry)通过filldir函数填充到dirent,最后拷贝dirent到用户态给ls
  这里,只要将想要隐藏的文件的proc_dir_entry从其父目录项的子目录项链表中摘除即可。                               
 
3.代码实现
  代码只实现隐藏文件/proc/bus,恢复隐藏和具体通用proc文件隐藏就没实现:b
---------------
/* kernel-2.6.34 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <asm/unistd.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
 
/* hide file: /proc/bus */
#define HIDEFILE "bus"
#define HIDEFILE_PARENT "/proc"
 
static int __init hide_init(void)
{
        struct file *filp;
        struct inode *inode;
        struct proc_dir_entry *pe, *se, *old_se;
        
        /* get parent proc_dir_entry */
        filp = filp_open(HIDEFILE_PARENT, O_RDONLY, 0644);
        if (filp == NULL) {
                printk(KERN_ALERT "cannot open %s/n", HIDEFILE_PARENT);
                return -1;
        }
        inode = filp->f_path.dentry->d_inode;
        pe = PDE(inode);
        
        /* search hidefile proc_dir_entry */
        for (old_se = NULL, se = pe->subdir; se; old_se = se, se = se->next) {
                if (strcmp(se->name, HIDEFILE) == 0)
                break;
        }
        
        if (se == NULL) {
                printk(KERN_ALERT "cannot find %s in dir %s/n",
                HIDEFILE, HIDEFILE_PARENT);
                return -1;
        }
        
        /* 'del' hidefile proc_dir_entry from list */
        if (old_se) {
                /* pe->subdir is just hidefile! */
                pe->subdir = se->next;
        }
        else {
                old_se->next = se->next;
        }
        filp_close(filp, NULL);
        printk(KERN_ALERT "hide init/n");
        return 0;
}
 
static void __exit hide_exit(void)
{
        /*
        * You should recover the hidefile!
        * I'm too lazy to do it :P
        */
}
 
module_init(hide_init);
module_exit(hide_exit);
MODULE_LICENSE("GPL");
-------------
 
4. 测试
#ls /proc | grep bus
bus
#insmod hidefile.ko
hide init
#ls /proc | grep bus
#cat /proc/bus
cat: /proc/bus/: Is a directory
#cd /proc/bus              
#                                         //进入/proc/bus
 
经过我的测试,隐藏文件和隐藏目录还是有区别的,文件可以隐藏后完全找不到,
但是目录隐藏后仍然可以连接进入,只能骗骗ls
 
5.
  继续vfs,看看最后能不能做个把文件从内核关系集合中剥离的LKM :)
  Just for funnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn!

 

原创粉丝点击