Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using SharpPcap;
- namespace Example6
- {
- public class Program
- {
- public static void Main()
- {
- var ver = Pcap.SharpPcapVersion;
- /* Print SharpPcap version */
- Console.WriteLine("SharpPcap {0}, Example6.DumpTCP.cs", ver);
- Console.WriteLine();
- /* Retrieve the device list */
- var devices = CaptureDeviceList.Instance;
- /*If no device exists, print error */
- if (devices.Count < 1)
- {
- Console.WriteLine("No device found on this machine");
- return;
- }
- Console.WriteLine("The following devices are available on this machine:");
- Console.WriteLine("----------------------------------------------------");
- Console.WriteLine();
- int i = 0;
- /* Scan the list printing every entry */
- foreach (var dev in devices)
- {
- /* Description */
- Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
- i++;
- }
- Console.WriteLine();
- Console.Write("-- Please choose a device to capture: ");
- i = int.Parse(Console.ReadLine());
- using var device = devices[i];
- //Register our handler function to the 'packet arrival' event
- device.OnPacketArrival +=
- new PacketArrivalEventHandler(device_OnPacketArrival);
- // Open the device for capturing
- int readTimeoutMilliseconds = 1000;
- device.Open(DeviceModes.Promiscuous, readTimeoutMilliseconds);
- //tcpdump filter to capture only TCP/IP packets
- string filter = "(ip and udp) or (ip and icmp)";
- device.Filter = filter;
- Console.WriteLine();
- Console.WriteLine
- ("-- The following tcpdump filter will be applied: \"{0}\"",
- filter);
- Console.WriteLine
- ("-- Listening on {0}, hit 'Ctrl-C' to exit...",
- device.Description);
- // Start capture 'INFINTE' number of packets
- device.Capture();
- }
- /// <summary>
- /// Prints the time, length, src ip, src port, dst ip and dst port
- /// for each TCP/IP packet received on the network
- /// </summary>
- private static void device_OnPacketArrival(object sender, PacketCapture e)
- {
- var time = e.Header.Timeval.Date;
- var len = e.Data.Length;
- var rawPacket = e.GetPacket();
- var packet = PacketDotNet.Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
- var udpPacket = packet.Extract<PacketDotNet.UdpPacket>();
- if (udpPacket != null)
- {
- var ipPacket = (PacketDotNet.IPPacket)udpPacket.ParentPacket;
- System.Net.IPAddress srcIp = ipPacket.SourceAddress;
- System.Net.IPAddress dstIp = ipPacket.DestinationAddress;
- int srcPort = udpPacket.SourcePort;
- int dstPort = udpPacket.DestinationPort;
- Console.WriteLine("{0}:{1} -> {2}:{3}", srcIp, srcPort, dstIp, dstPort);
- }
- var icmpPacket = packet.Extract<PacketDotNet.IcmpV4Packet>();
- if (icmpPacket != null)
- {
- var ipPacket = (PacketDotNet.IPPacket)icmpPacket.ParentPacket;
- System.Net.IPAddress srcIp = ipPacket.SourceAddress;
- System.Net.IPAddress dstIp = ipPacket.DestinationAddress;
- Console.WriteLine("ICMP {0} -> {1} {2}", srcIp,dstIp, icmpPacket.Id);
- Console.WriteLine(icmpPacket.TypeCode);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement