Exemple #1
0
        //Create a base packet to use as parent for the other classes
        public virtual void GetBase(string MACsrc, string MACdst, string IPsrc, string IPdst,
            string IpId, string TTL)
        {
            // Ethernet Layer
            ethernetLayer = new EthernetLayer
            {
                Source = new MacAddress(MACsrc),
                Destination = new MacAddress(MACdst),
                // Set ethernet type
                EtherType = EthernetType.None
            };

            // IpV4 Layer
            ipV4Layer = new IpV4Layer
            {
                Source = new IpV4Address(IPsrc),
                CurrentDestination = new IpV4Address(IPdst),
                Fragmentation = IpV4Fragmentation.None,
                HeaderChecksum = null, // Will be filled automatically.
                Identification = StringToUShort(IpId),
                Options = IpV4Options.None,
                Protocol = null, // Will be filled automatically.
                Ttl = StringToByte(TTL),
                TypeOfService = 0,
            };
        }
Exemple #2
0
 /// <summary>
 /// True iff the two IPv4 layers have the same TypeOfService, Identification, Fragmentation, Ttl, Protocol, HeaderChecksum, Source, Destination and Options.
 /// </summary>
 public bool Equals(IpV4Layer other)
 {
     return(other != null &&
            TypeOfService == other.TypeOfService && Identification == other.Identification &&
            Fragmentation == other.Fragmentation && Ttl == other.Ttl &&
            Protocol == other.Protocol &&
            HeaderChecksum == other.HeaderChecksum &&
            Source == other.Source && CurrentDestination == other.CurrentDestination &&
            Options.Equals(other.Options));
 }
 public bool Equals(IpV4Layer other)
 {
     if (other != null && (int)this.TypeOfService == (int)other.TypeOfService && ((int)this.Identification == (int)other.Identification && this.Fragmentation == other.Fragmentation) && (int)this.Ttl == (int)other.Ttl)
     {
         IpV4Protocol?protocol1 = this.Protocol;
         IpV4Protocol?protocol2 = other.Protocol;
         if ((protocol1.GetValueOrDefault() != protocol2.GetValueOrDefault() ? 0 : (protocol1.HasValue == protocol2.HasValue ? 1 : 0)) != 0)
         {
             ushort?headerChecksum1 = this.HeaderChecksum;
             ushort?headerChecksum2 = other.HeaderChecksum;
             if (((int)headerChecksum1.GetValueOrDefault() != (int)headerChecksum2.GetValueOrDefault() ? 0 : (headerChecksum1.HasValue == headerChecksum2.HasValue ? 1 : 0)) != 0 && this.Source == other.Source && this.CurrentDestination == other.CurrentDestination)
             {
                 return(this.Options.Equals((PcapDotNet.Packets.Options <IpV4Option>)other.Options));
             }
         }
     }
     return(false);
 }
 public OwnPacketBuilder(PacketType packetType)
 {
     GetLocalInformation.LocalMacAndIPAddress( out _ownIpAddress, out _ownMacAddress );
     ethernetLayer = null;
     ipV4Layer = null;
     tcpLayer = null;
     arpLayer = null;
     switch(packetType)
     {
         case PacketType.ARP :
             BuildingArpPacket();
             break;
         case PacketType.TCP :
             BuildingTcpPacket();
             break;
         case PacketType.Unknown :
             break;
         default :
             break;
     }
 }
 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));
 }
            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));
                    }

                }
            }
Exemple #7
0
        /// <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);
        }
Exemple #8
0
        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);
                }
            }
        }
Exemple #9
0
        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));
                 }
             }
        }
Exemple #10
0
 /// <summary>
 /// True iff the two IPv4 layers have the same TypeOfService, Identification, Fragmentation, Ttl, Protocol, HeaderChecksum, Source, Destination and Options.
 /// </summary>
 public bool Equals(IpV4Layer other)
 {
     return other != null &&
            TypeOfService == other.TypeOfService && Identification == other.Identification &&
            Fragmentation == other.Fragmentation && Ttl == other.Ttl &&
            Protocol == other.Protocol &&
            HeaderChecksum == other.HeaderChecksum &&
            Source == other.Source && CurrentDestination == other.CurrentDestination &&
            Options.Equals(other.Options);
 }
Exemple #11
0
        private void InitializePackets()
        {
            //Fill with dummy data
            _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 =
                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 =
                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 =
                new PayloadLayer
                {
                    Data = new Datagram(Encoding.ASCII.GetBytes("CAM Flooding Packet")),
                };
        }
