C#使用Wintun的个人残缺记录
本来是想用Wintun创建一个TUN然后能用wireshark抓TUN的包,然后自己写一写TCP协议的,发现完全不能用wireshark抓Wintun创建的TUN的包
要么wireshark没显示TUN接口,要么出错说无法设置混杂模式,我取消了混杂模式也不行
反正我没找到,有朋友知道可以告诉我
Wintun官网 https://www.wintun.net/
wintun文档 https://git.zx2c4.com/wintun/about/
感觉Wintun确实比较简单,直接用C#本机调用也不是特别麻烦
把对应架构的Dll放到你的应用根目录,其实只要默认加载策略能找到该dll就行
假如你加载并且调用了dll中的方法,第一次运行时windows会提示是否要安装该dll,注意一定要以管理员运行,否则出错
然后你就多了一个TUN 或者说虚拟网卡,能在控制面板的网络设备中找到,跟WIFI的条目类似
创建一个TUN后需要手动或者通过其他方式设置TUN的ip ,子网掩码,网关,DNS。我找了文档好像没有设置的API
这个时候假如你还链接了wifi之类的也不会断网,只不过你的电脑多了一个ip,你一运行程序就类似于插上了一个网线,应用假如主动走这个ip
那么他的ip数据包就会传递给你的应用, 好像Wintun本身没有办法强迫应用走TUN,可能要通过其他办法
读取的示例是这样,但是有的资源我没有释放
using System; using System.ComponentModel; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace Test { class Program { [DllImport("wintun.dll", SetLastError = true)] extern static IntPtr WintunCreateAdapter(IntPtr a, IntPtr b, IntPtr pp, IntPtr p); [DllImport("wintun.dll", SetLastError = true)] extern static IntPtr WintunStartSession(IntPtr p, int n); [DllImport("wintun.dll", SetLastError = true)] extern static IntPtr WintunOpenAdapter(IntPtr a, IntPtr b); [DllImport("wintun.dll", SetLastError = true)] extern static IntPtr WintunReceivePacket(IntPtr han, out int size); [DllImport("wintun.dll", SetLastError = true)] extern static IntPtr WintunAllocateSendPacket(IntPtr han, int size); [DllImport("wintun.dll", SetLastError = true)] extern static void WintunSendPacket(IntPtr han, IntPtr buffer); static string POOL_NAME = "WireGuard"; static string NAME_NAME = "Test"; static IntPtr WintunCreateAdapter() { return WintunCreateAdapter(Marshal.StringToHGlobalUni(POOL_NAME), Marshal.StringToHGlobalUni(NAME_NAME), IntPtr.Zero, IntPtr.Zero); } static IntPtr WintunOpenAdapter() { return WintunOpenAdapter(Marshal.StringToHGlobalUni(POOL_NAME), Marshal.StringToHGlobalUni(NAME_NAME)); } static void Main(string[] args) { IntPtr sess = WintunStartSession(WintunCreateAdapter(), 0x400000); while (true) { IntPtr p = WintunReceivePacket(sess, out int size); if (p == IntPtr.Zero) { } else { Console.WriteLine(size); } } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构