Jack's Studio.

linux系统调用hook

字数统计: 3.2k阅读时长: 14 min
2023/06/21
loading

在求职中碰到的,经常见到的一个概念,就了解一下。针对hook技术,很多都是一种劫持的解读,linux系统调用hook,就是在linux系统中的系统调用劫持了,主要是安防的。

一、基础知识

在cpp中,一个可执行文件的生成,会有很多中间文件的生成,其中就有.o目标文件,目标文件内含符号表,用于在链接环节进行特定链接,而hook一个系统调用,就是修改内核符号表从而达到劫持作用。如下:

1
2
3
4
5
6
7
// test.cpp
#include <iostream>

int main(){
std::cout << "Hello, test." << std::endl;
return 0;
}

然后编译出目标文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
PS D:\Desktop\test> g++ -c test.cpp -o test.o
PS D:\Desktop\test> nm test.o
0000000000000000 b .bss
0000000000000000 d .ctors
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata
0000000000000000 r .rdata$.refptr._ZSt4cout
0000000000000000 r .rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
0000000000000000 r .rdata$zzz
0000000000000000 R .refptr._ZSt4cout
0000000000000000 R .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
0000000000000000 t .text
0000000000000000 r .xdata
U __main
000000000000003a t __tcf_0
0000000000000091 t _GLOBAL__sub_I_main
0000000000000055 t _Z41__static_initialization_and_destruction_0ii
U _ZNSolsEPFRSoS_E
U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev
U _ZSt4cout
U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
0000000000000000 r _ZStL19piecewise_construct
0000000000000000 b _ZStL8__ioinit
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
U atexit
0000000000000000 T main
PS D:\Desktop\test>

可执行文件也有很多符号,但用nm来检查就太多了,有很多标准库链接进来了的多余符号。

二、机制介绍

嗯,要知道的是,Intel的CPU的执行级别分四种:Ring0、Ring1、Ring2、Ring3,内核就处于Ring0级别;在windows中,普通程序如果有执行Ring0指令,windows会显示非法指令的错误信息。我们的日常程序,就运行在Ring3级别,在需要对文件进行读写的情况下,往往需要通过系统调用来进行,这也往往被称为陷入内核,期间,CPU执行级别从Ring3切换到Ring0,然后代替程序完成读写操作。

2.1 利用0x80劫持system_call

针对进程地址空间,可执行文件的各种地址都是制定了的,除非运行时确定的特性,而系统调用,就会引入0x80处的软中断,从而引入系统调用处理程序,跳转到其入口system_call,system_call会根据系统调用号来确定调用服务,然后根据内核符号表来进行特定跳转。所以,修改这里内核符号表,就可以跳转到自己编写的函数中,完成hook。

这里的做法,普遍是获取sys_call_table的基地址,也就是获取终端描述符的地址;在里面查找0x80中断的服务,获取其内存空间,从而获取sys_call_table的基地址,即可修改指定偏移对应的系统调用。不过这一步笔者不太熟悉。

2.2 利用LD_PRELOAD动态链接进行hook

linux中,LD_PRELOAD可以影响程序的运行时链接,允许用户在运行前优先加载指定库,可以通过这个指定我们预定义的库,指定库中符号为程序动态链接所需库的同名符号,这样就能实现覆盖,使得程序只访问我们指定符号。如下为一段正常代码:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <string.h>

int main(){
char msg[30];
strcpy(msg, "test\0");
int res = strcmp(msg, "aaa");
printf("%d\n", res);
return 0;
}

然后编译执行:

1
2
3
4
5
6
7
jam@jam-S1-Pro-Series:~/Desktop/test$ gcc test.c -o test
jam@jam-S1-Pro-Series:~/Desktop/test$ ./test
test.
jam@jam-S1-Pro-Series:~/Desktop/test$ ldd test
linux-vdso.so.1 (0x00007ffeb89f2000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5c80200000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5c8047f000)

简单查看了一下,test的正常运行和依赖如上,现在要做的就是针对libc.so.6的

1
2
3
4
5
6
#include <stdio.h>

int strcmp(const char *s1, const char *s2){
printf("传入参数:<%s> <%s>\n", s1, s2);
return 0;
}

如上,因为是展示,所以专门做了个strcmp的同名函数,因为没有实际的需求,所以就给一个printf就算了:

1
2
3
4
jam@jam-S1-Pro-Series:~/Desktop/test$ gcc -fPIC -shared hook.c -o hook.so
jam@jam-S1-Pro-Series:~/Desktop/test$ LD_PRELOAD=./hook.so ./test
传入参数:<test> <aaa>
0

这样就一个利用LD_PRELOAD来达成的系统调用hook了,如果是针对这个变量进行export短暂生效的,可以使用unset来复原,然后继续实验。本来准备hook一下strcpy的,但参数都对上了,怎么都没有链接上自己的动态库。实验了一下只带strcpy的测试代码,发现动态依赖没变,所以也是没法子了,先放一放。或许是符号。嗯,除了编译器来生成符号链接以外,我们也可以显式声明来进行特定库的调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <stdlib.h>

