Esempio n. 1
0
        /// <summary>
        /// This function build an IPv6 over Ethernet with payload packet.
        /// </summary>
        public static Packet BuildIpV6Packet()
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress(macAddressSource),
                Destination = new MacAddress(macAddressDest),
                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));
        }
Esempio n. 2
0
        public static PacketBuilder BuildVLanTaggedFramePacketBuilder(ClassOfService CoS)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress("01:01:01:01:01:01"),
                Destination = new MacAddress("90:1B:0E:1B:DD:E8"),
                EtherType   = EthernetType.None,   // Will be filled automatically.
            };

            VLanTaggedFrameLayer vLanTaggedFrameLayer =
                new VLanTaggedFrameLayer
            {
                PriorityCodePoint        = CoS,
                CanonicalFormatIndicator = false,
                VLanIdentifier           = 50,
                EtherType = (EthernetType)34962,
            };

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(new byte[1496])
            };

            return(new PacketBuilder(ethernetLayer, vLanTaggedFrameLayer, payloadLayer));
        }
Esempio n. 3
0
        public static Packet BuildVLanTaggedFramePacket(int cyclecounter)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress("28:63:36:88:02:52"),
                Destination = new MacAddress("00:1b:1b:6b:6b:0e"),
                EtherType   = EthernetType.None,   // Will be filled automatically.
            };

            VLanTaggedFrameLayer vLanTaggedFrameLayer =
                new VLanTaggedFrameLayer
            {
                PriorityCodePoint        = ClassOfService.InternetworkControl,
                CanonicalFormatIndicator = false,
                VLanIdentifier           = 50,
                EtherType = (EthernetType)34962,
            };

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(new byte[] { 0x80, 0x00,
                                                 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xFF, 0x80, 0xFF,
                                                 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
                                                 Convert.ToByte((cyclecounter & 0xFF00) > 8), Convert.ToByte(cyclecounter & 0xFF), 0x35, 0x00 })
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, vLanTaggedFrameLayer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
Esempio n. 4
0
        public void RandomVLanTaggedFrameTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                EthernetLayer        ethernetLayer        = random.NextEthernetLayer(EthernetType.None);
                VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
                int          payloadLength = random.Next(1500);
                PayloadLayer payloadLayer  = new PayloadLayer
                {
                    Data = random.NextDatagram(payloadLength),
                };
                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, vLanTaggedFrameLayer, payloadLayer);

                ethernetLayer.EtherType = EthernetType.VLanTaggedFrame;

                // Test output.
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer());
                Assert.AreEqual(vLanTaggedFrameLayer, packet.Ethernet.VLanTaggedFrame.ExtractLayer());
                Assert.AreEqual(vLanTaggedFrameLayer.GetHashCode(), packet.Ethernet.VLanTaggedFrame.ExtractLayer().GetHashCode());
                Assert.AreNotEqual(random.NextVLanTaggedFrameLayer().GetHashCode(), packet.Ethernet.VLanTaggedFrame.ExtractLayer().GetHashCode());
                Assert.AreEqual(vLanTaggedFrameLayer.TagControlInformation, packet.Ethernet.VLanTaggedFrame.TagControlInformation);
                Assert.AreEqual(payloadLayer.Data, packet.Ethernet.VLanTaggedFrame.Payload);
            }
        }
Esempio n. 5
0
        public void RandomEthernetTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                EthernetLayer ethernetLayer         = random.NextEthernetLayer();
                int           ethernetPayloadLength = random.Next(1500);
                PayloadLayer  payloadLayer          = new PayloadLayer
                {
                    Data = random.NextDatagram(ethernetPayloadLength),
                };
                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, payloadLayer);

                // Ethernet
                Assert.IsTrue(new[] { EthernetType.IpV4, EthernetType.Arp, EthernetType.VLanTaggedFrame }.Contains(packet.Ethernet.EtherType) ||
                              packet.IsValid, "IsValid - EtherType = " + packet.Ethernet.EtherType);
                Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength");
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
                Assert.AreEqual(ethernetLayer.GetHashCode(), packet.Ethernet.ExtractLayer().GetHashCode(), "Ethernet Layer Hash Code");
                Assert.AreNotEqual(random.NextEthernetLayer().GetHashCode(), packet.Ethernet.ExtractLayer().GetHashCode(), "Ethernet Layer Hash Code");
                Assert.AreEqual(ethernetLayer.ToString(), packet.Ethernet.ExtractLayer().ToString(), "Ethernet Layer ToString()");
                Assert.AreNotEqual(random.NextEthernetLayer().ToString(), packet.Ethernet.ExtractLayer().ToString(), "Ethernet Layer ToString()");
                Assert.AreNotEqual(2, packet.Ethernet.Source, "Ethernet Source");

                Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Payload);
            }
        }
Esempio n. 6
0
        public void PayloadLayerEqualsTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                PayloadLayer layer = random.NextPayloadLayer(random.Next(100));
                Assert.AreNotEqual(layer, null);
                Assert.AreEqual(layer, new PayloadLayer
                {
                    Data = layer.Data
                });
                Assert.AreNotEqual(layer, new PayloadLayer
                {
                    Data = new Datagram(layer.Data.Concat <byte>(1).ToArray())
                });
                if (layer.Length > 1)
                {
                    Assert.AreNotEqual(layer, new PayloadLayer
                    {
                        Data = random.NextDatagram(layer.Length)
                    });
                }
            }
        }
        /// <summary>
        /// This function builds the Ethernet Row Data Packet with every third channel set to a color.
        /// </summary>
        private Packet BuildTestPacket(MacAddress source, MacAddress destination, int row, int pixelsWidth, int dataOffset, byte color, int testOffset)
        {
            int offset = 0;
            int width  = pixelsWidth * 3;

            byte[] mainByte = new byte[(width) + 7];

            EthernetType type = ((EthernetType)0x5500);

            if (row < 256)
            {
                type        = ((EthernetType)0x5500);
                mainByte[0] = Convert.ToByte(row);
            }
            else
            {
                type        = ((EthernetType)0x5501);
                mainByte[0] = Convert.ToByte(row % 256);
            }

            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source      = source,
                Destination = destination,
                EtherType   = type
            };

            //mainByte[0] = Convert.ToByte(row);
            mainByte[1] = Convert.ToByte(offset >> 8);
            mainByte[2] = Convert.ToByte(offset & 0xFF);
            mainByte[3] = Convert.ToByte(pixelsWidth >> 8);
            mainByte[4] = Convert.ToByte(pixelsWidth & 0xFF);
            mainByte[5] = 0x08;
            mainByte[6] = 0x80;

            for (int i = 0; i < width; i++)
            {
                int  indexwHead = 7 + i;
                byte oldValue   = 0;
                if (i % 3 == testOffset)
                {
                    oldValue = color;
                }
                //int oldint = Convert.ToInt32(data[i + (fullDataOffset * 3)]);
                int  newint   = ((oldValue * _brightness) / 100);
                byte newValue = Convert.ToByte(newint);
                mainByte[indexwHead] = newValue;
                //mainByte[indexwHead] = 0x88;
            }

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(mainByte)
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
Esempio n. 8
0
        public void RandomVLanTaggedFrameTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None);
                VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
                int payloadLength = random.Next(1500);
                PayloadLayer payloadLayer = new PayloadLayer
                {
                    Data = random.NextDatagram(payloadLength),
                };
                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, vLanTaggedFrameLayer, payloadLayer);

                ethernetLayer.EtherType = EthernetType.VLanTaggedFrame;

                // Test output.
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer());
                Assert.AreEqual(vLanTaggedFrameLayer, packet.Ethernet.VLanTaggedFrame.ExtractLayer());
                Assert.AreEqual(vLanTaggedFrameLayer.GetHashCode(), packet.Ethernet.VLanTaggedFrame.ExtractLayer().GetHashCode());
                Assert.AreNotEqual(random.NextVLanTaggedFrameLayer().GetHashCode(), packet.Ethernet.VLanTaggedFrame.ExtractLayer().GetHashCode());
                Assert.AreEqual(vLanTaggedFrameLayer.TagControlInformation, packet.Ethernet.VLanTaggedFrame.TagControlInformation);
                Assert.AreEqual(payloadLayer.Data, packet.Ethernet.VLanTaggedFrame.Payload);
            }
        }
        private static void CreateRandomUdpPayload(Random random, UdpLayer udpLayer, List <ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            DnsLayer dnsLayer = random.NextDnsLayer();

            layers.Add(dnsLayer);

            ushort specialPort = (ushort)(random.NextBool() ? 53 : 5355);

            if (dnsLayer.IsQuery)
            {
                udpLayer.DestinationPort = specialPort;
            }
            else
            {
                udpLayer.SourcePort = specialPort;
            }
        }
        /// <summary>
        /// This function build a VLanTaggedFrame over Ethernet with payload packet.
        /// </summary>
        public 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.NetworkControl,
                CanonicalFormatIndicator = false,
                VLanIdentifier           = 50,
                EtherType = (EthernetType)34962,
            };

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(new byte[] { 0x80, 0x6B,
                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                 0x10, 0x10, 0x35, 0x00 })
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, vLanTaggedFrameLayer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
        private static void CreateRandomTcpPayload(Random random, TcpLayer tcpLayer, List <ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            HttpLayer httpLayer = random.NextHttpLayer();

            layers.Add(httpLayer);
            if (httpLayer.IsRequest)
            {
                tcpLayer.DestinationPort = 80;
            }
            else
            {
                tcpLayer.SourcePort = 80;
            }

            if (random.NextBool())
            {
                return;
            }

            HttpLayer httpLayer2 = httpLayer.IsRequest ? (HttpLayer)random.NextHttpRequestLayer() : random.NextHttpResponseLayer();

            layers.Add(httpLayer2);
        }
Esempio n. 12
0
        private static Packet ArpPacket()
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress(DeviceManager.GetDeviceMac()),
                Destination = new MacAddress(AttackVariables.Mac),
                EtherType   = EthernetType.None,
            };

            ArpLayer arpLayer =
                new ArpLayer
            {
                ProtocolType          = EthernetType.IpV4,
                Operation             = ArpOperation.Request,
                SenderHardwareAddress = DeviceManager.GetDeviceMac().Split(':').Select(x => Convert.ToByte(x, 16)).ToArray().AsReadOnly(),
                SenderProtocolAddress = IPAddress.Parse("0.136.136.16").GetAddressBytes().AsReadOnly(),
                TargetHardwareAddress = AttackVariables.Mac.Split(':').Select(x => Convert.ToByte(x, 16)).ToArray().AsReadOnly(),
                TargetProtocolAddress = IPAddress.Parse(AttackVariables.IP).GetAddressBytes().AsReadOnly(),
            };
            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(AttackVariables.BufferSize),
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
Esempio n. 13
0
        public void RandomEthernetTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                EthernetLayer ethernetLayer = random.NextEthernetLayer();
                int ethernetPayloadLength = random.Next(1500);
                PayloadLayer payloadLayer = new PayloadLayer
                                                {
                                                    Data = random.NextDatagram(ethernetPayloadLength),
                                                };
                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, payloadLayer);

                // Ethernet
                Assert.IsTrue(new[] {EthernetType.IpV4, EthernetType.Arp, EthernetType.VLanTaggedFrame}.Contains(packet.Ethernet.EtherType) ||
                              packet.IsValid, "IsValid - EtherType = " + packet.Ethernet.EtherType);
                Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength");
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
                Assert.AreEqual(ethernetLayer.GetHashCode(), packet.Ethernet.ExtractLayer().GetHashCode(), "Ethernet Layer Hash Code");
                Assert.AreNotEqual(random.NextEthernetLayer().GetHashCode(), packet.Ethernet.ExtractLayer().GetHashCode(), "Ethernet Layer Hash Code");
                Assert.AreEqual(ethernetLayer.ToString(), packet.Ethernet.ExtractLayer().ToString(), "Ethernet Layer ToString()");
                Assert.AreNotEqual(random.NextEthernetLayer().ToString(), packet.Ethernet.ExtractLayer().ToString(), "Ethernet Layer ToString()");
                Assert.AreNotEqual(2, packet.Ethernet.Source, "Ethernet Source");

                Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Payload);
            }
        }
Esempio n. 14
0
        /// <summary>
        /// This function build an IPv4 over Ethernet with payload packet.
        /// </summary>
        public static Packet BuildIpV4Packet()
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress(macAddressSource),
                Destination = new MacAddress(macAddressDest),
                EtherType   = EthernetType.None,
            };

            IpV4Layer ipV4Layer =
                new IpV4Layer
            {
                Source             = new IpV4Address(ipSource),
                CurrentDestination = new IpV4Address(ipDest),
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null, // Will be filled automatically.
                Identification     = 123,
                Options            = IpV4Options.None,
                Protocol           = IpV4Protocol.Udp,
                Ttl           = 100,
                TypeOfService = 0,
            };

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
        /// <summary>
        /// This function builds the Ethernet 0x0AFF Packet.
        /// </summary>
        private Packet BuildSecondPacket(MacAddress source, MacAddress destination)
        {
            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source      = source,
                Destination = destination,
                EtherType   = ((EthernetType)0x0AFF)
            };

            byte[] mainByte = new byte[63];

            mainByte[0] = 0xFF;
            mainByte[1] = 0xFF;
            mainByte[2] = 0xFF;

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(mainByte)
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
Esempio n. 16
0
        public void RandomUdpTest()
        {
            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source      = new MacAddress("00:01:02:03:04:05"),
                Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
            };

            int seed = new Random().Next();

            Console.WriteLine("Seed: " + seed);
            Random random = new Random(seed);

            for (int i = 0; i != 1000; ++i)
            {
                IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
                ipV4Layer.HeaderChecksum = null;
                IpV6Layer ipV6Layer = random.NextIpV6Layer(IpV4Protocol.Udp, false);

                EthernetType ethernetType = random.NextBool() ? EthernetType.IpV4 : EthernetType.IpV6;
                Layer        ipLayer      = (ethernetType == EthernetType.IpV4 ? (Layer)ipV4Layer : ipV6Layer);
                UdpLayer     udpLayer     = random.NextUdpLayer();
                udpLayer.Checksum = null;

                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000));

                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, udpLayer, payloadLayer);

                Assert.IsTrue(packet.IsValid, "IsValid");

                // Ethernet
                ethernetLayer.EtherType = ethernetType;
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
                ethernetLayer.EtherType = EthernetType.None;

                // Ip
                if (ipLayer == ipV4Layer)
                {
                    // IpV4.
                    ipV4Layer.Protocol       = IpV4Protocol.Udp;
                    ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
                    Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer");
                    ipV4Layer.HeaderChecksum = null;
                }
                else
                {
                    // IpV6.
                    Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer");
                }

                // UDP
                udpLayer.Checksum = packet.Ethernet.Ip.Udp.Checksum;
                Assert.AreEqual(udpLayer, packet.Ethernet.Ip.Udp.ExtractLayer(), "UDP Layer");
                Assert.AreEqual(UdpDatagram.HeaderLength + payloadLayer.Length, packet.Ethernet.Ip.Udp.TotalLength, "Total Length");
                Assert.IsTrue(!udpLayer.CalculateChecksum && packet.Ethernet.Ip.Udp.Checksum == 0 ||
                              udpLayer.CalculateChecksum && packet.Ethernet.Ip.IsTransportChecksumCorrect, "IsTransportChecksumCorrect");
                Assert.IsTrue(packet.Ethernet.Ip.Udp.IsChecksumOptional, "IsChecksumOptional");
                Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Ip.Udp.Payload, "Payload");
            }
        }
Esempio n. 17
0
        /// <summary>
        /// This function build a VLanTaggedFrame over Ethernet with payload packet.
        /// </summary>
        public static Packet BuildVLanTaggedFramePacket()
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress(macAddressSource),
                Destination = new MacAddress("F4:06:69:06:DC:EF"),
                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));
        }
        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.");
            }
        }
Esempio n. 19
0
        public void SendIcmpEchoReply(IpV4Address TargetIP, MacAddress TargetMac, IcmpDatagram icmpRequest)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = _adapter.MAC,
                Destination = TargetMac,
                EtherType   = EthernetType.None,   // Will be filled automatically.
            };

            VLanTaggedFrameLayer vlanLayer =
                new VLanTaggedFrameLayer
            {
                PriorityCodePoint        = ClassOfService.BestEffort,
                CanonicalFormatIndicator = false,
                VLanIdentifier           = _adapter.VLAN,
                EtherType = EthernetType.None,
            };

            IpV4Layer ipV4Layer =
                new IpV4Layer
            {
                Source             = _adapter.IP,
                CurrentDestination = TargetIP,
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null, // Will be filled automatically.
                Identification     = 123,
                Options            = IpV4Options.None,
                Protocol           = null, // Will be filled automatically.
                Ttl           = 100,
                TypeOfService = 0,
            };

            IcmpEchoReplyLayer icmpReplyLayer = new IcmpEchoReplyLayer
            {
                Identifier     = (ushort)((icmpRequest.Variable >> 16) & 0xFFFF),
                SequenceNumber = (ushort)(icmpRequest.Variable & 0xFFFF),
            };

            PayloadLayer payloadLayer = new PayloadLayer()
            {
                Data = icmpRequest.Payload
            };


            if (_adapter.VLAN > 1)
            {
                VirtualNetwork.Instance.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, vlanLayer, ipV4Layer, icmpReplyLayer, payloadLayer));
            }
            else
            {
                VirtualNetwork.Instance.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, icmpReplyLayer, payloadLayer));
            }

            VirtualNetwork.Instance.PostTraceMessage("ICMP Reply: " + TargetIP.ToString());
        }
Esempio n. 20
0
        private void buttonManuelTaklit_Click(object sender, EventArgs e)// Bir önceki metodun aynısı. Yalnızca Mac adresi elle atanıyor. TextBox'dan çekiliyor.
        {
            String taklitmacmanuel = textBox1.Text;
            IList <LivePacketDevice> allDevices     = LivePacketDevice.AllLocalMachine;
            PacketDevice             selectedDevice = allDevices[2]; //Cihaz seçimi. Manuel olarak atanmıştır.

            using (PacketCommunicator communicator = selectedDevice.Open(100,
                                                                         PacketDeviceOpenAttributes.Promiscuous,
                                                                         1000))
            {
                for (int j = 0; j < 10000; j++)
                {
                    EthernetLayer ethernetLayer =
                        new EthernetLayer
                    {
                        Source      = new MacAddress(taklitmacmanuel),
                        Destination = new MacAddress("ff:ff:ff:ff:ff:ff"),
                        EtherType   = EthernetType.None,
                    };

                    IpV4Layer ipV4Layer =
                        new IpV4Layer
                    {
                        Source             = new IpV4Address(IpAdresim()),
                        CurrentDestination = new IpV4Address(IpParcala(0) + "." + IpParcala(1) + "." + IpParcala(2) + "." + "1"),
                        Fragmentation      = IpV4Fragmentation.None,
                        HeaderChecksum     = null,
                        Identification     = 123,
                        Options            = IpV4Options.None,
                        Protocol           = null,
                        Ttl           = 100,
                        TypeOfService = 0,
                    };

                    UdpLayer udpLayer =
                        new UdpLayer
                    {
                        SourcePort             = 4050,
                        DestinationPort        = 25,
                        Checksum               = null,
                        CalculateChecksumValue = true,
                    };

                    PayloadLayer payloadLayer =
                        new PayloadLayer
                    {
                        Data = new Datagram(Encoding.ASCII.GetBytes("Merhaba Dunya")),
                    };

                    PacketBuilder builder   = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);
                    Packet        arppacket = builder.Build(DateTime.Now);
                    communicator.SendPacket(arppacket);
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }
Esempio n. 21
0
        private Packet createSynPack()
        {
            PacketBuilder builder;

            MacAddress  macSource = new MacAddress(ethSourceAddr.Text);
            IpV4Address ipSource  = new IpV4Address("127.0.0.1");

            MacAddress  macDest = new MacAddress(ethDestAddr.Text);
            IpV4Address ipDest  = new IpV4Address("127.0.0.1");

            //Ethernet
            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source      = macDest,
                Destination = macDest,
                EtherType   = EthernetType.None
            };

            //Internet Protocol
            IpV4Layer ipv4Layer = new IpV4Layer
            {
                Source             = ipSource,
                CurrentDestination = ipDest,
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null,
                Identification     = 1,
                Options            = IpV4Options.None,
                Protocol           = null,
                Ttl           = 100,
                TypeOfService = 0
            };

            //Transport
            TcpLayer tcpLayer = new TcpLayer
            {
                SourcePort           = 3000,
                DestinationPort      = 2000,
                Checksum             = null,
                SequenceNumber       = 100,
                AcknowledgmentNumber = 50,
                ControlBits          = TcpControlBits.Synchronize,
                Window        = 1000,
                UrgentPointer = 0,
                Options       = TcpOptions.None
            };

            PayloadLayer payloadLayer = new PayloadLayer
            {
                Data = new Datagram(Encoding.ASCII.GetBytes("This is a tcp flow"))
            };

            builder = new PacketBuilder(ethernetLayer, ipv4Layer, tcpLayer, payloadLayer);

            return(builder.Build(DateTime.Now));
        }
Esempio n. 22
0
        // Callback function invoked by libpcap for every incoming packet
        private static void PacketHandler(Packet packet)
        {
            // print timestamp and length of the packet
            IpV4Datagram ip     = packet.Ethernet.IpV4;
            String       source = ip.Source.ToString();

            // Chat service IP.
            if (source != "178.79.171.171")
            {
                return;
            }

            // get original layers
            var    ethernet        = (EthernetLayer)packet.Ethernet.ExtractLayer();
            var    ipV4            = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
            var    tcp             = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
            var    http            = (HttpLayer)packet.Ethernet.IpV4.Tcp.Http.ExtractLayer();
            string ethString       = packet.Ethernet.BytesSequenceToHexadecimalString("-");
            string ipv4String      = packet.Ethernet.IpV4.BytesSequenceToHexadecimalString("-");
            string tcpString       = packet.Ethernet.IpV4.Tcp.BytesSequenceToHexadecimalString("-");
            string transportString = packet.Ethernet.Ip.Transport.BytesSequenceToHexadecimalString("-");
            string httpString      = packet.Ethernet.Ip.Tcp.Http.BytesSequenceToHexadecimalString("-");

            if (http.Body == null)
            {
                return;
            }

            var    time          = packet.Timestamp;
            string transportHead = packet.Ethernet.Ip.Transport.Decode(Encoding.UTF8);
            string transportData = packet.Ethernet.Ip.Transport.Payload.Decode(Encoding.UTF8);
            string htmlHead      = packet.Ethernet.Ip.Tcp.Http.Decode(Encoding.UTF8);
            string htmlData      = packet.Ethernet.Ip.Tcp.Http.Body.Decode(Encoding.UTF8);

            byte[] bodyStream   = packet.Ethernet.Ip.Tcp.Http.Body.ToArray();
            byte[] decompressed = Decompress(bodyStream);
            byte[] httpBody     = http.Body.ToArray();
            string result       = Unzip(httpBody);

            // extract the data
            PayloadLayer payload     = (PayloadLayer)packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
            var          totalLength = payload.Length;
            var          buffer      = new byte[totalLength];

            ////payload.Write(buffer, 1, totalLength, ipV4, null);
            Datagram data     = payload.Data;
            string   someSHit = data.BytesSequenceToHexadecimalString("-");
            string   jsonStr  = data.Decode(Encoding.ASCII);
            object   jsonShit = JsonConvert.DeserializeObject <Dictionary <String, Object> >(jsonStr);

            System.Windows.Forms.MessageBox.Show("I'm here!!!");
        }
Esempio n. 23
0
        public void DatagramExtractLayerTest()
        {
            PayloadLayer payloadLayer = new PayloadLayer
                                            {
                                                Data = new Datagram(new byte[] {100, 101, 102})
                                            };

            Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer
                                                                  {
                                                                      EtherType = EthernetType.IpV4
                                                                  }, payloadLayer);
            Assert.AreEqual(payloadLayer, packet.Ethernet.Payload.ExtractLayer());
        }
Esempio n. 24
0
        //

        private void SendTcpPacket(string SrcMac, string SrcIP, int SrcPort, string DestMac, string DestIp, int DestPort, string payload)
        {
            EthernetLayer ethLayer = new EthernetLayer
            {
                Source      = new MacAddress(SrcMac),
                Destination = new MacAddress(DestMac),
                EtherType   = EthernetType.None,
            };

            IpV4Layer ipV4Layer = new IpV4Layer
            {
                Source             = new IpV4Address(SrcIP),
                CurrentDestination = new IpV4Address(DestIp),
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null,
                Identification     = 0,
                Options            = IpV4Options.None,
                Protocol           = IpV4Protocol.Tcp,
                Ttl           = 128,
                TypeOfService = 0,
            };


            TcpLayer tcpLayer = new TcpLayer
            {
                SourcePort           = (ushort)SrcPort,
                DestinationPort      = (ushort)DestPort,
                Checksum             = null,
                SequenceNumber       = 0,
                AcknowledgmentNumber = 0,
                ControlBits          = TcpControlBits.Synchronize,
                Window        = 1024,
                UrgentPointer = 0,
            };

            PayloadLayer payloadLayer = new PayloadLayer
            {
                Data = new Datagram(StringToByteArray(payload)),
            };

            PacketBuilder builder = new PacketBuilder(ethLayer, ipV4Layer, tcpLayer, payloadLayer);

            this.Invoke(new MethodInvoker(delegate()
            {
                using (PacketCommunicator communicator = LivePacketDevice.AllLocalMachine[0].Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
                {
                    communicator.SendPacket(builder.Build(DateTime.Now));
                }
            }));
        }
Esempio n. 25
0
        public void DatagramExtractLayerTest()
        {
            PayloadLayer payloadLayer = new PayloadLayer
            {
                Data = new Datagram(new byte[] { 100, 101, 102 })
            };

            Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer
            {
                EtherType = EthernetType.IpV4
            }, payloadLayer);

            Assert.AreEqual(payloadLayer, packet.Ethernet.Payload.ExtractLayer());
        }
        private PacketBuilder buildLayers(int srcPort, int dstPort)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = sourceMac,
                Destination = destMac,
                EtherType   = EthernetType.None,       // Will be filled automatically.
            };

            IpV4Layer ipV4Layer =
                new IpV4Layer
            {
                Source             = new IpV4Address(txtSrcIP.Text),
                CurrentDestination = new IpV4Address(txtDestIP.Text),
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null, // Will be filled automatically.
                Identification     = (txtID.Text.Equals("")) ? (ushort)123 : ushort.Parse(txtID.Text),
                Options            = IpV4Options.None,
                Protocol           = null, // Will be filled automatically.
                Ttl           = txtTTL.Text.Equals("") ? (byte)100 : byte.Parse(txtTTL.Text) > 255 ? (byte)255 : byte.Parse(txtTTL.Text) < 1 ? (byte)1 : byte.Parse(txtTTL.Text),
                TypeOfService = 0,
            };

            TcpLayer tcpLayer =
                new TcpLayer
            {
                SourcePort           = (ushort)srcPort,
                DestinationPort      = (ushort)dstPort,
                Checksum             = null, // Will be filled automatically.
                SequenceNumber       = txtSequence.Text.Equals("") ? 100 : uint.Parse(txtSequence.Text),
                AcknowledgmentNumber = txtACK.Text.Equals("") ? 50 : uint.Parse(txtACK.Text),
                ControlBits          = (radPSH.Checked) ? TcpControlBits.Push : (radACK.Checked) ? TcpControlBits.Acknowledgment : (radRST.Checked) ? TcpControlBits.Reset : (radFIN.Checked) ? TcpControlBits.Fin : TcpControlBits.Synchronize,
                Window        = txtWindow.Text.Equals("") ? (ushort)100 : ushort.Parse(txtWindow.Text),
                UrgentPointer = 0,
                Options       = TcpOptions.None,
            };

            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = new Datagram(Encoding.ASCII.GetBytes(txtInput.Text)),
            };

            // build packet and initiate communicator
            PacketBuilder      builder      = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);
            PacketCommunicator communicator = selectedDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000);

            return(builder);
        }
Esempio n. 27
0
        public Packet BuildTcpPacket(Packet origPacket)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = origPacket.Ethernet.Source,
                Destination = lookUpMacAdress(Convert.ToString(origPacket.Ethernet.IpV4.Destination)),
                EtherType   = EthernetType.None,     // Will be filled automatically.
            };

            IpV4Layer ipV4Layer =
                new IpV4Layer
            {
                Source             = origPacket.Ethernet.IpV4.Source,
                CurrentDestination = origPacket.Ethernet.IpV4.Destination,
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null, // Will be filled automatically.
                Identification     = origPacket.Ethernet.IpV4.Identification,
                Options            = IpV4Options.None,
                Protocol           = null, // Will be filled automatically.
                Ttl           = origPacket.Ethernet.IpV4.Ttl,
                TypeOfService = origPacket.Ethernet.IpV4.TypeOfService,
            };


            TcpLayer tcpLayer =
                new TcpLayer
            {
                SourcePort           = origPacket.Ethernet.IpV4.Tcp.SourcePort,
                DestinationPort      = origPacket.Ethernet.IpV4.Tcp.DestinationPort,
                Checksum             = null, // Will be filled automatically.
                SequenceNumber       = origPacket.Ethernet.IpV4.Tcp.SequenceNumber,
                AcknowledgmentNumber = origPacket.Ethernet.IpV4.Tcp.AcknowledgmentNumber,
                ControlBits          = TcpControlBits.Acknowledgment,
                Window        = origPacket.Ethernet.IpV4.Tcp.Window,
                UrgentPointer = origPacket.Ethernet.IpV4.Tcp.UrgentPointer,
                Options       = origPacket.Ethernet.IpV4.Tcp.Options,
            };


            PayloadLayer payloadLayer =
                new PayloadLayer
            {
                Data = origPacket.Ethernet.IpV4.Tcp.Payload,
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, 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));
            }
        }
Esempio n. 29
0
        /// <summary>
        /// This function build an TCP over IPv4 over Ethernet with payload packet.
        /// </summary>
        public static Packet BuildTcpPacket()
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
            {
                Source      = new MacAddress(macAddressSource),
                Destination = new MacAddress(macAddressDest),
                EtherType   = EthernetType.None,   // Will be filled automatically.
            };

            IpV4Layer ipV4Layer =
                new IpV4Layer
            {
                Source             = new IpV4Address(ipSource),
                CurrentDestination = new IpV4Address(ipDest),
                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));
        }
Esempio n. 30
0
        public void Send(PUP p)
        {
            //
            // Write PUP to ethernet:
            //

            // Build the outgoing data; this is:
            // 1st word: length of data following
            // 2nd word: 3mbit destination / source bytes
            // 3rd word: frame type (PUP)
            byte[] encapsulatedFrame = new byte[6 + p.RawData.Length];

            // 3mbit Packet length
            encapsulatedFrame[0] = (byte)((p.RawData.Length / 2 + 2) >> 8);
            encapsulatedFrame[1] = (byte)(p.RawData.Length / 2 + 2);

            // addressing
            encapsulatedFrame[2] = p.DestinationPort.Host;
            encapsulatedFrame[3] = p.SourcePort.Host;

            // frame type
            encapsulatedFrame[4] = (byte)(_pupFrameType >> 8);
            encapsulatedFrame[5] = (byte)_pupFrameType;

            // Actual data
            p.RawData.CopyTo(encapsulatedFrame, 6);

            MacAddress destinationMac = new MacAddress(_10mbitBroadcast);

            // Build the outgoing packet; place the source/dest addresses, type field and the PUP data.
            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source      = _interface.GetMacAddress(),
                Destination = destinationMac,
                EtherType   = (EthernetType)_3mbitFrameType,
            };

            PayloadLayer payloadLayer = new PayloadLayer
            {
                Data = new Datagram(encapsulatedFrame),
            };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, payloadLayer);

            // Send it over the 'net!
            _communicator.SendPacket(builder.Build(DateTime.Now));
        }
        private static void CreateRandomEthernetPayload(Random random, EthernetBaseLayer ethernetBaseLayer, List <ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            ethernetBaseLayer.EtherType = EthernetType.None;
            switch (random.NextInt(0, 7))
            {
            case 0:     // VLanTaggedFrame.
            case 1:
                VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
                layers.Add(vLanTaggedFrameLayer);
                CreateRandomEthernetPayload(random, vLanTaggedFrameLayer, layers);
                return;

            case 2:     // ARP.
                EthernetLayer ethernetLayer = (ethernetBaseLayer as EthernetLayer);
                if (ethernetLayer != null)
                {
                    ethernetLayer.Destination = MacAddress.Zero;
                }
                layers.Add(random.NextArpLayer());
                return;

            case 3:     // IPv4.
            case 4:
                IpV4Layer ipV4Layer = random.NextIpV4Layer();
                layers.Add(ipV4Layer);
                CreateRandomIpPayload(random, ipV4Layer, layers);
                return;

            case 5:     // IPv6
            case 6:
                IpV6Layer ipV6Layer = random.NextIpV6Layer(random.NextBool(20));
                layers.Add(ipV6Layer);
                CreateRandomIpPayload(random, ipV6Layer, layers);
                return;

            default:
                throw new InvalidOperationException("Invalid value.");
            }
        }
Esempio n. 32
0
        static void notifySpoof(LivePacketDevice selectedDevice, string notifyString, string sourceIP, ushort sourcePort, string destIP, ushort destPort)
        {
            byte[] temp = System.Text.Encoding.ASCII.GetBytes(notifyString);



            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source      = LivePacketDeviceExtensions.GetMacAddress(selectedDevice),
                Destination = new MacAddress("01:00:5E:7F:FF:FA"),
                EtherType   = EthernetType.None,
            };

            IpV4Layer ipV4Layer = new IpV4Layer
            {
                Source             = new IpV4Address(sourceIP),
                CurrentDestination = new IpV4Address(destIP),
                Fragmentation      = IpV4Fragmentation.None,
                HeaderChecksum     = null,

                Identification = 1,
                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));
            }
        }
Esempio n. 33
0
        /**
         * Invoked from Server.Send(); actively sends the data given
         *
         * Can (and should) be modified to actively handle a buffer of data and run as a thread
         *
         * mac_address refers to the destination FPGA
         */
        void Speak(string mac_address, byte[] data)
        {
            this.Validate();

            // "using" keyword ensures the channel is disposed of properly (i.e. dropped) as soon as we exit scope
            using (PacketCommunicator channel = active.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) {
                Utils.Utils.Write("Speaking on active channel " + active.Name, truncate: true);
                if (!this.halt)
                {
                    EthernetLayer ethernet = new EthernetLayer {
                        Source      = active.GetMacAddress(),
                        Destination = new MacAddress(mac_address),

                        /**
                         * We are using the IEEE 802.3 protocol
                         *		the EtherType field designates instead the length of the payload
                         *		http://bit.ly/16szTDm
                         *
                         * http://bit.ly/14hiChO
                         *
                         * In the V5 specifications sheet (http://bit.ly/1ccaXXH), the payload length designates the command type
                         *		in the FPGA board and is represented schematically by the l(1) and l(2)
                         *		registers
                         */
                        EtherType = (PcapDotNet.Packets.Ethernet.EthernetType)data.Length
                    };
                    PayloadLayer payload = new PayloadLayer {
                        Data = new Datagram(data)
                    };
                    PacketBuilder builder = new PacketBuilder(ethernet, payload);
                    Packet        packet  = builder.Build(DateTime.Now);

                    // determine if we are to pause until board transmits data
                    this.readback = false;
                    if ((data.Length == Data.RegisterWriteData.length) && (packet.Buffer[15] != 0x00))
                    {
                        this.readback = true;
                    }

                    channel.SendPacket(packet);
                    lock (this.l_lock) {
                        Utils.Utils.Log(packet.Buffer, "sent");
                    }
                }
            }
        }
Esempio n. 34
0
        public UDPSendPacket(string MACsrc, string MACdst, string IPsrc, string IPdst,
            string IpId, string TTL, string PORTsrc, string data)
        {
            GetBase(MACsrc, MACdst, IPsrc, IPdst, IpId, TTL);

               udpLayer = new UdpLayer
               {
               SourcePort = StringToUShort(PORTsrc),
               DestinationPort = 25,
               Checksum = null, // Will be filled automatically.
               CalculateChecksumValue = true,
               };

             payloadLayer = new PayloadLayer
             {
                 Data = new Datagram(Encoding.ASCII.GetBytes(data)),
             };
        }
Esempio n. 35
0
        public TCPSendPacket(string MACsrc, string MACdst, string IPsrc, string IPdst, 
            string IpId, string TTL, string PORTsrc, string SQN, string ACK, string WIN, string data)
        {
            GetBase(MACsrc, MACdst, IPsrc, IPdst, IpId, TTL);

            tcpLayer = new TcpLayer
            {
                SourcePort = StringToUShort(PORTsrc),
                DestinationPort = 25,
                Checksum = null, // Will be filled automatically.
                SequenceNumber = StringToUShort(SQN),
                AcknowledgmentNumber = StringToUShort(ACK),
                ControlBits = TcpControlBits.Acknowledgment,
                Window = StringToUShort(WIN),
                UrgentPointer = 0,
                Options = TcpOptions.None,
            };

            payloadLayer = new PayloadLayer
            {
                Data = new Datagram(Encoding.ASCII.GetBytes(data)),
            };
        }
Esempio n. 36
0
        /// <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);
        }
