Esempio n. 1
0
        /// <summary>
        /// Disconnect the SSH transport protocol.
        /// </summary>
        /// <param name="disconnectReason">A descriptive reason for the disconnection.</param>
        /// <param name="reason">The SSH reason code.</param>
        public void Disconnect(String disconnectReason, DisconnectionReason reason)
        {
            try
            {
                this.disconnectReason = disconnectReason;
                SSHPacket packet = GetSSHPacket(true);
                packet.WriteByte(SSH_MSG_DISCONNECT);
                packet.WriteUINT32((int)reason);
                packet.WriteString(disconnectReason);
                packet.WriteString("");

#if DEBUG
                System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_DISCONNECT");
                System.Diagnostics.Trace.WriteLine(disconnectReason);
#endif
                SendMessage(packet);
            }
            catch
            {
            }
            finally
            {
                InternalDisconnect();
            }
        }
Esempio n. 2
0
        internal void  SendKeyExchangeInit()
        {
            try
            {
                FireStateChange(TransportProtocolState.PERFORMING_KEYEXCHANGE);

                numIncomingBytesSinceKEX      = 0;
                numIncomingSSHPacketsSinceKEX = 0;
                numOutgoingBytesSinceKEX      = 0;
                numOutgoingSSHPacketsSinceKEX = 0;

                currentState = TransportProtocolState.PERFORMING_KEYEXCHANGE;

                SSHPacket packet = GetSSHPacket(true);

                SSH2Context transportContext = (SSH2Context)client.Context;
                byte[]      cookie           = new byte[16];
                rnd.GetBytes(cookie);
                packet.WriteByte((byte)SSH_MSG_KEX_INIT);
                packet.WriteBytes(cookie);
                packet.WriteString("diffie-hellman-group1-sha1");
                packet.WriteString(transportContext.SupportedPublicKeys.List(transportContext.PreferredPublicKey));
                packet.WriteString(transportContext.SupportedCiphers.List(transportContext.PreferredCipherCS));
                packet.WriteString(transportContext.SupportedCiphers.List(transportContext.PreferredCipherSC));
                packet.WriteString(transportContext.SupportedMACs.List(transportContext.PreferredMacCS));
                packet.WriteString(transportContext.SupportedMACs.List(transportContext.PreferredMacSC));
                packet.WriteString("none");
                packet.WriteString("none");
                packet.WriteString("");
                packet.WriteString("");
                packet.WriteByte((byte)0);
                packet.WriteUINT32(0);

#if DEBUG
                System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_KEX_INIT");
#endif

                localkex = SendMessage(packet, true);
            }
            catch (System.IO.IOException ex)
            {
                throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a global request.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="wantreply"></param>
        /// <returns></returns>
        public bool SendGlobalRequest(GlobalRequest request, bool wantreply)
        {
            try
            {
                SSHPacket packet = GetPacket();
                packet.WriteByte(SSH_MSG_GLOBAL_REQUEST);
                packet.WriteString(request.Name);
                packet.WriteBool(wantreply);
                if (request.Data != null)
                {
                    packet.WriteBytes(request.Data);
                }

#if DEBUG
                System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_GLOBAL_REQUEST");
                System.Diagnostics.Trace.WriteLine(request.Name);
#endif
                SendMessage(packet);

                if (wantreply)
                {
                    packet = GlobalMessages.NextMessage(GLOBAL_REQUEST_MESSAGES);
                    if (packet.MessageID == SSH_MSG_REQUEST_SUCCESS)
                    {
                        if (packet.Available > 1)
                        {
                            byte[] tmp = new byte[packet.Available];
                            packet.ReadBytes(tmp);
                            request.Data = tmp;
                        }
                        else
                        {
                            request.Data = null;
                        }
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(true);
                }
            }
            catch (System.IO.IOException ex)
            {
                throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Send an authentication request. This method will be called from the <see cref="Maverick.SSH2.SSH2AuthenticationClient.Authenticate"/>
 /// method to initiate the authentication proceedure.
 /// </summary>
 /// <param name="username"></param>
 /// <param name="servicename"></param>
 /// <param name="methodname"></param>
 /// <param name="requestdata"></param>
 public void SendRequest(String username,
                         String servicename,
                         String methodname,
                         byte[] requestdata)
 {
     try
     {
         SSHPacket packet = transport.GetSSHPacket(true);
         packet.WriteByte(SSH_MSG_USERAUTH_REQUEST);
         packet.WriteString(username);
         packet.WriteString(servicename);
         packet.WriteString(methodname);
         if (requestdata != null)
         {
             packet.WriteBytes(requestdata);
         }
         transport.SendMessage(packet);
     }
     catch (IOException ex)
     {
         throw new SSHException(ex.Message,
                                SSHException.INTERNAL_ERROR);
     }
 }
Esempio n. 5
0
        internal void StartService(String serviceName)
        {
            SSHPacket packet = GetSSHPacket(true);

            packet.WriteByte(SSH_MSG_SERVICE_REQUEST);
            packet.WriteString(serviceName);

#if DEBUG
            System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_SERVICE_REQUEST");
            System.Diagnostics.Trace.WriteLine(serviceName);
#endif
            SendMessage(packet);

            do
            {
                packet = ReadMessage();
            }while(ProcessMessage(packet) || packet.MessageID != SSH_MSG_SERVICE_ACCEPT);
        }
Esempio n. 6
0
        /// <summary>
        /// Sends a channel request. Many channels have extensions that are specific to that particular
        /// channel type, an example of which is requesting a pseudo terminal from an interactive session.
        /// </summary>
        /// <param name="requesttype">the name of the request, for example "pty-req"</param>
        /// <param name="wantreply">specifies whether the remote side should send a success/failure message</param>
        /// <param name="requestdata">the request data</param>
        /// <returns></returns>
        public bool SendRequest(System.String requesttype, bool wantreply, byte[] requestdata)
        {
            lock (this)
            {
                try
                {
                    SSHPacket packet = connection.GetPacket();
                    packet.WriteByte((System.Byte)SSH_MSG_CHANNEL_REQUEST);
                    packet.WriteUINT32(remoteid);
                    packet.WriteString(requesttype);
                    packet.WriteBool(wantreply);
                    if (requestdata != null)
                    {
                        packet.WriteBytes(requestdata);
                    }

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_REQUEST " + requesttype);
                    System.Diagnostics.Trace.WriteLine("Channelid=" + ChannelID);
#endif
                    connection.SendMessage(packet);

                    bool result = false;

                    if (wantreply)
                    {
                        packet = ProcessMessages(CHANNEL_REQUEST_MESSAGES);
                        return(packet.MessageID == SSH_MSG_CHANNEL_SUCCESS);
                    }

                    return(result);
                }
                catch (System.IO.IOException ex)
                {
                    throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Process a channel open request.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="remoteid"></param>
        /// <param name="remotewindow"></param>
        /// <param name="remotepacket"></param>
        /// <param name="requestdata"></param>
        internal void  ProcessChannelOpenRequest(System.String type, int remoteid, int remotewindow, int remotepacket, byte[] requestdata)
        {
            try
            {
                SSHPacket packet = GetPacket();

                if (channelfactories.ContainsKey(type))
                {
                    try
                    {
                        SSH2Channel channel = ((ChannelFactory)channelfactories[type]).CreateChannel(type, requestdata);

                        // Allocate a channel
                        int localid = AllocateChannel(channel);

                        if (localid > -1)
                        {
                            try
                            {
                                channel.Init(this, localid);
                                byte[] responsedata = channel.Create();
                                packet.WriteByte(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                                packet.WriteUINT32(remoteid);
                                packet.WriteUINT32(localid);
                                packet.WriteUINT32(channel.WindowSize);
                                packet.WriteUINT32(channel.PacketSize);
                                if (responsedata != null)
                                {
                                    packet.WriteBytes(requestdata);
                                }

#if DEBUG
                                System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_OPEN_CONFIRMATION");
                                System.Diagnostics.Trace.WriteLine("Channelid=" + localid);
                                System.Diagnostics.Trace.WriteLine("Remoteid=" + remoteid);
#endif
                                transport.SendMessage(packet);

                                channel.Open(remoteid, remotewindow, remotepacket);

                                return;
                            }
                            catch (SSHException ex)
                            {
#if DEBUG
                                System.Diagnostics.Trace.WriteLine("Exception occured whilst opening channel");
                                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
#endif
                                packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE);
                                packet.WriteUINT32(remoteid);
                                packet.WriteUINT32(ChannelOpenException.CONNECT_FAILED);
                                packet.WriteString(ex.Message);
                                packet.WriteString("");
                            }
                        }
                        else
                        {
#if DEBUG
                            System.Diagnostics.Trace.WriteLine("Maximum allowable open channel limit of " + MaximumNumChannels + " exceeded!");
#endif
                            packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE);
                            packet.WriteUINT32(remoteid);
                            packet.WriteUINT32(ChannelOpenException.RESOURCE_SHORTAGE);
                            packet.WriteString("Maximum allowable open channel limit of " + MaximumNumChannels + " exceeded!");
                            packet.WriteString("");
                        }
                    }
                    catch (ChannelOpenException ex)
                    {
#if DEBUG
                        System.Diagnostics.Trace.WriteLine("Channel open exception occured whilst opening channel");
                        System.Diagnostics.Trace.WriteLine(ex.StackTrace);
#endif
                        packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE);
                        packet.WriteUINT32(remoteid);
                        packet.WriteUINT32(ex.Reason);
                        packet.WriteString(ex.Message);
                        packet.WriteString("");
                    }
                }
                else
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine(type + " is not a supported channel type");
#endif
                    packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE);
                    packet.WriteUINT32(remoteid);
                    packet.WriteUINT32(ChannelOpenException.UNKNOWN_CHANNEL_TYPE);
                    packet.WriteString(type + " is not a supported channel type!");
                    packet.WriteString("");
                }

#if DEBUG
                System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_OPEN_FAILURE");
                System.Diagnostics.Trace.WriteLine("Remoteid=" + remoteid);
                System.Diagnostics.Trace.WriteLine("Name=" + type);
#endif
                transport.SendMessage(packet);
            }
            catch (System.IO.IOException ex1)
            {
                throw new SSHException(ex1.Message, SSHException.INTERNAL_ERROR);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Opens a channel
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="requestdata"></param>
        public void OpenChannel(SSH2Channel channel, byte[] requestdata)
        {
            lock (this)
            {
                try
                {
                    int channelid = AllocateChannel(channel);

                    if (channelid == -1)
                    {
                        throw new ChannelOpenException("Maximum number of channels exceeded",
                                                       ChannelOpenException.RESOURCE_SHORTAGE);
                    }

                    channel.Init(this, channelid);
                    SSHPacket packet = GetPacket();
                    packet.WriteByte(SSH_MSG_CHANNEL_OPEN);
                    packet.WriteString(channel.Name);
                    packet.WriteUINT32(channel.ChannelID);
                    packet.WriteUINT32(channel.WindowSize);
                    packet.WriteUINT32(channel.PacketSize);
                    if (requestdata != null)
                    {
                        packet.WriteBytes(requestdata);
                    }

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_OPEN");
                    System.Diagnostics.Trace.WriteLine("Channelid=" + channel.ChannelID);
                    System.Diagnostics.Trace.WriteLine("Name=" + channel.Name);
#endif

                    transport.SendMessage(packet);

                    packet = channel.MessageStore.NextMessage(CHANNEL_OPEN_RESPONSE_MESSAGES);

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Received reply to SSH_MSG_CHANNEL_OPEN");
                    channel.LogMessage(packet);
#endif
                    if (packet.MessageID == SSH_MSG_CHANNEL_OPEN_FAILURE)
                    {
                        FreeChannel(channel);
                        int reason = (int)packet.ReadUINT32();
                        throw new ChannelOpenException(packet.ReadString(), reason);
                    }
                    else
                    {
                        int    remoteid     = (int)packet.ReadUINT32();
                        int    remotewindow = (int)packet.ReadUINT32();
                        int    remotepacket = (int)packet.ReadUINT32();
                        byte[] responsedata = new byte[packet.Available];
                        packet.ReadBytes(responsedata);

                        channel.Open(remoteid, remotewindow, remotepacket, responsedata);

                        return;
                    }
                }
                catch (System.IO.IOException ex)
                {
                    throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Start the authentication request.
        /// </summary>
        /// <param name="authentication">The authentication protocol instance.</param>
        /// <param name="serviceName">The name of the service to start upon a successful authentication.</param>
        public void Authenticate(AuthenticationProtocol authentication, String serviceName)
        {
            if (InteractivePrompt == null)
            {
                throw new SSHException("An interactive prompt event must be set!",
                                       SSHException.BAD_API_USAGE);
            }

            ByteBuffer baw = new ByteBuffer();

            baw.WriteString("");
            baw.WriteString("");

            authentication.SendRequest(username,
                                       serviceName,
                                       "keyboard-interactive",
                                       baw.ToByteArray());

            while (true)
            {
                SSHPacket msg = authentication.ReadMessage();
                if (msg.MessageID != SSH_MSG_USERAUTH_INFO_REQUEST)
                {
                    authentication.transport.Disconnect("Unexpected authentication message received!",
                                                        DisconnectionReason.PROTOCOL_ERROR);
                    throw new SSHException("Unexpected authentication message received!",
                                           SSHException.PROTOCOL_VIOLATION);
                }

                String name        = msg.ReadString();
                String instruction = msg.ReadString();
                String langtag     = msg.ReadString();

                int         num = (int)msg.ReadInt();
                String      prompt;
                bool        echo;
                KBIPrompt[] prompts = new KBIPrompt[num];
                for (int i = 0; i < num; i++)
                {
                    prompt     = msg.ReadString();
                    echo       = (msg.ReadBool());
                    prompts[i] = new KBIPrompt(prompt, echo);
                }

                if (InteractivePrompt(name, instruction, prompts))
                {
                    msg = authentication.transport.GetSSHPacket(true);
                    msg.WriteByte(SSH_MSG_USERAUTH_INFO_RESPONSE);
                    msg.WriteInt(prompts.Length);

                    for (int i = 0; i < prompts.Length; i++)
                    {
                        msg.WriteString(prompts[i].Response);
                    }

                    authentication.transport.SendMessage(msg);
                }
                else
                {
                    throw new SSHException("User cancelled during authentication",
                                           SSHException.USER_CANCELATION);
                }
            }
        }