跨平台底层网络库libdnet源码分析系列(一)
官网:http://securitytech.cc
源码分析mettle后门工具学习 所使用的依赖库

libdnet Windows 平台编译完整指南
目录
- 概述
- 系统要求
- 环境准备
- configure 修改说明
- 编译步骤
- 依赖库说明
- 编译测试程序
- 常见问题
概述
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 终端):
# 更新包数据库pacman -Syu# 安装编译工具链pacman -S --noconfirm --needed \mingw-w64-x86_64-gcc \mingw-w64-x86_64-make \mingw-w64-x86_64-autoconf \mingw-w64-x86_64-automake \mingw-w64-x86_64-libtool \make \autoconf \automake \libtool
1.3 验证安装
# 检查 GCCgcc --version# 检查 Makemake --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:
C:\mingw64\bin
2.3 验证安装
打开 CMD 或 PowerShell:
gcc --version
configure 修改说明
问题描述
在 MSYS2/MINGW64 环境下运行 ./configure 时,可能会遇到以下错误:
checking forPython... configure: error: need MingW32package to build under Cygwin
原因分析
configure 脚本中的第 12177-12269 行检测到 CYGWIN=yes 时,会检查是否存在 mingw 目录:
if test "$CYGWIN"= yes ;thenif test -d /usr/include/mingw || test -d /usr/i686-w64-mingw32;then# 使用 mingw 编译...else# 报错as_fn_error $?"need MingW32 package to build under Cygwin""$LINENO"5fifi
MSYS2 环境虽然被检测为类似 Cygwin,但它是原生 MinGW,不需要 Cygwin 的 mingw 包。
修改方案
原始代码(第 12177-12182 行)
if test "$CYGWIN"= yes ;thenif test -d /usr/include/mingw || test -d /usr/i686-w64-mingw32;thenif test "$MINGW"= no ;thenCPPFLAGS="$CPPFLAGS -mno-cygwin"CFLAGS="$CFLAGS -mno-cygwin"fi$as_echo "#define WIN32_LEAN_AND_MEAN 1">>confdefs.h{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lws2_32">&5...
错误触发代码(第 12267-12269 行)
elseas_fn_error $?"need MingW32 package to build under Cygwin""$LINENO"5fifi
修改后的代码
注释掉 mingw 目录检查和错误提示:
if test "$CYGWIN"= yes ;then# Skip the mingw check for MSYS2/MINGW64 - MSYS2 is native MinGW# if test -d /usr/include/mingw || test -d /usr/i686-w64-mingw32; then# if test "$MINGW" = no ; then# CPPFLAGS="$CPPFLAGS -mno-cygwin"# CFLAGS="$CFLAGS -mno-cygwin"# fi$as_echo "#define WIN32_LEAN_AND_MEAN 1">>confdefs.h{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lws2_32">&5...#else# as_fn_error $? "need MingW32 package to build under Cygwin" "$LINENO" 5#fi
修改步骤
- 打开
configure文件 - 定位到第 12177 行
- 按上述注释方式修改代码
- 保存文件
自动化修改脚本
# 在 libdnet 根目录下运行sed -i '12178s/^/#/' configuresed -i '12179s/^/#/' configuresed -i '12180s/^/#/' configuresed -i '12181s/^/#/' configuresed -i '12182s/^/#/' configuresed -i '12267s/^/#/' configuresed -i '12268s/^/#/' configuresed -i '12269s/^/#/' configure
编译步骤
在 MSYS2 MinGW64 终端中编译
# 进入 libdnet 目录cd /c/Users/Administrator/libev/libdnet-1.13# 清理旧的编译文件make clean 2>/dev/null || truerm -f config.h config.status libtool# 配置项目./configure --host=x86_64-w64-mingw32 --prefix=/usr/local# 编译make# 可选:安装make install
配置选项说明
|
|
|
|---|---|
--host=x86_64-w64-mingw32 |
|
--prefix=/usr/local |
|
--disable-shared |
|
--enable-debug |
|
--with-python |
|
只编译静态库
./configure --host=x86_64-w64-mingw32 --disable-sharedmake
清理编译产物
make clean
依赖库说明
系统依赖(必需)
1. Winsock 2.2 ( ws2_32.dll)
- 用途:网络套接字 API
- 链接选项:
-lws2_32 - 系统自带:Windows XP 及以上
- 头文件:
winsock2.hws2tcpip.h
2. IP Helper API ( iphlpapi.dll)
- 用途:网络接口、路由、ARP 表操作
- 链接选项:
-liphlpapi - 系统自带:Windows 2000 及以上
- 头文件:
iphlpapi.hipexport.hiptypes.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 会自动检测并链接以下库:
链接命令示例
使用动态库编译
gcc test.c -Iinclude-Lsrc/.libs -ldnet -liphlpapi -lws2_32 -o test.exe
使用静态库编译
gcc test.c -Iinclude src/.libs/libdnet.a -liphlpapi -lws2_32 -static -o test.exe
完全静态链接(无运行时依赖)
gcc test.c -Iinclude src/.libs/libdnet.a -liphlpapi -lws2_32 -static -static-libgcc -o test.exe
编译测试程序
示例代码
创建 test.c:
#include<dnet.h>#include<stdio.h>int main(void){intf_t*intf;struct intf_entry entry;int ret =0;// 打开网络接口if((intf = intf_open())== NULL){perror("intf_open");return1;}// 遍历所有网络接口entry.intf_len =sizeof(entry);printf("网络接口列表:\n");printf("====================================\n");if(intf_loop(intf,[](struct intf_entry *entry,void*arg)->int{printf("接口: %s\n", entry->intf_name);printf(" 类型: 0x%04x\n", entry->intf_type);printf(" 标志: 0x%08x\n", entry->intf_flags);printf(" MTU: %u\n", entry->intf_mtu);if(entry->intf_addr.addr_type != ADDR_TYPE_NONE){printf(" IP: %s/%d\n",addr_ntoa(&entry->intf_addr),entry->intf_addr.addr_bits);}if(entry->intf_link_addr.addr_type == ADDR_TYPE_ETH){printf(" MAC: %s\n",addr_ntoa(&entry->intf_link_addr));}printf("\n");return0;}, NULL)<0){perror("intf_loop");ret =1;}intf_close(intf);return ret;}
编译测试程序
在 MSYS2 MinGW64 终端中
# 动态链接gcc test.c -Iinclude-Lsrc/.libs -ldnet -liphlpapi -lws2_32 -o test.exe# 静态链接(推荐)gcc test.c -Iinclude src/.libs/libdnet.a -liphlpapi -lws2_32 -static -o test.exe
在 Windows CMD/PowerShell 中
rem 静态链接(需要添加MinGW64 bin 到 PATH)gcc test.c -Iinclude src\.libs\libdnet.a -liphlpapi -lws2_32 -static-o test.exe
运行测试程序
# 需要管理员权限./test.exe
或在 Windows CMD 中:
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
gcc test.c -ldnet -liphlpapi -lws2_32 -o test.exe
Q4: 编译出的程序在别的电脑上运行报错
原因:使用了动态链接,目标机器缺少 libdnet.dll 或 MSVC 运行时。
解决方案:使用静态链接
gcc test.c -Iinclude src/.libs/libdnet.a -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
原因:库路径未设置或编译失败。
解决方案:
检查编译产物:
ls -lh src/.libs/libdnet.als -lh src/.libs/libdnet.so*ls -lh src/.libs/libdnet.dll*
如果是 .so 文件,说明是在 Linux 下编译的,需要重新配置为 Windows 平台:
make clean./configure --host=x86_64-w64-mingw32make
Q7: 如何查看编译日志?
方法 1:查看 config.log
cat config.log | grep error
方法 2:使用详细的 make 输出
make V=1
Windows 平台特性
编译产物
在 .libs 目录下生成:
libdnet.a # 静态库libdnet.dll # 动态库(如果启用)libdnet.dll.a # DLL 的导入库
库文件大小参考
libdnet.a 约370 KBlibdnet.dll 约200 KB
支持的功能模块
|
|
|
|
|---|---|---|
intf |
intf-win32.c |
|
arp |
arp-win32.c |
|
route |
route-win32.c |
|
ip |
ip-win32.c |
|
eth |
eth-win32.c |
|
fw |
fw-pktfilter.c |
|
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)
@echo offsetlocal EnableDelayedExpansionecho ================================================echo libdnet Windows一键编译脚本echo ================================================echo.REM 检查 MSYS2ifnot exist "C:\msys64\mingw64.exe"(echo [错误]找不到 MSYS2 MinGW64pauseexit/b 1)echo [1/4]启动编译...echo."C:\msys64\mingw64.exe"--login -c ^"cd /c/Users/Administrator/libev/libdnet-1.13 && ^make clean 2^>/dev/null || true && ^rm -f config.h config.status && ^./configure --host=x86_64-w64-mingw32 --prefix=/usr/local && ^make"echo.echo [2/4]检查编译结果...if exist "src\.libs\libdnet.a"(echo ✓编译成功!dir "src\.libs\libdnet.a")else(echo ✗编译失败)echo.echo ================================================echo 编译完成!echo ================================================echo.echo 编译测试程序示例:echo gcc test.c -Iinclude-Lsrc/.libs -ldnet -liphlpapi -lws2_32 -o test.exeecho.pause
总结
编译 libdnet 在 Windows 平台需要:
- ✅ 安装 MSYS2 MinGW64 编译环境
- ✅ 修改 configure 文件(注释 mingw 检查)
- ✅ 运行
./configure--host=x86_64-w64-mingw32 - ✅ 运行
make - ✅ 链接时添加
-liphlpapi-lws2_32
生成的库文件位于 src/.libs/libdnet.a,可以直接用于编译 Windows 应用程序。
-
公众号:安全狗的自我修养
-
vx:2207344074
-
http://gitee.com/haidragon
-
http://github.com/haidragon
-
bilibili:haidragonx
-


夜雨聆风