Exemplo n.º 1
0
 public TCPopWS(byte[] data, int offset) //Offset will include Kind and Len
 {
     //'(24 bits)'
     offset += 2;
     NetLib.ReadByte08(data, ref offset, out WindowScale);
     Log_Verb("Got Window scale of " + WindowScale);
 }
Exemplo n.º 2
0
 public DHCPopClientID(byte[] data, int offset) //Offset will include Kind and Len
 {
     offset += 1;
     NetLib.ReadByte08(data, ref offset, out len);
     //ClientID = new byte[len];
     NetLib.ReadByteArray(data, ref offset, len, out clientID);
 }
Exemplo n.º 3
0
        public ARPPacket(EthernetFrame Ef)
        {
            int pktOffset = Ef.HeaderLength;

            NetLib.ReadUInt16(Ef.RawPacket.buffer, ref pktOffset, out HardWareType);
            //
            DataLib.ReadUInt16(Ef.RawPacket.buffer, ref pktOffset, out Protocol);
            //
            NetLib.ReadByte08(Ef.RawPacket.buffer, ref pktOffset, out HardwareAddressLength);
            NetLib.ReadByte08(Ef.RawPacket.buffer, ref pktOffset, out ProtocolAddressLength);
            NetLib.ReadUInt16(Ef.RawPacket.buffer, ref pktOffset, out OP);
            //Error.WriteLine("OP" + OP);

            NetLib.ReadByteArray(Ef.RawPacket.buffer, ref pktOffset, HardwareAddressLength, out SenderHardwareAddress);
            //WriteLine("sender MAC :" + SenderHardwareAddress[0] + ":" + SenderHardwareAddress[1] + ":" + SenderHardwareAddress[2] + ":" + SenderHardwareAddress[3] + ":" + SenderHardwareAddress[4] + ":" + SenderHardwareAddress[5]);

            NetLib.ReadByteArray(Ef.RawPacket.buffer, ref pktOffset, ProtocolAddressLength, out SenderProtocolAddress);
            //WriteLine("sender IP :" + SenderProtocolAddress[0] + "." + SenderProtocolAddress[1] + "." + SenderProtocolAddress[2] + "." + SenderProtocolAddress[3]);

            NetLib.ReadByteArray(Ef.RawPacket.buffer, ref pktOffset, HardwareAddressLength, out TargetHardwareAddress);
            //WriteLine("target MAC :" + TargetHardwareAddress[0] + ":" + TargetHardwareAddress[1] + ":" + TargetHardwareAddress[2] + ":" + TargetHardwareAddress[3] + ":" + TargetHardwareAddress[4] + ":" + TargetHardwareAddress[5]);

            NetLib.ReadByteArray(Ef.RawPacket.buffer, ref pktOffset, ProtocolAddressLength, out TargetProtocolAddress);
            //WriteLine("target IP :" + TargetProtocolAddress[0] + "." + TargetProtocolAddress[1] + "." + TargetProtocolAddress[2] + "." + TargetProtocolAddress[3]);
        }
Exemplo n.º 4
0
 public IGMP(byte[] buffer, int offset, int Length)
 {
     NetLib.ReadByte08(buffer, ref offset, out Type);
     NetLib.ReadByte08(buffer, ref offset, out maxResponseTime);
     NetLib.ReadUInt16(buffer, ref offset, out checksum);
     NetLib.ReadByteArray(buffer, ref offset, 4, out GroupAddress);
     //TODO version 3
 }
Exemplo n.º 5
0
        public ICMP(byte[] buffer, int offset, int Length) //Length = IP payload len
        {
            NetLib.ReadByte08(buffer, ref offset, out Type);
            //Error.WriteLine("Type = " + Type);
            NetLib.ReadByte08(buffer, ref offset, out Code);
            //Error.WriteLine("Code = " + Code);
            NetLib.ReadUInt16(buffer, ref offset, out checksum);
            NetLib.ReadByteArray(buffer, ref offset, 4, out HeaderData);

            NetLib.ReadByteArray(buffer, ref offset, Length - 8, out Data);
        }
Exemplo n.º 6
0
        public DHCPopRouter(byte[] data, int offset) //Offset will include Kind and Len
        {
            offset += 1;
            byte len;

            NetLib.ReadByte08(data, ref offset, out len);
            len = (byte)((len) / 4);
            byte[] rIP;
            for (int x = 0; x < len; x++)
            {
                NetLib.ReadByteArray(data, ref offset, 4, out rIP);
                routers.Add(rIP);
            }
        }
Exemplo n.º 7
0
        public DHCP(byte[] data)
        {
            int offset = 0;

            //Bits 0-31 //Bytes 0-3
            NetLib.ReadByte08(data, ref offset, out OP);
            Log_Verb("OP " + OP);
            NetLib.ReadByte08(data, ref offset, out HardwareType);
            //Error.WriteLine("HWt " + HardwareType);
            NetLib.ReadByte08(data, ref offset, out HardwareAddressLength);
            //Error.WriteLine("HWaddrlen " + HardwareAddressLength);
            NetLib.ReadByte08(data, ref offset, out Hops);
            //Error.WriteLine("Hops " + Hops);

            //Bits 32-63 //Bytes 4-7
            //TransactionID = BitConverter.ToInt32(data, 4);
            NetLib.ReadUInt32(data, ref offset, out TransactionID);
            Log_Verb("xid " + TransactionID);

            //Bits 64-95 //Bytes 8-11
            NetLib.ReadUInt16(data, ref offset, out Seconds);
            //Error.WriteLine("sec " + Seconds);
            NetLib.ReadUInt16(data, ref offset, out Flags);
            //Error.WriteLine("Flags " + Flags);

            //Bits 96-127 //Bytes 12-15
            NetLib.ReadByteArray(data, ref offset, 4, out ClientIP);
            Log_Info("CIP " + ClientIP[0] + "." + ClientIP[1] + "." + ClientIP[2] + "." + ClientIP[3]);

            //Bits 128-159 //Bytes 16-19
            NetLib.ReadByteArray(data, ref offset, 4, out YourIP);
            Log_Info("YIP " + YourIP[0] + "." + YourIP[1] + "." + YourIP[2] + "." + YourIP[3]);

            //Bits 160-191 //Bytes 20-23
            NetLib.ReadByteArray(data, ref offset, 4, out ServerIP);
            Log_Info("SIP " + ServerIP[0] + "." + ServerIP[1] + "." + ServerIP[2] + "." + ServerIP[3]);

            //Bits 192-223 //Bytes 24-27
            NetLib.ReadByteArray(data, ref offset, 4, out GatewayIP);
            Log_Info("GIP " + GatewayIP[0] + "." + GatewayIP[1] + "." + GatewayIP[2] + "." + GatewayIP[3]);

            //Bits 192+ //Bytes 28-43
            NetLib.ReadByteArray(data, ref offset, 16, out ClientHardwareAddress);

            //Bytes 44-107

            byte[] sNamebytes;
            NetLib.ReadByteArray(data, ref offset, 64, out sNamebytes);

            //Bytes 108-235
            byte[] filebytes;
            NetLib.ReadByteArray(data, ref offset, 128, out filebytes);

            //Bytes 236-239
            NetLib.ReadUInt32(data, ref offset, out MagicCookie);
            //Error.WriteLine("Cookie " + MagicCookie);
            bool opReadFin = false;

            //int op_offset = 240;
            do
            {
                byte opKind = data[offset];
                if (opKind == 255)
                {
                    Options.Add(new DHCPopEND());
                    opReadFin = true;
                    offset   += 1;
                    continue;
                }
                if ((offset + 1) >= data.Length)
                {
                    Log_Error("Unexpected end of packet");
                    Options.Add(new DHCPopEND());
                    opReadFin = true;
                    continue;
                }
                byte opLen = data[offset + 1];
                switch (opKind)
                {
                case 0:
                    //Error.WriteLine("Got NOP");
                    Options.Add(new DHCPopNOP());
                    offset += 1;
                    continue;

                case 1:
                    //Error.WriteLine("Got Subnet");
                    Options.Add(new DHCPopSubnet(data, offset));
                    break;

                case 3:
                    //Error.WriteLine("Got Router");
                    Options.Add(new DHCPopRouter(data, offset));
                    break;

                case 6:
                    //Error.WriteLine("Got DNS Servers");
                    Options.Add(new DHCPopDNS(data, offset));
                    break;

                case 12:
                    Options.Add(new DHCPopHOSTNAME(data, offset));
                    break;

                case 15:
                    //Error.WriteLine("Got Domain Name (Not supported)");
                    Options.Add(new DHCPopDNSNAME(data, offset));
                    break;

                case 28:
                    //Error.WriteLine("Got broadcast");
                    Options.Add(new DHCPopBCIP(data, offset));
                    break;

                case 46:
                    //Error.WriteLine("Got Request IP");
                    Options.Add(new DHCPopNBOIPType(data, offset));
                    break;

                case 50:
                    //Error.WriteLine("Got Request IP");
                    Options.Add(new DHCPopREQIP(data, offset));
                    break;

                case 51:
                    //Error.WriteLine("Got IP Address Lease Time");
                    Options.Add(new DHCPopIPLT(data, offset));
                    break;

                case 53:
                    //Error.WriteLine("Got MSG");
                    Options.Add(new DHCPopMSG(data, offset));
                    break;

                case 54:
                    //Error.WriteLine("Got Server IP");
                    Options.Add(new DHCPopSERVIP(data, offset));
                    break;

                case 55:
                    //Error.WriteLine("Got Request List");
                    Options.Add(new DHCPopREQLIST(data, offset));
                    break;

                case 56:
                    Options.Add(new DHCPopMSGStr(data, offset));
                    break;

                case 57:
                    //Error.WriteLine("Got Max Message Size");
                    Options.Add(new DHCPopMMSGS(data, offset));
                    break;

                case 58:
                    //Error.WriteLine("Got Renewal (T1) Time");
                    Options.Add(new DHCPopT1(data, offset));
                    break;

                case 59:
                    //Error.WriteLine("Got Rebinding (T2) Time");
                    Options.Add(new DHCPopT2(data, offset));
                    break;

                case 60:
                    //Error.WriteLine("Got Max Message Size");
                    Options.Add(new DHCPopClassID(data, offset));
                    break;

                case 61:
                    //Error.WriteLine("Got Client ID");
                    Options.Add(new DHCPopClientID(data, offset));
                    break;

                default:
                    Log_Error("Got Unknown Option " + opKind + " with len " + opLen);
                    break;
                }
                offset += opLen + 2;
                if (offset >= data.Length)
                {
                    Log_Error("Unexpected end of packet");
                    Options.Add(new DHCPopEND());
                    opReadFin = true;
                }
            } while (opReadFin == false);
        }
Exemplo n.º 8
0
 public DHCPopClassID(byte[] data, int offset) //Offset will include Kind and Len
 {
     offset += 1;
     NetLib.ReadByte08(data, ref offset, out len);
     NetLib.ReadByteArray(data, ref offset, len, out classBytes);
 }
Exemplo n.º 9
0
 public DHCPopREQLIST(byte[] data, int offset) //Offset will include Kind and Len
 {
     offset += 1;
     NetLib.ReadByte08(data, ref offset, out len);
     NetLib.ReadByteArray(data, ref offset, len, out requests);
 }
Exemplo n.º 10
0
 public DHCPopMSG(byte[] data, int offset) //Offset will include Kind and Len
 {
     offset += 2;
     NetLib.ReadByte08(data, ref offset, out msg);
 }
Exemplo n.º 11
0
 public DHCPopNBOIPType(byte[] data, int offset) //Offset will include Kind and Len
 {
     offset += 2;
     NetLib.ReadByte08(data, ref offset, out type);
 }
Exemplo n.º 12
0
        public DNS(byte[] data)
        {
            int offset = 0;

            //Bits 0-31 //Bytes 0-3
            NetLib.ReadUInt16(data, ref offset, out ID);
            Log_Info("ID " + ID);
            NetLib.ReadByte08(data, ref offset, out flags1);
            NetLib.ReadByte08(data, ref offset, out flags2);
            Log_Info("Is Response " + QR);
            Log_Info("OpCode " + (DNSOPCode)OPCode);
            Log_Info("Is Authoritative (not cached) " + AA);
            Log_Info("Is Truncated " + TC);
            Log_Info("Recursion Desired " + RD);
            Log_Info("Recursion Available " + RA);
            Log_Info("Zero " + ZO);
            Log_Info("Authenticated Data? " + AD);
            Log_Info("Checking Disabled? " + CD);
            Log_Info("Result " + (DNSRCode)RCode);
            //Bits 32-63 //Bytes 4-7
            UInt16 qCount;
            UInt16 aCount;
            UInt16 auCount;
            UInt16 adCount;

            NetLib.ReadUInt16(data, ref offset, out qCount);
            Log_Info("QuestionCount " + qCount);
            NetLib.ReadUInt16(data, ref offset, out aCount);
            Log_Info("AnswerCount " + aCount);
            //Bits 64-95 //Bytes 8-11
            NetLib.ReadUInt16(data, ref offset, out auCount);
            Log_Info("Authority Count " + auCount);
            NetLib.ReadUInt16(data, ref offset, out adCount);
            Log_Info("Additional Count " + adCount);
            //Bits 96+   //Bytes 8+
            for (int i = 0; i < qCount; i++)
            {
                DNSQuestionEntry entry = new DNSQuestionEntry(data, offset);
                Log_Info("Q" + i + " Name " + entry.Name);
                Log_Info("Q" + i + " Type " + entry.Type);
                Log_Info("Q" + i + " Class " + entry.Class);
                Log_Info("Q" + i + " Length " + entry.Length);
                offset += entry.Length;
                Questions.Add(entry);
            }
            for (int i = 0; i < aCount; i++)
            {
                DNSResponseEntry entry = new DNSResponseEntry(data, offset);
                Log_Info("Ans" + i + " Name " + entry.Name);
                Log_Info("Ans" + i + " Type " + entry.Type);
                Log_Info("Ans" + i + " Class " + entry.Class);
                Log_Info("Ans" + i + " TTL " + entry.TTL);
                string str = "";
                for (int y = 0; y < entry.Data.Length; y++)
                {
                    str += entry.Data[y] + ":";
                }
                Log_Info("Ans" + i + " Data " + str.Substring(0, str.Length - 1));
                Log_Info("ANS" + i + " Length " + entry.Length);
                offset += entry.Length;
                Answers.Add(entry);
            }
            for (int i = 0; i < auCount; i++)
            {
                DNSResponseEntry entry = new DNSResponseEntry(data, offset);
                Log_Info("Auth" + i + " Name " + entry.Name);
                Log_Info("Auth" + i + " Type " + entry.Type);
                Log_Info("Auth" + i + " Class " + entry.Class);
                Log_Info("Auth" + i + " TTL " + entry.TTL);
                string str = "";
                for (int y = 0; y < entry.Data.Length; y++)
                {
                    str += entry.Data[y] + ":";
                }
                Log_Info("Auth" + i + " Data " + str.Substring(0, str.Length - 1));
                Log_Info("Auth" + i + " Length " + entry.Length);
                offset += entry.Length;
                Authorities.Add(entry);
            }
            for (int i = 0; i < adCount; i++)
            {
                DNSResponseEntry entry = new DNSResponseEntry(data, offset);
                Log_Info("Add" + i + " Name " + entry.Name);
                Log_Info("Add" + i + " Type " + entry.Type);
                Log_Info("Add" + i + " Class " + entry.Class);
                Log_Info("Add" + i + " TTL " + entry.TTL);
                string str = "";
                for (int y = 0; y < entry.Data.Length; y++)
                {
                    str += entry.Data[y] + ":";
                }
                Log_Info("Add" + i + " Data " + str.Substring(0, str.Length - 1));
                Log_Info("Add" + i + " Length " + entry.Length);
                offset += entry.Length;
                Additional.Add(entry);
            }
        }
Exemplo n.º 13
0
        private void ReadBuffer(byte[] buffer, int offset, int bufferSize, bool fromICMP)
        {
            int initialOffset = offset;
            int pktOffset     = offset;

            //Bits 0-31
            byte v_hl;

            NetLib.ReadByte08(buffer, ref pktOffset, out v_hl);
            hLen = ((v_hl & 0xF) << 2);
            NetLib.ReadByte08(buffer, ref pktOffset, out typeOfService); //TODO, Implement this

            //Not sure PS2 supports this

            //Log_Error("Class :" + Class.ToString());
            ////(DSCP support)
            //Log_Error("DropValue :" + DropProbability.ToString());
            //Log_Error("Supports ECN :" + IsECNCapable.ToString());
            //Log_Error("Congestion :" + CongestionEvent.ToString());
            ////TOS Support
            //Log_Error("LowDelay :" + TOSDelay.ToString());
            //Log_Error("HighThroughput :" + TOSThroughout.ToString());
            //Log_Error("LowCost :" + TOSCost.ToString());

            NetLib.ReadUInt16(buffer, ref pktOffset, out length);
            if (length > bufferSize - offset)
            {
                if (!fromICMP)
                {
                    Log_Error("Unexpected Length");
                }
                length = (UInt16)(bufferSize - offset);
            }

            //Bits 32-63
            NetLib.ReadUInt16(buffer, ref pktOffset, out id); //Send packets with unique IDs
            NetLib.ReadByte08(buffer, ref pktOffset, out fragmentFlags1);
            NetLib.ReadByte08(buffer, ref pktOffset, out fragmentFlags2);
            if (MoreFragments | FragmentOffset != 0)
            {
                Log_Error("FragmentedPacket");
                throw new NotImplementedException("Fragmented Packets are not supported");
            }

            //Bits 64-95
            NetLib.ReadByte08(buffer, ref pktOffset, out ttl);
            NetLib.ReadByte08(buffer, ref pktOffset, out Protocol);
            NetLib.ReadUInt16(buffer, ref pktOffset, out checksum);

            //Bits 96-127
            NetLib.ReadByteArray(buffer, ref pktOffset, 4, out SourceIP);
            //Bits 128-159
            NetLib.ReadByteArray(buffer, ref pktOffset, 4, out DestinationIP);
            //WriteLine("Target IP :" + DestinationIP[0] + "." + DestinationIP[1] + "." + DestinationIP[2] + "." + DestinationIP[3]);

            //Bits 160+
            if (hLen > 20) //IP options (if any)
            {
                bool opReadFin = false;
                do
                {
                    byte opKind = buffer[pktOffset];
                    byte opLen  = buffer[pktOffset + 1];
                    switch (opKind)
                    {
                    case 0:
                        opReadFin = true;
                        break;

                    case 1:
                        Options.Add(new IPopNOP());
                        pktOffset += 1;
                        continue;

                    case 148:
                        Options.Add(new IPopRouterAlert(buffer, offset));
                        break;

                    default:
                        Log_Error("Got IP Unknown Option " + opKind + " with len " + opLen);
                        throw new Exception("Got IP Unknown Option " + opKind + " with len " + opLen);
                        //break;
                    }
                    pktOffset += opLen;
                    if (pktOffset == initialOffset + hLen)
                    {
                        opReadFin = true;
                    }
                } while (opReadFin == false);
            }
            pktOffset = initialOffset + hLen;

            switch (Protocol) //(Prase Payload)
            {
            case (byte)IPType.ICMP:
                _pl = new ICMP(buffer, pktOffset, Length - hLen);
                //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.IGMP:
                _pl = new IGMP(buffer, pktOffset, Length - hLen);
                //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.TCP:
                _pl = new TCP(buffer, pktOffset, Length - hLen);
                //((TCP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.UDP:
                _pl = new UDP(buffer, pktOffset, Length - hLen);
                //((UDP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            default:
                _pl = new IPUnkown(buffer, pktOffset, Length - hLen);
                Log_Error("Unkown IPv4 Protocol " + Protocol.ToString("X2"));
                break;
            }
        }
