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); } } } } }
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号