Ejemplo n.º 1
0
 public Connection(Tamir.IPLib.Packets.TCPPacket packet)
 {
     m_srcIp   = packet.SourceAddress;
     m_dstIp   = packet.DestinationAddress;
     m_srcPort = (ushort)packet.SourcePort;
     m_dstPort = (ushort)packet.DestinationPort;
 }
Ejemplo n.º 2
0
 public static void MakeValid(TCPPacket tcp)
 {
     tcp.IPVersion       = 4;
     tcp.IPTotalLength   = tcp.Bytes.Length - 14;                                //Set the correct IP length
     tcp.IPHeaderLength  = IPFields_Fields.IP_HEADER_LEN;
     tcp.TCPHeaderLength = TCPFields_Fields.TCP_HEADER_LEN;                      //Set the correct TCP header length
     //Calculate checksums
     tcp.ComputeIPChecksum();
     tcp.ComputeTCPChecksum();
 }
Ejemplo n.º 3
0
        public static TCPPacket RandomPacket(int size)
        {
            if (size < 54)
            {
                throw new Exception("Size should be at least 54 (Eth + IP + TCP)");
            }

            byte[] bytes = new byte[size];
            Tamir.IPLib.Util.Rand.Instance.GetBytes(bytes);
            TCPPacket tcp = new TCPPacket(14, bytes, true);

            MakeValid(tcp);
            return(tcp);
        }
Ejemplo n.º 4
0
        public TCPPacket processPacket(TCPPacket packet)
        {
            if (packet.FragmentFlags == 0x04) // not Fragmented
            {
                return packet;
            }
            else if (packet.FragmentFlags == 0x00 && packet.Data.Length == PacketReassembler.PACKET_LENGHT) //maybe Fragmented?
            {
                buffer.Add(packet);
            }
            else if (packet.FragmentFlags == 0x00
                && packet.Data.Length != PacketReassembler.PACKET_LENGHT) //maybe Fragmented?
            {
                buffer.Add(packet);

                long ackNr = packet.AcknowledgmentNumber;
                int size = packet.HeaderLength;
                foreach (TCPPacket p in buffer)
                {
                    if (p.AcknowledgmentNumber == ackNr)
                    {
                        size += p.Data.Length;
                    }
                }
                if (size == packet.HeaderLength)
                {
                    return null;
                }
                byte[] data = new byte[size];
                Array.Copy(packet.Header, 0, data, 0, packet.HeaderLength);
                int pos = packet.HeaderLength;
                foreach (TCPPacket p in buffer)
                {
                    if (p.AcknowledgmentNumber == ackNr)
                    {
                        Array.Copy(p.Data, 0, data, pos, p.Data.Length);
                        pos += p.Data.Length;
                        buffer.Remove(p);
                    }
                }

                TCPPacket reassembledPacket = new TCPPacket((int)size, data);
                return reassembledPacket;
            }

            return null;
        }
Ejemplo n.º 5
0
        public static TCPPacket RandomPacket(int size)
        {
            if(size<54)
                throw new Exception("Size should be at least 54 (Eth + IP + TCP)");

            byte[] bytes = new byte[size];
            Tamir.IPLib.Util.Rand.Instance.GetBytes(bytes);
            TCPPacket tcp = new TCPPacket(14, bytes, true);
            MakeValid(tcp);
            return tcp;
        }
Ejemplo n.º 6
0
 public static void MakeValid(TCPPacket tcp)
 {
     tcp.IPVersion = 4;
     tcp.IPTotalLength = tcp.Bytes.Length-14;			//Set the correct IP length
     tcp.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;
     tcp.TCPHeaderLength = TCPFields_Fields.TCP_HEADER_LEN;			//Set the correct TCP header length
     //Calculate checksums
     tcp.ComputeIPChecksum();
     tcp.ComputeTCPChecksum();
 }
Ejemplo n.º 7
0
        private static void Save(TCPPacket _TCPPacket, Direction _Direction, Client _Client, SortedList<int, byte[]> _Stream)
        {
            if (!_Stream.ContainsKey(_TCPPacket.Id))
            {
                
                string s = "<<<<<<<<<" + _Direction + " " + _Client.ip + ":" + _Client.Port + " id:" + _Client.i + " bytes:" + _TCPPacket.Data.Length + ">>>>>>>>";
                Console.WriteLine(s); Trace.WriteLine(s);
                Trace.WriteLine(ASCIIEncoding.ASCII.GetString(_TCPPacket.Data));
                _Stream.Add(_TCPPacket.Id, _TCPPacket.Data);
                using (FileStream _FileStream = new FileStream(_Client.i + " " + _Direction + ".html", FileMode.Create, FileAccess.Write))
                {
                    using (MemoryStream _MemoryStream = new MemoryStream())
                    {

                        for (int i = _Stream.Keys.First(); _Stream.ContainsKey(i); i++)
                        {
                            _FileStream.Write(_Stream[i]);
                        }                        
                    }
                }
            }
        }
Ejemplo n.º 8
0
 private string getPacketId(TCPPacket packet)
 {
     return string.Format("{0}:{1} - {2}:{3}", packet.SourceAddress, packet.SourcePort, packet.DestinationAddress, packet.DestinationPort);
 }
Ejemplo n.º 9
0
        public static void SendTcpSyn(NetworkDevice dev)
        {
            byte[] bytes = new byte[54];

            TCPPacket tcp = new TCPPacket(lLen, bytes, true);

            //Ethernet fields
            tcp.SourceHwAddress = dev.MacAddress;			//Set the source mac of the local device
            tcp.DestinationHwAddress = destMAC;		//Set the dest MAC of the gateway
            tcp.EthernetProtocol = EthernetProtocols_Fields.IP;

            //IP fields
            tcp.DestinationAddress = destIP;			//The IP of the destination host
            tcp.SourceAddress = dev.IpAddress;			//The IP of the local device
            tcp.IPProtocol = IPProtocols_Fields.TCP;
            tcp.TimeToLive = 20;
            tcp.Id = 100;
            tcp.Version = 4;
            tcp.IPTotalLength = bytes.Length-lLen;			//Set the correct IP length
            tcp.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;

            //TCP fields
            tcp.SourcePort = sourcePort;				//The TCP source port
            tcp.DestinationPort = destPort;				//The TCP dest port
            tcp.Syn = true;						//Set the SYN flag on
            tcp.WindowSize = 555;
            tcp.AcknowledgementNumber = 1000;
            tcp.SequenceNumber = 1000;
            tcp.TCPHeaderLength = TCPFields_Fields.TCP_HEADER_LEN;			//Set the correct TCP header length

            //tcp.SetData( System.Text.Encoding.ASCII.GetBytes("HELLO") );

            //Calculate checksums
            tcp.ComputeIPChecksum();
            tcp.ComputeTCPChecksum();

            dev.PcapOpen(true, 20);

            //Set a filter to capture only replies
            dev.PcapSetFilter("ip src "+destIP+" and ip dst "+
                dev.IpAddress+" and tcp src port "+destPort+" and tcp dst port "+sourcePort);

            //Send the packet
            Console.Write("Sending packet: "+tcp+"...");
            dev.PcapSendPacket(tcp);
            Console.WriteLine("Packet sent.");

            //Holds the reply
            Packet reply;
            //Wait till you get a reply
            while((reply=dev.PcapGetNextPacket())==null);
            //print the reply
            Console.WriteLine("Reply received: "+reply);

            dev.PcapClose();
        }