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);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#設(shè)置本地網(wǎng)絡(luò)如DNS、網(wǎng)關(guān)、子網(wǎng)掩碼、IP等等
- C# 網(wǎng)絡(luò)編程之UDP
- c# 網(wǎng)絡(luò)編程之tcp
- c# 網(wǎng)絡(luò)編程之http
- C# 操作網(wǎng)絡(luò)適配器的示例
- 深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(下)
- 深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(上)
- C#訪問網(wǎng)絡(luò)共享文件夾的方法
- 淺談C#網(wǎng)絡(luò)編程詳解篇
- c# 如何對(duì)網(wǎng)絡(luò)信息進(jìn)行相關(guān)設(shè)置(ip,dns,網(wǎng)關(guān)等)
相關(guān)文章
C# 9 新特性——record的相關(guān)總結(jié)
這篇文章主要介紹了C# 9 新特性——record的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c# 9的新特性,感興趣的朋友可以了解下2021-02-02
C# 控件屬性和InitializeComponent()關(guān)系案例詳解
這篇文章主要介紹了C# 控件屬性和InitializeComponent()關(guān)系案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C#利用FluentFTP實(shí)現(xiàn)FTP上傳下載功能詳解
FTP作為日常工作學(xué)習(xí)中,非常重要的一個(gè)文件傳輸存儲(chǔ)空間,想必大家都非常的熟悉了,那么如何快速的實(shí)現(xiàn)文件的上傳下載功能呢,本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過FluentFTP實(shí)現(xiàn)文件的上傳和下載功能2023-02-02
WPF實(shí)現(xiàn)自定義Panel面板的示例詳解
WPF中的Panel(面板),是繼承自FrameworkElement的抽象類,表示一個(gè)可以用來排列子元素的面板,本文主要來和大家聊聊WPF如何實(shí)現(xiàn)自定義Panel,感興趣的可以了解下2023-09-09
C#中使用CAS實(shí)現(xiàn)無鎖算法的示例詳解
CAS(Compare-and-Swap)是一種多線程并發(fā)編程中常用的原子操作,用于實(shí)現(xiàn)多線程間的同步和互斥訪問。本文將利用CAS實(shí)現(xiàn)無鎖算法,需要的可以參考一下2023-04-04
C#線程委托BeginInvoke與EndInvoke的用法
這篇文章介紹了C#線程委托BeginInvoke與EndInvoke的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C# 繪制統(tǒng)計(jì)圖大全(柱狀圖, 折線圖, 扇形圖)
本篇文章介紹了C# 繪制統(tǒng)計(jì)圖大全,其中包括狀圖, 折線圖, 扇形圖,有需要的同學(xué)可以了解一下。2016-11-11

