淺析一下Linux上對(duì)函數(shù)進(jìn)行hook的兩種方式
一、背景
1. 講故事
前兩篇我們介紹了 Minhook 在 Windows 平臺(tái)上的強(qiáng)大功效,這一篇我們來(lái)聊一聊如何在 Linux 上對(duì)函數(shù)進(jìn)行hook,這里介紹兩種方式。
1.輕量級(jí)的 LD_PRELOAD 攔截
LD_PRELOAD是一種共享庫(kù)攔截,這種方式的優(yōu)點(diǎn)在于不需要對(duì)源程序做任何修改,達(dá)到無(wú)侵入的功效,這是windows平臺(tái)上不可想象的。
2.funchook 攔截
在 github 有很多可用于 linux 上的函數(shù) hook,我發(fā)現(xiàn)輕量級(jí)的,活躍的,開(kāi)源的 要屬 funchook 吧。
二、兩種攔截方式
1. LD_PRELOAD 如何實(shí)現(xiàn)攔截
要想明白 LD_PRELOAD 如何實(shí)現(xiàn)攔截?需要你對(duì) linux 上的進(jìn)程初始化時(shí)的鏈接器 ld.so 的工作過(guò)程有一個(gè)了解,簡(jiǎn)單來(lái)說(shuō)就是它的加載順序?yàn)?nbsp;主程序的可執(zhí)行文件 -> LD_PRELOAD 指定的庫(kù) -> glibc 標(biāo)準(zhǔn)庫(kù) -> 其他依賴庫(kù) 。
由于 LD_PRELOAD 指定的 so 文件優(yōu)于 glibc.so 解析,所以可以利用這種先入為主的方式覆蓋后續(xù)的同名符號(hào)方法,那 ld.so 長(zhǎng)啥樣呢?在我的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]
說(shuō)了這么多,接下來(lái)我們演示下如何對(duì) openat 進(jìn)行攔截,首先定義一個(gè) LD_PRELOAD 需要加載的共享庫(kù),代碼如下:
#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 做成動(dòng)態(tài)鏈接庫(kù),其中的 -ldl 表示對(duì)外提供加載該庫(kù)的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
共享庫(kù)搞定之后,接下來(lái)就是寫(xiě) C 代碼來(lái)調(diào)用了,這里我們通過(guò) openat 打開(kāi)文件,然后讓 libhookopenat.so 攔截,參考代碼如下:
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// 在當(dāng)前目錄下創(chuàng)建一個(gè)新文件
int fd = openat(AT_FDCWD, "example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
{
perror("openat failed");
exit(EXIT_FAILURE);
}
// 寫(xiě)入一些內(nèi)容到文件
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);
}
// 關(guān)閉文件
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 如何實(shí)現(xiàn)攔截
LD_PRELOAD 這種共享庫(kù)的粒度還是太大,如果粒度再小一點(diǎn)就更加靈活了,比如函數(shù)級(jí),這就是本節(jié)要介紹到的 funchook,源碼在github上:https://github.com/kubo/funchook ,唯一麻煩一點(diǎn)的就是你需要通過(guò)源碼編譯來(lái)生成對(duì)應(yīng)的 頭文件,靜態(tài)鏈接文件,動(dòng)態(tài)鏈接庫(kù) ,參考如下:
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
由于默認(rèn)安裝在了 /usr/local/lib 下,一定要記得用 ldconfig 命令刷新下,否則程序可能找不到新庫(kù),最后就是 C 的調(diào)用代碼,參考如下:
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <funchook.h>
// 原始函數(shù)指針
static int (*orig_openat)(int dirfd, const char *pathname, int flags, mode_t mode);
// 鉤子函數(shù)
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);
// 調(diào)用原始函數(shù)
return orig_openat(dirfd, pathname, flags, mode);
}
int main()
{
// 獲取原始 openat 函數(shù)地址
orig_openat = dlsym(RTLD_NEXT, "openat");
if (!orig_openat)
{
fprintf(stderr, "Failed to find openat: %s\n", dlerror());
return 1;
}
// 創(chuàng)建 funchook 實(shí)例
funchook_t *funchook = funchook_create();
if (!funchook)
{
perror("funchook_create failed");
return 1;
}
// 準(zhǔn)備 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;
}
// 測(cè)試調(diào)用
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;
}接下來(lái)就是編譯執(zhí)行了。
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
一切都是美好的,當(dāng)然如果你想可視化的單步調(diào)試,可以配置到 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"
}
三、總結(jié)
這里給大家總結(jié)的兩種注入方式,LD_PRELOAD 雖然簡(jiǎn)單,但粒度粗,適合簡(jiǎn)單的無(wú)侵入場(chǎng)景,如果希望更細(xì)粒度,建議使用活躍的 funchook 吧。
到此這篇關(guān)于淺析一下Linux上對(duì)函數(shù)進(jìn)行hook的兩種方式的文章就介紹到這了,更多相關(guān)Linux函數(shù)hook內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux設(shè)置文件夾權(quán)限的幾種常用操作方法
在Linux系統(tǒng)里,若要把文件夾權(quán)限修改成所有用戶都能對(duì)其進(jìn)行修改,可借助chmod命令達(dá)成這一目的,不過(guò),這樣的設(shè)置會(huì)帶來(lái)較大的安全風(fēng)險(xiǎn),所以建議僅在測(cè)試環(huán)境或者對(duì)安全性要求不高的場(chǎng)景中使用,下面為你介紹幾種常用的操作方法,需要的朋友可以參考下2025-05-05
Ubuntu無(wú)網(wǎng)絡(luò)標(biāo)識(shí)的問(wèn)題及解決
這篇文章主要介紹了Ubuntu無(wú)網(wǎng)絡(luò)標(biāo)識(shí)的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
CentOS7 重新分配分區(qū)大小的實(shí)現(xiàn)方法
這篇文章主要介紹了CentOS7 重新分配分區(qū)大小的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
從信號(hào)機(jī)制到進(jìn)程管理深度解析Linux中的kill命令
kill?命令的本質(zhì)是?Linux?信號(hào)機(jī)制,而非簡(jiǎn)單的進(jìn)程終止,本文深入解析?SIGTERM、SIGKILL、SIGHUP?等核心信號(hào)的區(qū)別與使用場(chǎng)景,涵蓋優(yōu)雅終止?vs?強(qiáng)制終止的最佳實(shí)踐2026-06-06
Linux 文件權(quán)限與chmod的實(shí)現(xiàn)示例
今天小編就為大家分享一篇Linux 文件權(quán)限與chmod的實(shí)現(xiàn)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2019-07-07
CentOS7 配置Nginx支持HTTPS訪問(wèn)的實(shí)現(xiàn)方案
這篇文章主要介紹了CentOS7 配置Nginx支持HTTPS訪問(wèn)的實(shí)現(xiàn)方案的相關(guān)資料,這里實(shí)現(xiàn)該功能的步驟進(jìn)行了詳解,需要的朋友可以參考下2016-11-11
無(wú)法登錄Linux系統(tǒng)的問(wèn)題及解決
本文介紹了幾種解決無(wú)法登錄Linux系統(tǒng)的問(wèn)題的方法,包括密碼恢復(fù)、修復(fù)文件系統(tǒng)、恢復(fù)配置文件以及重裝系統(tǒng),每種方法都提供了詳細(xì)的步驟和操作指南,幫助用戶在遇到類似問(wèn)題時(shí)能夠迅速找到解決方案2026-02-02
linux服務(wù)器安裝SonarQube代碼檢測(cè)工具的詳細(xì)步驟
這篇文章主要介紹了linux服務(wù)器安裝SonarQube代碼檢測(cè)工具,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Linux服務(wù)器間文件實(shí)時(shí)同步的實(shí)現(xiàn)
這篇文章主要介紹了Linux服務(wù)器間文件實(shí)時(shí)同步的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