Exemple #12
0
        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);
                    }
                }
            }
        }
        private static void CreateRandomIpV4Payload(Random random, IpV4Layer ipV4Layer, List<ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            ipV4Layer.Protocol = null;
            if (random.NextBool())
                ipV4Layer.Fragmentation = IpV4Fragmentation.None;

            switch (random.Next(0, 9))
            {
                case 0: // IpV4.
                case 1:
                    IpV4Layer innerIpV4Layer = random.NextIpV4Layer();
                    layers.Add(innerIpV4Layer);
                    CreateRandomIpV4Payload(random, innerIpV4Layer, layers);
                    return;

                case 2: // Igmp.
                    layers.Add(random.NextIgmpLayer());
                    return;

                case 3: // Icmp.
                    IcmpLayer icmpLayer = random.NextIcmpLayer();
                    layers.Add(icmpLayer);
                    layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer));
                    return;

                case 4: // Gre.
                    GreLayer greLayer = random.NextGreLayer();
                    layers.Add(greLayer);
                    CreateRandomEthernetPayload(random, greLayer, layers);
                    return;
                    
                case 5: // Udp.
                case 6:
                    UdpLayer udpLayer = random.NextUdpLayer();
                    layers.Add(udpLayer);
                    CreateRandomUdpPayload(random, udpLayer, layers);
                    return;

                case 7: // Tcp.
                case 8:
                    TcpLayer tcpLayer = random.NextTcpLayer();
                    layers.Add(tcpLayer);
                    CreateRandomTcpPayload(random, tcpLayer, layers);
                    return;

                default:
                    throw new InvalidOperationException("Invalid value.");
            }
        }
        //# 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);
        }
Exemple #15
0
 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;
     }
 }
Exemple #16
0
        public static void Test_SendPacket_01(string deviceName = null)
        {
            //// 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];
            PacketDevice device = SelectDevice(deviceName);

            // Open the output device, promiscuous mode
            using (PacketCommunicator communicator = device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                // Supposing to be on ethernet, set mac source to 01:01:01:01:01:01
                MacAddress source = new MacAddress("01:01:01:01:01:01");

                // set mac destination to 02:02:02:02:02:02
                MacAddress destination = new MacAddress("02:02:02:02:02:02");

                // 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.CurrentDestination = 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);
                }

                communicator.SendPacket(BuildEthernetPacket());
                communicator.SendPacket(BuildArpPacket());
                communicator.SendPacket(BuildVLanTaggedFramePacket());
                communicator.SendPacket(BuildIpV4Packet());
                communicator.SendPacket(BuildIcmpPacket());
                communicator.SendPacket(BuildIgmpPacket());
                communicator.SendPacket(BuildGrePacket());
                communicator.SendPacket(BuildUdpPacket());
                communicator.SendPacket(BuildTcpPacket());
                communicator.SendPacket(BuildDnsPacket());
                communicator.SendPacket(BuildHttpPacket());
                communicator.SendPacket(BuildComplexPacket());
            }
        }
        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));
            }

        }
Exemple #18
0
        /// <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);
        }
Exemple #19
0
        private static void preparePcap()
        {
            var source = new MacAddress(getClientMAC());
            var dest = new MacAddress(getRouterMAC());

            device = LivePacketDevice.AllLocalMachine[0];
            communicator = device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000);

            EthernetLayer ethLayer = new EthernetLayer { Source = source, Destination = dest };
            ipLayer = new IpV4Layer { Source = new IpV4Address(LocalIPAddress()), Ttl = 128 };
            icmpLayer = new IcmpEchoLayer();
            icmpBuilder = new PacketBuilder(ethLayer, ipLayer, icmpLayer);

            TCPLayer = new TcpLayer();
            TCPBuilder = new PacketBuilder(ethLayer, ipLayer, TCPLayer);

            if (!TCPMode)
            {
                communicator.SetFilter("icmp[0] = 0");
            }
            else
            {
                //tcp[13] = 18
                communicator.SetFilter("tcp[13] = 18 and port " + TCPPort);
            }
        }
Exemple #20
0
        /// <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);
        }
Exemple #21
0
 public void IpV4LayerNullChecksumAndProtocolGetHashCodeTest()
 {
     IpV4Layer layer = new IpV4Layer
                           {
                               HeaderChecksum = null,
                               Protocol = null,
                           };
     Assert.IsNotNull(layer.GetHashCode());
 }