typedef int(*STRCMP)(const char*, const char*);

int strcmp(const char *s1, const char *s2){

static void *handle = NULL;
static STRCMP old_strcmp = NULL;

handle = dlopen ("libc.so.6", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s ", dlerror());
exit(1);
}
old = (STRCMP)dlsym(handle, "strcmp");

printf("传入参数:<%s> <%s>\n", s1, s2);
dlclose(handle);
return old(s1, s2);
}

如上的示例代码,使用dlopen来获得lib.so.6的操作权,然后声明与hook函数同类型的函数指针,再用dlsym来对这个函数指针进行初始化,然后调用。这样一来,程序正常进行,我们也进行了自己的操作,如果没有显式的一些输出或者记录,这就相当于隐身偷了别人的家了吧。

三、一些hook例子

下面是针对一些系统调用的hook,然后进行尝试的例子。

3.1 针对malloc的hook

malloc是C中资源分配的重点,也是各种面试的重点。所以这里也针对malloc进行一下hook,来查看一下它的一些信息“

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

static void* (*hook_malloc)(size_t) = NULL;

static void init(){
hook_malloc = dlsym(RTLD_NEXT, "malloc");
if(!hook_malloc){
fputs("无法获取malloc符号\n", stderr);
exit(-1);
}
fputs("成功初始化\n", stdout);
}

void *malloc(size_t size){
if(!hook_malloc)
init();
void *res = hook_malloc(size);
fprintf(stderr, "size: %ld, ptr: %p\n", size, res);
return res;
}

如上,gcc -fPIC -shared hook.c -o hook.so编译得出一个共享库,然后这个库就可以供给c或者cpp进行调用了,如果cpp中有用到对应malloc的话。测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
char *msg = (char*)malloc(10);
strcpy(msg, "test\0");
printf("%s, %ld\n", msg, strlen(msg));
free(msg);
return 0;
}

然后进行测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
jam@jam-ubuntu:~/Desktop/test$ cc test.c -o test
jam@jam-ubuntu:~/Desktop/test$ ./test
test, 4
jam@jam-ubuntu:~/Desktop/test$ ldd test
linux-vdso.so.1 (0x00007fff2cd66000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6422a00000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6422d17000)
jam@jam-ubuntu:~/Desktop/test$ gcc -fPIC -shared hook.c -o hook.so
jam@jam-ubuntu:~/Desktop/test$ LD_PRELOAD=./hook.so ./test
size: 1024, ptr: 0x562eb2a842a0
成功初始化
size: 10, ptr: 0x562eb2a846b0
test, 4
jam@jam-ubuntu:~/Desktop/test$

如上,就一个简单分配char内存的例子,然后ldd设置了LD_PRELOAD的test可以发现:

1
2
3
4
   linux-vdso.so.1 (0x00007ffc36357000)
./hook.so (0x00007fb0cfcda000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb0cfa00000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb0cfce6000)

实际上,输出并不像上面那样,那么简洁,甚至有着很多其他的各种输出,因为好像有部分操作会持续用到malloc,不止我们的测试代码,所以这段时间应用到malloc系统调用的,都在这输出了。另外,还有一个问题,就是上面的自己重写的malloc中,最后的fprintf的输出方向,如果不选择stderr,就会出现coredump,因为我也是不是很熟悉内存泄露这块,就没有找出问题所在,如果有明白的可以联系一下笔者。

因为上面重写的malloc是系统调用,而且编译出了动态库,也就意味着,咱们cpp也能用,下面来测试一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class A{
public:
A(){a = new int;};
~A(){
if(a){
delete a;
a = nullptr;
}
}
private:
int *a;
};

int main(){
A a;
return 0;
}

好的,跑起来:

1
2
3
4
5
6
jam@jam-ubuntu:~/Desktop/test$ LD_PRELOAD=./hook.so ./test
size: 1024, ptr: 0x558ca65232a0
成功初始化
size: 72704, ptr: 0x558ca65236b0
size: 4, ptr: 0x558ca65352c0
jam@jam-ubuntu:~/Desktop/test$

如上,证明了new和malloc都是同源的,最终落到系统调用上,都是一个底子,所以专注语言的使用,方便工作的推进,但了解底层,似乎对调优有着更大的帮助。把上面的自定义类,换成vector,也能得到malloc调用的一些信息,因为它的底层,用到的内存分配std::allocator在底层,用到了malloc的逻辑。

3.2 劫持命令

在c语言中,很多接口都是和系统直接相关的,而这些个函数,在经编译器编译后符号也没有变化,所以它算是直接拿系统调用来编程的,而这些个系统调用,也是很多的命令的底层接口,所以完全可以通过LD_PRELOAD来对系统命令进行hook。比如,常用的ls命令,首先看看它都调用了什么库函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
jam@jam-ubuntu:~/Desktop/test$ readelf -Ws /usr/bin/ls | head -n 15

