Advertisement
Zhobra

Untitled

Dec 30th, 2022
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | Source Code | 0 0
  1. using System;
  2. using SharpPcap;
  3.  
  4. namespace Example6
  5. {
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             var ver = Pcap.SharpPcapVersion;
  11.             /* Print SharpPcap version */
  12.             Console.WriteLine("SharpPcap {0}, Example6.DumpTCP.cs", ver);
  13.             Console.WriteLine();
  14.  
  15.             /* Retrieve the device list */
  16.             var devices = CaptureDeviceList.Instance;
  17.  
  18.             /*If no device exists, print error */
  19.             if (devices.Count < 1)
  20.             {
  21.                 Console.WriteLine("No device found on this machine");
  22.                 return;
  23.             }
  24.  
  25.             Console.WriteLine("The following devices are available on this machine:");
  26.             Console.WriteLine("----------------------------------------------------");
  27.             Console.WriteLine();
  28.  
  29.             int i = 0;
  30.  
  31.             /* Scan the list printing every entry */
  32.             foreach (var dev in devices)
  33.             {
  34.                 /* Description */
  35.                 Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
  36.                 i++;
  37.             }
  38.  
  39.             Console.WriteLine();
  40.             Console.Write("-- Please choose a device to capture: ");
  41.             i = int.Parse(Console.ReadLine());
  42.  
  43.             using var device = devices[i];
  44.  
  45.             //Register our handler function to the 'packet arrival' event
  46.             device.OnPacketArrival +=
  47.                 new PacketArrivalEventHandler(device_OnPacketArrival);
  48.  
  49.             // Open the device for capturing
  50.             int readTimeoutMilliseconds = 1000;
  51.             device.Open(DeviceModes.Promiscuous, readTimeoutMilliseconds);
  52.  
  53.             //tcpdump filter to capture only TCP/IP packets
  54.             string filter = "(ip and udp) or (ip and icmp)";
  55.             device.Filter = filter;
  56.  
  57.             Console.WriteLine();
  58.             Console.WriteLine
  59.                 ("-- The following tcpdump filter will be applied: \"{0}\"",
  60.                 filter);
  61.             Console.WriteLine
  62.                 ("-- Listening on {0}, hit 'Ctrl-C' to exit...",
  63.                 device.Description);
  64.  
  65.             // Start capture 'INFINTE' number of packets
  66.             device.Capture();
  67.  
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Prints the time, length, src ip, src port, dst ip and dst port
  72.         /// for each TCP/IP packet received on the network
  73.         /// </summary>
  74.         private static void device_OnPacketArrival(object sender, PacketCapture e)
  75.         {
  76.             var time = e.Header.Timeval.Date;
  77.             var len = e.Data.Length;
  78.             var rawPacket = e.GetPacket();
  79.  
  80.             var packet = PacketDotNet.Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
  81.  
  82.             var udpPacket = packet.Extract<PacketDotNet.UdpPacket>();
  83.             if (udpPacket != null)
  84.             {
  85.                 var ipPacket = (PacketDotNet.IPPacket)udpPacket.ParentPacket;
  86.                 System.Net.IPAddress srcIp = ipPacket.SourceAddress;
  87.                 System.Net.IPAddress dstIp = ipPacket.DestinationAddress;
  88.                 int srcPort = udpPacket.SourcePort;
  89.                 int dstPort = udpPacket.DestinationPort;
  90.  
  91.                 Console.WriteLine("{0}:{1} -> {2}:{3}", srcIp, srcPort, dstIp, dstPort);
  92.  
  93.             }
  94.  
  95.             var icmpPacket = packet.Extract<PacketDotNet.IcmpV4Packet>();
  96.             if (icmpPacket != null)
  97.             {
  98.                 var ipPacket = (PacketDotNet.IPPacket)icmpPacket.ParentPacket;
  99.                 System.Net.IPAddress srcIp = ipPacket.SourceAddress;
  100.                 System.Net.IPAddress dstIp = ipPacket.DestinationAddress;
  101.                 Console.WriteLine("ICMP {0} -> {1} {2}", srcIp,dstIp, icmpPacket.Id);
  102.                 Console.WriteLine(icmpPacket.TypeCode);
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement