本來是想用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); } } } } }
