百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

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

gudong366 2025-07-24 20:44 6 浏览

一:背景

1. 讲故事

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

  1. 轻量级的 LD_PRELOAD 拦截

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

2. 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


root@ubuntu2404:/data2# cat /proc/5322/maps
60c0f8687000-60c0f8688000 r--p 0000000008:031966089 /data2/main
60c0f8688000-60c0f8689000 r-xp 0000100008:031966089 /data2/main
60c0f8689000-60c0f868a000 r--p 0000200008:031966089 /data2/main
60c0f868a000-60c0f868b000 r--p 0000200008:031966089 /data2/main
60c0f868b000-60c0f868c000 rw-p 0000300008:031966089 /data2/main
60c1266de000-60c1266ff000 rw-p 0000000000:000 [heap]
7efd5c600000-7efd5c628000 r--p 0000000008:032242169 /usr/lib/x86_64-linux-gnu/libc.so.6
7efd5c628000-7efd5c7b0000 r-xp 0002800008:032242169 /usr/lib/x86_64-linux-gnu/libc.so.6
7efd5c7b0000-7efd5c7ff000 r--p 001b0000 08:032242169 /usr/lib/x86_64-linux-gnu/libc.so.6
7efd5c7ff000-7efd5c803000 r--p 001fe000 08:032242169 /usr/lib/x86_64-linux-gnu/libc.so.6
7efd5c803000-7efd5c805000 rw-p 0020200008:032242169 /usr/lib/x86_64-linux-gnu/libc.so.6
7efd5c805000-7efd5c812000 rw-p 0000000000:000
7efd5c964000-7efd5c967000 rw-p 0000000000:000
7efd5c977000-7efd5c979000 rw-p 0000000000:000
7efd5c979000-7efd5c97d000 r--p 0000000000:000 [vvar]
7efd5c97d000-7efd5c97f000 r-xp 0000000000:000 [vdso]
7efd5c97f000-7efd5c980000 r--p 0000000008:032242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7efd5c980000-7efd5c9ab000 r-xp 0000100008:032242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7efd5c9ab000-7efd5c9b5000 r--p 0002c000 08:032242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7efd5c9b5000-7efd5c9b7000 r--p 0003600008:032242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7efd5c9b7000-7efd5c9b9000 rw-p 0003800008:032242166 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7ffe03c95000-7ffe03cb6000 rw-p 0000000000:000 [stack]
ffffffffff600000-ffffffffff601000 --xp 0000000000:000 [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, ...) = ;

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);
}

// 写入一些内容到文件
constchar *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);

return0;
}


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());
return1;
}

// 创建 funchook 实例
funchook_t *funchook = funchook_create();
if (!funchook)
{
perror("funchook_create failed");
return1;
}

// 准备 Hook
int rv = funchook_prepare(funchook, (void **)&orig_openat, hooked_openat);
if (rv != 0)
{
fprintf(stderr, "Prepare failed: %s\n", funchook_error_message(funchook));
return1;
}

// 安装 Hook
rv = funchook_install(funchook, 0);
if (rv != 0)
{
fprintf(stderr, "Install failed: %s\n", funchook_error_message(funchook));
return1;
}

// 测试调用
printf("=== Testing openat hook ===\n");
int fd = openat(AT_FDCWD, "/etc/passwd", O_RDONLY);
if (fd >= 0)
{
printf("Successfully opened file, fd=%d\n", fd);
close(fd);
}
else
{
perror("openat failed");
}

// 清理
funchook_uninstall(funchook, 0);
funchook_destroy(funchook);
return0;
}

接下来就是编译执行了。


root@ubuntu2404:/data2# gcc -o main main.c -lfunchook -ldl
root@ubuntu2404:/data2# ./main
=== Testing openat hook ===
Hooked openat called: path=/etc/passwd, flags=0x0
Successfully opened file, fd=3

一切都是美好的,当然如果你想可视化的单步调试,可以配置到 vs 的 tasks.json 中,参考如下:


{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lfunchook",
"-L/usr/local/lib"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

三:总结

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


相关推荐

理解Linux进程和线程(linux的进程和线程的区别)

#进程-进程是一个执行中的程序,它拥有自己独立的内存空间,不同进程的地址空间是相互隔离的。-进程有自身的代码段,数据段,堆,栈等。进程需要耗费资源创建和销毁。-进程之间的通信需要借助IPC(I...

Linux进程上下文切换过程context_switch详解

1前言1.1Linux的调度器组成2个调度器可以用两种方法来激活调度一种是直接的,比如进程打算睡眠或出于其他原因放弃CPU另一种是通过周期性的机制,以固定的频率运行,不时的检测是否有必要因此...

linux init进程(linux init 1)

一.init是Linux系统操作中不可缺少的程序之一。所谓的init进程,它是一个由内核启动的用户级进程。内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通...

【Linux系统编程】特殊进程之守护进程

01.守护进程概述守护进程(DaemonProcess),也就是通常说的Daemon进程(精灵进程),是Linux中的后台服务进程。它是一个生存期较长的进程,通常独立于控制终端并且周期性地...

在 Linux 中如何强制停止进程?kill 和 killall 命令有什么区别?

在日常工作中,您会遇到两个用于在Linux中强制结束程序的命令;kill和killall。虽然许多Linux用户都知道kill命令,但知道并使用killall命令的人并不多。这两个命令...

嵌入式Linux系统编程——连进程间通信都不懂,还自称linux大神?

所有学嵌入式Linux系统的看过来了,以下内容是每一位想学习Linux嵌入式系统想要了解的内容,真的很想要分享给大家!本文分享的内容主要如下几个方面:(绝对的精品资料,不收藏可惜了)6.1共享内存...

Linux基础运维篇:Linux进程与服务管理(第010课)

在Linux系统里,进程和服务管理就像是一个大管家的工作,得把各种程序的运行安排得明明白白,这样系统才能稳稳当当地干活。进程就是程序跑起来的一个实例,服务呢,是那种一直在后台默默工作的进程,咱下面...

深度剖析Linux内核《如何唤醒线程》

linux内核如何唤醒线程//本文代码片段出自linux内核版本:4.1.15linux内核唤醒线程主要使用wake_up_process()。一、wake_up_process()分析在linux内...

字节因它而跳动!顶级资深大牛整理的“深入理解Linux内核”

如果你对Linux如何工作。其性能又为什么会如此之高怀有强烈的好奇心。你将会从这里找到答案.阅读本文之后,你会通过上千行代码找到自己的方式来区别重要数据结构和次要数据结构的不同,简而言之,你蒋成为一名...

都说Linux内核很吊,它到底是个啥玩意儿?

了解完基本信息之后,我们来看一看,为什么说它吊?吊在哪里?甚至我觉得不仅仅是c/c++Linux开发的可以学习,Java、Python等方面的都可以学习提升一下。linux内核有什么用?linux内核...

77% 的 Linux 运维都不懂的内核问题,这篇全告诉你了

前言之前在实习时,听了OOM的分享之后,就对Linux内核内存管理充满兴趣,但是这块知识非常庞大,没有一定积累,不敢写下,担心误人子弟,所以经过一个一段时间的积累,对内核内存有一定了解之后,今...

Linux 内核开发流程的一个典型例子

>authorLinusTorvalds<torvalds@linux-foundation.org>2025-07-0813:31:29-0700>committ...

Vold原理介绍(volte基本原理)

一、Vold简介Android中Vold是volumeDaemon,即Volume守护进程,用来管理Android中存储类的热拔插事件。这里的热插拔涉及的场景如:手机usb以MTP或者传输照片方式...

2-剖析Linux内核源码分析《中断处理》

一、中断向量及汇编指令1、中断向量Intelx86系列机器共支持256种向量中断,Intel用一个8位无符号整数叫做一个向量,因此也叫中断向量。所有256种中断可分为两大类:异常和中断,异常又称为故...

剖析linux内核(一文看懂linux内核)

PASmm_struct详解malloc()函数是用户态常用的分配内存接口,mmap()函数是用户态常用创建文件映射或匿名映射。进程地址空间在linux内核当中使用structvm_area...