一:背景

1. 讲故事

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

  1. 轻量级的 LD_PRELOAD 拦截

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

  1. 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 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;
} // 创建 funchook 实例
funchook_t *funchook = funchook_create();
if (!funchook)
{
perror("funchook_create failed");
return 1;
} // 准备 Hook
int rv = funchook_prepare(funchook, (void **)&orig_openat, hooked_openat);
if (rv != 0)
{
fprintf(stderr, "Prepare failed: %s\n", funchook_error_message(funchook));
return 1;
} // 安装 Hook
rv = funchook_install(funchook, 0);
if (rv != 0)
{
fprintf(stderr, "Install failed: %s\n", funchook_error_message(funchook));
return 1;
} // 测试调用
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);
return 0;
}

接下来就是编译执行了。


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 上对函数进行 hook 的两种方式的更多相关文章

  1. Android手机上监听短信的两种方式

    Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...

  2. Hyper-V 下linux虚拟机静态IP上网配置的两种方式(2)

    工作需要,搭建linux环境,网上搜了两种Hyper-V配置linux静态IP及上网的方式,记录一下,方便查阅,如下设置网络共享方式: win10下使用hyper-v在本机安装linux虚拟机后,网络 ...

  3. Hyper-V 下linux虚拟机静态IP上网配置的两种方式(1)

    工作需要,搭建linux环境,网上搜了两种Hyper-V配置linux静态IP及上网的方式,记录一下,方便查阅,如下是桥接方式的配置: 本实例所用的各项资源说明,系统是windows10企业版64bi ...

  4. AspNetCore 文件上传(模型绑定、Ajax) 两种方式 get到了吗?

    就目前来说,ASP.NET Core2.1了,已经相当成熟了,希望下个项目争取使用吧!! 上传文件的三种方式("我会的,说不定还有其他方式") 模型绑定 Ajax WebUploa ...

  5. 在github上删除项目或某个文件(两种方式)

    一.使用命令删除 首先先上传一个文件到远程仓库,测试一下 输入命令git rm -r --cached 文件名 删除本地跟暂存区的文件,如下图 再输入命令 git commit -m "删除 ...

  6. AntDesign VUE:上传组件自定义限制的两种方式(Boolean、Promise)

    AntD上传组件 AntDesign VUE文档 第一种方式 beforeUpload(file) { let isLt = true if (filesSize) { isLt = file.siz ...

  7. .net 调用SAP RFC函数获取数据的两种方式

    方式1:使用客户端自带的组件 安装客户端以后,添加引用:SAPFunctionsOCX(.net 的Com列表里一般找不到,需要引用DLL[一般位于以下路径:Program Files\SAP\Fro ...

  8. Linux学习笔记21——线程同步的两种方式

    一  用信号量同步 1 信号量函数的名字都以sem_开头,线程中使用的基本信号量函数有4个 2 创建信号量 #include<semaphore.h> int sem_init(sem_t ...

  9. C++函数传递数组的两种方式

    数组与指针. 传首地址过去,然后通过地址输出数组元素. 1.一维数组 #include<iostream> using namespace std; #include <cstrin ...

  10. Lua 调用的 C 函数保存 state 的两种方式: Storing State in C Functions 笔记

    http://yanbin.is-programmer.com/posts/94214.html Registery的Key 1. 整数Key用于Lua的引用机制,所以不要使用整数作为Key 2. 通 ...

随机推荐

  1. 手把手教你安装TrueNas(基础篇)

            玩过蜗牛星际,体验过黑群晖系统崩掉导致里面珍藏12t大姐姐全没了(此处有哭声),我技术又菜,自己恢复是不可能恢复的,装的盗版系统,又不可能联系群晖官方售后恢复.于是乎就想要一个稳定.开 ...

  2. 如何不购买域名在云服务器上搭建HTTPS服务

    step 1: 事前准备 step 1.1: 云服务器 购买一台云服务器(带有弹性公网IP),阿里云,腾讯云,华为云什么的都可以. 选择ubuntu系统 开放安全组策略(把你需要的协议/端口暴露出来) ...

  3. 【虚拟机】VirtualBox设置共享文件夹

    VirtualBox设置共享文件夹 1.选中你要设置的虚拟机,点设置 2.共享文件夹,点右边的加号,设置一个共享文件夹路径,选择其他, 3.选一个你知道的位置,比如我的在E盘的共享文件夹下面 4.选好 ...

  4. wpf关于设备无关性的理解

    wpf的像素单位是1/96*系统dpi.当前系统dpi是96,那么wpf的一个单位长就是1px像素.这个系统dpi的意思就是物理单位一英寸里有多少个像素点,比如windows标准的96dpi,意味着一 ...

  5. 洛谷 - B4276 [蓝桥杯青少年组国赛 2023] 八进制回文平方数 - 题解

    题目传送门 主要思路 首先,这道题范围在 \(10^9\),我们不可能直接从 \(1\) 循环到 \(N\).我们不难看出,这道题是求平方数的八进制是否回文,那些不是平方数的例如 \(2\) 呀,\( ...

  6. Redis 集群实现分布式缓存的示例操作流程【Redis 系列之五】

    〇.前言 Redis 集群的核心优势在于高可用性.可扩展性和高性能,特别适合需要处理大规模数据和高并发请求的应用场景. 本文先介绍了什么是 Redis 集群,然后通过示例,以手动和自动两种方式搭建集群 ...

  7. python爬虫爬取B站视频字幕,词频统计,使用pyecharts画词云(wordcloud)

    我们使用beatifulsop爬取到B站视频的字幕:https://www.cnblogs.com/becks/p/14540355.html 然后将爬取的字幕,使用pandas处理后写到CSV文件中 ...

  8. Windows下将QT打包为可执行文件(exe)的完整流程,包含第三方库。

    打包我的 Qt/C++ 视觉应用:从依赖部署到单文件 EXE 的踩坑之旅 一.前言 最近完成了一个基于 Qt/C++ 的桌面视觉应用项目(proj_ai_vision_app).这个项目功能还挺复杂, ...

  9. php-ffmpeg保存为.mp4格式时报错

     原文: php-ffmpeg保存为.mp4格式时报错:FFMpeg\Exception\RuntimeException: Encoding failed in - 搜栈网 (seekstack.c ...

  10. K8s新手系列之K8s架构

    应用部署方式演变 在部署应用程序的方式上,主要经历了三个时代: 传统部署 互联网早期,会直接将应用程序部署在物理机上 优点:简单,不需要其它技术的参与缺点:不能为应用程序定义资源使用边界,很难合理地分 ...