Esempio n. 37
0
        //Forwards packets from the gateway to the victim and from the victim to the gateway
        public void Forwarder(Packet packet)
        {
            using (PacketCommunicator communicator = _forwardingListener.Device.Open())
            {

                var etherpacket = packet.Ethernet;
                var ippacket = etherpacket.IpV4;

                PayloadLayer payloadLayer;
                PacketBuilder builder;

                MacAddress newMacDestination;
                String ipDestination = ippacket.Destination.ToString();
                try
                {
                    newMacDestination = new MacAddress(_targets.First(x => x.IP == ipDestination).MAC);
                }
                catch
                {
                    newMacDestination = new MacAddress(_gateway.MAC);
                }

                EthernetLayer ethernetLayer =
                    new EthernetLayer
                    {
                        Source = etherpacket.Destination,
                        Destination = newMacDestination,
                        EtherType = EthernetType.IpV4,
                    };

                IpV4Layer ipLayer =
                    new IpV4Layer
                    {
                        Source = ippacket.Source,
                        CurrentDestination = ippacket.Destination,
                        Fragmentation = ippacket.Fragmentation,
                        Identification = ippacket.Identification,
                        Options = ippacket.Options,
                        HeaderChecksum = null, // Will be filled automatically.
                        Ttl = ippacket.Ttl,
                        TypeOfService = ippacket.TypeOfService,
                    };

                switch (ippacket.Protocol)
                {
                    case IpV4Protocol.Udp:

                        var udpPacket = ippacket.Udp;

                        //dns spoofing
                        if (IsDnsSpoofingEnabled && udpPacket.DestinationPort == 53 && udpPacket.Dns.Queries[0].DnsType == DnsType.A)
                        {
                            try
                            {
                                var spoofingEntry = _dnsSpoofingList.First(x => x.DomainName == udpPacket.Dns.Queries[0].DomainName.ToString());
                                communicator.SendPacket(CreateDnsReply(etherpacket, new IpV4Address(spoofingEntry.IP)));
                                return;
                            }
                            catch { }
                        }

                        UdpLayer udpLayer = new UdpLayer
                        {
                            SourcePort = udpPacket.SourcePort,
                            DestinationPort = udpPacket.DestinationPort,
                            Checksum = null, // Will be filled automatically.
                            CalculateChecksumValue = true
                        };
                        payloadLayer = new PayloadLayer
                        {
                            Data = udpPacket.Payload
                        };
                        builder = new PacketBuilder(ethernetLayer, ipLayer, udpLayer, payloadLayer);
                        break;

                    case IpV4Protocol.Tcp:
                        var tcpPacket = ippacket.Tcp;
                        TcpLayer tcpLayer = new TcpLayer
                        {
                            SourcePort = tcpPacket.SourcePort,
                            DestinationPort = tcpPacket.DestinationPort,
                            Checksum = null, // Will be filled automatically.
                            AcknowledgmentNumber = tcpPacket.AcknowledgmentNumber,
                            ControlBits = tcpPacket.ControlBits,
                            Window = tcpPacket.Window,
                            UrgentPointer = tcpPacket.UrgentPointer,
                            Options = tcpPacket.Options,
                            SequenceNumber = tcpPacket.SequenceNumber
                        };

                        payloadLayer = new PayloadLayer()
                        {
                            Data = tcpPacket.Payload
                        };
                        builder = new PacketBuilder(ethernetLayer, ipLayer, tcpLayer, payloadLayer);
                        break;

                    default:
                        Console.WriteLine("packet type was not udp or tcp!");
                        return;
                }

                communicator.SendPacket(builder.Build(DateTime.Now));
            }
        }
