public void ReceivePackets() { // Retrieve the device list from the local machine IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; // Find the NPF device of the TAP interface PacketDevice selectedDevice = null; for (int i = 0; i != allDevices.Count; ++i) { LivePacketDevice device = allDevices[i]; if (device.Name.ToUpper().Contains(Guid.ToString().ToUpper())) { selectedDevice = device; break; } } if (selectedDevice == null) { Initialized.Set(); Global.ShowTrayTip("Load Balancer", "Interface " + Name + " not captured by WinPcap.", System.Windows.Forms.ToolTipIcon.Warning); Global.WriteLog("Load Balancer: Interface " + Name + " not captured by WinPcap."); return; } try { using (communicator = selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 1000)) { Global.WriteLog("Load Balancer: Listening on " + Name + "..."); communicator.SetFilter("(ether dst " + ownHardwareAddressString + " and ((ip and (tcp or udp or icmp)) or arp)) or (ether dst FF:FF:FF:FF:FF:FF and arp)"); Initialized.Set(); communicator.ReceivePackets(0, (packet) => { if (!ThreadActive.IsSet) { communicator.Break(); return; } if (packet.Ethernet.EtherType == EthernetType.Arp) { if (packet.Ethernet.Arp.TargetProtocolAddress.SequenceEqual(ownProtocolAddressByte) && packet.Ethernet.Arp.Operation == ArpOperation.Request) { EthernetLayer ethernetLayer = new EthernetLayer { Source = ownHardwareAddress, Destination = packet.Ethernet.Source, EtherType = EthernetType.None, }; ArpLayer arpLayer = new ArpLayer { ProtocolType = EthernetType.IpV4, Operation = ArpOperation.Reply, SenderHardwareAddress = ownHardwareAddressByte.AsReadOnly(), SenderProtocolAddress = packet.Ethernet.Arp.TargetProtocolAddress, TargetHardwareAddress = packet.Ethernet.Arp.SenderHardwareAddress, TargetProtocolAddress = packet.Ethernet.Arp.SenderProtocolAddress, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer); SendPacket(builder.Build(DateTime.Now)); } } else if (packet.Ethernet.EtherType.ToString() == "IpV4") { if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.DoNotFragment || packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset == 0) { IpV4Handler(packet); } else if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.MoreFragments || packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset > 0) { // TODO: fix warning spam if (Global.Config.LoadBalancer.ShowTrayTipsWarnings) Global.ShowTrayTip("Load Balancer", "IP fragmentation detected on " + Name + ".\n\nIP fragmentation is not supported.", ToolTipIcon.Warning); Global.WriteLog("Load Balancer: IP fragmentation detected on " + Name); //fragments = fragBuffer.Add(packet); //if (fragments != null) // for (int i = 0; i < fragments.Count; i++) // { // IpV4Handler(fragments[i], fragments); // } } } }); } } catch (Exception e) { if (ThreadActive.IsSet) { ThreadActive.Reset(); Global.WriteLog("Load Balancer: " + Name + " has disconnected"); if (Global.Config.Gadget.Debug) Global.WriteLog(e.ToString()); Global.ShowTrayTip("Load Balancer", Name + " has disconnected", System.Windows.Forms.ToolTipIcon.Warning); } } }
/// <summary> /// This function build an ARP over Ethernet packet. /// </summary> public Packet BuildArpPacket( byte[] destIP ) { if ( ethernetLayer == null && arpLayer == null ) { throw new InvalidOperationException( "Trying to build an ARP packet while BuildingArpPacket() wasn't invoked first." ); } arpLayer.TargetProtocolAddress = Array.AsReadOnly( destIP ); PacketBuilder builder = new PacketBuilder( ethernetLayer, arpLayer ); return builder.Build( DateTime.Now ); }
//# Método de pcapDotNet, construcción de paquete IP, UDP, Ethernet y finalmente payload: private Packet BuildUdpPacket(byte[] bytes_a_enviar) { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(this.mac_origen), Destination = new MacAddress(this.mac_destino), EtherType = EthernetType.IpV4, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(this.ip_origen), CurrentDestination = new IpV4Address(this.ip_destino), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, Identification = 123, Options = IpV4Options.None, Protocol = null, Ttl = 100, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = (ushort)this.puerto_origen, DestinationPort = (ushort)this.puerto_destino, Checksum = null, CalculateChecksumValue = true, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(bytes_a_enviar), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); return builder.Build(DateTime.Now); }
public void SendSynAck(Guid guid) { EthernetLayer ethernetLayer = new EthernetLayer { Source = ownHardwareAddress, Destination = ifHardwareAddress, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = RoutingTable.connParams[guid].remoteIp, CurrentDestination = ifProtocolAddress, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = ipID, Options = IpV4Options.None, Protocol = IpV4Protocol.Tcp, Ttl = 128, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = RoutingTable.connParams[guid].remotePort, DestinationPort = RoutingTable.connParams[guid].localPort, Checksum = null, // Will be filled automatically. SequenceNumber = RoutingTable.connParams[guid].seq, AcknowledgmentNumber = RoutingTable.connParams[guid].ack, ControlBits = TcpControlBits.Synchronize | TcpControlBits.Acknowledgment, Window = RoutingTable.connParams[guid].GetWindow(), UrgentPointer = 0, Options = new TcpOptions(new TcpOptionMaximumSegmentSize((ushort)MSS), new TcpOptionWindowScale(RoutingTable.connParams[guid].windowScale)), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer); SendPacket(builder.Build(DateTime.Now)); }
private static void DiscoverNetworkBroadcast(PacketCommunicator communicator, MyDevice device) { // Supposing to be on ethernet, set mac source MacAddress source = new MacAddress(device.MacAddressWithDots()); // set mac destination to broadcast MacAddress destination = new MacAddress("FF:FF:FF:FF:FF:FF"); // Create the packets layers // Ethernet Layer EthernetLayer ethernetLayer = new EthernetLayer { Source = source, Destination = destination }; // IPv4 Layer IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(device.IPAddress), Ttl = 128, // The rest of the important parameters will be set for each packet }; // ICMP Layer IcmpEchoLayer icmpLayer = new IcmpEchoLayer(); // Create the builder that will build our packets PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); string ipBeg = device.IpWithoutEnd(); //Send 100 Pings to different destination with different parameters for (int i = 0; i < 256; i++) { // Set IPv4 parameters ipV4Layer.CurrentDestination = new IpV4Address(ipBeg + i); ipV4Layer.Identification = (ushort)i; // Set ICMP parameters icmpLayer.SequenceNumber = (ushort)i; icmpLayer.Identifier = (ushort)i; // Build the packet Packet packet = builder.Build(DateTime.Now); // Send down the packet communicator.SendPacket(packet); //Console.WriteLine("172.16.1." + i); } }
private void buttonSend00_Click(object sender, EventArgs e) { PacketDevice senderDevice = LivePacketDevice.AllLocalMachine[comboInterface.SelectedIndex]; using (PacketCommunicator communicator = senderDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { for (int j = 0; j < 1; j++) { MacAddress sourceMac = new MacAddress(textSourceMac.Text); MacAddress destinationMac = new MacAddress(textDestinationMac.Text); EthernetLayer ethernetLayer = new EthernetLayer { Source = sourceMac, Destination = destinationMac }; IpV4Layer ipv4Layer = new IpV4Layer { Source = new IpV4Address(textSourceIP.Text), CurrentDestination = new IpV4Address(textDestinationIP.Text), HeaderChecksum = null, Identification = 123, Options = IpV4Options.None, Protocol = null, Ttl = 100, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = (ushort)Convert.ToUInt16(textSourcePort.Text), DestinationPort = (ushort)Convert.ToUInt16(textDestinationPort.Text), Checksum = null, CalculateChecksumValue = true, }; byte[] buff = new byte[128]; // goo size //byte[] buff = new byte[192]; // monlist buff[0] = 0x17; buff[1] = 0x00; buff[2] = 0x03; // monlist buff[3] = 0x2a; // memstats // buff[3] = 0x07; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(buff), // Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipv4Layer, udpLayer, payloadLayer); communicator.SendPacket(builder.Build(DateTime.Now)); } } }
public Packet GetBuilder() { builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); return ICMPpacket = builder.Build(DateTime.Now); }
/// <summary> /// This function build an ARP over Ethernet packet. /// </summary> private static Packet BuildArpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; ArpLayer arpLayer = new ArpLayer { ProtocolType = EthernetType.IpV4, Operation = ArpOperation.Request, SenderHardwareAddress = new byte[] {3, 3, 3, 3, 3, 3}.AsReadOnly(), // 03:03:03:03:03:03. SenderProtocolAddress = new byte[] {1, 2, 3, 4}.AsReadOnly(), // 1.2.3.4. TargetHardwareAddress = new byte[] {4, 4, 4, 4, 4, 4}.AsReadOnly(), // 04:04:04:04:04:04. TargetProtocolAddress = new byte[] {11, 22, 33, 44}.AsReadOnly(), // 11.22.33.44. }; PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer); return builder.Build(DateTime.Now); }
/// <summary> /// This function build a VLanTaggedFrame over Ethernet with payload packet. /// </summary> private static Packet BuildVLanTaggedFramePacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; VLanTaggedFrameLayer vLanTaggedFrameLayer = new VLanTaggedFrameLayer { PriorityCodePoint = ClassOfService.Background, CanonicalFormatIndicator = false, VLanIdentifier = 50, EtherType = EthernetType.IpV4, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, vLanTaggedFrameLayer, payloadLayer); return builder.Build(DateTime.Now); }
public Packet GetBuilder() { builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer); return DNSpacket = builder.Build(DateTime.Now); }
/// <summary> /// This function build an Ethernet with payload packet. /// </summary> private static Packet BuildEthernetPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.IpV4, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, payloadLayer); return builder.Build(DateTime.Now); }
public Packet GetBuilder() { builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); return TCPpacket = builder.Build(DateTime.Now); }
private Packet GeneratePacket(Tuple<MacAddress, IpV4Address> senderEntry) { _ethernetLayer.Source = senderEntry.Item1; _ethernetLayer.Destination = new MacAddress((UInt48)_random.Next()); _ipV4Layer.Source = senderEntry.Item2; _ipV4Layer.CurrentDestination = new IpV4Address((uint)_random.Next()); PacketBuilder builder = new PacketBuilder(_ethernetLayer, _ipV4Layer, _tcpLayer, _payloadLayer); return builder.Build(DateTime.Now); }
public static void WakeDestination(string macAddress) { IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; if (allDevices.Count == 0) { throw new Exception("No interfaces found! Make sure WinPcap is installed."); } foreach (var selectedDevice in allDevices) { using (var communicator = selectedDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { var ipV4SocketAddresses = from d in selectedDevice.Addresses where d.Address is IpV4SocketAddress select new { ((IpV4SocketAddress)d.Address).Address, Broadcast = ((IpV4SocketAddress)d.Broadcast).Address }; foreach (var deviceAddress in ipV4SocketAddresses) { var ethernetLayer = new EthernetLayer { Source = selectedDevice.GetMacAddress(), Destination = new MacAddress(macAddress), EtherType = EthernetType.None // Will be filled automatically. }; var ipV4Layer = new IpV4Layer { Source = deviceAddress.Address, CurrentDestination = deviceAddress.Broadcast, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0 }; var udpLayer = new UdpLayer { DestinationPort = 7, Checksum = null, // Will be filled automatically. CalculateChecksumValue = true }; var payloadLayer = new PayloadLayer { Data = new Datagram(BuildWolPacketBody(macAddress)) }; var builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); var packet = builder.Build(DateTime.Now); communicator.SendPacket(packet); } } } }
static void msearch_response_spoof(LivePacketDevice selectedDevice, string msearch_string, string sourceIP, ushort sourcePort, string destIP, ushort destPort, string destMac) { byte[] temp = System.Text.Encoding.ASCII.GetBytes(msearch_string); EthernetLayer ethernetLayer = new EthernetLayer { Source = LivePacketDeviceExtensions.GetMacAddress(selectedDevice), Destination = new MacAddress(destMac), EtherType = EthernetType.None, }; var options = IpV4FragmentationOptions.DoNotFragment; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(sourceIP), CurrentDestination = new IpV4Address(destIP), Fragmentation = new IpV4Fragmentation(options,0), HeaderChecksum = null, Identification = 0, Options = IpV4Options.None, Protocol = null, Ttl = 64, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = sourcePort, DestinationPort = destPort, Checksum = null, CalculateChecksumValue = true, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(temp), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); using (PacketCommunicator communicator = selectedDevice.Open(69559, PacketDeviceOpenAttributes.Promiscuous, 1000)) // read timeout { communicator.SendPacket(builder.Build(DateTime.Now)); } }
/// <summary> /// This function build an IPv6 over Ethernet with payload packet. /// </summary> private static Packet BuildIpV6Packet() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, }; IpV6Layer ipV6Layer = new IpV6Layer { Source = new IpV6Address("0123:4567:89AB:CDEF:0123:4567:89AB:CDEF"), CurrentDestination = new IpV6Address("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"), FlowLabel = 123, HopLimit = 100, NextHeader = IpV4Protocol.Udp, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV6Layer, payloadLayer); return builder.Build(DateTime.Now); }
static void msearch_spoof(PacketDevice selectedDevice, String source_ip, UInt16 port) { String msearch_string = "M-SEARCH * HTTP/1.1\r\nHOST:239.255.255.250:1900\r\nST: ssdp:all\r\nMAN: \"ssdp:discover\"\r\nMX:2\r\n\r\n"; byte[] temp = System.Text.Encoding.ASCII.GetBytes(msearch_string); EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(), Destination = new MacAddress("01:00:5E:7F:FF:FA"), EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(source_ip), CurrentDestination = new IpV4Address("239.255.255.250"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, Identification = 1, Options = IpV4Options.None, Protocol = null, Ttl = 64, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = port, DestinationPort = 1900, Checksum = null, CalculateChecksumValue = true, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(temp), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); using (PacketCommunicator communicator = selectedDevice.Open(69559, PacketDeviceOpenAttributes.Promiscuous, 1000)) // read timeout { communicator.SendPacket(builder.Build(DateTime.Now)); } }
/// <summary> /// This function build an IGMP over IPv4 over Ethernet packet. /// </summary> private static Packet BuildIgmpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IgmpQueryVersion1Layer igmpLayer = new IgmpQueryVersion1Layer { GroupAddress = new IpV4Address("1.2.3.4"), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, igmpLayer); return builder.Build(DateTime.Now); }
static void Main(string[] args) { // Send anonymous statistics about the usage of Pcap.Net PcapDotNet.Analysis.PcapDotNetAnalysis.OptIn = true; // Retrieve the device list from the local machine IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; if (allDevices.Count == 0) { Console.WriteLine("No interfaces found! Make sure WinPcap is installed."); return; } // Print the list for (int i = 0; i != allDevices.Count; ++i) { LivePacketDevice device = allDevices[i]; Console.Write((i + 1) + ". " + device.Name); if (device.Description != null) Console.WriteLine(" (" + device.Description + ")"); else Console.WriteLine(" (No description available)"); } int deviceIndex = 0; do { Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):"); string deviceIndexString = Console.ReadLine(); if (!int.TryParse(deviceIndexString, out deviceIndex) || deviceIndex < 1 || deviceIndex > allDevices.Count) { deviceIndex = 0; } } while (deviceIndex == 0); // Take the selected adapter PacketDevice selectedDevice = allDevices[deviceIndex - 1]; // Open the output device using (PacketCommunicator communicator = selectedDevice.Open(100, // name of the device PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { // Supposing to be on ethernet, set mac source to 1:1:1:1:1:1 MacAddress source = new MacAddress("1:1:1:1:1:1"); // set mac destination to 2:2:2:2:2:2 MacAddress destination = new MacAddress("2:2:2:2:2:2"); // Create the packets layers // Ethernet Layer EthernetLayer ethernetLayer = new EthernetLayer { Source = source, Destination = destination }; // IPv4 Layer IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), Ttl = 128, // The rest of the important parameters will be set for each packet }; // ICMP Layer IcmpEchoLayer icmpLayer = new IcmpEchoLayer(); // Create the builder that will build our packets PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); // Send 100 Pings to different destination with different parameters for (int i = 0; i != 100; ++i) { // Set IPv4 parameters ipV4Layer.Destination = new IpV4Address("2.3.4." + i); ipV4Layer.Identification = (ushort)i; // Set ICMP parameters icmpLayer.SequenceNumber = (ushort)i; icmpLayer.Identifier = (ushort)i; // Build the packet Packet packet = builder.Build(DateTime.Now); // Send down the packet communicator.SendPacket(packet); } } }
/// <summary> /// This function build an IPv4 over GRE over IPv4 over Ethernet packet. /// </summary> private static Packet BuildGrePacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; GreLayer greLayer = new GreLayer { Version = GreVersion.Gre, ProtocolType = EthernetType.None, // Will be filled automatically. RecursionControl = 0, FutureUseBits = 0, ChecksumPresent = true, Checksum = null, // Will be filled automatically. Key = null, SequenceNumber = 123, AcknowledgmentSequenceNumber = null, RoutingOffset = null, Routing = null, StrictSourceRoute = false, }; IpV4Layer innerIpV4Layer = new IpV4Layer { Source = new IpV4Address("100.200.201.202"), CurrentDestination = new IpV4Address("123.254.132.40"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = IpV4Protocol.Udp, Ttl = 120, TypeOfService = 0, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer, innerIpV4Layer); return builder.Build(DateTime.Now); }
/// <summary> /// This function build an TCP over IPv4 over Ethernet with payload packet. /// </summary> private static Packet BuildTcpPacket(MacAddress SourceMac, MacAddress DestinationMac, IpV4Layer ipV4Layer, TcpLayer tcpLayer, PayloadLayer payloadLayer) { EthernetLayer ethernetLayer = new EthernetLayer { Source = SourceMac, Destination = DestinationMac, EtherType = EthernetType.None, // Will be filled automatically. }; //IpV4Layer ipV4Layer = // new IpV4Layer // { // Source = SourceIp, // CurrentDestination = DestinatinIp, // Fragmentation = IpV4Fragmentation.None, // HeaderChecksum = null, // Will be filled automatically. // Identification = 123, // Options = IpV4Options.None, // Protocol = null, // Will be filled automatically. // Ttl = 100, // TypeOfService = 0, // }; //TcpLayer tcpLayer = // new TcpLayer // { // SourcePort = 4050, // DestinationPort = 25, // Checksum = null, // Will be filled automatically. // SequenceNumber = 100, // AcknowledgmentNumber = 50, // ControlBits = TcpControlBits.Acknowledgment, // Window = 100, // UrgentPointer = 0, // Options = TcpOptions.None, // }; //PayloadLayer payloadLayer = // new PayloadLayer // { // Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), // }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); return builder.Build(DateTime.Now); }
/// <summary> /// This function build an UDP over IPv4 over Ethernet with payload packet. /// </summary> private static Packet BuildUdpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = 4050, DestinationPort = 25, Checksum = null, // Will be filled automatically. CalculateChecksumValue = true, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); return builder.Build(DateTime.Now); }
public void IpV4Handler(Packet packet, IList<Packet> packetList = null) { if (packet.Ethernet.IpV4.Length > MTU) { EthernetLayer ethernetLayer = new EthernetLayer { Source = ownHardwareAddress, Destination = packet.Ethernet.Source, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = packet.Ethernet.IpV4.Destination, CurrentDestination = packet.Ethernet.IpV4.Source, Fragmentation = packet.Ethernet.IpV4.Fragmentation, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = packet.Ethernet.IpV4.Options, Protocol = packet.Ethernet.IpV4.Protocol, Ttl = packet.Ethernet.IpV4.Ttl, TypeOfService = packet.Ethernet.IpV4.TypeOfService, }; IcmpDestinationUnreachableLayer icmpLayer = new IcmpDestinationUnreachableLayer { Code = IcmpCodeDestinationUnreachable.FragmentationNeededAndDoNotFragmentSet, NextHopMaximumTransmissionUnit = (ushort)MTU, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); SendPacket(builder.Build(DateTime.Now)); // TODO: fix warning spam if (Global.Config.LoadBalancer.ShowTrayTipsWarnings) Global.ShowTrayTip("Load Balancer", "IP packet larger than the MTU detected on " + Name + ".\n\nIP fragmentation is not supported.", ToolTipIcon.Warning); Global.WriteLog("Load Balancer: IP packet larger than the MTU detected on " + Name); return; } if (packet.Ethernet.IpV4.Ttl < 2) { EthernetLayer ethernetLayer = new EthernetLayer { Source = ownHardwareAddress, Destination = packet.Ethernet.Source, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = ownProtocolAddress, CurrentDestination = packet.Ethernet.IpV4.Source, Fragmentation = packet.Ethernet.IpV4.Fragmentation, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = packet.Ethernet.IpV4.Options, Protocol = packet.Ethernet.IpV4.Protocol, Ttl = packet.Ethernet.IpV4.Ttl, TypeOfService = packet.Ethernet.IpV4.TypeOfService, }; IcmpTimeExceededLayer icmpLayer = new IcmpTimeExceededLayer { Code = 0, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer, packet.Ethernet.IpV4.ExtractLayer(), packet.Ethernet.IpV4.Payload.ExtractLayer()); SendPacket(builder.Build(DateTime.Now)); return; } if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.InternetControlMessageProtocol) { if (packet.Ethernet.IpV4.Destination == ownProtocolAddress) { if ((packet.Ethernet.IpV4.Icmp.MessageType == IcmpMessageType.Echo || packet.Ethernet.IpV4.Fragmentation.Offset > 0)) { EthernetLayer ethernetLayer = new EthernetLayer { Source = ownHardwareAddress, Destination = packet.Ethernet.Source, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = packet.Ethernet.IpV4.Destination, CurrentDestination = packet.Ethernet.IpV4.Source, Fragmentation = packet.Ethernet.IpV4.Fragmentation, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = packet.Ethernet.IpV4.Options, Protocol = packet.Ethernet.IpV4.Protocol, Ttl = packet.Ethernet.IpV4.Ttl, TypeOfService = packet.Ethernet.IpV4.TypeOfService, }; if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.DoNotFragment || packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset == 0) { byte[] icmpPacket = packet.Ethernet.IpV4.Payload.ToArray(); icmpPacket[0] = 0; icmpPacket[2] = 0; icmpPacket[3] = 0; ushort checksum = IpFunctions.ComputeIpChecksum(icmpPacket); icmpPacket[2] = (byte)(checksum >> 8); icmpPacket[3] = (byte)(checksum & 0xff); PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(icmpPacket), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer); SendPacket(builder.Build(DateTime.Now)); } else if (packet.Ethernet.IpV4.Fragmentation.Offset == 0 && packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.MoreFragments) { byte[] icmpHeader = packet.Ethernet.IpV4.Payload.ToArray(); icmpHeader[0] = 0; icmpHeader[2] = 0; icmpHeader[3] = 0; int icmpPacketLength = icmpHeader.Length; for (int i = 1; i < packetList.Count; i++) { icmpPacketLength += packetList[i].Ethernet.IpV4.Payload.ToArray().Length; } byte[] icmpPacket = new byte[icmpPacketLength]; Buffer.BlockCopy(icmpHeader, 0, icmpPacket, 0, icmpHeader.Length); icmpPacketLength = icmpHeader.Length; for (int i = 1; i < packetList.Count; i++) { Buffer.BlockCopy(packetList[i].Ethernet.IpV4.Payload.ToArray(), 0, icmpPacket, icmpPacketLength, packetList[i].Ethernet.IpV4.Payload.ToArray().Length); icmpPacketLength += packetList[i].Ethernet.IpV4.Payload.ToArray().Length; } ushort checksum = IpFunctions.ComputeIpChecksum(icmpPacket); icmpHeader[2] = (byte)(checksum >> 8); icmpHeader[3] = (byte)(checksum & 0xff); PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(icmpHeader), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer); SendPacket(builder.Build(DateTime.Now)); } else { PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer); SendPacket(builder.Build(DateTime.Now)); } } } else if (packet.Ethernet.IpV4.Source == ifProtocolAddress) if ((packet.Ethernet.IpV4.Icmp.MessageType == IcmpMessageType.Echo || packet.Ethernet.IpV4.Fragmentation.Offset > 0)) { { tapRoutingObject = RoutingTable.GetInterface(packet); if (tapRoutingObject.response == -1) return; EthernetLayer ethernetLayer = new EthernetLayer { Source = physicalWorkers[tapRoutingObject.ifIndex].ifHardwareAddress, Destination = physicalWorkers[tapRoutingObject.ifIndex].gatewayHardwareAddress, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = physicalWorkers[tapRoutingObject.ifIndex].ifProtocolAddress, CurrentDestination = packet.Ethernet.IpV4.Destination, Fragmentation = packet.Ethernet.IpV4.Fragmentation, HeaderChecksum = null, // Will be filled automatically. Identification = packet.Ethernet.IpV4.Identification, Options = packet.Ethernet.IpV4.Options, Protocol = packet.Ethernet.IpV4.Protocol, Ttl = (byte)(packet.Ethernet.IpV4.Ttl - 1), TypeOfService = packet.Ethernet.IpV4.TypeOfService, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer); physicalWorkers[tapRoutingObject.ifIndex].SendPacket(builder.Build(DateTime.Now)); } } } else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp) { if (packet.Ethernet.IpV4.Source == ifProtocolAddress) { tapRoutingObject = RoutingTable.GetInterface(packet); if (tapRoutingObject.response == -1) return; if (tapRoutingObject.response == -3) { lock (RoutingTable.connParams[tapRoutingObject.guid]) SendAck(tapRoutingObject.guid); return; } lock (RoutingTable.connParams[tapRoutingObject.guid]) { RoutingTable.connParams[tapRoutingObject.guid].freeWindow = (int)((packet.Ethernet.IpV4.Tcp.Window << RoutingTable.connParams[tapRoutingObject.guid].windowScale) - (RoutingTable.connParams[tapRoutingObject.guid].seq - packet.Ethernet.IpV4.Tcp.AcknowledgmentNumber)); if (RoutingTable.connParams[tapRoutingObject.guid].windowFull) { if (RoutingTable.connParams[tapRoutingObject.guid].freeWindow > RoutingTable.connParams[tapRoutingObject.guid].bytesReceived) { SendData(tapRoutingObject.guid, RoutingTable.connParams[tapRoutingObject.guid].bytesReceived); RoutingTable.connParams[tapRoutingObject.guid].freeWindow -= RoutingTable.connParams[tapRoutingObject.guid].bytesReceived; RoutingTable.connParams[tapRoutingObject.guid].windowFull = false; RoutingTable.connParams[tapRoutingObject.guid].UpdateSeq(RoutingTable.connParams[tapRoutingObject.guid].bytesReceived); try { RoutingTable.connParams[tapRoutingObject.guid].socket.BeginReceive(RoutingTable.connParams[tapRoutingObject.guid].receivingBuffer, 0, RoutingTable.connParams[tapRoutingObject.guid].receivingBuffer.Count(), 0, new AsyncCallback(physicalWorkers[tapRoutingObject.ifIndex].ReceiveCallback), tapRoutingObject.guid); } catch (Exception) { RoutingTable.RequestAccess(); if (!RoutingTable.connStatus[tapRoutingObject.guid].remoteEPFin) tapWorker.SendRst(tapRoutingObject.guid); RoutingTable.connStatus[tapRoutingObject.guid].remoteEPFin = true; RoutingTable.connStatus[tapRoutingObject.guid].localEPFin = true; RoutingTable.ReleaseAccess(); } } } } if (packet.Ethernet.IpV4.Tcp.PayloadLength > 0 || tapRoutingObject.response == -2) physicalWorkers[tapRoutingObject.ifIndex].SendData(packet, tapRoutingObject.guid); } } else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp) { if (packet.Ethernet.IpV4.Source == ifProtocolAddress) { tapRoutingObject = RoutingTable.GetInterface(packet); if (tapRoutingObject.response == -1) return; EthernetLayer ethernetLayer = new EthernetLayer { Source = physicalWorkers[tapRoutingObject.ifIndex].ifHardwareAddress, Destination = physicalWorkers[tapRoutingObject.ifIndex].gatewayHardwareAddress, EtherType = packet.Ethernet.EtherType, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = physicalWorkers[tapRoutingObject.ifIndex].ifProtocolAddress, CurrentDestination = packet.Ethernet.IpV4.Destination, Fragmentation = packet.Ethernet.IpV4.Fragmentation, HeaderChecksum = null, // Will be filled automatically. Identification = packet.Ethernet.IpV4.Identification, Options = packet.Ethernet.IpV4.Options, Protocol = packet.Ethernet.IpV4.Protocol, Ttl = (byte)(packet.Ethernet.IpV4.Ttl - 1), TypeOfService = packet.Ethernet.IpV4.TypeOfService, }; UdpLayer udpLayer = new UdpLayer { SourcePort = packet.Ethernet.IpV4.Udp.SourcePort, DestinationPort = packet.Ethernet.IpV4.Udp.DestinationPort, Checksum = null, // Will be filled automatically. CalculateChecksumValue = packet.Ethernet.IpV4.Udp.IsChecksumOptional, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(packet.Ethernet.IpV4.Udp.Payload.ToArray()), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); physicalWorkers[tapRoutingObject.ifIndex].SendPacket(builder.Build(DateTime.Now)); } } }
/// <summary> /// This function build an TCP over IPv4 over Ethernet with payload packet. /// </summary> private static Packet BuildTcpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = 4050, DestinationPort = 25, Checksum = null, // Will be filled automatically. SequenceNumber = 100, AcknowledgmentNumber = 50, ControlBits = TcpControlBits.Acknowledgment, Window = 100, UrgentPointer = 0, Options = TcpOptions.None, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); return builder.Build(DateTime.Now); }
public void SendData(Guid guid, int payloadLength) { EthernetLayer ethernetLayer = new EthernetLayer { Source = ownHardwareAddress, Destination = ifHardwareAddress, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = RoutingTable.connParams[guid].remoteIp, CurrentDestination = ifProtocolAddress, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = ipID, Options = IpV4Options.None, Protocol = IpV4Protocol.Tcp, Ttl = 128, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = RoutingTable.connParams[guid].remotePort, DestinationPort = RoutingTable.connParams[guid].localPort, Checksum = null, // Will be filled automatically. SequenceNumber = RoutingTable.connParams[guid].seq, AcknowledgmentNumber = RoutingTable.connParams[guid].ack, ControlBits = TcpControlBits.Acknowledgment, Window = RoutingTable.connParams[guid].GetWindow(), UrgentPointer = 0, Options = TcpOptions.None, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(RoutingTable.connParams[guid].receivingBuffer.Take(payloadLength).ToArray()), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); SendPacket(builder.Build(DateTime.Now)); }
/// <summary> /// This function build a DNS over UDP over IPv4 over Ethernet packet. /// </summary> private static Packet BuildDnsPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = 4050, DestinationPort = 53, Checksum = null, // Will be filled automatically. CalculateChecksumValue = true, }; DnsLayer dnsLayer = new DnsLayer { Id = 100, IsResponse = false, OpCode = DnsOpCode.Query, IsAuthoritativeAnswer = false, IsTruncated = false, IsRecursionDesired = true, IsRecursionAvailable = false, FutureUse = false, IsAuthenticData = false, IsCheckingDisabled = false, ResponseCode = DnsResponseCode.NoError, Queries = new[] { new DnsQueryResourceRecord(new DnsDomainName("pcapdot.net"), DnsType.A, DnsClass.Internet), }, Answers = null, Authorities = null, Additionals = null, DomainNameCompressionMode = DnsDomainNameCompressionMode.All, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer); return builder.Build(DateTime.Now); }
public Packet BuildTcpPacket( string ipAddress, string macAddress, ushort portToScan ) { ethernetLayer.Destination = new MacAddress( ToMac( macAddress.ToString() ) ); ipV4Layer.CurrentDestination = new IpV4Address( ipAddress ); tcpLayer.DestinationPort = portToScan; PacketBuilder builder = new PacketBuilder( ethernetLayer, ipV4Layer, tcpLayer ); return builder.Build( DateTime.Now ); }
/// <summary> /// This function build an HTTP over TCP over IPv4 over Ethernet packet. /// </summary> private static Packet BuildHttpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = 4050, DestinationPort = 80, Checksum = null, // Will be filled automatically. SequenceNumber = 100, AcknowledgmentNumber = 50, ControlBits = TcpControlBits.Acknowledgment, Window = 100, UrgentPointer = 0, Options = TcpOptions.None, }; HttpRequestLayer httpLayer = new HttpRequestLayer { Version = HttpVersion.Version11, Header = new HttpHeader(new HttpContentLengthField(11)), Body = new Datagram(Encoding.ASCII.GetBytes("hello world")), Method = new HttpRequestMethod(HttpRequestKnownMethod.Get), Uri = @"http://pcapdot.net/", }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, httpLayer); return builder.Build(DateTime.Now); }
/// <summary> /// This function build an ICMP over IPv4 over Ethernet packet. /// </summary> public Packet BuildIcmpPacket( string TargetIpAddress, string RouterMacAddress ) { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress( ToMac( _ownMacAddress.ToString() ) ), Destination = new MacAddress( ToMac( RouterMacAddress.ToString() ) ), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address( new IPAddress( _ownIpAddress ).ToString() ), CurrentDestination = new IpV4Address( TargetIpAddress ), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IcmpEchoLayer icmpLayer = new IcmpEchoLayer { Checksum = null, // Will be filled automatically. Identifier = 456, SequenceNumber = 800, }; PacketBuilder builder = new PacketBuilder( ethernetLayer, ipV4Layer, icmpLayer ); return builder.Build( DateTime.Now ); }
private void CreateRequestReply() { if (_streamReply != null) { _streamReply.Close(); byte[] data = _streamReply.ToArray(); _streamReply = null; EthernetLayer ethernetLayer = new EthernetLayer { Source = _tcpConnection.Destination.MacAddress, Destination = _tcpConnection.Source.MacAddress }; IpV4Layer ipV4Layer = new IpV4Layer { Source = _tcpConnection.Destination.IpAddress, CurrentDestination = _tcpConnection.Source.IpAddress, Fragmentation = IpV4Fragmentation.None, //HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, //Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0 }; TcpLayer tcpLayer = new TcpLayer() { SourcePort = _tcpConnection.Destination.Port, DestinationPort = _tcpConnection.Source.Port, //Checksum = null, // Will be filled automatically. SequenceNumber = 100, AcknowledgmentNumber = 50, ControlBits = TcpControlBits.Acknowledgment, Window = 100, UrgentPointer = 0, Options = TcpOptions.None }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(data) }; PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); if (PacketHandle != null) PacketHandle(TcpDirection.DestinationToSource, packetBuilder.Build(_replyTimestamp)); _replyTimestamp = DateTime.MinValue; } }