当前位置:   article > 正文

C# .NET 使用第三方库SharpPcap进行抓包示例_c# sharppcap

c# sharppcap

        本文主要讲述使用C#库进行抓包的示例代码,其本人在网上搜索了很久,找到了很多关于使用SharpPcap库的示例,但均会报错,通过VS种 '管理NuGet程序包'安装好了SharpPcap及其依赖,但会因为版本及其内部代码变更而导致程序报错。后面我会将完整代码上传以供参考学习(如下载我所编写代码,可以保证其库文件及依赖版本正确,visual studio版本为Microsoft Visual Studio Community 2022 (64 位) - Current版本 17.9.1)。

代码部分

1.此为初始化及开始抓包部分示例

  1. private void MonitorBtn_Click(object sender, EventArgs e)
  2. {
  3. var devices = CaptureDeviceList.Instance; //初始化
  4. //devices[0].
  5. foreach (var dev in devices)
  6. {
  7. Console.WriteLine("{0}\n", dev.ToString()); // 选择网卡
  8. }
  9. device = LibPcapLiveDeviceList.Instance[1];
  10. if (MonitorBtn.Text == "开始监听")
  11. {
  12. device.Open(DeviceModes.Promiscuous);
  13. device.OnPacketArrival += Device_OnPacketArrival;
  14. device.StartCapture();
  15. MonitorAllBtn.Enabled = true;
  16. MonitorBtn.Text = "关闭监听";
  17. }
  18. else if(MonitorBtn.Text == "关闭监听")
  19. {
  20. device.Close();
  21. MonitorBtn.Text = "开始监听";
  22. }
  23. }

2.内容部分

  1. public void Device_OnPacketArrival(object sender, PacketCapture e)
  2. {
  3. rawPacket = e.GetPacket();
  4. //--------------------------------------------------------------------------
  5. var packet = PacketDotNet.Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
  6. var tcpPacket = packet.Extract<PacketDotNet.TcpPacket>();// 如果是TCP协议的数据包,则提取TcpPacket对象,否则为null
  7. var udpPacket = packet.Extract<PacketDotNet.UdpPacket>(); // 如果是UDP协议的数据包,则提取UdpPacket对象,否则为null
  8. //提取端口号
  9. if (tcpPacket != null)
  10. {
  11. ipPacket = (IPPacket)tcpPacket.ParentPacket;
  12. var sourcePort = tcpPacket.SourcePort; // 源端口号(TCP协议)
  13. destinationPort = tcpPacket.DestinationPort; // 目标端口号(TCP协议)
  14. // 提取校验和
  15. var checksum = tcpPacket.Checksum;
  16. //Console.WriteLine($"源端口: {sourcePort}, 目标端口: {destinationPort}");
  17. //Console.WriteLine("校验和: " + checksum);
  18. if (tcpPacket.PayloadData != null)
  19. {
  20. // 提取载荷
  21. payload = Encoding.ASCII.GetString(tcpPacket.PayloadData);
  22. }
  23. else
  24. {
  25. payload = "无可用有效载荷";
  26. }
  27. }
  28. else if (udpPacket != null)
  29. {
  30. ipPacket = (IPPacket)udpPacket.ParentPacket;
  31. var sourcePort = udpPacket.SourcePort; // 源端口号(UDP协议)
  32. destinationPort = udpPacket.DestinationPort; // 目标端口号(UDP协议)
  33. // 提取校验和
  34. var checksum = udpPacket.Checksum;
  35. //Console.WriteLine($"源端口: {sourcePort}, 目标端口: {destinationPort}");
  36. //Console.WriteLine("校验和: " + checksum);
  37. }
  38. if (ipPacket != null)
  39. {
  40. // 提取IP地址
  41. var sourceIpAddress = ipPacket.SourceAddress;
  42. var destinationIpAddress = ipPacket.DestinationAddress;
  43. // 提取协议类型
  44. var protocolType = ipPacket.Protocol;
  45. // 提取TTL
  46. var ttl = ipPacket.TimeToLive;
  47. // 提取MAC地址
  48. var ethernetPacket = packet.Extract<EthernetPacket>();
  49. var sourceMacAddress = ethernetPacket.SourceHardwareAddress;
  50. var destinationMacAddress = ethernetPacket.DestinationHardwareAddress;
  51. // 提取时间戳
  52. var timestamp = rawPacket.Timeval.Date;
  53. // 提取传输数据
  54. var data = rawPacket.Data;
  55. }
  56. //此为将抓取的数据包中的内容转换为ASCII码形式
  57. string sum1111 = "";
  58. if (tcpPacket != null && tcpPacket.PayloadData != null)
  59. {
  60. for (int i = 1; i <= tcpPacket.PayloadData.Length; i++)
  61. {
  62. if (tcpPacket.PayloadData[i - 1] >= 33 && tcpPacket.PayloadData[i - 1] <= 126)
  63. {
  64. sum1111 += Encoding.ASCII.GetString(new byte[1] { tcpPacket.PayloadData[i - 1] });
  65. }
  66. else
  67. {
  68. //sum1111 += tcpPacket.PayloadData[i - 1].ToString();
  69. sum1111 += ".";
  70. }
  71. }
  72. }
  73. else if (udpPacket != null && udpPacket.PayloadData != null)
  74. {
  75. for (int i = 1; i <= udpPacket.PayloadData.Length; i++)
  76. {
  77. if ((udpPacket.PayloadData[i - 1] >= 33 && udpPacket.PayloadData[i - 1] <= 126))
  78. {
  79. sum1111 += Encoding.ASCII.GetString(new byte[1] { udpPacket.PayloadData[i - 1] });
  80. }
  81. else
  82. {
  83. //sum1111 += tcpPacket.PayloadData[i - 1].ToString();
  84. sum1111 += ".";
  85. }
  86. }
  87. if (FindSubstring(sum1111, "http") != null || FindSubstring(sum1111, "www") != null || FindSubstring(sum1111, "com") != null || FindSubstring(sum1111, "cn") != null)
  88. {
  89. if(DataListGridView.Rows.Count != 0)
  90. {
  91. if(DataListGridView.CurrentRow.Index != -1)
  92. {
  93. rowsSele = DataListGridView.CurrentRow.Index;
  94. if (sourceIpAddress.ToString() == DataListGridView.Rows[DataListGridView.CurrentRow.Index].Cells[1].Value.ToString())
  95. {
  96. this.Invoke(new Action(() =>
  97. {
  98. textBox1.Text += "源IP地址: " + sourceIpAddress + "\r\n";
  99. textBox1.Text += "目标IP地址: " + destinationIpAddress + "\r\n";
  100. textBox1.Text += "目标MAC地址: " + destinationMacAddress + "\r\n";
  101. textBox1.Text += "目标端口: " + destinationPort.ToString() + "\r\n";
  102. textBox1.Text += "时间戳: " + timestamp + "\r\n";
  103. textBox1.Text += "传输数据: " + sum1111 + "\r\n";
  104. textBox1.Text += "----------------------------------------------------------" + "\r\n";
  105. textBox1.Text += "\r\n";
  106. textBox1.SelectionStart = textBox1.Text.Length;
  107. textBox1.ScrollToCaret();
  108. }));
  109. }
  110. }
  111. }
  112. }
  113. }

此为我所编写源代码    后面可以下载我所上传资源直接查看程序会更加的清晰明了

资源下载链接:https://download.csdn.net/download/m0_54169323/88881319?spm=1001.2014.3001.5501

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/487226
推荐阅读
相关标签
  

闽ICP备14008679号