Beispiel #1
0
        /// <summary> Convert captured packet data into an object.
        /// </summary>
        public static Packet dataToPacket(int linkType, byte[] bytes, Timeval tv)
        {
            int ethProtocol;

            // record the length of the headers associated with this link layer type.
            // this length is the offset to the header embedded in the packet.
            lLen = LinkLayer.getLinkLayerLength(linkType);

            // extract the protocol code for the type of header embedded in the
            // link-layer of the packet
            int offset = LinkLayer.getProtoOffset(linkType);

            if (offset == -1)
            {
                // if there is no embedded protocol, assume IP?
                ethProtocol = Packets.EthernetProtocols_Fields.IP;
            }
            else
            {
                ethProtocol = ArrayHelper.extractInteger(bytes, offset, Packets.EthernetFields.ETH_CODE_LEN);
            }

            // try to recognize the ethernet type..
            switch (ethProtocol)
            {
            // arp
            case Packets.EthernetProtocols_Fields.ARP:
                return(new ARPPacket(lLen, bytes, tv));

            case Packets.EthernetProtocols_Fields.IP:
                int ipProtocol = IPProtocol.extractProtocol(lLen, bytes);
                switch (ipProtocol)
                {
                // icmp
                case Packets.IPProtocols_Fields.ICMP:  return(new ICMPPacket(lLen, bytes, tv));

                // igmp

                case Packets.IPProtocols_Fields.IGMP:  return(new IGMPPacket(lLen, bytes, tv));

                // tcp

                case Packets.IPProtocols_Fields.TCP:  return(new TCPPacket(lLen, bytes, tv));

                // udp

                case Packets.IPProtocols_Fields.UDP:  return(new UDPPacket(lLen, bytes, tv));

                // unidentified ip..

                default:  return(new IPPacket(lLen, bytes, tv));
                }
            // ethernet level code not recognized, default to anonymous packet..
            //goto default;

            default:  return(new EthernetPacket(lLen, bytes, tv));
            }
        }
Beispiel #2
0
        public virtual void  setUp()
        {
            // get link layer length
            int linkLayerLen = LinkLayer.getLinkLayerLength(Packets.LinkLayers.EN10MB);

            // create syn-ack packet
            _synAck = new IPPacket(linkLayerLen, SYN_ACK_PACKET);
            // create psh-ack packet
            _pshAck = new IPPacket(linkLayerLen, PSH_ACK_PACKET);
            // create packet with random garbage
            byte[] badBytes = new byte[SYN_ACK_PACKET.Length];
            (new System.Random()).NextBytes((badBytes));
            _baddie = new IPPacket(linkLayerLen, badBytes);
        }
Beispiel #3
0
        public static void MyTest1()
        {
            byte[] SYN_ACK_PACKET = new byte[] { (byte)(0x00), (byte)(0x10), (byte)(0x7b), (byte)(0x38), (byte)(0x46), (byte)(0x33), (byte)(0x08), (byte)(0x00), (byte)(0x20), (byte)(0x89), (byte)(0xa5), (byte)(0x9f), (byte)(0x08), (byte)(0x00), (byte)(0x45), (byte)(0x00), (byte)(0x00), (byte)(0x2c), (byte)(0x93), (byte)(0x83), (byte)(0x40), (byte)(0x00), (byte)(0xff), (byte)(0x06), (byte)(0x6c), (byte)(0x38), (byte)(0xac), (byte)(0x10), (byte)(0x70), (byte)(0x32), (byte)(0x87), (byte)(0x0d), (byte)(0xd8), (byte)(0xbf), (byte)(0x00), (byte)(0x19), (byte)(0x50), (byte)(0x49), (byte)(0x78), (byte)(0xbe), (byte)(0xe0), (byte)(0xa7), (byte)(0x9f), (byte)(0x3a), (byte)(0xb4), (byte)(0x03), (byte)(0x60), (byte)(0x12), (byte)(0x22), (byte)(0x38), (byte)(0xfc), (byte)(0xc7), (byte)(0x00), (byte)(0x00), (byte)(0x02), (byte)(0x04), (byte)(0x05), (byte)(0xb4), (byte)(0x70), (byte)(0x6c) };
            byte[] PSH_ACK_PACKET = new byte[] { (byte)(0x08), (byte)(0x00), (byte)(0x20), (byte)(0x89), (byte)(0xa5), (byte)(0x9f), (byte)(0x00), (byte)(0x10), (byte)(0x7b), (byte)(0x38), (byte)(0x46), (byte)(0x33), (byte)(0x08), (byte)(0x00), (byte)(0x45), (byte)(0x00), (byte)(0x00), (byte)(0x3e), (byte)(0x87), (byte)(0x08), (byte)(0x40), (byte)(0x00), (byte)(0x3f), (byte)(0x06), (byte)(0x38), (byte)(0xa2), (byte)(0x87), (byte)(0x0d), (byte)(0xd8), (byte)(0xbf), (byte)(0xac), (byte)(0x10), (byte)(0x70), (byte)(0x32), (byte)(0x50), (byte)(0x49), (byte)(0x00), (byte)(0x19), (byte)(0x9f), (byte)(0x3a), (byte)(0xb4), (byte)(0x03), (byte)(0x78), (byte)(0xbe), (byte)(0xe0), (byte)(0xf8), (byte)(0x50), (byte)(0x18), (byte)(0x7d), (byte)(0x78), (byte)(0x86), (byte)(0xf0), (byte)(0x00), (byte)(0x00), (byte)(0x45), (byte)(0x48), (byte)(0x4c), (byte)(0x4f), (byte)(0x20), (byte)(0x61), (byte)(0x6c), (byte)(0x70), (byte)(0x68), (byte)(0x61), (byte)(0x2e), (byte)(0x61), (byte)(0x70), (byte)(0x70), (byte)(0x6c), (byte)(0x65), (byte)(0x2e), (byte)(0x65), (byte)(0x64), (byte)(0x75), (byte)(0x0d), (byte)(0x0a) };

            // get link layer length
            int linkLayerLen = LinkLayer.getLinkLayerLength(Packets.LinkLayers.EN10MB);
            // create syn-ack packet
            IPPacket _synAck = new IPPacket(linkLayerLen, SYN_ACK_PACKET);
            // create psh-ack packet
            IPPacket _pshAck = new IPPacket(linkLayerLen, PSH_ACK_PACKET);

            // create packet with random garbage
            byte[] badBytes = new byte[SYN_ACK_PACKET.Length];
            (new System.Random()).NextBytes((badBytes));
            IPPacket _baddie = new IPPacket(linkLayerLen, badBytes);

            IPPacket.TestProbe probe = new Packets.IPPacket.TestProbe(_synAck);
            Console.WriteLine("Computed IP checksum mismatch, should be " + System.Convert.ToString(_synAck.IPChecksum, 16) + ", but is " + System.Convert.ToString(probe.ComputedSenderIPChecksum, 16) + ", (" + System.Convert.ToString(probe.ComputedReceiverIPChecksum, 16) + ") ---- {0}", _synAck.ValidChecksum);

            Console.WriteLine("From: {0}, To:{1}", _synAck.SourceAddress, _synAck.DestinationAddress);
        }