本地库覆盖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>

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...