linux内核有没有hook机制

linux内核有没有hook机制,第1张

有啊,一切顺序逻辑,都有被hook的可能。 下面是一个linux上的hook的实例

截获write系统调用:

#ifndef MODULE

#define MODULE

#endif

                                                                              

#ifndef __KERNEL__

#define __KERNEL__

#endif 

#include <linux/init.h>

#include <linux/module.h>

#include <linux/version.h>

#include <linux/kernel.h>

#include <asm/unistd.h>

#include <linux/slab.h>

/*

#include <sys/types.h>

#include <asm/fcntl.h>

#include <linux/malloc.h>

#include <linux/types.h>

#include <linux/string.h>

#include <linux/fs.h>

#include <asm/errno.h> 

#include <sys/syscall.h>

*/ 

MODULE_LICENSE("GPL")

struct descriptor_idt

{

        unsigned short offset_low

        unsigned short ignore1

        unsigned short ignore2

        unsigned short offset_high

}

static struct {

        unsigned short limit

        unsigned long base

}__attribute__ ((packed)) idt48

static unsigned int SYS_CALL_TABLE_ADDR

void **sys_call_table

int base_system_call

int (*orig_write)(unsigned int fd,char *buf,unsigned int count)

unsigned char opcode_call[3]={0xff,0x14,0x85}

int match(unsigned char *source)

{

        int i

        for(i=0i<3i++){

                if(source[i] != opcode_call[i])

                        return 0

        }

        return 1

}

int get_sys_call_table(void)

{

        int i,j

        unsigned char *ins=(unsigned char *)base_system_call

        unsigned int sct

                                                                              

        for(i=0i<100i++){

                if(ins[i]==opcode_call[0]){

                        if(match(ins+i)){

                                sct=*((unsigned int *)(ins+3+i))

                                printk(KERN_ALERT "sys_call_tabl's address is

0x%X\n",sct)

                                return sct

                        }

                }

        }

                                                                              

        printk(KERN_ALERT "can't find the address of sys_call_table\n")

        return -1

}

int hacked_write(unsigned int fd,char *buf,unsigned int count)

 char *hide="hello"

 if(strstr(buf,hide)!=NULL){

  printk(KERN_ALERT "find name.\n")

  return count

 }

 else{

  return orig_write(fd,buf,count)

 }

}

int init_module(void)

{

        __asm__ volatile ("sidt %0": "=m" (idt48))

        struct descriptor_idt *pIdt80 = (struct descriptor_idt *)(idt48.base + 8*0x80)

        base_system_call = (pIdt80->offset_high<<16 | pIdt80->offset_low)

        printk(KERN_ALERT "system_call address at 0x%x\n",base_system_call)

 SYS_CALL_TABLE_ADDR=get_sys_call_table()

 sys_call_table=(void **)SYS_CALL_TABLE_ADDR

 orig_write=sys_call_table[__NR_write]

 sys_call_table[__NR_write]=hacked_write

        return 0

}

void cleanup_module()

{

 sys_call_table[__NR_write]=orig_write

}

检测内存泄露主要有以下5种方法:1、在需要内存泄漏检查的代码的开始调用void mtrace(void) (该函数在头文件mcheck.h中有声明)。mtrace为malloc等函数安装hook,用于记录内存分配信息.在需要内存泄漏检查的代码的结束调用void muntrace(void)。注意: 一般情况下不要调用muntrace, 而让程序自然结束. 因为可能有些释放内存代码要到muntrace之后才运行. 2、用debug模式编译被检查代码(-g或-ggdb)。3、设置环境变量MALLOC_TRACE为一文件名, 这一文件将存有内存分配信息。 4、运行被检查程序, 直至结束或muntrace被调用。5、用mtrace命令解析内存分配Log文件($MALLOC_TRACE)(mtrace foo $MALLOC_TRACE, where foo is the executible name)如果有内存泄漏,mtrace会输出分配泄漏内存的代码位置,以及分配数量。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7187171.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-02
下一篇2023-04-02

发表评论

登录后才能评论

评论列表(0条)

    保存