本地库覆盖glibc的API
admin
2024-04-10 12:54:25
0

本地库覆盖glibc的getifaddrs

基本原理:(原理类似libSegFault.so这个库)

使用linux的环境变量“LD_PRELOAD”来提前载入一个库,然后这个库里面的同名API就可以覆盖后续的共享库的API了(glibc里面的若链接的API是可以被覆盖的)。如我们在自己的共享库中重写一个API

int getifaddrs (struct ifaddrs **ifap);

主要修改代码点:

  1. 在一个新的共享库里重写/覆盖getifaddrs(), 生产一个共享库libovfuns.so,把它拷贝到/opt/lib里面。主要修改点事去掉原有glibc里__netlink_assert_response(...)的abort处理,同时是在系统的文件里面记录一条异常log:(/opt/log/overwirte_fun.log)

    static void __netlink_assert_response (int fd, ssize_t result)
    {if (result < 0){/* Check if the error is unexpected.  */bool terminate = false;int error_code = errno;int family = get_address_family (fd);if (family != AF_NETLINK)/* If the address family does not match (or getsocknamefailed), report the original error.  */terminate = true;else if (error_code == EBADF|| error_code == ENOTCONN|| error_code == ENOTSOCK|| error_code == ECONNREFUSED)/* These errors indicate that the descriptor is not aconnected socket.  */terminate = true;else if (error_code == EAGAIN || error_code == EWOULDBLOCK){/* The kernel might return EAGAIN for other reasons than anon-blocking socket.  But if the socket is not blocking,it is not ours, so report the error.  */int mode = fcntl (fd, F_GETFL, 0);if (mode < 0 || (mode & O_NONBLOCK) != 0)terminate = true;}printf("[%s %d]\n", __func__, __LINE__);if (terminate){char message[200];if (family < 0)snprintf (message, sizeof (message),"Inovance overwrite: Unexpected error %d on netlink descriptor %d",error_code, fd);elsesnprintf (message, sizeof (message),"Inovance overwrite: Unexpected error %d on netlink descriptor %d"" (address family %d)",error_code, fd, family);//just use the log message replace the "__libc_fatal"//    __libc_fatal (message);overwrite_fun_log("getifaddrs", message);}else/* Restore orignal errno value.  */set_errno (error_code);}else if (result < sizeof (struct nlmsghdr)){char message[200];int family = get_address_family (fd);if (family < 0)snprintf (message, sizeof (message),"Inovance overwrite: Unexpected netlink response of size %zd"" on descriptor %d",result, fd);elsesnprintf (message, sizeof (message),"Inovance overwrite: Unexpected netlink response of size %zd"" on descriptor %d (address family %d)",result, fd, family);//just use the log message replace the "__libc_fatal"//   __libc_fatal (message);overwrite_fun_log("getifaddrs", message);}
    }
    

  2. 修改test启动的服务脚本:在原有服务脚本里面增加如下行,用于设置环境变量Environment="LD_PRELOAD=/opt/runtime/libovfuns.so"

  3. root@xxx:# cat /lib/systemd/system/test.service
    [Unit]
    Description=Inovance CoDeSys Control Progres
    Wants=inovance-daemon.service
    After=inovance-daemon.service[Service]
    Type=simple
    Environment="LD_PRELOAD=/opt/lib/libovfuns.so"
    PIDFile=/run/test.pid
    ExecStartPre=/bin/sleep 5
    ExecStart=/opt/xxx/test
    StandardOutput=null
    StandardError=null
    #Restart=always
    #RestartSec=1
    #StartLimitInterval=0[Install]
    WantedBy=multi-user.target

  4. 下面是测试代码,编译时使用gcc -L/opt/runtime/ -I./lib -lovfuns -o test test.c

  5. 生产test测试程序

     #define _GNU_SOURCE     /* To get defns of NI_MAXSERV and NI_MAXHOST */#include #include #include #include "my_ifaddrs.h"#include #include #include #include int main(int argc, char *argv[]){struct ifaddrs *ifaddr, *ifa;int family, s, n;char host[NI_MAXHOST];if (getifaddrs(&ifaddr) == -1) {perror("getifaddrs");exit(EXIT_FAILURE);}/* Walk through linked list, maintaining head pointer so wecan free list later */for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {if (ifa->ifa_addr == NULL)continue;family = ifa->ifa_addr->sa_family;/* Display interface name and family (including symbolicform of the latter for the common families) */printf("%-8s %s (%d)\n",ifa->ifa_name,(family == AF_PACKET) ? "AF_PACKET" :(family == AF_INET) ? "AF_INET" :(family == AF_INET6) ? "AF_INET6" : "???",family);/* For an AF_INET* interface address, display the address */if (family == AF_INET || family == AF_INET6) {s = getnameinfo(ifa->ifa_addr,(family == AF_INET) ? sizeof(struct sockaddr_in) :sizeof(struct sockaddr_in6),host, NI_MAXHOST,NULL, 0, NI_NUMERICHOST);if (s != 0) {printf("getnameinfo() failed: %s\n", gai_strerror(s));exit(EXIT_FAILURE);}printf("\t\taddress: <%s>\n", host);} else if (family == AF_PACKET && ifa->ifa_data != NULL) {struct rtnl_link_stats *stats = ifa->ifa_data;printf("\t\ttx_packets = %10u; rx_packets = %10u\n""\t\ttx_bytes   = %10u; rx_bytes   = %10u\n",stats->tx_packets, stats->rx_packets,stats->tx_bytes, stats->rx_bytes);}}freeifaddrs(ifaddr);exit(EXIT_SUCCESS);}
    

    测试添加和不添加环境量,程序使用的API是不同的,具体测试log如下所示:

    不添加环境变量,程序使用原始API

    root@xxx:# ./test

    lo AF_PACKET (17)

    tx_packets = 65; rx_packets = 65

    tx_bytes = 3180; rx_bytes = 3180

    enp1s0 AF_PACKET (17)

    tx_packets = 686; rx_packets = 3008

    tx_bytes = 50762; rx_bytes = 192738

    enp2s0 AF_PACKET (17)

    tx_packets = 118; rx_packets = 204

    tx_bytes = 19366; rx_bytes = 22391

    enp3s0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    enp4s0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 16

    tx_bytes = 0; rx_bytes = 960

    tunl0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    tap0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    tap1 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    lo AF_INET (2)

    address: <127.0.0.1>

    enp1s0 AF_INET (2)

    address: <10.61.64.75>

    enp2s0 AF_INET (2)

    address: <192.168.2.88>

    添加环境变量,程序使用新的API

    root@xxx:# env LD_PRELOAD=/opt/runtime/libovfuns.so ./test

    [__netlink_request 601] <------这个log是新的API里面的

    [__netlink_request 604]

    [__netlink_request 601]

    [__netlink_request 604]

    [__netlink_request 601]

    [__netlink_request 604]

    [__netlink_request 601]

    [__netlink_request 604]

    [__netlink_request 601]

    [__netlink_request 604]

    lo AF_PACKET (17)

    tx_packets = 107; rx_packets = 107

    tx_bytes = 5196; rx_bytes = 5196

    enp1s0 AF_PACKET (17)

    tx_packets = 1138; rx_packets = 4968

    tx_bytes = 83894; rx_bytes = 316171

    enp2s0 AF_PACKET (17)

    tx_packets = 160; rx_packets = 251

    tx_bytes = 23366; rx_bytes = 26263

    enp3s0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    enp4s0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 24

    tx_bytes = 0; rx_bytes = 1440

    tunl0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    tap0 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    tap1 AF_PACKET (17)

    tx_packets = 0; rx_packets = 0

    tx_bytes = 0; rx_bytes = 0

    lo AF_INET (2)

    address: <127.0.0.1>

    enp1s0 AF_INET (2)

    address: <10.61.64.75>

    enp2s0 AF_INET (2)

    address: <192.168.2.88>

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
A.机器学习入门算法(三):基... 机器学习算法(三):K近邻(k-nearest neigh...
数字温湿度传感器DHT11模块... 模块实例https://blog.csdn.net/qq_38393591/article/deta...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
Redis 所有支持的数据结构... Redis 是一种开源的基于键值对存储的 NoSQL 数据库,支持多种数据结构。以下是...
win下pytorch安装—c... 安装目录一、cuda安装1.1、cuda版本选择1.2、下载安装二、cudnn安装三、pytorch...
MySQL基础-多表查询 文章目录MySQL基础-多表查询一、案例及引入1、基础概念2、笛卡尔积的理解二、多表查询的分类1、等...
keil调试专题篇 调试的前提是需要连接调试器比如STLINK。 然后点击菜单或者快捷图标均可进入调试模式。 如果前面...
MATLAB | 全网最详细网... 一篇超超超长,超超超全面网络图绘制教程,本篇基本能讲清楚所有绘制要点&#...
IHome主页 - 让你的浏览... 随着互联网的发展,人们越来越离不开浏览器了。每天上班、学习、娱乐,浏览器...
TCP 协议 一、TCP 协议概念 TCP即传输控制协议(Transmission Control ...
营业执照的经营范围有哪些 营业执照的经营范围有哪些 经营范围是指企业可以从事的生产经营与服务项目,是进行公司注册...
C++ 可变体(variant... 一、可变体(variant) 基础用法 Union的问题: 无法知道当前使用的类型是什...
血压计语音芯片,电子医疗设备声... 语音电子血压计是带有语音提示功能的电子血压计,测量前至测量结果全程语音播报࿰...
MySQL OCP888题解0... 文章目录1、原题1.1、英文原题1.2、答案2、题目解析2.1、题干解析2.2、选项解析3、知识点3...
【2023-Pytorch-检... (肆十二想说的一些话)Yolo这个系列我们已经更新了大概一年的时间,现在基本的流程也走走通了,包含数...
实战项目:保险行业用户分类 这里写目录标题1、项目介绍1.1 行业背景1.2 数据介绍2、代码实现导入数据探索数据处理列标签名异...
记录--我在前端干工地(thr... 这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前段时间接触了Th...
43 openEuler搭建A... 文章目录43 openEuler搭建Apache服务器-配置文件说明和管理模块43.1 配置文件说明...