激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#利用SharpPcap實現(xiàn)網(wǎng)絡(luò)包捕獲嗅探

C#利用SharpPcap實現(xiàn)網(wǎng)絡(luò)包捕獲嗅探

2022-02-21 14:42Alan.hsiang C#

這篇文章主要為大家詳細(xì)介紹了C#利用SharpPcap實現(xiàn)網(wǎng)絡(luò)包捕獲嗅探,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文是利用sharppcap實現(xiàn)網(wǎng)絡(luò)包的捕獲的小例子,實現(xiàn)了端口監(jiān)控,數(shù)據(jù)包捕獲等功能,主要用于學(xué)習(xí)分享。

什么是sharppcap?

sharppcap 是一個.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ù)包的解析

思路:

通過進程名字獲取對應(yīng)的端口號。
sharppcap獲取對應(yīng)的數(shù)據(jù)包,通過解析數(shù)據(jù)包過濾相關(guān)的端口。

涉及知識點:

process 獲取相關(guān)進程信息。
netstat命令:netstat -ano|find "3844" 獲取進程對應(yīng)的端口
sharppcap相關(guān)信息:

       通過capturedevicelist的靜態(tài)方法獲取設(shè)備列表。
       通過onpacketarrival事件接收數(shù)據(jù)包。
       通過packetdotnet來解析數(shù)據(jù)包

效果圖下:

C#利用SharpPcap實現(xiàn)網(wǎng)絡(luò)包捕獲嗅探

sharppcap核心代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/// <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);
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/hsiang/p/7696347.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄色免费在线网站 | 日韩黄在线观看 | 久久精品亚洲精品国产欧美kt∨ | 欧美三日本三级少妇三级99观看视频 | 99riav国产在线观看 | 激情综合婷婷久久 | 免费黄色小视频网站 | julieann艳星激情办公室 | 精品亚洲一 | 影视免费观看 | 91www成人久久 | 成人免费毛片在线观看 | 中文字幕在线视频网站 | 亚洲精品免费播放 | 看一级毛片 | 4p一女两男做爰在线观看 | 亚洲成人在线免费 | 欧美午夜网 | 一级毛片真人免费播放视频 | 久久久久久99| 黄色网址免费在线 | 精品一二三区视频 | 毛片一级片 | 在线播放免费播放av片 | 美国黄色毛片女人性生活片 | 国产精品久久久久影院老司 | 亚洲成人福利在线观看 | 美女被免费网站在线软件 | 狠狠干b | 曰韩一二三区 | 宅男噜噜噜66国产免费观看 | 天堂二区 | 久久精品中文字幕一区二区三区 | 黄色片网页 | 国产精品美女久久久免费 | 久久精品成人免费国产片桃视频 | 日本在线看片 | 91高清完整版在线观看 | 99成人精品视频 | 国产流白浆高潮在线观看 | 欧美精品一区二区免费 |