[关闭]
@xunuo 2017-08-11T10:37:59.000000Z 字数 2527 阅读 1455

c#分析http协议


网络数据包分析


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SharpPcap;
  7. using SharpPcap.LibPcap;
  8. using PacketDotNet;
  9. using TwzyProtocol;
  10. namespace practice1
  11. {
  12. class Program
  13. {
  14. //static int vis = 0;
  15. static string stringdata;
  16. static void Main(string[] args)
  17. {
  18. //获取网络设备
  19. var device = CaptureDeviceList.Instance;
  20. if (device.Count < 1)
  21. {
  22. Console.WriteLine("暂无可用网络设备!\n");
  23. return;
  24. }
  25. int i = 0;
  26. foreach (ICaptureDevice cap in device)
  27. {
  28. Console.WriteLine("{0},{1},{2}", i, cap.Name, cap.Description);//打印所有可用网络设备;
  29. i++;
  30. }
  31. //选择为要用的设备:
  32. Console.WriteLine("请选择要使用的网络设备:");
  33. int j = int.Parse(Console.ReadLine());
  34. if (j > i || j < 0)
  35. {
  36. Console.WriteLine("该设备不存在!\n");
  37. return;
  38. }
  39. ICaptureDevice dev = device[j];
  40. //string filter = "ip and tcp";
  41. dev.OnPacketArrival += new PacketArrivalEventHandler(dev_OnPacketArrival);
  42. dev.Open(DeviceMode.Normal, 1000);
  43. dev.Filter = "port 80";
  44. dev.StartCapture();
  45. }
  46. public static string ToHexString(byte[] bytes) // byte[]转16进制string
  47. {
  48. string hexString = string.Empty;
  49. if (bytes != null)
  50. {
  51. StringBuilder strB = new StringBuilder();
  52. for (int i = 0; i < bytes.Length; i++)
  53. {
  54. strB.Append(bytes[i].ToString("X2"));
  55. }
  56. hexString = strB.ToString();
  57. }
  58. return hexString;
  59. }
  60. static void dev_OnPacketArrival(object sender, CaptureEventArgs e)
  61. {
  62. // packetdata = e.Packet.Data;
  63. stringdata = ToHexString(e.Packet.Data);
  64. // Console.WriteLine(ToHexString(packetdata));
  65. if (e.Packet.LinkLayerType == LinkLayers.Ethernet)
  66. {
  67. var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
  68. Ethernet(packet);
  69. }
  70. }
  71. static private void Ethernet(Packet packet)
  72. {
  73. var Ethernetpacket = PacketDotNet.EthernetPacket.GetEncapsulated(packet);
  74. if (Ethernetpacket.Type == EthernetPacketType.IpV4 || Ethernetpacket.Type == EthernetPacketType.IpV6)
  75. {
  76. IpPacket ip = IpPacket.GetEncapsulated(packet);
  77. if(ip.Protocol==IPProtocolType.TCP)
  78. {
  79. TcpPacket tcp = TcpPacket.GetEncapsulated(packet);
  80. if(tcp.SourcePort==80||tcp.DestinationPort==80)
  81. {
  82. if (tcp.PayloadData.Length == 0)
  83. return;
  84. HttpPacket http = new HttpPacket(tcp.PayloadData);
  85. Class1.Http(http);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SharpPcap;
  7. using SharpPcap.LibPcap;
  8. using PacketDotNet;
  9. using TwzyProtocol;
  10. namespace practice1
  11. {
  12. class Class1
  13. {
  14. static char[] ReplaceCharArry =
  15. {
  16. '\0','\a','\b','\f','\t','\v','?'
  17. };//将字符表示为UTF-16代码单位;
  18. static public void Http(HttpPacket http)
  19. {
  20. Console.WriteLine("Data Length:" + http.DataLength);
  21. List<CommandTypeHead> httplist = http.CreatHeadList();
  22. if (httplist.Count == 0)
  23. return;
  24. foreach (var i in httplist)
  25. {
  26. string ilist = i.Content;
  27. foreach (var j in ReplaceCharArry)
  28. {
  29. ilist = ilist.Replace(j, '.');
  30. }
  31. Console.WriteLine(ilist);
  32. }
  33. }
  34. }
  35. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注