private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e)
        {
            if (e.Packet is EthernetPacket)
            {
                EthernetPacket eth = ((EthernetPacket)e.Packet);
                Console.WriteLine("Original Eth packet: " + eth.ToColoredString(false));

                //Manipulate ethernet parameters
                eth.SourceHwAddress      = PhysicalAddress.Parse("00-11-22-33-44-55");
                eth.DestinationHwAddress = PhysicalAddress.Parse("00-99-88-77-66-55");

                if (e.Packet is IPPacket)
                {
                    IPPacket ip = ((IPPacket)e.Packet);
                    Console.WriteLine("Original IP packet: " + ip.ToColoredString(false));

                    //manipulate IP parameters
                    ip.SourceAddress      = System.Net.IPAddress.Parse("1.2.3.4");
                    ip.DestinationAddress = System.Net.IPAddress.Parse("44.33.22.11");
                    ip.TimeToLive         = 11;

                    //Recalculate the IP checksum
                    ip.ComputeIPChecksum();

                    if (ip is TCPPacket)
                    {
                        TCPPacket tcp = ((TCPPacket)ip);
                        Console.WriteLine("Original TCP packet: " + tcp.ToColoredString(false));

                        //manipulate TCP parameters
                        tcp.SourcePort           = 9999;
                        tcp.DestinationPort      = 8888;
                        tcp.Syn                  = !tcp.Syn;
                        tcp.Fin                  = !tcp.Fin;
                        tcp.Ack                  = !tcp.Ack;
                        tcp.WindowSize           = 500;
                        tcp.AcknowledgmentNumber = 800;
                        tcp.SequenceNumber       = 800;

                        //Recalculate the TCP checksum
                        tcp.ComputeTCPChecksum();
                    }

                    if (ip is UDPPacket)
                    {
                        UDPPacket udp = ((UDPPacket)ip);
                        Console.WriteLine("Original UDP packet: " + udp.ToColoredString(false));

                        //manipulate UDP parameters
                        udp.SourcePort      = 9999;
                        udp.DestinationPort = 8888;

                        //Recalculate the UDP checksum
                        udp.ComputeUDPChecksum();
                    }
                }
                Console.WriteLine("Manipulated Eth packet: " + eth.ToColoredString(false));
            }
        }
        private static void device_PcapOnPacketArrival(object sender, Tamir.IPLib.Packets.Packet packet)
        {
            if (packet is EthernetPacket)
            {
                EthernetPacket eth = ((EthernetPacket)packet);
                Console.WriteLine("Original packet: " + eth.ToColoredString(false));

                //Manipulate ethernet parameters
                eth.SourceHwAddress      = "00:11:22:33:44:55";
                eth.DestinationHwAddress = "00:99:88:77:66:55";

                if (packet is IPPacket)
                {
                    IPPacket ip = ((IPPacket)packet);

                    //manipulate IP parameters
                    ip.SourceAddress      = "1.2.3.4";
                    ip.DestinationAddress = "44.33.22.11";
                    ip.TimeToLive         = 11;

                    //Recalculate the IP checksum
                    ip.ComputeIPChecksum();

                    if (ip is TCPPacket)
                    {
                        TCPPacket tcp = ((TCPPacket)ip);

                        //manipulate TCP parameters
                        tcp.SourcePort            = 9999;
                        tcp.DestinationPort       = 8888;
                        tcp.Syn                   = !tcp.Syn;
                        tcp.Fin                   = !tcp.Fin;
                        tcp.Ack                   = !tcp.Ack;
                        tcp.WindowSize            = 500;
                        tcp.AcknowledgementNumber = 800;
                        tcp.SequenceNumber        = 800;

                        //Recalculate the TCP checksum
                        tcp.ComputeTCPChecksum();
                    }

                    if (ip is UDPPacket)
                    {
                        UDPPacket udp = ((UDPPacket)ip);

                        //manipulate UDP parameters
                        udp.SourcePort      = 9999;
                        udp.DestinationPort = 8888;

                        //Recalculate the UDP checksum
                        udp.ComputeUDPChecksum();
                    }
                }
                Console.WriteLine("Manipulated packet: " + eth.ToColoredString(false));
            }
        }
Esempio n. 3
0
        public UDPPacket CreateUdpPacket(IPv4Packet ipv4Packet)
        {
            UDPPacket udpPacket = new UDPPacket(
                EthernetFields_Fields.ETH_HEADER_LEN,
                ipv4Packet.Bytes);

            // UDP fields
            udpPacket.SourcePort      = Int32.Parse(textBoxSourcePort.Text);
            udpPacket.DestinationPort = Int32.Parse(textBoxDestinationPort.Text);
            udpPacket.Length          = Int32.Parse(textBoxLength.Text);

            // Calculate checksum
            udpPacket.ComputeUDPChecksum(true);

            return(udpPacket);
        }
Esempio n. 4
0
        public static void Run(string[] args)
        {
            string S    = "Hello";
            int    lLen = EthernetFields_Fields.ETH_HEADER_LEN;
            //bytes = System.Convert.ToByte(S);
            const int MIN_PKT_LEN = 42;

            byte[] data  = System.Text.Encoding.ASCII.GetBytes("HELLO");
            byte[] bytes = new byte[MIN_PKT_LEN + data.Length];
            Array.Copy(data, 0, bytes, MIN_PKT_LEN, data.Length);

            PcapDeviceList devices = SharpPcap.GetAllDevices();
            PcapDevice     device  = devices[2];
            NetworkDevice  netdev  = (NetworkDevice)device;

            UDPPacket packet = new UDPPacket(lLen, bytes);

            //Ethernet Fields
            packet.DestinationHwAddress = "001122334455";
            packet.SourceHwAddress      = netdev.MacAddress;
            packet.EthernetProtocol     = EthernetProtocols_Fields.IP;


            //IP Fields
            packet.DestinationAddress = "58.100.187.167";

            packet.SourceAddress  = netdev.IpAddress;
            packet.IPProtocol     = IPProtocols_Fields.UDP;
            packet.TimeToLive     = 20;
            packet.Id             = 100;
            packet.Version        = 4;
            packet.IPTotalLength  = bytes.Length - lLen;
            packet.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;

            //UDP Fields
            packet.DestinationPort = 9898;
            packet.SourcePort      = 80;
            packet.ComputeIPChecksum();
            packet.ComputeUDPChecksum();

            device.PcapOpen();
            device.PcapSendPacket(packet);
            device.PcapClose();
        }