news 2026/7/3 2:36:05

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
聊一聊 Linux 上对函数进行 hook 的两种方式

两种拦截方式

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

root@ubuntu2404:/data2# cat /proc/5322/maps 60c0f8687000-60c0f8688000 r--p 00000000 08:03 1966089 /data2/main 60c0f8688000-60c0f8689000 r-xp 00001000 08:03 1966089 /data2/main 60c0f8689000-60c0f868a000 r--p 00002000 08:03 1966089 /data2/main 60c0f868a000-60c0f868b000 r--p 00002000 08:03 1966089 /data2/main 60c0f868b000-60c0f868c000 rw-p 00003000 08:03 1966089 /data2/main 60c1266de000-60c1266ff000 rw-p 00000000 00:00 0 [heap] 7efd5c600000-7efd5c628000 r--p 00000000 08:03 2242169 /usr/lib/x86_64-linux-gnu/libc.so.6 7efd5c628000-7efd5c7b0000 r-xp 00028000 08:03 2242169 /usr/lib/x86_64-linux-gnu/libc.so.6 7efd5c7b0000-7efd5c7ff000 r--p 001b0000 08:03 2242169 /usr/lib/x86_64-linux-gnu/libc.so.6 7efd5c7ff000-7efd5c803000 r--p 001fe000 08:03 2242169 /usr/lib/x86_64-linux-gnu/libc.so.6 7efd5c803000-7efd5c805000 rw-p 00202000 08:03 2242169 /usr/lib/x86_64-linux-gnu/libc.so.6 7efd5c805000-7efd5c812000 rw-p 00000000 00:00 0 7efd5c964000-7efd5c967000 rw-p 00000000 00:00 0 7efd5c977000-7efd5c979000 rw-p 00000000 00:00 0 7efd5c979000-7efd5c97d000 r--p 00000000 00:00 0 [vvar] 7efd5c97d000-7efd5c97f000 r-xp 00000000 00:00 0 [vdso] 7efd5c97f000-7efd5c980000 r--p 00000000 08:03 2242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 7efd5c980000-7efd5c9ab000 r-xp 00001000 08:03 2242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 7efd5c9ab000-7efd5c9b5000 r--p 0002c000 08:03 2242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 7efd5c9b5000-7efd5c9b7000 r--p 00036000 08:03 2242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 7efd5c9b7000-7efd5c9b9000 rw-p 00038000 08:03 2242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 7ffe03c95000-7ffe03cb6000 rw-p 00000000 00:00 0 [stack] ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

说了这么多,接下来我们演示下如何对 openat 进行拦截,首先定义一个 LD_PRELOAD 需要加载的共享库,代码如下:

#define _GNU_SOURCE #include <dlfcn.h> #include <stdio.h> #include <fcntl.h> #include <stdarg.h> #include <unistd.h> #include <sys/types.h> static int (*real_openat)(int, const char *, int, ...) = NULL; int openat(int dirfd, const char *pathname, int flags, ...) { mode_t mode = 0; pid_t pid = getpid(); pid_t tid = gettid(); printf("hooked openat: PID=%d, TID=%d, path=%s\n", pid, tid, pathname); if (!real_openat) { real_openat = dlsym(RTLD_NEXT, "openat"); } if (flags & O_CREAT) { return real_openat(dirfd, pathname, flags, mode); } else { return real_openat(dirfd, pathname, flags); } }

将上面的 hook_openat.c 做成动态链接库,其中的-ldl表示对外提供加载该库的api,比如(dlopen,dlsym), 参考如下:

root@ubuntu2404:/data2# gcc -shared -fPIC -o libhookopenat.so hook_openat.c -ldl root@ubuntu2404:/data2# ls -lh total 24K -rw-r--r-- 1 root root 688 Jun 12 09:14 hook_openat.c -rwxr-xr-x 1 root root 16K Jun 12 09:20 libhookopenat.so -rw-r--r-- 1 root root 782 Jun 12 09:18 main.c

共享库搞定之后,接下来就是写 C 代码来调用了,这里我们通过 openat 打开文件,然后让 libhookopenat.so 拦截,参考代码如下:

#define _GNU_SOURCE #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { // 在当前目录下创建一个新文件 int fd = openat(AT_FDCWD, "example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd == -1) { perror("openat failed"); exit(EXIT_FAILURE); } // 写入一些内容到文件 const char *text = "This is a test file created with openat!\n"; ssize_t bytes_written = write(fd, text, strlen(text)); if (bytes_written == -1) { perror("write failed"); close(fd); exit(EXIT_FAILURE); } // 关闭文件 close(fd); printf("File created and written successfully! Wrote %zd bytes.\n", bytes_written); return 0; }
root@ubuntu2404:/data2# gcc -o main ./main.c root@ubuntu2404:/data2# LD_PRELOAD=./libhookopenat.so ./main hooked openat: PID=4646, TID=4646, path=example.txt File created and written successfully! Wrote 41 bytes.

从卦中可以清晰的看到 hook 成功!

2. funchook 如何实现拦截

