C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探
本文是利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包的捕獲的小例子,實(shí)現(xiàn)了端口監(jiān)控,數(shù)據(jù)包捕獲等功能,主要用于學(xué)習(xí)分享。
什么是SharpPcap?
SharpPcap 是一個(gè).NET 環(huán)境下的網(wǎng)絡(luò)包捕獲框架,基于著名的 pcap/WinPcap 庫開發(fā)。提供了捕獲、注入、分析和構(gòu)建的功能,適用于 C# 和 VB NET 開發(fā)語言。
SharpPcap有兩部分組成:1> SharpPcap.dll 負(fù)責(zé)數(shù)據(jù)的捕獲 2> PacketDotNet.dll負(fù)責(zé)數(shù)據(jù)包的解析
思路:
通過進(jìn)程名字獲取對(duì)應(yīng)的端口號(hào)。
SharpPcap獲取對(duì)應(yīng)的數(shù)據(jù)包,通過解析數(shù)據(jù)包過濾相關(guān)的端口。
涉及知識(shí)點(diǎn):
Process 獲取相關(guān)進(jìn)程信息。
netstat命令:netstat -ano|find "3844" 獲取進(jìn)程對(duì)應(yīng)的端口
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,"請(qǐng)選擇一個(gè)設(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); }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C# 在項(xiàng)目中引用x86 x64的非托管代碼的方法
本文標(biāo)題:C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5249.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery