Beispiel #1
0
 public bool Send(bool isdata)
 {
     //Apps.System.Debugger.debugger.Send("Sending TCP packet...");
     if (isdata)
     {
         TCPPacket packet = new TCPPacket(source, dest, localPort, destPort, data, sequencenumber, acknowledgmentnb, 0x50, Flags, WSValue, 0x0000, false, false);
         OutgoingBuffer.AddPacket(packet);
     }
     else
     {
         TCPPacket packet = new TCPPacket(source, dest, localPort, destPort, data, sequencenumber, acknowledgmentnb, 0x50, Flags, WSValue, 0x0000, true, true);
         OutgoingBuffer.AddPacket(packet);
     }
     NetworkStack.Update();
     //Apps.System.Debugger.debugger.Send("Sent!");
     return(true);
 }
Beispiel #2
0
 public static bool CheckCRC(TCPPacket packet)
 {
     byte[] header = MakeHeader(packet.sourceIP.address, packet.destIP.address, packet.tcpLen, packet.sourcePort, packet.destPort, (uint)packet.sequencenumber, (uint)packet.acknowledgmentnb, packet.Headerlenght, packet.Flags, packet.WSValue, packet.UrgentPointer, packet.TCP_Data, false);
     UInt16 calculatedcrc = Check(header, 0, header.Length);
     //Apps.System.Debugger.debugger.Send("Calculated checksum: 0x" + Utils.Conversion.DecToHex(calculatedcrc));
     //Apps.System.Debugger.debugger.Send("Received checksum :  0x" + Utils.Conversion.DecToHex(packet.Checksum));
     if (calculatedcrc == packet.Checksum)
     {
         //Apps.System.Debugger.debugger.Send("checksum ok!!");
         return true;
     }
     else
     {
         //Apps.System.Debugger.debugger.Send("checksum wrong!!");
         return false;
     }
 }
