找回密码
 立即注册
首页 业界区 业界 聊一聊 Linux 上对函数进行 hook 的两种方式 ...

聊一聊 Linux 上对函数进行 hook 的两种方式

癖艺泣 2025-9-24 18:02:29
一:背景

1. 讲故事

前两篇我们介绍了 Minhook 在 Windows 平台上的强大功效,这一篇我们来聊一聊如何在 Linux 上对函数进行hook,这里介绍两种方式。

  • 轻量级的 LD_PRELOAD 拦截
LD_PRELOAD是一种共享库拦截,这种方式的优点在于不需要对源程序做任何修改,达到无侵入的功效,这是windows平台上不可想象的。

  • funchook 拦截
在 github 有很多可用于 linux 上的函数 hook,我发现轻量级的,活跃的,开源的 要属 funchook 吧。
二:两种拦截方式

1. LD_PRELOAD 如何实现拦截

要想明白 LD_PRELOAD 如何实现拦截?需要你对 linux 上的进程初始化时的链接器 ld.so 的工作过程有一个了解,简单来说就是它的加载顺序为 主程序的可执行文件 -> LD_PRELOAD 指定的库 -> glibc 标准库 -> 其他依赖库 。
由于 LD_PRELOAD 指定的 so 文件优于 glibc.so 解析,所以可以利用这种先入为主的方式覆盖后续的同名符号方法,那 ld.so 长啥样呢?在我的ubuntu上就是 ld-linux-x86-64.so.2。
  1. root@ubuntu2404:/data2# cat /proc/5322/maps
  2. 60c0f8687000-60c0f8688000 r--p 00000000 08:03 1966089                    /data2/main
  3. 60c0f8688000-60c0f8689000 r-xp 00001000 08:03 1966089                    /data2/main
  4. 60c0f8689000-60c0f868a000 r--p 00002000 08:03 1966089                    /data2/main
  5. 60c0f868a000-60c0f868b000 r--p 00002000 08:03 1966089                    /data2/main
  6. 60c0f868b000-60c0f868c000 rw-p 00003000 08:03 1966089                    /data2/main
  7. 60c1266de000-60c1266ff000 rw-p 00000000 00:00 0                          [heap]
  8. 7efd5c600000-7efd5c628000 r--p 00000000 08:03 2242169                    /usr/lib/x86_64-linux-gnu/libc.so.6
  9. 7efd5c628000-7efd5c7b0000 r-xp 00028000 08:03 2242169                    /usr/lib/x86_64-linux-gnu/libc.so.6
  10. 7efd5c7b0000-7efd5c7ff000 r--p 001b0000 08:03 2242169                    /usr/lib/x86_64-linux-gnu/libc.so.6
  11. 7efd5c7ff000-7efd5c803000 r--p 001fe000 08:03 2242169                    /usr/lib/x86_64-linux-gnu/libc.so.6
  12. 7efd5c803000-7efd5c805000 rw-p 00202000 08:03 2242169                    /usr/lib/x86_64-linux-gnu/libc.so.6
  13. 7efd5c805000-7efd5c812000 rw-p 00000000 00:00 0
  14. 7efd5c964000-7efd5c967000 rw-p 00000000 00:00 0
  15. 7efd5c977000-7efd5c979000 rw-p 00000000 00:00 0
  16. 7efd5c979000-7efd5c97d000 r--p 00000000 00:00 0                          [vvar]
  17. 7efd5c97d000-7efd5c97f000 r-xp 00000000 00:00 0                          [vdso]
  18. 7efd5c97f000-7efd5c980000 r--p 00000000 08:03 2242166                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
  19. 7efd5c980000-7efd5c9ab000 r-xp 00001000 08:03 2242166                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
  20. 7efd5c9ab000-7efd5c9b5000 r--p 0002c000 08:03 2242166                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
  21. 7efd5c9b5000-7efd5c9b7000 r--p 00036000 08:03 2242166                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
  22. 7efd5c9b7000-7efd5c9b9000 rw-p 00038000 08:03 2242166                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
  23. 7ffe03c95000-7ffe03cb6000 rw-p 00000000 00:00 0                          [stack]
  24. ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]
