乐于分享
好东西不私藏

跨平台底层网络库libdnet源码分析系列(一)

跨平台底层网络库libdnet源码分析系列(一)

官网:http://securitytech.cc

源码分析mettle后门工具学习 所使用的依赖库

libdnet Windows 平台编译完整指南

目录

  1. 概述
  2. 系统要求
  3. 环境准备
  4. configure 修改说明
  5. 编译步骤
  6. 依赖库说明
  7. 编译测试程序
  8. 常见问题

概述

libdnet (libdumbnet) 是一个简化版的网络工具库,提供网络接口、ARP、路由、IP、以太网等功能的跨平台访问。

Windows 平台特点

  • 使用 Windows IP Helper API ( iphlpapi.dll)
  • 使用 Winsock 2.2 ( ws2_32.dll)
  • 某些功能需要第三方库(如 WinPcap)
  • 需要 MSYS2 或 MinGW-w64 编译工具链

系统要求

操作系统

  • Windows 7 或更高版本
  • Windows 10/11(推荐)
  • Windows Server 2012 及以上

必需软件

  • MSYS2 MinGW64(推荐)或 MinGW-w64
  • GCC 7.x 或更高版本
  • Make、Autoconf、Automake、Libtool

可选软件

  • WinPcap(用于原始以太网帧访问)
  • PktFilter(用于防火墙功能)

环境准备

方案 1:安装 MSYS2(推荐)

1.1 下载安装 MSYS2

访问 https://www.msys2.org/ 下载安装程序,默认安装到 C:\msys64

1.2 安装编译工具链

打开 “MSYS2 MinGW64” 终端(不是普通的 MSYS2 终端):

  1. # 更新包数据库
  2. pacman -Syu
  3. # 安装编译工具链
  4. pacman ---noconfirm --needed \
  5.   mingw-w64-x86_64-gcc \
  6.   mingw-w64-x86_64-make \
  7.   mingw-w64-x86_64-autoconf \
  8.   mingw-w64-x86_64-automake \
  9.   mingw-w64-x86_64-libtool \
  10.   make \
  11.   autoconf \
  12.   automake \
  13.   libtool

1.3 验证安装

  1. # 检查 GCC
  2. gcc --version
  3. # 检查 Make
  4. make --version

方案 2:使用独立的 MinGW-w64

2.1 下载 MinGW-w64

从以下任一源下载:

  • https://github.com/niXman/mingw-builds-binaries/releases
  • https://winlibs.com/

选择 x86_64-posix-seh 版本,解压到 C:\mingw64

2.2 配置环境变量

添加到系统 PATH:

  1. C:\mingw64\bin

2.3 验证安装

打开 CMD 或 PowerShell:

  1. gcc --version

configure 修改说明

问题描述

在 MSYS2/MINGW64 环境下运行 ./configure 时,可能会遇到以下错误:

  1. checking forPython... configure: error: need MingW32package to build under Cygwin

原因分析

configure 脚本中的第 12177-12269 行检测到 CYGWIN=yes 时,会检查是否存在 mingw 目录:

  1. if test "$CYGWIN"= yes ;then
  2. if test -/usr/include/mingw || test -/usr/i686-w64-mingw32;then
  3. # 使用 mingw 编译
  4. ...
  5. else
  6. # 报错
  7.         as_fn_error $?"need MingW32 package to build under Cygwin""$LINENO"5
  8. fi
  9. fi

MSYS2 环境虽然被检测为类似 Cygwin,但它是原生 MinGW,不需要 Cygwin 的 mingw 包。

修改方案