Beispiel #3
0
        internal static void TCPHandler(byte[] packetData)
        {
            TCPPacket tcp_packet = new TCPPacket(packetData);

            if (Firewall.TCP.TCPIncomingFilter(tcp_packet))
            {
                //"=== [FIREWALL] TCP INCOMING PACKET BLOCKED " + tcp_packet.SourceIP.ToString() + ":" + tcp_packet.SourcePort.ToString() + " ===
                return;
            }

            foreach (Drivers.NetworkDevice device in NetworkConfig.Keys)
            {
                if (device.Name == "PCNETII")
                {
                    if (packetData.Length == 64 && tcp_packet.ipLength < 50)
                    {
                        byte[] rdata = tcp_packet.mRawData;
                        int lenght = 64 - (64 - (tcp_packet.ipLength + 14));
                        tcp_packet.tcpLen = (ushort)(lenght - 34);
                        tcp_packet.mRawData = new byte[lenght];
                        for (int b = 0; b <= lenght; b++)
                        {
                            tcp_packet.mRawData[b] = rdata[b];
                        }
                    }
                }
                if (device.Name == "RTL8168")
                {
                    byte[] rdata = tcp_packet.mRawData;
                    int lenght = 64 - (64 - (tcp_packet.ipLength + 14));
                    tcp_packet.tcpLen = (ushort)(lenght - 34);
                    tcp_packet.mRawData = new byte[lenght];
                    for (int b = 0; b <= lenght; b++)
                    {
                        tcp_packet.mRawData[b] = rdata[b];
                    }
                }
            }

            //=== Received TCP packet from " + tcp_packet.SourceIP.ToString() + ":" + tcp_packet.SourcePort.ToString() + " Len:" + tcp_packet.tcpLen.ToString() + "===

            if (CheckCRC(tcp_packet))
            {

                bool SYN = (packetData[47] & (1 << 1)) != 0;
                bool ACK = (packetData[47] & (1 << 4)) != 0;
                bool FIN = (packetData[47] & (1 << 0)) != 0;
                bool PSH = (packetData[47] & (1 << 3)) != 0;
                bool RST = (packetData[47] & (1 << 2)) != 0;

                ulong CID = tcp_packet.SourceIP.Hash + tcp_packet.sourcePort + tcp_packet.destPort;

                TCPConnection.Connection connection = new TCPConnection.Connection();

                if (SYN && !ACK)
                {

                    //FLAG: SYN, New connection

                    if (TCPConnection.Connections.ContainsKey((uint)CID))
                    {
                        TCPConnection.Connections.Remove((uint)CID);
                    }
                    else
                    {
                        connection = new TCPConnection.Connection();
                    }

                    TCPConnection.Connections.Add((uint)CID, connection);

                    connection.CID = CID;

                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + 1;

                    connection.WSValue = 1024;

                    connection.sequencenumber = 3455719727;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x12;

                    connection.Send(false);

                    connection.IsOpen = true;

                    return;
                }
                else if (SYN && ACK)
                {
                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + 1;

                    connection.WSValue = 1024;

                    connection.sequencenumber = tcp_packet.acknowledgmentnb;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x10;

                    connection.Send(false);

                    TCPClient.lastack = tcp_packet.sequencenumber + 1;
                    TCPClient.lastsn = tcp_packet.acknowledgmentnb;

                    TCPConnection.Connections[(uint)CID].IsOpen = true;
                }
                else if ((FIN || RST) && ACK && TCPConnection.Connections[(uint)CID].IsOpen)
                {
                    //FLAG: FIN, ACK, Disconnected by host!!!

                    if (tcp_packet.sourcePort == 4224 && tcp_packet.destPort == 4224)
                    {
                        Console.WriteLine("Debugger closed by client computer!");
                        //Kernel.debugger.Stop();
                    }

                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + 1;

                    connection.WSValue = 1024;

                    connection.sequencenumber = tcp_packet.acknowledgmentnb;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x10;

                    connection.Send(false);

                    connection.Flags = 0x11;

                    connection.Send(false);

                    connection.Close();

                    return;
                }
                else if (PSH && ACK && TCPConnection.Connections[(uint)CID].IsOpen)
                {
                    //FLAG: PSH, ACK, Data received!? :D
                    TCPClient receiver = TCPClient.Client(tcp_packet.DestinationPort);
                    if (receiver != null)
                    {
                        receiver.receiveData(tcp_packet);
                    }

                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + (ulong)tcp_packet.TCP_Data.Length;

                    connection.WSValue = 1024;

                    connection.sequencenumber = tcp_packet.acknowledgmentnb;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x10;

                    connection.Send(false);

                    connection.IsOpen = false;

                    return;
                }
                else if (RST && ACK)
                {
                    //Apps.System.Debugger.debugger.Send("FLAG: RST, ACK, Port not listening on remote machine?");
                    return;
                }
                else if (RST)
                {
                    //Apps.System.Debugger.debugger.Send("FLAG: RST, Connection aborted by host.");
                    return;
                }
                else if (ACK && TCPConnection.Connections[(uint)CID].IsOpen)
                {
                    TCPClient.lastack = tcp_packet.sequencenumber;
                    TCPClient.lastsn = tcp_packet.acknowledgmentnb;
                    TCPClient.readytosend = true;
                    return;
                }
                else
                {
                    //Apps.System.Debugger.debugger.Send("What is going wrong? D:");
                }
            }
            else
            {
                //Apps.System.Debugger.debugger.Send("But checksum is incorrect... Packet Passed.");

                TCPConnection.Connection connection = new TCPConnection.Connection();

                connection.dest = tcp_packet.sourceIP;
                connection.source = tcp_packet.destIP;

                connection.localPort = tcp_packet.destPort;
                connection.destPort = tcp_packet.sourcePort;

                connection.acknowledgmentnb = 0x0000;

                connection.WSValue = 1024;

                connection.sequencenumber = tcp_packet.acknowledgmentnb;

                connection.Checksum = 0x0000;

                connection.Flags = 0x04;

                connection.Send(false);

                connection.Close();

                return;
            }
        }