一種Linux下隱藏檔案的新方法

來源:互聯網
上載者:User

一種Linux下隱藏檔案的新方法
       
Author: wzt
EMail: wzt@xsec.org
Site: http://www.xsec.org & http://hi.baidu.com/wzt85
Date: 2008-9-23

一. 概述

目前通用的隱藏檔案方法還是hooksys_getdents64系統調用, 大致流程就是先調用原始的
sys_getdents64系統調用,然後在在buf中做過濾。修改sys_call_table是比較原始的rk技術了,
碰到好點的管理員, 基本上gdb一下vmlinux就能檢測出來。 如何想做到更加隱形話,就要
尋找新的技術。 inline hook也是目前比較流行的做法,不容易檢測。本文通過講解一種利用
inline hook核心中某函數, 來達到隱藏檔案的方法。

二. 剖析sys_getdnts64系統調用
想隱藏檔案, 還是要從sys_dents64系統調用下手。 去看下它在核心中是如何?的。
代碼在linux-2.6.26/fs/readdir.c中:

asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count)
{
        struct file * file;
        struct linux_dirent64 __user * lastdirent;
        struct getdents_callback64 buf;
        int error;

        error = -EFAULT;
        if (!access_ok(VERIFY_WRITE, dirent, count))
                goto out;

        error = -EBADF;
        file = fget(fd);
        if (!file)
                goto out;

        buf.current_dir = dirent;
        buf.previous = NULL;
        buf.count = count;
        buf.error = 0;

        error = vfs_readdir(file, filldir64, &buf);
        if (error < 0)
                goto out_putf;
        error = buf.error;
        lastdirent = buf.previous;
        if (lastdirent) {
                typeof(lastdirent->d_off) d_off = file->f_pos;
                error = -EFAULT;
                if (__put_user(d_off, &lastdirent->d_off))
                        goto out_putf;
                error = count - buf.count;
        }

out_putf:
        fput(file);
out:
        return error;
}

首先調用access_ok來驗證是下使用者空間的dirent地址是否越界,是否可寫。 接著根據fd,
利用fget找到對應的file結構。 接著出現了一個填充buf資料結構的操作,先不管它是幹什麼的,
接著往下看。
vfs_readdir(file, filldir64, &buf);
函數最終還是調用vfs層的vfs_readdir來擷取檔案清單的。 到這,我們可以是否通過hook
vfs_readdir來達到隱藏檔案的效果呢。 繼續跟蹤vfs_readdir看看這個想法是否可行。

原始碼在同一檔案中:

int vfs_readdir(struct file *file, filldir_t filler, void *buf)
{
        struct inode *inode = file->f_path.dentry->d_inode;
        int res = -ENOTDIR;
        if (!file->f_op || !file->f_op->readdir)
                goto out;

        res = security_file_permission(file, MAY_READ);
        if (res)
                goto out;

        res = mutex_lock_killable(&inode->i_mutex);
        if (res)
                goto out;

        res = -ENOENT;
        if (!IS_DEADDIR(inode)) {
                res = file->f_op->readdir(file, buf, filler);
                file_accessed(file);
        }
        mutex_unlock(&inode->i_mutex);
out:
        return res;
}

EXPORT_SYMBOL(vfs_readdir);

它有3個參數,第一個是通過fget得到的file結構指標, 第2個通過結合上下文可得知,這是一個
回呼函數用來填充第3個參數開始的使用者空間的指標。 接著看看它具體是怎麼實現的。
通過security_file_permission()驗證後, 在用mutex_lock_killable()對inode結構加了鎖。
然後調用ile->f_op->readdir(file, buf, filler);通過進一步的底層函數來對buf進行填充。
這個buf就是使用者空間strcut dirent64結構的開始地址。

所以到這裡我們可以斷定通過hook vfs_readdir函數對buf做過濾還是可以完成隱藏檔案的功能。
而且vfs_readdir的地址是匯出的, 這樣就不用複雜的方法找它的地址了。

但是還有沒有更進一步的方法呢? 前面不是提到過有個filldir64函數嗎, 它用來填充buf結構的。
也許通過hook它來做更隱形隱藏檔案方法。 繼續跟蹤filldir64,看看它是怎麼實現的。

static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
                     u64 ino, unsigned int d_type)
{
        struct linux_dirent64 __user *dirent;
        struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
        int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 1, sizeof(u64));

        buf->error = -EINVAL;
        if (reclen > buf->count)
                return -EINVAL;
        dirent = buf->previous;
        if (dirent) {
                if (__put_user(offset, &dirent->d_off))
                        goto efault;
        }
        dirent = buf->current_dir;
        if (__put_user(ino, &dirent->d_ino))
                goto efault;
        if (__put_user(0, &dirent->d_off))
             &

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.