/// <summary> /// Prints the time and length of each received packet /// </summary> private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { DateTime time = e.Packet.PcapHeader.Date; uint len = e.Packet.PcapHeader.PacketLength; Console.WriteLine("{0}:{1}:{2},{3} Len={4}", time.Hour, time.Minute, time.Second, time.Millisecond, len); }
private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { if (e.Packet is EthernetPacket) { EthernetPacket eth = ((EthernetPacket)e.Packet); Console.WriteLine("Original Eth packet: " + eth.ToColoredString(false)); //Manipulate ethernet parameters eth.SourceHwAddress = PhysicalAddress.Parse("00-11-22-33-44-55"); eth.DestinationHwAddress = PhysicalAddress.Parse("00-99-88-77-66-55"); if (e.Packet is IPPacket) { IPPacket ip = ((IPPacket)e.Packet); Console.WriteLine("Original IP packet: " + ip.ToColoredString(false)); //manipulate IP parameters ip.SourceAddress = System.Net.IPAddress.Parse("1.2.3.4"); ip.DestinationAddress = System.Net.IPAddress.Parse("44.33.22.11"); ip.TimeToLive = 11; //Recalculate the IP checksum ip.ComputeIPChecksum(); if (ip is TCPPacket) { TCPPacket tcp = ((TCPPacket)ip); Console.WriteLine("Original TCP packet: " + tcp.ToColoredString(false)); //manipulate TCP parameters tcp.SourcePort = 9999; tcp.DestinationPort = 8888; tcp.Syn = !tcp.Syn; tcp.Fin = !tcp.Fin; tcp.Ack = !tcp.Ack; tcp.WindowSize = 500; tcp.AcknowledgmentNumber = 800; tcp.SequenceNumber = 800; //Recalculate the TCP checksum tcp.ComputeTCPChecksum(); } if (ip is UDPPacket) { UDPPacket udp = ((UDPPacket)ip); Console.WriteLine("Original UDP packet: " + udp.ToColoredString(false)); //manipulate UDP parameters udp.SourcePort = 9999; udp.DestinationPort = 8888; //Recalculate the UDP checksum udp.ComputeUDPChecksum(); } } Console.WriteLine("Manipulated Eth packet: " + eth.ToColoredString(false)); } }
/// <summary> /// Prints the time and length of each received packet /// </summary> private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { DateTime time = e.Packet.PcapHeader.Date; uint len = e.Packet.PcapHeader.PacketLength; Console.WriteLine("{0}:{1}:{2},{3} Len={4}", time.Hour, time.Minute, time.Second, time.Millisecond, len); }
/// <summary> /// Prints the source and dest MAC addresses of each received Ethernet frame /// </summary> private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { if (e.Packet is EthernetPacket) { EthernetPacket etherFrame = (EthernetPacket)e.Packet; Console.WriteLine("At: {0}:{1}: MAC:{2} -> MAC:{3}", etherFrame.PcapHeader.Date.ToString(), etherFrame.PcapHeader.Date.Millisecond, etherFrame.SourceHwAddress, etherFrame.DestinationHwAddress); } }
/// <summary> /// Dumps each received packet to a pcap file /// </summary> private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { PcapDevice device = (PcapDevice)sender; //if device has a dump file opened if (device.DumpOpened) { //dump the packet to the file device.Dump(e.Packet); Console.WriteLine("Packet dumped to file."); } }
/// <summary> /// Prints the source and dest MAC addresses of each received Ethernet frame /// </summary> private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { if( e.Packet is EthernetPacket ) { EthernetPacket etherFrame = (EthernetPacket)e.Packet; Console.WriteLine("At: {0}:{1}: MAC:{2} -> MAC:{3}", etherFrame.PcapHeader.Date.ToString(), etherFrame.PcapHeader.Date.Millisecond, etherFrame.SourceHwAddress, etherFrame.DestinationHwAddress); } }
/// <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_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { if (e.Packet is TCPPacket) { DateTime time = e.Packet.PcapHeader.Date; uint len = e.Packet.PcapHeader.PacketLength; TCPPacket tcp = (TCPPacket)e.Packet; System.Net.IPAddress srcIp = tcp.SourceAddress; System.Net.IPAddress dstIp = tcp.DestinationAddress; int srcPort = tcp.SourcePort; int dstPort = tcp.DestinationPort; Console.WriteLine("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}", time.Hour, time.Minute, time.Second, time.Millisecond, len, srcIp, srcPort, dstIp, dstPort); } }
/// <summary> /// Dumps each received packet to a pcap file /// </summary> private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { PcapDevice device = (PcapDevice)sender; //if device has a dump file opened if( device.DumpOpened ) { //dump the packet to the file device.Dump( e.Packet ); Console.WriteLine("Packet dumped to file."); } }
/// <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_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e) { if(e.Packet is TCPPacket) { DateTime time = e.Packet.PcapHeader.Date; uint len = e.Packet.PcapHeader.PacketLength; TCPPacket tcp = (TCPPacket)e.Packet; System.Net.IPAddress srcIp = tcp.SourceAddress; System.Net.IPAddress dstIp = tcp.DestinationAddress; int srcPort = tcp.SourcePort; int dstPort = tcp.DestinationPort; Console.WriteLine("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}", time.Hour, time.Minute, time.Second, time.Millisecond, len, srcIp, srcPort, dstIp, dstPort); } }
private void device_OnPacketArrival(object sender, PcapCaptureEventArgs e) { DisplayPacketData(e.Packet); }
// This handles every incoming packet private void PacketHandler(object sender, PcapCaptureEventArgs e) { }