Radius invalid packet - how can i add more layers?

Mar 7, 2014 at 11:07 AM
Edited Mar 14, 2014 at 11:15 AM
I want to build Radius Packet and i have this code:
        /// <summary>
        /// Creates a start RADIUS packet to start a session
        /// </summary>
        public byte[] CreateLogInRADIUSPacket(byte packetCode, byte packetID)
        {
            return CreateRADIUSPacket(1, packetCode, packetID);
        }

        /// <summary>
        /// Creates a stop RADIUS packet to stop a session
        /// </summary>
        public byte[] CreateLogOutRADIUSPacket(byte packetCode, byte packetID)
        {
            return CreateRADIUSPacket(2, packetCode, packetID);
        }

        /// <summary>
        /// Creates a start or stop RADIUS packet
        /// </summary>
        public byte[] CreateRADIUSPacket(byte Type, byte packetCode, byte packetId)
        {
            // packetCode = 4 --> accounting request, 5 = accounting respons
            try
            {
                // Variables
                string Username = "raviv";
                string IPString = "1.1.1.1";
                uint IP = (uint)IPAddress.Parse(IPString).Address;
                string SessionID = "123";

                // Variable Lengths
                int HeaderLength = 20;
                int UsernameLength = 2 + Username.Length;
                int IPLength = 2 + 4;
                int AccountStatusTypeLength = 2 + 4;
                int SessionIDLength = 2 + SessionID.Length;

                // Calculate Length
                short Length = (short)(HeaderLength + UsernameLength + IPLength + AccountStatusTypeLength + SessionIDLength);

                // Create Packet Array
                byte[] Packet = new byte[Length];

                // Set Type
                Packet[0] = packetCode;


                Packet[1] = packetId;

                // Set Length
                Packet[2] = (byte)((Length >> 8) & 0xFF);
                Packet[3] = (byte)(Length & 0xFF);

                // Set Username
                int Index = 20;
                Packet[Index] = 1;
                Packet[Index + 1] = (byte)(Username.Length + 2);
                for (int i = 0; i < Username.Length; i++)
                    Packet[Index + 2 + i] = (byte)Username[i];

                // Set IP Address
                Index = Index + UsernameLength;
                Packet[Index] = 8;
                Packet[Index + 1] = 4 + 2;
                Packet[Index + 2] = (byte)(IP & 0xFF);
                Packet[Index + 3] = (byte)((IP >> 8) & 0xFF);
                Packet[Index + 4] = (byte)((IP >> 16) & 0xFF);
                Packet[Index + 5] = (byte)((IP >> 24) & 0xFF);

                // Set Account Status Type
                Index = Index + IPLength;
                Packet[Index] = 40;
                Packet[Index + 1] = 4 + 2;
                Packet[Index + 5] = Type;


                // Set Session ID
                Index = Index + AccountStatusTypeLength;
                Packet[Index] = 44;
                Packet[Index + 1] = (byte)(SessionID.Length + 2);
                for (int i = 0; i < SessionID.Length; i++)
                    Packet[Index + 2 + i] = (byte)SessionID[i];

                return Packet;
            }
            catch (Exception e)
            {
                return null;
            }
        }        
Main:
        static void Main(string[] args)
        {
            Radius radius = new Radius();

            byte[] StartPacketRequestBuffer = radius.CreateLogInRADIUSPacket(4, 8);
            byte[] StartPacketResponseBuffer = radius.CreateLogInRADIUSPacket(5, 8);

            byte[] StopPacketRequestBuffer = radius.CreateLogOutRADIUSPacket(4, 9);
            byte[] StopPacketResponseBuffer = radius.CreateLogOutRADIUSPacket(5, 9);
        }
After build this Packet and play this (using PcapDotNet) it seems that IPv4 layers is missing and i will glad for some help to complete this layer and add it to my radius packet in order to be a valid packet.

How can i do that ?
Coordinator
Mar 14, 2014 at 10:44 AM
Hi psptst1,

Can you provide:
  1. A .pcap file with an example of a RADIUS packet you try to create.
  2. The code you use to save the packet to a .pcap file.
  3. A .pcap file with the output of your code.
Boaz.
Mar 14, 2014 at 11:19 AM
Edited Mar 14, 2014 at 11:20 AM
Hi Boaz,

Please see my update, main added.
  1. i don't have radius packet example, i want to create new one and my code did it but need to add the other layers
  2. after create the packet i don't want to save this packet but immediately send this packet
  3. currently because i don't know how to add more layers this is not valid packet so it meaningless to try to send this packet
Coordinator
Mar 14, 2014 at 11:40 AM
Can you give details on what layer you want and in what order?
Mar 14, 2014 at 12:18 PM
currently i only have the radius amd all the rest is missing: ethernet, ipv4
Coordinator
Mar 14, 2014 at 3:40 PM
If you want to build RADIUS over IPv4 over Ethernet, you can build an EthernetLayer, an IpV4Layer and a PayloadLayer with the RADIUS data.