Exemple #22
0
        /// <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);
        }
Exemple #23
0
        private static Packet BuildIcmpPacket(MacAddress SourceMac, MacAddress DestinationMac, IpV4Address SourceIp, IpV4Address CurrentDestination, string packetType)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
                {
                    Source = SourceMac,
                    Destination = DestinationMac,
                    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"),
                    Source = SourceIp,
                    CurrentDestination = CurrentDestination,
                    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 = null;
            IcmpEchoReplyLayer icmpRLayer = null;

            PacketBuilder builder = null;

            if (packetType.Equals("REQUEST"))
            {
                Console.WriteLine("Request");
                icmpLayer =
                    new IcmpEchoLayer
                    {
                        Checksum = null, // Will be filled automatically.
                        Identifier = 456,
                        SequenceNumber = 800,
                    };
                builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer);
            }
            else
            {
                Console.WriteLine("Reply");
                icmpRLayer =
                    new IcmpEchoReplyLayer
                    {
                        Checksum = null, // Will be filled automatically.
                        Identifier = 456,
                        SequenceNumber = 800,
                    };
                builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpRLayer);
            }

            return builder.Build(DateTime.Now);
        }
Exemple #24
0
        /// <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);
        }
Exemple #25
0
        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);
            }
        }
Exemple #26
0
        /// <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);
        }
Exemple #27
0
 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));
 }
Exemple #28
0
        /// <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);
        }
 void BuildingTcpPacket()
 {
     ethernetLayer = new EthernetLayer
     {
         Source = new MacAddress( ToMac( _ownMacAddress.ToString() ) ),
         EtherType = EthernetType.None, // Will be filled automatically.
     };
     ipV4Layer = new IpV4Layer
     {
         Source = new IpV4Address( new IPAddress( _ownIpAddress ).ToString() ),
         Fragmentation = IpV4Fragmentation.None,
         HeaderChecksum = null, // Will be filled automatically.
         Identification = 0,
         Options = IpV4Options.None,
         Protocol = null, // Will be filled automatically.
         Ttl = 100,
         TypeOfService = 0,
     };
     tcpLayer = new TcpLayer
     {
         SourcePort = 62000,
         Checksum = null, // Will be filled automatically.
         SequenceNumber = 100,
         AcknowledgmentNumber = 0,
         ControlBits = TcpControlBits.Synchronize,
         Window = 8192,
         UrgentPointer = 0,
         Options = new TcpOptions( new TcpOptionMaximumSegmentSize( 1460 ), TcpOption.Nop, new TcpOptionWindowScale( 2 ), new TcpOptionSelectiveAcknowledgmentPermitted(), new TcpOptionTimestamp( (uint)DateTime.Now.Ticks, 0 ) ),
     };
 }
        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 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 );
        }
Exemple #32
0
        public Packet CreateDnsReply(EthernetDatagram etherpacket, IpV4Address newAddress)
        {
            var ipPacket = etherpacket.IpV4;
            var udpPacket = ipPacket.Udp;
            var dnsPacket = udpPacket.Dns;

            if (!dnsPacket.IsQuery)
                throw new Exception("Packet should be a dns query!");

            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source = etherpacket.Destination,
                Destination = etherpacket.Source,
            };

            IpV4Layer ipLayer = new IpV4Layer
            {
                Source = ipPacket.Destination,
                CurrentDestination = ipPacket.Source,
            };

            UdpLayer udpLayer = new UdpLayer
            {
                SourcePort = udpPacket.DestinationPort,
                DestinationPort = udpPacket.SourcePort
            };

            DnsResourceData resourceData = new DnsResourceDataIpV4(newAddress);
            DnsDataResourceRecord resourceRecord = new DnsDataResourceRecord(dnsPacket.Queries[0].DomainName,
                    dnsPacket.Queries[0].DnsType,
                    dnsPacket.Queries[0].DnsClass,
                    60,
                    resourceData);

            DnsLayer dnsLayer = new DnsLayer
            {
                Queries = dnsPacket.Queries,
                IsQuery = false,
                IsResponse = true,
                Id = dnsPacket.Id,
                Answers = new[] { resourceRecord }
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, ipLayer, udpLayer, dnsLayer);
            return builder.Build(DateTime.Now);
        }