• linux系统调用拦截Centos7.6(三)の内核调用拦截


    编写自己的系统调用 替换系统调用 已open()为例

    hello.c

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. static char readbuf[100];
    17. static char writebuf[100];
    18. static unsigned long * sys_call_table ;
    19. asmlinkage int (*original_open) (const char*, int, int);
    20. asmlinkage int custom_open (const char* __user file_name, int flags, int mode);
    21. static int __init hello_init ( void )
    22. {
    23. sys_call_table = NULL;
    24. sys_call_table = (unsigned long *)kallsyms_lookup_name("sys_call_table");
    25. write_cr0 (read_cr0 () & (~ 0x10000));
    26. original_open = (void *)sys_call_table[__NR_open];
    27. sys_call_table[__NR_open] = custom_open;
    28. write_cr0 (read_cr0 () | 0x10000);
    29. printk ( KERN_INFO "hello module loaded successfully \n");
    30. return 0;
    31. }
    32. static void __exit hello_exit ( void )
    33. {
    34. write_cr0 (read_cr0 () & (~ 0x10000));
    35. sys_call_table[__NR_open] = original_open;
    36. write_cr0 (read_cr0 () | 0x10000);
    37. printk ( KERN_INFO "hello module unloaded\n" );
    38. }
    39. asmlinkage int custom_open (const char* __user file_name, int flags, int mode)
    40. {
    41. copy_from_user(writebuf, file_name, 80);
    42. printk ( KERN_INFO "hello %s\n", writebuf);
    43. printk ( KERN_INFO "hello custom_open successfully %d- %d \n",flags, mode);
    44. //获取当前进程的id
    45. int pid = current->pid;
    46. //获取当前进程的父进程id
    47. int ppid = current->real_parent->real_parent->pid;
    48. //获取当前进程的根目录
    49. const char *ppwd = (current->fs->root).dentry->d_name.name;
    50. struct path pwd;
    51. //获取当前目录
    52. get_fs_pwd(current->fs,&pwd);
    53. printk(KERN_WARNING "helloa PID=%d,parent=%d attempts to open!\n",pid,ppid);
    54. printk(KERN_WARNING "helloa ROOT:%s!\n",ppwd);
    55. printk(KERN_WARNING "helloa PWD:%s!\n",pwd.dentry->d_name.name);
    56. return original_open( file_name , flags, mode);
    57. }
    58. MODULE_LICENSE ( "GPL" );
    59. module_init ( hello_init );
    60. module_exit ( hello_exit );

    Makefile 文件

    1. obj-m:=hello.o
    2. PWD:= $(shell pwd)
    3. KERNELDIR:= /usr/src/linux-3.10.0-957.el7
    4. EXTRA_CFLAGS= -O0
    5. CONFIG_MODULE_SIG=n
    6. all:
    7. make -C $(KERNELDIR) M=$(PWD) modules
    8. clean:
    9. make -C $(KERNELDIR) M=$(PWD) clean

     把文件放在/opt/hello目录

     cd /opt/hello 目录

    执行 make... 

    报错 *** 没有规则可以创建“/opt/hello/hello.o”需要的目标“tools/objtool/objtool”。 停止。

    切换 /usr/src/linux-3.10.0-957.el7 执行如下指令

    yum install elfutils-libelf-devel

    make mrproper 

    make oldconfig

    make prepare

    make scripts

    继续 进入 /opt/hello 执行 make clean & make

    生成hello.ko 文件 

    执行 insmod hello.ko 注册模块

    执行 dmesg | tail  可以查看 日志

    执行rmmod hello 卸载模块

     

  • 相关阅读:
    国家开放大学 模拟试题 训练
    Spring 如何自己创建一个IOC 容器
    火车头翻译插件-免费火车头翻译插件-火车头自动翻译插件
    ESP8266使用记录(三)
    农业产业谋定颠覆与重构-国稻种芯-万祥军:现代农业发展前景
    ​Cloneable接口
    iOS 开发中上传 IPA 文件的方法(无需 Mac 电脑)
    MAC安全(防MAC泛洪攻击)
    《安富莱嵌入式周报》第326期:航空航天级CANopen协议栈,开源USB PD电源和功耗分析,开源EtherCAT伺服驱动板,时序绘制软件,现代机器人设计
    Find My自行车|苹果Find My技术与自行车结合,智能防丢,全球定位
  • 原文地址:https://blog.csdn.net/qq_23077365/article/details/127728759