复制代码
说了这么多,接下来我们演示下如何对 openat 进行拦截,首先定义一个 LD_PRELOAD 需要加载的共享库,代码如下:
  1. #define _GNU_SOURCE
  2. #include <dlfcn.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <stdarg.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. static int (*real_openat)(int, const char *, int, ...) = NULL;
  9. int openat(int dirfd, const char *pathname, int flags, ...)
  10. {
  11.     mode_t mode = 0;
  12.     pid_t pid = getpid();
  13.     pid_t tid = gettid();
  14.     printf("hooked openat: PID=%d, TID=%d, path=%s\n", pid, tid, pathname);
  15.     if (!real_openat)
  16.     {
  17.         real_openat = dlsym(RTLD_NEXT, "openat");
  18.     }
  19.     if (flags & O_CREAT)
  20.     {
  21.         return real_openat(dirfd, pathname, flags, mode);
  22.     }
  23.     else
  24.     {
  25.         return real_openat(dirfd, pathname, flags);
  26.     }
  27. }
复制代码
将上面的 hook_openat.c 做成动态链接库,其中的 -ldl 表示对外提供加载该库的api,比如(dlopen,dlsym), 参考如下:
  1. root@ubuntu2404:/data2# gcc -shared -fPIC -o libhookopenat.so hook_openat.c -ldl
  2. root@ubuntu2404:/data2# ls -lh
  3. total 24K
  4. -rw-r--r-- 1 root root 688 Jun 12 09:14 hook_openat.c
  5. -rwxr-xr-x 1 root root 16K Jun 12 09:20 libhookopenat.so
  6. -rw-r--r-- 1 root root 782 Jun 12 09:18 main.c