Exemplo n.º 14
0
        public TCP(byte[] buffer, int offset, int parLength) //Length = IP payload len
        {
            int initialOffset = offset;

            //Bits 0-31
            NetLib.ReadUInt16(buffer, ref offset, out SourcePort);
            //Error.WriteLine("src port=" + SourcePort);
            NetLib.ReadUInt16(buffer, ref offset, out DestinationPort);
            //Error.WriteLine("dts port=" + DestinationPort);

            //Bits 32-63
            NetLib.ReadUInt32(buffer, ref offset, out SequenceNumber);
            //Error.WriteLine("seq num=" + SequenceNumber); //Where in the stream the start of the payload is

            //Bits 64-95
            NetLib.ReadUInt32(buffer, ref offset, out AcknowledgementNumber);
            //Error.WriteLine("ack num=" + AcknowledgmentNumber); //the next expected byte(seq) number

            //Bits 96-127
            NetLib.ReadByte08(buffer, ref offset, out dataOffsetAndNS_Flag);
            //Error.WriteLine("TCP hlen=" + HeaderLength);
            NetLib.ReadByte08(buffer, ref offset, out flags);
            NetLib.ReadUInt16(buffer, ref offset, out WindowSize);
            //Error.WriteLine("win Size=" + WindowSize);

            //Bits 127-159
            NetLib.ReadUInt16(buffer, ref offset, out checksum);
            NetLib.ReadUInt16(buffer, ref offset, out urgentPointer);
            //Error.WriteLine("urg ptr=" + UrgentPointer);

            //Bits 160+
            if (headerLength > 20) //TCP options
            {
                bool opReadFin = false;
                do
                {
                    byte opKind = buffer[offset];
                    byte opLen  = buffer[offset + 1];
                    switch (opKind)
                    {
                    case 0:
                        //Error.WriteLine("Got End of Options List @ " + (op_offset-offset-1));
                        opReadFin = true;
                        break;

                    case 1:
                        //Error.WriteLine("Got NOP");
                        Options.Add(new TCPopNOP());
                        offset += 1;
                        continue;

                    case 2:
                        //Error.WriteLine("Got MMS");
                        Options.Add(new TCPopMSS(buffer, offset));
                        break;

                    case 3:
                        Options.Add(new TCPopWS(buffer, offset));
                        break;

                    case 8:
                        //Error.WriteLine("Got Timestamp");
                        Options.Add(new TCPopTS(buffer, offset));
                        break;

                    default:
                        Log_Error("Got TCP Unknown Option " + opKind + "with len" + opLen);
                        throw new Exception("Got TCP Unknown Option " + opKind + "with len" + opLen);
                        //break;
                    }
                    offset += opLen;
                    if (offset == initialOffset + headerLength)
                    {
                        //Error.WriteLine("Reached end of Options");
                        opReadFin = true;
                    }
                } while (opReadFin == false);
            }
            offset = initialOffset + headerLength;

            NetLib.ReadByteArray(buffer, ref offset, parLength - headerLength, out data);
            //AllDone
        }