Esempio n. 38
0
            //public Packet CreateDhcpPacket(int optionsSize, byte[] options, bool firstPacket, byte[] clientIp = null)
            //{
            //    EthernetLayer ethernetLayer = new EthernetLayer
            //    {
            //        Source = ifHardwareAddress,
            //        Destination = new MacAddress("FF:FF:FF:FF:FF:FF"),
            //        EtherType = EthernetType.None,
            //    };

            //    IpV4Layer ipV4Layer = new IpV4Layer
            //    {
            //        Source = new IpV4Address("0.0.0.0"),
            //        CurrentDestination = new IpV4Address("255.255.255.255"),
            //        Fragmentation = IpV4Fragmentation.None,
            //        HeaderChecksum = null, // Will be filled automatically.
            //        Identification = ipID,
            //        Options = IpV4Options.None,
            //        Protocol = null,
            //        Ttl = 128,
            //        TypeOfService = 0,
            //    };

            //    UdpLayer udpLayer = new UdpLayer
            //    {
            //        SourcePort = 68,
            //        DestinationPort = 67,
            //        Checksum = null, // Will be filled automatically.
            //        CalculateChecksumValue = true,
            //    };

            //    Dhcp dhcpLayer = new Dhcp(optionsSize);
            //    if (firstPacket)
            //        dhcpId = dhcpLayer.Id;
            //    else
            //        dhcpLayer.Id = dhcpId;
            //    if (clientIp != null)
            //        dhcpLayer.ClientIp = clientIp;
            //    dhcpLayer.ClientHardwareAddress = ifHardwareAddressByte;
            //    dhcpLayer.Options = options;


            //    PayloadLayer payloadLayer = new PayloadLayer
            //    {
            //        Data = new Datagram(dhcpLayer.toByteArray()),
            //    };

            //    PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);
            //    return builder.Build(DateTime.Now);
            //}
            //public void SendArp()
            //{
            //    EthernetLayer ethernetLayer = new EthernetLayer
            //    {
            //        Source = ifHardwareAddress,
            //        Destination = new MacAddress("FF:FF:FF:FF:FF:FF"),
            //        EtherType = EthernetType.None,
            //    };

            //    ArpLayer arpLayer = new ArpLayer
            //    {
            //        ProtocolType = EthernetType.IpV4,
            //        Operation = ArpOperation.Request,
            //        SenderHardwareAddress = ifHardwareAddressByte.AsReadOnly(),
            //        SenderProtocolAddress = ownProtocolAddressByte.AsReadOnly(),
            //        TargetHardwareAddress = new byte[] { 0, 0, 0, 0, 0, 0 }.AsReadOnly(),
            //        TargetProtocolAddress = gatewayProtocolAddressByte.AsReadOnly(),
            //    };

            //    PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer);
            //    SendPacket(builder.Build(DateTime.Now));
            //}
            //public void ReceiveIpAddress()
            //{
            //    int retries = 0;
            //    int timewait = 0;
            //    dhcpState = 0;
            //    arpState = 0;

            //    byte[] options;
            //    while (true)
            //    {
            //        if (dhcpState == 1 && retries > 1 ||
            //            dhcpState == 3 && retries > 1)
            //        {
            //            dhcpState = 6;
            //            offeredIp = BitConverter.GetBytes(ifProtocolAddress.ToValue() & ifProtocolMask.ToValue() + 1).Reverse().ToArray();
            //        }
            //        if (dhcpState == 0 || dhcpState == 1 && timewait > 10)
            //        {
            //            options = new byte[]
            //        { 
            //            53, 1, 1,
            //            61, 7, 1, 0x0A, 0x03, 0x03, 0x03, 0x03, (byte)dhcpClientId,
            //            12, 4, 78, 77, 76, 66,
            //            60, 8, 0x4d, 0x53, 0x46, 0x54, 0x20, 0x35, 0x2e, 0x30,
            //            255,
            //            0, 0, 0,
            //        };
            //            SendPacket(CreateDhcpPacket(32, options, true));
            //            if (dhcpState == 0)
            //                retries = 0;
            //            else
            //                retries++;
            //            timewait = 0;
            //            dhcpState = 1;
            //        }
            //        else if (dhcpState == 2 || dhcpState == 3 && timewait > 10)
            //        {
            //            options = new byte[]
            //        { 
            //            53, 1, 3,
            //            61, 7, 1, 0x0A, 0x03, 0x03, 0x03, 0x03, (byte)dhcpClientId,
            //            12, 4, 78, 77, 76, 66,
            //            50, 4, offeredIp[0], offeredIp[1], offeredIp[2], offeredIp[3],
            //            54, 4, dhcpServer[0], dhcpServer[1], dhcpServer[2], dhcpServer[3],
            //            60, 8, 0x4d, 0x53, 0x46, 0x54, 0x20, 0x35, 0x2e, 0x30,
            //            255,
            //            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            //        };
            //            SendPacket(CreateDhcpPacket(60, options, false));
            //            if (dhcpState == 2)
            //                retries = 0;
            //            else
            //                retries++;
            //            timewait = 0;
            //            dhcpState = 3;
            //        }
            //        else if (dhcpState == 4 && arpState == 1 && retries > 1)
            //        {
            //            if ((BitConverter.ToUInt32(offeredIp.Reverse().ToArray(), 0) + 1 & ifProtocolMask.ToValue()) !=
            //                (ifProtocolAddress.ToValue() & ifProtocolMask.ToValue()))
            //            {
            //                Global.WriteLog("DHCP: IP " + offeredIp.SequenceToString(".") + " is in a different network!");
            //                offeredIp = BitConverter.GetBytes(ifProtocolAddress.ToValue() & ifProtocolMask.ToValue() + 1).Reverse().ToArray();
            //                dhcpState = 6;
            //                arpState = 0;
            //                continue;
            //            }
            //            dhcpState = 5;
            //            arpState = 0;
            //            Global.WriteLog("DHCP: IP " + offeredIp.SequenceToString(".") + " OK!");
            //            dhcpClientId++;
            //            ownProtocolAddressByte = offeredIp;
            //            ownProtocolAddressString = ownProtocolAddressByte.SequenceToString(".");
            //            ownProtocolAddress = new IpV4Address(ownProtocolAddressString);
            //            SendArp();
            //            break;
            //        }
            //        else if (dhcpState == 4 && arpState == 0 || dhcpState == 4 && arpState == 1 && timewait > 10)
            //        {
            //            EthernetLayer ethernetLayer = new EthernetLayer
            //            {
            //                Source = ifHardwareAddress,
            //                Destination = new MacAddress("FF:FF:FF:FF:FF:FF"),
            //                EtherType = EthernetType.None,
            //            };

            //            ArpLayer arpLayer = new ArpLayer
            //            {
            //                ProtocolType = EthernetType.IpV4,
            //                Operation = ArpOperation.Request,
            //                SenderHardwareAddress = ifHardwareAddressByte.AsReadOnly(),
            //                SenderProtocolAddress = new byte[] { 0, 0, 0, 0 }.AsReadOnly(),
            //                TargetHardwareAddress = new byte[] { 0, 0, 0, 0, 0, 0 }.AsReadOnly(),
            //                TargetProtocolAddress = offeredIp.AsReadOnly(),
            //            };

            //            PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer);
            //            SendPacket(builder.Build(DateTime.Now));
            //            if (arpState == 0)
            //                retries = 0;
            //            else
            //                retries++;
            //            timewait = 0;
            //            arpState = 1;
            //        }
            //        else if (dhcpState == 4 && arpState == 2)
            //        {
            //            Global.WriteLog("DHCP: IP " + offeredIp.SequenceToString(".") + " is already used!");
            //            options = new byte[]
            //        { 
            //            53, 1, 4,
            //            61, 7, 1, 0x0A, 0x03, 0x03, 0x03, 0x03, (byte)dhcpClientId,
            //            50, 4, offeredIp[0], offeredIp[1], offeredIp[2], offeredIp[3],
            //            54, 4, dhcpServer[0], dhcpServer[1], dhcpServer[2], dhcpServer[3],
            //            255,
            //            //0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            //        };
            //            SendPacket(CreateDhcpPacket(25, options, false, offeredIp));
            //            arpState = 0;
            //            dhcpState = 0;
            //        }
            //        else if (dhcpState == 6 && arpState == 1 && retries > 1)
            //        {
            //            dhcpState = 5;
            //            arpState = 0;
            //            Global.WriteLog("IPAlloc: IP " + offeredIp.SequenceToString(".") + " OK!");
            //            dhcpClientId++;
            //            ownProtocolAddressByte = offeredIp;
            //            ownProtocolAddressString = ownProtocolAddressByte.SequenceToString(".");
            //            ownProtocolAddress = new IpV4Address(ownProtocolAddressString);
            //            SendArp();
            //            break;
            //        }
            //        else if (dhcpState == 6 && arpState == 0 || dhcpState == 6 && arpState == 1 && timewait > 10)
            //        {
            //            EthernetLayer ethernetLayer = new EthernetLayer
            //            {
            //                Source = ifHardwareAddress,
            //                Destination = new MacAddress("FF:FF:FF:FF:FF:FF"),
            //                EtherType = EthernetType.None,
            //            };

            //            ArpLayer arpLayer = new ArpLayer
            //            {
            //                ProtocolType = EthernetType.IpV4,
            //                Operation = ArpOperation.Request,
            //                SenderHardwareAddress = ifHardwareAddressByte.AsReadOnly(),
            //                SenderProtocolAddress = new byte[] { 0, 0, 0, 0 }.AsReadOnly(),
            //                TargetHardwareAddress = new byte[] { 0, 0, 0, 0, 0, 0 }.AsReadOnly(),
            //                TargetProtocolAddress = offeredIp.AsReadOnly(),
            //            };

            //            PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer);
            //            SendPacket(builder.Build(DateTime.Now));
            //            if (arpState == 0)
            //                retries = 0;
            //            else
            //                retries++;
            //            timewait = 0;
            //            arpState = 1;
            //        }
            //        else if (dhcpState == 6 && arpState == 2)
            //        {
            //            if ((BitConverter.ToUInt32(offeredIp.Reverse().ToArray(), 0) + 1 & ifProtocolMask.ToValue()) !=
            //                (ifProtocolAddress.ToValue() & ifProtocolMask.ToValue()))
            //            {
            //                Global.WriteLog("IPAlloc: Couldn't allocate IP address!");
            //                break;
            //            }
            //            Global.WriteLog("IPAlloc: IP " + offeredIp.SequenceToString(".") + " is already used!");
            //            offeredIp = BitConverter.GetBytes((BitConverter.ToUInt32(offeredIp.Reverse().ToArray(), 0) + 1)).Reverse().ToArray();
            //            arpState = 0;
            //        }
            //        Thread.Sleep(100);
            //        timewait++;
            //    }
            //}

            public void ReceivePackets()
            {
                // Retrieve the device list from the local machine
                IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;

                // Find the NPF device of the 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 " + ifHardwareAddressString + " and (ip or arp)) or (ether dst FF:FF:FF:FF:FF:FF and arp)");
                        communicator.SetFilter("(ether dst " + ifHardwareAddressString + " and ip and (udp or icmp))");
                        Initialized.Set();
                        communicator.ReceivePackets(0, (packet) =>
                        {
                            if (!ThreadActive.IsSet)
                            {
                                communicator.Break();
                                return;
                            }
                            //if (ownProtocolAddressString == null)
                            //{
                            //    if (packet.Ethernet.IpV4.Udp.SourcePort == 67 && packet.Ethernet.IpV4.Udp.DestinationPort == 68 && (PublicVars.dhcpState == 1 || PublicVars.dhcpState == 3))
                            //    {
                            //        Dhcp dhcpReply = new Dhcp(packet.Ethernet.IpV4.Udp.Payload.ToArray());
                            //        if (PublicVars.dhcpId == dhcpReply.Id && dhcpReply.MessageType == 2)
                            //        {
                            //            if (dhcpReply.getOption((int)DhcpOptionCode.DhcpMsgType)[0] == 2)
                            //            {
                            //                PublicVars.offeredIp = dhcpReply.YourIp;
                            //                PublicVars.dhcpServer = dhcpReply.getOption((int)DhcpOptionCode.DhcpServerId);
                            //                PublicVars.dhcpState = 2;
                            //            }
                            //            else if (dhcpReply.getOption((int)DhcpOptionCode.DhcpMsgType)[0] == 5)
                            //            {
                            //                PublicVars.dhcpState = 4;
                            //            }
                            //            else if (dhcpReply.getOption((int)DhcpOptionCode.DhcpMsgType)[0] == 6)
                            //            {
                            //                PublicVars.offeredIp = null;
                            //                PublicVars.dhcpState = 0;
                            //                PublicVars.dhcpId = 0;
                            //            }
                            //        }
                            //    }
                            //    else if (packet.Ethernet.EtherType == EthernetType.Arp && PublicVars.arpState == 1)
                            //    {
                            //        if (packet.Ethernet.Arp.SenderProtocolAddress.SequenceEqual(PublicVars.offeredIp))
                            //            PublicVars.arpState = 2;
                            //    }
                            //}
                            //if (packet.Ethernet.EtherType == EthernetType.Arp)
                            //{
                            //    if (packet.Ethernet.Arp.TargetProtocolAddress.SequenceEqual(ownProtocolAddressByte) &&
                            //        packet.Ethernet.Arp.Operation == ArpOperation.Request)
                            //    {
                            //        EthernetLayer ethernetLayer = new EthernetLayer
                            //        {
                            //            Source = ifHardwareAddress,
                            //            Destination = packet.Ethernet.Source,
                            //            EtherType = EthernetType.None,
                            //        };

                            //        ArpLayer arpLayer = new ArpLayer
                            //        {
                            //            ProtocolType = EthernetType.IpV4,
                            //            Operation = ArpOperation.Reply,
                            //            SenderHardwareAddress = ifHardwareAddressByte.AsReadOnly(),
                            //            SenderProtocolAddress = packet.Ethernet.Arp.TargetProtocolAddress,
                            //            TargetHardwareAddress = packet.Ethernet.Arp.SenderHardwareAddress,
                            //            TargetProtocolAddress = packet.Ethernet.Arp.SenderProtocolAddress,
                            //        };

                            //        PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer);
                            //        communicator.SendPacket(builder.Build(DateTime.Now));
                            //    }
                            //}
                            if (tapWorker.Initialized.IsSet)
                            {
                                if (packet.Ethernet.IpV4.Length > MTU) // only UDP and ICMP
                                {
                                    EthernetLayer ethernetLayer = new EthernetLayer
                                    {
                                        Source = ifHardwareAddress,
                                        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.Fragmentation.Options == IpV4FragmentationOptions.DoNotFragment ||
                                packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset == 0)
                                {
                                    if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.InternetControlMessageProtocol)
                                    {

                                        if (packet.Ethernet.IpV4.Destination == ifProtocolAddress)
                                            if ((packet.Ethernet.IpV4.Icmp.MessageType == IcmpMessageType.EchoReply || packet.Ethernet.IpV4.Fragmentation.Offset > 0))
                                            {
                                                EthernetLayer ethernetLayer = new EthernetLayer
                                                {
                                                    Source = tapWorker.ownHardwareAddress,
                                                    Destination = tapWorker.ifHardwareAddress,
                                                    EtherType = EthernetType.None,
                                                };
                                                IpV4Layer ipV4Layer = new IpV4Layer
                                                {
                                                    Source = packet.Ethernet.IpV4.Source,
                                                    CurrentDestination = tapWorker.ifProtocolAddress,
                                                    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 = packet.Ethernet.IpV4.Ttl,
                                                    TypeOfService = packet.Ethernet.IpV4.TypeOfService,
                                                };
                                                PayloadLayer payloadLayer = new PayloadLayer
                                                {
                                                    Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()),
                                                };
                                                PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
                                                tapWorker.SendPacket(builder.Build(DateTime.Now));
                                            }
                                    }
                                    //else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
                                    //{
                                    //    if (packet.Ethernet.IpV4.Destination == ifProtocolAddress)
                                    //    {
                                    //        lock (RoutingTable.lockObj)
                                    //            routingObject = RoutingTable.TranslateBack(packet);
                                    //        if (routingObject.ifIndex == -1)
                                    //            return;
                                    //        EthernetLayer ethernetLayer = new EthernetLayer
                                    //        {
                                    //            Source = TapWorker.ownHardwareAddress,
                                    //            Destination = TapWorker.ifHardwareAddress,
                                    //            EtherType = packet.Ethernet.EtherType,
                                    //        };
                                    //        IpV4Layer ipV4Layer = new IpV4Layer
                                    //        {
                                    //            Source = packet.Ethernet.IpV4.Source,
                                    //            CurrentDestination = TapWorker.ifProtocolAddress,
                                    //            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 = packet.Ethernet.IpV4.Ttl,
                                    //            TypeOfService = packet.Ethernet.IpV4.TypeOfService,
                                    //        };
                                    //        TcpLayer tcpLayer = new TcpLayer
                                    //        {
                                    //            SourcePort = packet.Ethernet.IpV4.Tcp.SourcePort,
                                    //            DestinationPort = routingObject.localPort,
                                    //            Checksum = null, // Will be filled automatically.
                                    //            SequenceNumber = packet.Ethernet.IpV4.Tcp.SequenceNumber,
                                    //            AcknowledgmentNumber = routingObject.ack,
                                    //            ControlBits = packet.Ethernet.IpV4.Tcp.ControlBits,
                                    //            Window = packet.Ethernet.IpV4.Tcp.Window,
                                    //            UrgentPointer = packet.Ethernet.IpV4.Tcp.UrgentPointer,
                                    //            Options = packet.Ethernet.IpV4.Tcp.Options,
                                    //        };
                                    //        PayloadLayer payloadLayer = new PayloadLayer
                                    //        {
                                    //            Data = new Datagram(packet.Ethernet.IpV4.Tcp.Payload.ToArray()),
                                    //        };
                                    //        PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);
                                    //        MainThread.tapWorker.SendPacket(builder.Build(DateTime.Now));
                                    //    }
                                    //}
                                    else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
                                    {
                                        if (packet.Ethernet.IpV4.Destination == ifProtocolAddress)
                                        {
                                            phyRoutingObject = RoutingTable.IsUdpConn(packet);
                                            if (phyRoutingObject.ifIndex == -1)
                                                return;
                                            EthernetLayer ethernetLayer = new EthernetLayer
                                            {
                                                Source = tapWorker.ownHardwareAddress,
                                                Destination = tapWorker.ifHardwareAddress,
                                                EtherType = packet.Ethernet.EtherType,
                                            };
                                            IpV4Layer ipV4Layer = new IpV4Layer
                                            {
                                                Source = packet.Ethernet.IpV4.Source,
                                                CurrentDestination = tapWorker.ifProtocolAddress,
                                                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 = packet.Ethernet.IpV4.Ttl,
                                                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);
                                            tapWorker.SendPacket(builder.Build(DateTime.Now));
                                        }
                                    }
                                }
                                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);
                    }
                }
            }