Symbol table '.dynsym' contains 123 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __ctype_toupper_loc@GLIBC_2.3 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND getenv@GLIBC_2.2.5 (3)
3: 0000000000000000 0 OBJECT GLOBAL DEFAULT UND __progname@GLIBC_2.2.5 (3)
4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND sigprocmask@GLIBC_2.2.5 (3)
5: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __snprintf_chk@GLIBC_2.3.4 (4)
6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND raise@GLIBC_2.2.5 (3)
7: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.34 (5)
8: 0000000000000000 0 FUNC GLOBAL DEFAULT UND abort@GLIBC_2.2.5 (3)
9: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __errno_location@GLIBC_2.2.5 (3)
10: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strncmp@GLIBC_2.2.5 (3)
11: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTable
jam@jam-ubuntu:~/Desktop/test$ ldd /usr/bin/ls
linux-vdso.so.1 (0x00007ffc6a794000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2648dfc000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2648a00000)
libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f2648d61000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2648e66000)
jam@jam-ubuntu:~/Desktop/test$

主要是想展示一下readelf的用法,所以就只显示前面15行,而且上面也有需要的信息了,比如strncmp,那么我们就实现这么一个函数,对应的动态库也就那么几个,没毛病,可以进行。实现一个strncmp函数。先上简单测试代码:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string.h>

int main(){
int res = strncmp("test", "aaa", 4);
printf("%d\n", res);

return 0;
}

测试一下:

1
2
3
4
5
6
7
8
jam@jam-ubuntu:~/Desktop/test$ gcc test.c -o test
jam@jam-ubuntu:~/Desktop/test$ ./test
19
jam@jam-ubuntu:~/Desktop/test$ ldd test
linux-vdso.so.1 (0x00007ffcd2d9a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2748400000)
/lib64/ld-linux-x86-64.so.2 (0x00007f27487a9000)
jam@jam-ubuntu:~/Desktop/test$

如上,可以看出对系统的依赖有很大的重合,所以也验证了系统命令多是基于系统调用的想法,还有初步锁定libc.so.6动态库吧。现在准备一下hook库,初步还是只看参数,而且不妨碍命令的正常执行,所以还是会调用系统库的strncmp,但自己会加一点输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <stdlib.h>

static int (*hook_strncmp)(const char *__s1, const char *__s2, size_t __n)=NULL;

void init(){
hook_strncmp = dlsym(RTLD_NEXT, "strncmp");
if(!hook_strncmp){
fputs("无法获取strncmp符号\n", stderr);
exit(-1);
}
fputs("初始化成功\n", stdout);
}

extern int strncmp(const char *__s1, const char *__s2, size_t __n){
if(!hook_strncmp)
init();
fprintf(stderr, "参数:%s, %s, %ld\n", __s1, __s2, __n);
return hook_strncmp(__s1, __s2, __n);
}

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
jam@jam-ubuntu:~/Desktop/test$ ./test
19
jam@jam-ubuntu:~/Desktop/test$ LD_PRELOAD=./hook.so ./test
19
jam@jam-ubuntu:~/Desktop/test$ LD_PRELOAD=./hook.so ls
初始化成功
参数:always, auto, 4
参数:yes, auto, 4
参数:force, auto, 4
参数:never, auto, 4
参数:no, auto, 4
参数:none, auto, 4
参数:auto, auto, 4
client.cpp clnt hook.c libhi.so server test test.cpp test.s unix_domain_socket.txt unix_server.c
client.h clnt.cpp hook.so main.cpp server.cpp test.c test.o unix_clnt.py unix_server unix_server.py
jam@jam-ubuntu:~/Desktop/test$

有意思的是,自己编写的测试程序对hook.so没反应,但ls却来劲了,之前的strcpy可能也是同样的原因失败的,照理来说,参数应该没问题的,毕竟ls调用都是成功的,那么为什么test程序一直没响应呢?

个人推荐

嗯,最后分享一个在线网站,它支持c++的一些语法糖的比较详细的解释,是一个,从cpp代码到代码的在线网站,对,从cpp代码到代码,哈哈哈。比如:

1
2
3
4
5
6
7
#include <iostream>
#include <vector>

int main(){
std::vector<int> num = {1,2,3,4};
return 0;
}

经网站解析后,是这样的:

1
2
3
4
5
6
7
8
#include <iostream>
#include <vector>

int main()
{
std::vector<int, std::allocator<int> > num = std::vector<int, std::allocator<int> >{std::initializer_list<int>{1, 2, 3, 4}};
return 0;
}

对了解语法糖非常有帮助,还有着根据代码,查看汇编码的网站,支持各种语言,比如之前就用过golang查看一个Println输出hello, world的例子,实在是。。。。。。杂(关于golang)。

地址 在线查看汇编码

CATALOG
  1. 一、基础知识
  2. 二、机制介绍
    1. 2.1 利用0x80劫持system_call
    2. 2.2 利用LD_PRELOAD动态链接进行hook
  3. 三、一些hook例子
    1. 3.1 针对malloc的hook
    2. 3.2 劫持命令
  4. 个人推荐