Пример #1
0
        public List <byte> ToArray()
        {
            byte x;

            TotalLength = SetTotalLength();
            List <byte> ret = new List <byte>();

            ret.Add(BytesOP.MakeByte(Version, HeaderLength));
            ret.Add(TypeOfService);
            ret.Add(BytesOP.GetHighByte(TotalLength));
            ret.Add(BytesOP.GetLowByte(TotalLength));
            ret.Add(BytesOP.GetHighByte(Identification));
            ret.Add(BytesOP.GetLowByte(Identification));
            x  = (byte)(Flags << 5);
            x += (byte)(FragmentOffset >> 8);
            ret.Add(x);
            ret.Add(BytesOP.GetLowByte(FragmentOffset));
            ret.Add(TimeToLive);
            ret.Add(Protocol);
            ret.Add(0x00);//校验和
            ret.Add(0x00);
            ret.Add(SourceAddress[0]);
            ret.Add(SourceAddress[1]);
            ret.Add(SourceAddress[2]);
            ret.Add(SourceAddress[3]);
            ret.Add(DastinationAddress[0]);
            ret.Add(DastinationAddress[1]);
            ret.Add(DastinationAddress[2]);
            ret.Add(DastinationAddress[3]);
            if (HeaderLength > 0x05)
            {
                for (int i = 0; i < ((HeaderLength - 0x05) * 4); i++)
                {
                    ret.Add(Options[i]);
                }
            }
            HeaderVerify = Verify.GetVerify_IP(ret.ToArray());
            ret[10]      = BytesOP.GetHighByte(HeaderVerify);
            ret[11]      = BytesOP.GetLowByte(HeaderVerify);
            switch (Protocol)
            {
            case 0x06:    //TCP
                FakeTcpHeader fh = new FakeTcpHeader(SourceAddress, DastinationAddress);
                fh.count = (ushort)(TotalLength - HeaderLength * 4);
                TCPPacket   tcp     = (TCPPacket)oProtocolContent;
                List <Byte> TCPList = tcp.ToArray(fh);
                foreach (byte b in TCPList)
                {
                    ret.Add(b);
                }
                break;
            }
            Identification++;
            return(ret);
        }
Пример #2
0
        public List <byte> ToArray(FakeTcpHeader fh)
        {
            List <Byte> FakeTCP = new List <Byte>();

            //构建TCP的伪首部
            FakeTCP.Add(fh.SourceAddress[0]);
            FakeTCP.Add(fh.SourceAddress[1]);
            FakeTCP.Add(fh.SourceAddress[2]);
            FakeTCP.Add(fh.SourceAddress[3]);
            FakeTCP.Add(fh.DastinationAddress[0]);
            FakeTCP.Add(fh.DastinationAddress[1]);
            FakeTCP.Add(fh.DastinationAddress[2]);
            FakeTCP.Add(fh.DastinationAddress[3]);
            FakeTCP.Add(0x00);
            FakeTCP.Add(0x06);
            FakeTCP.Add(BytesOP.GetHighByte(fh.count));
            FakeTCP.Add(BytesOP.GetLowByte(fh.count));
            //伪首部完成
            FakeTCP.Add(BytesOP.GetHighByte(SourcePort));
            FakeTCP.Add(BytesOP.GetLowByte(SourcePort));
            FakeTCP.Add(BytesOP.GetHighByte(DastinationPort));
            FakeTCP.Add(BytesOP.GetLowByte(DastinationPort));
            Union_UInt32 u32 = new Union_UInt32();

            u32.ofs_32 = InitialSeqNumber;
            FakeTCP.Add(u32.ofs_h.ofs_h);
            FakeTCP.Add(u32.ofs_h.ofs_l);
            FakeTCP.Add(u32.ofs_l.ofs_h);
            FakeTCP.Add(u32.ofs_l.ofs_l);
            u32.ofs_32 = AckSeqNumber;
            FakeTCP.Add(u32.ofs_h.ofs_h);
            FakeTCP.Add(u32.ofs_h.ofs_l);
            FakeTCP.Add(u32.ofs_l.ofs_h);
            FakeTCP.Add(u32.ofs_l.ofs_l);
            FakeTCP.Add((byte)(DataOffset << 4));
            FakeTCP.Add(TCPFlags);
            FakeTCP.Add(BytesOP.GetHighByte(Window));
            FakeTCP.Add(BytesOP.GetLowByte(Window));
            int sumaddr = FakeTCP.Count;

            FakeTCP.Add(0x00);
            FakeTCP.Add(0x00);
            FakeTCP.Add(BytesOP.GetHighByte(UrgentPoint));
            FakeTCP.Add(BytesOP.GetLowByte(UrgentPoint));
            if (DataOffset > 0x05)
            {
                for (int i = 0; i < ((DataOffset - 0x05) * 4); i++)
                {
                    FakeTCP.Add(TCPOptions[i]);
                }
            }
            foreach (byte b in Datas)
            {
                FakeTCP.Add(b);
            }
            CheckSum             = Verify.GetVerify_IP(FakeTCP.ToArray());
            FakeTCP[sumaddr]     = BytesOP.GetHighByte(CheckSum);
            FakeTCP[sumaddr + 1] = BytesOP.GetLowByte(CheckSum);
            FakeTCP.RemoveRange(0, 12);
            if (Datas.Count == 0)
            {
                if (TCPFlags == 0x02)
                {
                    InitialSeqNumber++;
                }
            }
            else
            {
                InitialSeqNumber += (uint)Datas.Count;
            }
            return(FakeTCP);
        }