原始代码(第 12177-12182 行)

  1. if test "$CYGWIN"= yes ;then
  2. if test -/usr/include/mingw || test -/usr/i686-w64-mingw32;then
  3. if test "$MINGW"= no ;then
  4.             CPPFLAGS="$CPPFLAGS -mno-cygwin"
  5.             CFLAGS="$CFLAGS -mno-cygwin"
  6. fi
  7.         $as_echo "#define WIN32_LEAN_AND_MEAN 1">>confdefs.h
  8. { $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lws2_32">&5
  9. ...

错误触发代码(第 12267-12269 行)

  1. else
  2.         as_fn_error $?"need MingW32 package to build under Cygwin""$LINENO"5
  3. fi
  4. fi

修改后的代码

注释掉 mingw 目录检查和错误提示:

  1. if test "$CYGWIN"= yes ;then
  2. # Skip the mingw check for MSYS2/MINGW64 - MSYS2 is native MinGW
  3. # if test -d /usr/include/mingw || test -d /usr/i686-w64-mingw32; then
  4. #   if test "$MINGW" = no ; then
  5. #       CPPFLAGS="$CPPFLAGS -mno-cygwin"
  6. #       CFLAGS="$CFLAGS -mno-cygwin"
  7. #   fi
  8.     $as_echo "#define WIN32_LEAN_AND_MEAN 1">>confdefs.h
  9. { $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lws2_32">&5
  10. ...
  11. #else
  12. #   as_fn_error $? "need MingW32 package to build under Cygwin" "$LINENO" 5
  13. #fi

修改步骤

  1. 打开 configure 文件
  2. 定位到第 12177 行
  3. 按上述注释方式修改代码
  4. 保存文件

自动化修改脚本

  1. # 在 libdnet 根目录下运行
  2. sed -'12178s/^/#/' configure
  3. sed -'12179s/^/#/' configure
  4. sed -'12180s/^/#/' configure
  5. sed -'12181s/^/#/' configure
  6. sed -'12182s/^/#/' configure
  7. sed -'12267s/^/#/' configure
  8. sed -'12268s/^/#/' configure
  9. sed -'12269s/^/#/' configure

编译步骤

在 MSYS2 MinGW64 终端中编译

  1. # 进入 libdnet 目录
  2. cd /c/Users/Administrator/libev/libdnet-1.13
  3. # 清理旧的编译文件
  4. make clean 2>/dev/null || true
  5. rm -f config.h config.status libtool
  6. # 配置项目
  7. ./configure --host=x86_64-w64-mingw32 --prefix=/usr/local
  8. # 编译
  9. make
  10. # 可选:安装
  11. make install

配置选项说明

选项
说明
--host=x86_64-w64-mingw32
指定目标平台为 Windows 64 位
--prefix=/usr/local
安装路径
--disable-shared
只编译静态库
--enable-debug
启用调试符号
--with-python
包含 Python 绑定

只编译静态库

  1. ./configure --host=x86_64-w64-mingw32 --disable-shared
  2. make

清理编译产物

  1. make clean

依赖库说明

系统依赖(必需)

1. Winsock 2.2 ( ws2_32.dll)

  • 用途:网络套接字 API
  • 链接选项: -lws2_32
  • 系统自带:Windows XP 及以上
  • 头文件
  • winsock2.h
  • ws2tcpip.h

2. IP Helper API ( iphlpapi.dll)

  • 用途:网络接口、路由、ARP 表操作
  • 链接选项: -liphlpapi
  • 系统自带:Windows 2000 及以上
  • 头文件
  • iphlpapi.h
  • ipexport.h
  • iptypes.h

可选依赖

3. WinPcap

  • 用途:原始以太网帧捕获和发送
  • 下载:https://www.winpcap.org/
  • 头文件: pcap.h
  • 链接选项: -lwpcap
  • 开发包:WinPcap Developer’s Pack

libdnet 的 Windows 实现:

  • eth-win32.c:空实现,依赖 WinPcap

4. PktFilter

  • 用途:防火墙规则管理
  • 下载:http://www.hsc.fr/ressources/outils/pktfilter/index.html.en
  • 通信方式:命名管道 \\.\pipe\PktFltPipe

libdnet 的 Windows 实现:

  • fw-pktfilter.c:通过命名管道与 PktFilter 服务通信

编译时自动链接的库

configure 会自动检测并链接以下库:

  1. // 自动添加的宏定义
  2. #define WIN32_LEAN_AND_MEAN 1
  3. #define HAVE_LIBWS2_32 1
  4. #define HAVE_LIBIPHLPAPI 1
  5. #define snprintf _snprintf

链接命令示例

使用动态库编译

  1. gcc test.-Iinclude-Lsrc/.libs -ldnet -liphlpapi -lws2_32 -o test.exe

使用静态库编译

  1. gcc test.-Iinclude src/.libs/libdnet.-liphlpapi -lws2_32 -static -o test.exe

完全静态链接(无运行时依赖)

  1. gcc test.-Iinclude src/.libs/libdnet.-liphlpapi -lws2_32 -static -static-libgcc -o test.exe

编译测试程序

示例代码

创建 test.c

  1. #include<dnet.h>
  2. #include<stdio.h>
  3. int main(void){
  4. intf_t*intf;
  5. struct intf_entry entry;
  6. int ret =0;
  7. // 打开网络接口
  8. if((intf = intf_open())== NULL){
  9.         perror("intf_open");
  10. return1;
  11. }
  12. // 遍历所有网络接口
  13.     entry.intf_len =sizeof(entry);
  14.     printf("网络接口列表:\n");
  15.     printf("====================================\n");
  16. if(intf_loop(intf,[](struct intf_entry *entry,void*arg)->int{
  17.         printf("接口: %s\n", entry->intf_name);
  18.         printf("  类型: 0x%04x\n", entry->intf_type);
  19.         printf("  标志: 0x%08x\n", entry->intf_flags);
  20.         printf("  MTU: %u\n", entry->intf_mtu);
  21. if(entry->intf_addr.addr_type != ADDR_TYPE_NONE){
  22.             printf("  IP: %s/%d\n",
  23.                    addr_ntoa(&entry->intf_addr),
  24.                    entry->intf_addr.addr_bits);
  25. }
  26. if(entry->intf_link_addr.addr_type == ADDR_TYPE_ETH){
  27.             printf("  MAC: %s\n",
  28.                    addr_ntoa(&entry->intf_link_addr));
  29. }
  30.         printf("\n");
  31. return0;
  32. }, NULL)<0){
  33.         perror("intf_loop");
  34.         ret =1;
  35. }
  36.     intf_close(intf);
  37. return ret;
  38. }

编译测试程序

在 MSYS2 MinGW64 终端中

  1. # 动态链接
  2. gcc test.-Iinclude-Lsrc/.libs -ldnet -liphlpapi -lws2_32 -o test.exe
  3. # 静态链接(推荐)
  4. gcc test.-Iinclude src/.libs/libdnet.-liphlpapi -lws2_32 -static -o test.exe

在 Windows CMD/PowerShell 中

  1. rem 静态链接(需要添加MinGW64 bin  PATH
  2. gcc test.-Iinclude src\.libs\libdnet.-liphlpapi -lws2_32 -static-o test.exe

运行测试程序

  1. # 需要管理员权限
  2. ./test.exe

或在 Windows CMD 中:

  1. test.exe

常见问题

Q1: configure 错误:need MingW32 package to build under Cygwin

原因:configure 检测到 CYGWIN=yes,但没有找到 mingw 目录。

解决方案

方案 1:修改 configure 文件(见上文 configure 修改说明)

方案 2:手动跳过检查,直接编译 Windows 源文件

Q2: 找不到 iphlpapi.h 或 ws2tcpip.h

原因:Windows SDK 路径未正确配置。

解决方案

  • 确保 MSYS2 MinGW64 已正确安装
  • 检查头文件是否存在: bash ls/mingw64/x86_64-w64-mingw32/include/iphlpapi.h ls/mingw64/x86_64-w64-mingw32/include/ws2tcpip.h

Q3: undefined reference to GetAdaptersAddresses

原因:缺少 iphlpapi 库。

解决方案:链接时添加 -liphlpapi

  1. gcc test.-ldnet -liphlpapi -lws2_32 -o test.exe

Q4: 编译出的程序在别的电脑上运行报错

原因:使用了动态链接,目标机器缺少 libdnet.dll 或 MSVC 运行时。

解决方案:使用静态链接

  1. gcc test.-Iinclude src/.libs/libdnet.-liphlpapi -lws2_32 -static -o test.exe

或复制必要的 DLL:

  • libdnet.dll
  • libgccsseh-1.dll
  • libstdc++-6.dll

Q5: 运行时提示 Permission denied

原因:某些操作需要管理员权限。

解决方案:以管理员身份运行

  • 右键点击程序 → “以管理员身份运行”
  • 或在管理员权限的 CMD/PowerShell 中运行

Q6: 找不到 libdnet.so 或 libdnet.dll

原因:库路径未设置或编译失败。

解决方案

检查编译产物:

  1. ls -lh src/.libs/libdnet.a
  2. ls -lh src/.libs/libdnet.so*
  3. ls -lh src/.libs/libdnet.dll*

如果是 .so 文件,说明是在 Linux 下编译的,需要重新配置为 Windows 平台:

  1. make clean
  2. ./configure --host=x86_64-w64-mingw32
  3. make

Q7: 如何查看编译日志?

方法 1:查看 config.log

  1. cat config.log | grep error

方法 2:使用详细的 make 输出

  1. make V=1

Windows 平台特性

编译产物

在 .libs 目录下生成:

  1. libdnet.a              # 静态库
  2. libdnet.dll           # 动态库(如果启用)
  3. libdnet.dll.a        # DLL 的导入库

库文件大小参考

  1. libdnet.a            370 KB
  2. libdnet.dll          200 KB

支持的功能模块

模块
Windows 实现
说明
intf intf-win32.c
网络接口操作
arp arp-win32.c
ARP 表操作
route route-win32.c
路由表操作
ip ip-win32.c
IP 层数据包发送
eth eth-win32.c
空实现(需要 WinPcap)
fw fw-pktfilter.c
防火墙(需要 PktFilter)
tun tun-none.c
隧道设备(空实现)

参考资料

官方资源

  • libdnet GitHub: https://github.com/dugsong/libdnet
  • libdnet Wiki: https://github.com/dugsong/libdnet/wiki

依赖库

  • MSYS2: https://www.msys2.org/
  • MinGW-w64: https://www.mingw-w64.org/
  • WinPcap: https://www.winpcap.org/
  • PktFilter: http://www.hsc.fr/ressources/outils/pktfilter/index.html.en

Windows API 文档

  • IP Helper API: https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/
  • Winsock 2.2: https://docs.microsoft.com/en-us/windows/win32/winsock/

完整编译脚本

一键编译脚本 (build_all.bat)

  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. echo ================================================
  4. echo libdnet Windows一键编译脚本
  5. echo ================================================
  6. echo.
  7. REM 检查 MSYS2
  8. ifnot exist "C:\msys64\mingw64.exe"(
  9.     echo [错误]找不到 MSYS2 MinGW64
  10.     pause
  11. exit/1
  12. )
  13. echo [1/4]启动编译...
  14. echo.
  15. "C:\msys64\mingw64.exe"--login -^
  16. "cd /c/Users/Administrator/libev/libdnet-1.13 && ^
  17.    make clean 2^>/dev/null || true && ^
  18.    rm -f config.h config.status && ^
  19.    ./configure --host=x86_64-w64-mingw32 --prefix=/usr/local && ^
  20.    make"
  21. echo.
  22. echo [2/4]检查编译结果...
  23. if exist "src\.libs\libdnet.a"(
  24.     echo 编译成功!
  25.     dir "src\.libs\libdnet.a"
  26. )else(
  27.     echo 编译失败
  28. )
  29. echo.
  30. echo ================================================
  31. echo 编译完成!
  32. echo ================================================
  33. echo.
  34. echo 编译测试程序示例:
  35. echo   gcc test.-Iinclude-Lsrc/.libs -ldnet -liphlpapi -lws2_32 -o test.exe
  36. echo.
  37. pause

总结

编译 libdnet 在 Windows 平台需要:

  1. ✅ 安装 MSYS2 MinGW64 编译环境
  2. ✅ 修改 configure 文件(注释 mingw 检查)
  3. ✅ 运行 ./configure--host=x86_64-w64-mingw32
  4. ✅ 运行 make
  5. ✅ 链接时添加 -liphlpapi-lws2_32

生成的库文件位于 src/.libs/libdnet.a,可以直接用于编译 Windows 应用程序。

  • 公众号:安全狗的自我修养

  • vx:2207344074

  • http://gitee.com/haidragon

  • http://github.com/haidragon

  • bilibili:haidragonx

本站文章均为手工撰写未经允许谢绝转载:夜雨聆风 » 跨平台底层网络库libdnet源码分析系列(一)

猜你喜欢

  • 暂无文章