本文是利用sharppcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享。
什么是sharppcap?
sharppcap 是一个.net 环境下的网络包捕获框架,基于著名的 pcap/winpcap 库开发。提供了捕获、注入、分析和构建的功能,适用于 c# 和 vb net 开发语言。
sharppcap有两部分组成:1> sharppcap.dll 负责数据的捕获 2> packetdotnet.dll负责数据包的解析
思路:
通过进程名字获取对应的端口号。
sharppcap获取对应的数据包,通过解析数据包过滤相关的端口。
涉及知识点:
process 获取相关进程信息。
netstat命令:netstat -ano|find "3844" 获取进程对应的端口
sharppcap相关信息:
通过capturedevicelist的静态方法获取设备列表。
通过onpacketarrival事件接收数据包。
通过packetdotnet来解析数据包
效果图下:
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 , "请选择一个设备" , "提示" ,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> /// 设备接收事件 /// </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> /// 设备停止事件 /// </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); } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/hsiang/p/7696347.html