Linux hook内核函数

在内核中,如果要hook某个函数,有三种方法,

1. 修改syscall table

2.替换对象指针

3. inline hook 只要把函数开头的5个字节替换成call/jmp指令.

前两种比较简单,这里记录下inline hook的实现过程.

步骤

1. 保存orig 函数的前5个字节指令.

2.定义一个stub函数,用于跳转回orig函数.

3.在新函数中,调用stub函数.

 

具体实现

#include<linux/module.h>
#include<linux/printk.h>
#include<linux/kobject.h>
#include<linux/kernel.h>
#include<asm/unistd_64.h>
#include<linux/syscalls.h>
#include<linux/delay.h>
#include<linux/kallsyms.h>
#include<asm/syscall.h>
#include<asm/paravirt.h>
#include <asm/nops.h>
#include <linux/net.h>
#include <linux/in.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <linux/init.h>
#include <asm/insn.h>

typedef void *(*text_poke_t)(void *addr, const void *opcode, size_t len);
typedef void (*insn_init_t)(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
typedef void (*insn_get_len_t)(struct insn *insn);

static int (*orig_ip_rcv)(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
	   struct net_device *orig_dev) ;


static int stub_ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
	   struct net_device *orig_dev)
{

	printk("stub\n");
	asm("nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop");
	return 0;
}


static int hook_ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
	   struct net_device *orig_dev)
{
	printk(KERN_ERR"I'm hook function\n");
	return stub_ip_rcv(skb,dev,pt,orig_dev);
}

static char save_ip[15];
static char jump_ip[15];
#define JUMP_LEN  (5)
static int save_len = 0;
text_poke_t poke_fun = NULL ;
static int __init rootkits_lkm_init(void)
{
	int ret = 0;
	int len = 0;
	int * tmp = 0;
	char * tmp1 = NULL;
	insn_init_t  ins_init ;
	insn_get_len_t  ins_get_len ;
	struct insn ins ;
	text_poke_t  text_poke = kallsyms_lookup_name("text_poke");
	if(!text_poke){
		printk(KERN_ERR"get text poke error\n");
		return 0;
	}

	ins_init = kallsyms_lookup_name("insn_init");
	ins_get_len = kallsyms_lookup_name("insn_get_length");
	if(!ins_init||!ins_get_len){
		printk("get insn error\n");
		return 0;
	}

	orig_ip_rcv = kallsyms_lookup_name("ip_rcv");
	/* jump offset */
	jump_ip[0] = 0xe9 ;
	tmp = (int *)&jump_ip[1] ;

	/*offset = to_ip - (from_ip+JUMP_LEN)  */
	*tmp = (unsigned long )hook_ip_rcv - ((unsigned long)orig_ip_rcv+JUMP_LEN);

	/* Get first instrucion len*/
	while(ret<JUMP_LEN){
		ins_init(&ins,orig_ip_rcv+ret,15,1);
		ins_get_len(&ins);
		ret +=ins.length ;
	}

	len = ret ;
	memcpy(save_ip,orig_ip_rcv,len);
	tmp1 = (char *)orig_ip_rcv ;

	printk(KERN_ERR"We get len = %d [%x]\n",len,*tmp1) ;
	for(ret=0;ret<len;ret++)
		printk(KERN_ERR"[%x]",*(tmp1+ret));

	/*offset = to_ip - (from_ip+JUMP_LEN)  */
	ret = ((unsigned long)orig_ip_rcv+len) - ((unsigned long)stub_ip_rcv+JUMP_LEN+len);
	save_ip[len] = 0xe9 ;
	tmp = (int *)&save_ip[len+1] ;
	*tmp = ret ;

	text_poke(stub_ip_rcv,save_ip,JUMP_LEN+len);

	text_poke(orig_ip_rcv,jump_ip,JUMP_LEN);
	poke_fun = text_poke ;
	save_len = len ;

	return 0;
}

static void __exit root_kits_lkm_uninit(void)
{
	printk("hell world lsm uninit\n");
	poke_fun(orig_ip_rcv,save_ip,save_len);
}

module_init(rootkits_lkm_init);
module_exit(root_kits_lkm_uninit);
MODULE_LICENSE("GPL");

给函数插入指令
  扫描函数所有的指令序列
  校准针对函数外部的相对寻址的指令(call/jmp),使其操作数减去函数搬移的偏移值
   offset = function_A - stub_B + offset

注意事项:

1. 如果不使用text_poke接口,需要手动对ip_rcv的内存进行重新映射,让内存变为可写,或者直接禁止CR0写保护

2. stub函数和orig function需要在2G范围内。

3.如果时给内核函数插入指令,需要校准相对偏移
    校准针对函数外部的相对寻址的指令(call/jmp),使其操作数减去函数搬移的偏移值
   offset_B = function_A - stub_B + offset_A

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值