Beispiel #1
0
        public void ConnectHost(string i_HostIp)
        {
            m_Socket.Connect(i_HostIp, 22);

            // Recieving server version
            m_Buffer = new byte[m_BufferLength];
            m_RecievedBytes = m_Socket.Receive(m_Buffer);

            // checking the recieved buffer if it is the server version
            if (m_RecievedBytes > 0)
            {
                // removing \r\n
                if (m_Buffer[m_RecievedBytes - 1] == 10)
                {
                    m_RecievedBytes--;

                    if (m_Buffer[m_RecievedBytes - 1] == 13)
                    {
                        m_RecievedBytes--;
                    }

                    // Decoding server version
                    m_ServerVersionString= Encoding.ASCII.GetString(m_Buffer, 0, m_RecievedBytes);

                    Trace.WriteLine(string.Format("Server version {0}", m_ServerVersionString));

                    // Sending client version
                    m_Socket.Send(Encoding.ASCII.GetBytes(m_ClientVersionString));

                    // Client send key exchange supported encryptions
                    m_KexPhasesManager = new KexPhasesManager();

                    m_KexPhasesManager.ClientKexPacket.MessageCode = eSshMessages.SSH_MSG_KEXINIT;

                    m_KexPhasesManager.ClientKexPacket.kex_algorithms.SetNameList("diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1");
                    m_KexPhasesManager.ClientKexPacket.server_host_key_algorithms.SetNameList("ssh-rsa,ssh-dss");
                    m_KexPhasesManager.ClientKexPacket.encryption_algorithms_client_to_server.SetNameList("aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]");
                    m_KexPhasesManager.ClientKexPacket.encryption_algorithms_server_to_client.SetNameList("aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]");
                    m_KexPhasesManager.ClientKexPacket.mac_algorithms_client_to_server.SetNameList("hmac-sha1,hmac-sha1-96,hmac-md5");
                    m_KexPhasesManager.ClientKexPacket.mac_algorithms_server_to_client.SetNameList("hmac-sha1,hmac-sha1-96,hmac-md5");
                    m_KexPhasesManager.ClientKexPacket.compression_algorithms_client_to_server.SetNameList("none,zlib");
                    m_KexPhasesManager.ClientKexPacket.compression_algorithms_server_to_client.SetNameList("none,zlib");
                    m_KexPhasesManager.ClientKexPacket.languages_client_to_server.SetNameList(string.Empty);
                    m_KexPhasesManager.ClientKexPacket.languages_server_to_client.SetNameList(string.Empty);

                    /*m_SshKeyExchangePacketClient.kex_algorithms.SetNameList("diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1");
                    m_SshKeyExchangePacketClient.server_host_key_algorithms.SetNameList("ssh-rsa,ssh-dss");
                    m_SshKeyExchangePacketClient.encryption_algorithms_client_to_server.SetNameList("3des-cbc,aes128-cbc");
                    m_SshKeyExchangePacketClient.encryption_algorithms_server_to_client.SetNameList("3des-cbc,aes128-cbc");
                    m_SshKeyExchangePacketClient.mac_algorithms_client_to_server.SetNameList("hmac-md5,hmac-sha1");
                    m_SshKeyExchangePacketClient.mac_algorithms_server_to_client.SetNameList("hmac-md5,hmac-sha1");
                    m_SshKeyExchangePacketClient.compression_algorithms_client_to_server.SetNameList("none");
                    m_SshKeyExchangePacketClient.compression_algorithms_server_to_client.SetNameList("none");
                    m_SshKeyExchangePacketClient.languages_client_to_server.SetNameList(string.Empty);
                    m_SshKeyExchangePacketClient.languages_server_to_client.SetNameList(string.Empty);
                    */

                    m_KexPhasesManager.SetPayloadAsClientKex();

                    // sending to server
                    m_Socket.Send(m_KexPhasesManager.SshPacket.ToByteArray());

                    // receiving from server
                    m_RecievedBytes = m_Socket.Receive(m_Buffer);

                    m_KexPhasesManager.SshPacket.ParseBuffer(m_Buffer);

                    if (m_KexPhasesManager.ParseServerSshMsgKexInit())
                    {
                        // server sent SSH_MSG_KEXINIT
                        if (m_KexPhasesManager.ServerKexPacket.first_kex_packet_follows)
                        {
                            Debug.WriteLine("Next packet is guessed key");
                        }
                        else
                        {
                            if (m_KexPhasesManager.SearchKexAlgorithem() == true)
                            {
                                m_KexPhasesManager.CreateSshMsgKexDhGexRequestOld(4096);
                                // SSH_MSG_KEX_DH_GEX_REQUEST_OLD
                                m_Socket.Send(m_KexPhasesManager.SshPacket.ToByteArray());
                                // DH key exchange reply

                                #region DH region

                                m_Buffer = new byte[m_BufferLength];

                                m_RecievedBytes = m_Socket.Receive(m_Buffer);

                                m_KexPhasesManager.SshPacket.ParseBuffer(m_Buffer);

                                if (m_KexPhasesManager.ComputePublicKeyToServer())
                                {
                                    #region SSH_MSG_KEX_DH_GEX_INIT

                                    m_KexPhasesManager.CreateSshMsgKexDhGexInit();
                                    m_Socket.Send(m_KexPhasesManager.SshPacket.ToByteArray());
                                    #endregion SSH_MSG_KEX_DH_GEX_INIT

                                    m_Buffer = new byte[m_BufferLength];

                                    m_RecievedBytes = m_Socket.Receive(m_Buffer);

                                    m_KexPhasesManager.SshPacket.ParseBuffer(m_Buffer);

                                    m_KexPhasesManager.ParseServerSshMsgKexDhGexReply();

                                    Debug.Write("Finished DH Key Exchange Reply");

                                }

                            }
                            #endregion DH region
                            else
                            {
                                Debug.WriteLine("No supported kex_algorithms was found, disconnecting session");
                                m_Socket.Shutdown(SocketShutdown.Both);
                                m_Socket.Disconnect(false);
                            }

                        }

                    }

                }

            }
            else
            {
                Trace.WriteLine("Corrupt packege recieved");
            }
        }
Beispiel #2
0
        public ClassSshLayer()
        {
            m_Random = new Random();

            m_KexPhasesManager = null;

            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_Buffer = null;
            m_ClientVersionString = "SSH-2.0-SshLayer_1.0.0.0\r\n";
        }