您好,登錄后才能下訂單哦!
小編給大家分享一下C#如何利用SharpPcap實現(xiàn)網(wǎng)絡(luò)包捕獲,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
什么是SharpPcap?
SharpPcap 是一個.NET 環(huán)境下的網(wǎng)絡(luò)包捕獲框架,基于著名的 pcap/WinPcap 庫開發(fā)。提供了捕獲、注入、分析和構(gòu)建的功能,適用于 C# 和 VB NET 開發(fā)語言。
SharpPcap有兩部分組成:1> SharpPcap.dll 負責數(shù)據(jù)的捕獲 2> PacketDotNet.dll負責數(shù)據(jù)包的解析
思路:
通過進程名字獲取對應的端口號。
SharpPcap獲取對應的數(shù)據(jù)包,通過解析數(shù)據(jù)包過濾相關(guān)的端口。
涉及知識點:
Process 獲取相關(guān)進程信息。
netstat命令:netstat -ano|find "3844" 獲取進程對應的端口
SharpPcap相關(guān)信息:
通過CaptureDeviceList的靜態(tài)方法獲取設(shè)備列表。
通過OnPacketArrival事件接收數(shù)據(jù)包。
通過PacketDotNet來解析數(shù)據(jù)包
效果圖下:
SharpPcap核心代碼:
/// <summary> /// 開始捕捉 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { if (this.combDevice.SelectedIndex > -1) { StartCapture(this.combDevice.SelectedIndex); this.btnStart.Enabled = false; this.btnStop.Enabled = true; } else { MessageBox.Show(this,"請選擇一個設(shè)備","提示",MessageBoxButtons.OK); } } /// <summary> /// 停止捕捉 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStop_Click(object sender, EventArgs e) { Shutdown(); this.btnStop.Enabled = false; this.btnStart.Enabled = true; } private void StartCapture(int itemIndex) { packetCount = 0; device = CaptureDeviceList.Instance[itemIndex]; packetStrings = new Queue<PacketWrapper>(); bs = new BindingSource(); dgvData.DataSource = bs; LastStatisticsOutput = DateTime.Now; // start the background thread backgroundThreadStop = false; backgroundThread = new Thread(BackgroundThread); backgroundThread.Start(); // setup background capture device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival); device.OnCaptureStopped += new CaptureStoppedEventHandler(device_OnCaptureStopped); device.Open(); // tcpdump filter to capture only TCP/IP packets string filter = "ip and tcp"; device.Filter = filter; // force an initial statistics update captureStatistics = device.Statistics; UpdateCaptureStatistics(); // start the background capture device.StartCapture(); btnStop.Enabled = true; } /// <summary> /// 設(shè)備接收事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void device_OnPacketArrival(object sender, CaptureEventArgs e) { // print out periodic statistics about this device var Now = DateTime.Now; var interval = Now - LastStatisticsOutput; if (interval > new TimeSpan(0, 0, 2)) { Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics); captureStatistics = e.Device.Statistics; statisticsUiNeedsUpdate = true; LastStatisticsOutput = Now; } lock (QueueLock) { PacketQueue.Add(e.Packet); } } /// <summary> /// 設(shè)備停止事件 /// </summary> /// <param name="sender"></param> /// <param name="status"></param> private void device_OnCaptureStopped(object sender, CaptureStoppedEventStatus status) { if (status != CaptureStoppedEventStatus.CompletedWithoutError) { MessageBox.Show("Error stopping capture", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void UpdateCaptureStatistics() { tlblStatistic.Text = string.Format("接收包: {0}, 丟棄包: {1}, 接口丟棄包: {2}", captureStatistics.ReceivedPackets,captureStatistics.DroppedPackets, captureStatistics.InterfaceDroppedPackets); }
看完了這篇文章,相信你對“C#如何利用SharpPcap實現(xiàn)網(wǎng)絡(luò)包捕獲”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。