hook glibc
来源:互联网 发布:淘宝高硼硅玻璃有毒吗 编辑:程序博客网 时间:2024/06/03 19:31
我们知道,Linux的用C库的都是glibc,有一个叫libc.so.6的文件,这是几乎所有Linux下命令的动态链接中,其中有标准C的各种函数,默认情况下,linux所编译的程序中对标准C函数的链接,都是通过动态链接方式来链接libc.so.6这个函数库的。这也意味着我们在通过我们注入的.so来实现函数覆盖劫持之后需要从libc.so.6中取得原本的正常函数,让程序继续正常执行
正常程序main.c:
#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){ if( strcmp(argv[1], "test") ) { printf("Incorrect password\n"); } else { printf("Correct password\n"); } return 0;}
用于劫持函数的.so代码hook.c
#include <stdio.h>#include <string.h>#include <dlfcn.h>/*hook的目标是strcmp,所以typedef了一个STRCMP函数指针hook的目的是要控制函数行为,从原库libc.so.6中拿到strcmp指针,保存成old_strcmp以备调用*/typedef int(*STRCMP)(const char*, const char*);int strcmp(const char *s1, const char *s2){ static void *handle = NULL; static STRCMP old_strcmp = NULL; if( !handle ) { handle = dlopen("libc.so.6", RTLD_LAZY); old_strcmp = (STRCMP)dlsym(handle, "strcmp"); } printf("oops!!! hack function invoked. s1=<%s> s2=<%s>\n", s1, s2); return old_strcmp(s1, s2);}
编译:
gcc -o test main.cgcc -fPIC -shared -o hook.so hook.c -ldl
运行:
LD_PRELOAD=./hook.so ./test 123
阅读全文
0 0
- hook glibc
- glibc
- glibc
- glibc
- Glibc
- glibc
- glibc
- glibc
- glibc
- glibc
- glibc
- hook
- HOOK
- hook
- Hook
- Hook
- hook
- Hook
- 11、Android开发基础之Logcat的使用
- linux彻底卸载mysql
- 当设置了PASSWORD_LIFE_TIME后,密码到期之前会有提示
- JVM运行时数据区域
- android 横屏(全屏)搜索时无效是为什么?
- hook glibc
- yii checkList设置默认选项
- Kotlin的委托属性和区间
- Unity Shaders and Effects Cookbook 中的Packing and blending textures
- Java提高之HashMap
- BZOJ3876 [Ahoi2014&Jsoi2014]支线剧情
- 游标的作用
- ValueError: Negative dimension size caused by subtracting 2 from 1 for 'MaxPool_1' (op: 'MaxPool') w
- 12、Android开发基础之数据存储