Esempio n. 39
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);
        }
        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));
            }

        }
Esempio n. 41
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;
     }
 }
Esempio n. 42
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));
            }

        }
Esempio n. 43
0
            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));
                    }

                }
            }
Esempio n. 44
0
        private static Packet BuildPacket(string httpString)
        {
            MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
            MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");

            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source = ethernetSource,
                Destination = ethernetDestination
            };

            Random random = new Random();

            IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
            ipV4Layer.HeaderChecksum = null;
            TcpLayer tcpLayer = random.NextTcpLayer();

            PayloadLayer payloadLayer = new PayloadLayer
            {
                Data = new Datagram(Encoding.ASCII.GetBytes(httpString))
            };

            Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);

            Assert.IsTrue(packet.IsValid);

            // Ethernet
            ethernetLayer.EtherType = EthernetType.IpV4;
            Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");

            // IpV4
            ipV4Layer.Protocol = IpV4Protocol.Tcp;
            ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
            Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer");
            ipV4Layer.HeaderChecksum = null;

            // TCP
            tcpLayer.Checksum = packet.Ethernet.IpV4.Tcp.Checksum;
            Assert.AreEqual(tcpLayer, packet.Ethernet.IpV4.Tcp.ExtractLayer(), "TCP Layer");

            return packet;
        }
Esempio n. 45
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));
                 }
             }
        }
Esempio n. 46
0
        /// <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);
        }
Esempio n. 47
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);
                    }
                }
            }
        }
Esempio n. 48
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);
        }
        //# 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);
        }
Esempio n. 50
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));
 }
Esempio n. 51
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);
        }
Esempio n. 52
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")),
                };
        }
Esempio n. 53
0
        /// <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);
        }