zoukankan      html  css  js  c++  java
  • 使用 LD_PRELOAD 变量拦截调用

    背景&原理

    很多 a.out 程序都依赖动态库 libc.so, 比如使用 strcmp() 比较密码, 其实是不安全的
    使用 LD_PRELOAD 变量可以使该变量中的可链接文件(编译时使用-rdynamic导出符号的a.out或.so)的符号优先被使用,
    如果我们自己编译一个libc.so加入LD_LIBRARY_PATH变量, 同时该库依赖系统libc.so, 那么就可以拦截想拦截的c函数!
    解决办法: ?

    实例

    • main.c
    #include <unistd.h>
    #include <stdio.h>
    #include <termios.h>
    #include <stdlib.h>
    #include <string.h>
    
    int
    main(int argc, char **argv) {
            char passwd[1024] = { 0 };
    //******输入密码
            struct termios save, current;
            tcgetattr(0, &save);// 得到原来的终端属性
            current = save;
            current.c_lflag &= ~ECHO;// 关闭回显
            tcsetattr(0, TCSANOW, &current);// 设置新的终端属性
    
    
            write(1, "请输入密码: ", 17);
            scanf("%s", passwd);
            setbuf(stdin, NULL);
    
            tcsetattr(0, TCSANOW, &save);// 恢复原来的终端属性
    //******
            puts("");
    
            if (strcmp("userpass", passwd) == 0) {
                    puts("密码正确");
            } else {
                    puts("密码错误");
            }
    
            return 0;
    }
    
    • holdStrcmp.c
    #include <stdio.h>
    
    int
    strcmp(const char *a, const char *b) {
            printf("拦截到调用, a="%s", b="%s" 
    ", a, b);
            return 0;
    }
    

    编译&运行

    develon@Topmain:~/c$ gcc passwd.c
    develon@Topmain:~/c$ gcc -shared -fPIC holdStrcmp.c -o libc.so
    develon@Topmain:~/c$ LD_LIBRARY_PATH=. LD_PRELOAD=./libc.so a.out
    

    正常运行

    拦截strcmp()

    图片LB是LD的错误键入, 但结果一致

  • 相关阅读:
    机器学习之线性回归
    Anaconda使用
    Pycharm使用总结
    Mysql使用小tips
    技术转型与考研总结
    C语言的学习
    python 使用小结
    RedHat Linux 忘记密码
    设计模式之单例模式
    Java 读写Properties配置文件
  • 原文地址:https://www.cnblogs.com/develon/p/10660094.html
Copyright © 2011-2022 走看看