复制代码
共享库搞定之后,接下来就是写 C 代码来调用了,这里我们通过 openat 打开文件,然后让 libhookopenat.so 拦截,参考代码如下:
  1. #define _GNU_SOURCE
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <stdio.h>  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int main()
  8. {
  9.     // 在当前目录下创建一个新文件
  10.     int fd = openat(AT_FDCWD, "example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  11.     if (fd == -1)
  12.     {
  13.         perror("openat failed");
  14.         exit(EXIT_FAILURE);
  15.     }
  16.     // 写入一些内容到文件
  17.     const char *text = "This is a test file created with openat!\n";
  18.     ssize_t bytes_written = write(fd, text, strlen(text));
  19.     if (bytes_written == -1)
  20.     {
  21.         perror("write failed");
  22.         close(fd);
  23.         exit(EXIT_FAILURE);
  24.     }
  25.     // 关闭文件
  26.     close(fd);
  27.     printf("File created and written successfully! Wrote %zd bytes.\n", bytes_written);
  28.     return 0;
  29. }
复制代码
  1. root@ubuntu2404:/data2# gcc -o main ./main.c
  2. root@ubuntu2404:/data2# LD_PRELOAD=./libhookopenat.so ./main
  3. hooked openat: PID=4646, TID=4646, path=example.txt
  4. File created and written successfully! Wrote 41 bytes.
复制代码
从卦中可以清晰的看到 hook 成功!
2. funchook 如何实现拦截

LD_PRELOAD 这种共享库的粒度还是太大,如果粒度再小一点就更加灵活了,比如函数级,这就是本节要介绍到的 funchook,源码在github上:https://github.com/kubo/funchook ,唯一麻烦一点的就是你需要通过源码编译来生成对应的 头文件,静态链接文件,动态链接库 ,参考如下:
  1. root@ubuntu2404:/data4# sudo apt install -y git gcc cmake make
  2. root@ubuntu2404:/data4# git clone https://github.com/kubo/funchook.git
  3. root@ubuntu2404:/data4# cd funchook
  4. root@ubuntu2404:/data4# mkdir build && cd build
  5. root@ubuntu2404:/data4# cmake ..
  6. root@ubuntu2404:/data4# make
  7. root@ubuntu2404:/data4/funchook/build# sudo make install
  8. [ 25%] Built target distorm
  9. [ 42%] Built target funchook-shared
  10. [ 60%] Built target funchook-static
  11. [ 71%] Built target funchook_test
  12. [ 85%] Built target funchook_test_shared
  13. [100%] Built target funchook_test_static
  14. Install the project...
  15. -- Install configuration: ""
  16. -- Installing: /usr/local/include/funchook.h
  17. -- Installing: /usr/local/lib/libfunchook.so.2.0.0
  18. -- Installing: /usr/local/lib/libfunchook.so.2
  19. -- Installing: /usr/local/lib/libfunchook.so
  20. -- Installing: /usr/local/lib/libfunchook.a
  21. root@ubuntu2404:/data4/funchook/build# ldconfig
复制代码
由于默认安装在了 /usr/local/lib 下,一定要记得用 ldconfig 命令刷新下,否则程序可能找不到新库,最后就是 C 的调用代码,参考如下:
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <dlfcn.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <funchook.h>
  7. // 原始函数指针
  8. static int (*orig_openat)(int dirfd, const char *pathname, int flags, mode_t mode);
  9. // 钩子函数
  10. int hooked_openat(int dirfd, const char *pathname, int flags, mode_t mode)
  11. {
  12.     printf("Hooked openat called: path=%s, flags=0x%x\n", pathname, flags);
  13.     // 调用原始函数
  14.     return orig_openat(dirfd, pathname, flags, mode);
  15. }
  16. int main()
  17. {
  18.     // 获取原始 openat 函数地址
  19.     orig_openat = dlsym(RTLD_NEXT, "openat");
  20.     if (!orig_openat)
  21.     {
  22.         fprintf(stderr, "Failed to find openat: %s\n", dlerror());
  23.         return 1;
  24.     }
  25.     // 创建 funchook 实例
  26.     funchook_t *funchook = funchook_create();
  27.     if (!funchook)
  28.     {
  29.         perror("funchook_create failed");
  30.         return 1;
  31.     }
  32.     // 准备 Hook
  33.     int rv = funchook_prepare(funchook, (void **)&orig_openat, hooked_openat);
  34.     if (rv != 0)
  35.     {
  36.         fprintf(stderr, "Prepare failed: %s\n", funchook_error_message(funchook));
  37.         return 1;
  38.     }
  39.     // 安装 Hook
  40.     rv = funchook_install(funchook, 0);
  41.     if (rv != 0)
  42.     {
  43.         fprintf(stderr, "Install failed: %s\n", funchook_error_message(funchook));
  44.         return 1;
  45.     }
  46.     // 测试调用
  47.     printf("=== Testing openat hook ===\n");
  48.     int fd = openat(AT_FDCWD, "/etc/passwd", O_RDONLY);
  49.     if (fd >= 0)
  50.     {
  51.         printf("Successfully opened file, fd=%d\n", fd);
  52.         close(fd);
  53.     }
  54.     else
  55.     {
  56.         perror("openat failed");
  57.     }
  58.     // 清理
  59.     funchook_uninstall(funchook, 0);
  60.     funchook_destroy(funchook);
  61.     return 0;
  62. }
复制代码
接下来就是编译执行了。
  1. root@ubuntu2404:/data2# gcc -o main main.c -lfunchook -ldl
  2. root@ubuntu2404:/data2# ./main
  3. === Testing openat hook ===
  4. Hooked openat called: path=/etc/passwd, flags=0x0
  5. Successfully opened file, fd=3
复制代码
一切都是美好的,当然如果你想可视化的单步调试,可以配置到 vs 的 tasks.json 中,参考如下:
  1. {
  2.     "tasks": [
  3.         {
  4.             "type": "cppbuild",
  5.             "label": "C/C++: gcc build active file",
  6.             "command": "/usr/bin/gcc",
  7.             "args": [
  8.                 "-fdiagnostics-color=always",
  9.                 "-g",
  10.                 "${file}",
  11.                 "-o",
  12.                 "${fileDirname}/${fileBasenameNoExtension}",
  13.                 "-lfunchook",
  14.                 "-L/usr/local/lib"
  15.             ],
  16.             "options": {
  17.                 "cwd": "${fileDirname}"
  18.             },
  19.             "problemMatcher": [
  20.                 "$gcc"
  21.             ],
  22.             "group": {
  23.                 "kind": "build",
  24.                 "isDefault": true
  25.             },
  26.             "detail": "Task generated by Debugger."
  27.         }
  28.     ],
  29.     "version": "2.0.0"
  30. }
复制代码
1.png

三:总结

这里给大家总结的两种注入方式,LD_PRELOAD 虽然简单,但粒度粗,适合简单的无侵入场景,如果希望更细粒度,建议使用活跃的 funchook 吧,虽然是一个岛国大佬实现的。
2.jpeg


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册