LD_PRELOAD 这种共享库的粒度还是太大,如果粒度再小一点就更加灵活了,比如函数级,这就是本节要介绍到的 funchook,源码在github上:https://github.com/kubo/funchook ,唯一麻烦一点的就是你需要通过源码编译来生成对应的头文件,静态链接文件,动态链接库,参考如下:

root@ubuntu2404:/data4# sudo apt install -y git gcc cmake make root@ubuntu2404:/data4# git clone https://github.com/kubo/funchook.git root@ubuntu2404:/data4# cd funchook root@ubuntu2404:/data4# mkdir build && cd build root@ubuntu2404:/data4# cmake .. root@ubuntu2404:/data4# make root@ubuntu2404:/data4/funchook/build# sudo make install [ 25%] Built target distorm [ 42%] Built target funchook-shared [ 60%] Built target funchook-static [ 71%] Built target funchook_test [ 85%] Built target funchook_test_shared [100%] Built target funchook_test_static Install the project... -- Install configuration: "" -- Installing: /usr/local/include/funchook.h -- Installing: /usr/local/lib/libfunchook.so.2.0.0 -- Installing: /usr/local/lib/libfunchook.so.2 -- Installing: /usr/local/lib/libfunchook.so -- Installing: /usr/local/lib/libfunchook.a root@ubuntu2404:/data4/funchook/build# ldconfig

由于默认安装在了/usr/local/lib下,一定要记得用ldconfig命令刷新下,否则程序可能找不到新库,最后就是 C 的调用代码,参考如下:

#define _GNU_SOURCE #include <stdio.h> #include <dlfcn.h> #include <fcntl.h> #include <unistd.h> #include <funchook.h> // 原始函数指针 static int (*orig_openat)(int dirfd, const char *pathname, int flags, mode_t mode); // 钩子函数 int hooked_openat(int dirfd, const char *pathname, int flags, mode_t mode) { printf("Hooked openat called: path=%s, flags=0x%x\n", pathname, flags); // 调用原始函数 return orig_openat(dirfd, pathname, flags, mode); } int main() { // 获取原始 openat 函数地址 orig_openat = dlsym(RTLD_NEXT, "openat"); if (!orig_openat) { fprintf(stderr, "Failed to find openat: %s\n", dlerror()); return 1; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/3 2:33:37

SpringBoot日志系统

前言&#xff1a;日志是后端项目的“黑匣子”&#xff0c;是排查Bug、追踪业务、统计线上问题、复盘故障的核心依据。SpringBoot 对 Java 混乱的日志体系做了统一封装&#xff0c;开箱即用、无需复杂配置。一、日志的核心概念简单来说&#xff0c;日志就是程序运行的“运行记录…

作者头像 李华
网站建设 2026/7/3 2:31:50

Web安全标头实战指南:CSP、HSTS等核心配置详解与部署策略

1. 项目概述&#xff1a;为什么安全标头是Web安全的“第一道门锁”&#xff1f; 干了这么多年Web开发和运维&#xff0c;我见过太多因为基础安全配置缺失而导致的“低级”安全事故。很多团队把精力都花在了复杂的业务逻辑加密、防火墙策略上&#xff0c;却常常忽略了HTTP响应头…

作者头像 李华
网站建设 2026/7/3 2:31:15

2026在线去除背景工具汇总:免费免登录高清抠图实操指南

数字化图文处理需求持续增加&#xff0c;日常制作证件照、电商商品素材、自媒体配图时&#xff0c;常常需要把图片原有背景清除&#xff0c;获得透明底素材。依托浏览器运行的在线去背景工具无需下载安装软件&#xff0c;适配电脑、平板等多类设备&#xff0c;同时还有微信小程…

作者头像 李华
网站建设 2026/7/3 2:30:22

Spring AI 2.0.0 RAG 实战:别让知识库只活在内存里

本地验证 RAG 链路时&#xff0c;SimpleVectorStore 很顺手。 几段文本切一下&#xff0c;向量化&#xff0c;放进内存&#xff0c;接口一调就能看到相似内容。 这一步能先确认三件事&#xff1a; EmbeddingModel 能不能生成向量 VectorStore 能不能检索相似内容 ChatClie…

作者头像 李华
网站建设 2026/7/3 2:25:58

每天忙到停不下来,却不知道时间去哪了?用Traggo记录真实投入

前言 有时候&#xff0c;一天从早忙到晚&#xff0c;待办事项也完成了不少&#xff0c;可真正回顾时&#xff0c;却很难回答一个简单的问题&#xff1a;今天的时间到底花在了哪里&#xff1f; 开会、回复消息、临时处理文件、查资料、写方案&#xff0c;这些事情不断穿插在一…

作者头像 李华
网站建设 2026/7/3 2:25:12

AI写教材大揭秘:如何利用AI工具实现低查重,快速生成高质量教材?

编写教材离不开丰富的资料支持&#xff0c;但传统的资料整合方式已无法满足现代需求。以往&#xff0c;我们需要从多个渠道获取信息&#xff0c;比如课标文件、学术文献和教学案例&#xff0c;耗时数天来筛选有用的信息。即便资料搜集齐全&#xff0c;零散的信息也难以有效整合…

作者头像 李华