예제 #1
0
        private void ProcessPortforwardingRequest(ISSHConnectionEventReceiver receiver, SSH1Packet packet)
        {
            SSH1DataReader reader         = new SSH1DataReader(packet.Data);
            int            server_channel = reader.ReadInt32();

            byte[] tmpdata = reader.ReadString();
            string host    = Encoding.UTF8.GetString(tmpdata, 0, tmpdata.Length);
            int    port    = reader.ReadInt32();

            SSH1DataWriter            writer = new SSH1DataWriter();
            PortForwardingCheckResult result = receiver.CheckPortForwardingRequest(host, port, "", 0);

            if (result.allowed)
            {
                int local_id = this.RegisterChannelEventReceiver(null, result.channel)._localID;
                _eventReceiver.EstablishPortforwarding(result.channel, new SSH1Channel(this, ChannelType.ForwardedRemoteToLocal, local_id, server_channel));

                writer.Write(server_channel);
                writer.Write(local_id);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, writer.ToByteArray());
                p.WriteTo(_stream, _tCipher);
            }
            else
            {
                writer.Write(server_channel);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE, writer.ToByteArray());
                p.WriteTo(_stream, _tCipher);
            }
        }
예제 #2
0
        /**
         * resizes the size of terminal
         */
        public void ResizeTerminal(int width, int height, int pixel_width, int pixel_height)
        {
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(height);
            writer.Write(width);
            writer.Write(pixel_width);
            writer.Write(pixel_height);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_WINDOW_SIZE, writer.ToByteArray());

            Transmit(p);
        }
예제 #3
0
        public string DumpHostKeyInKnownHostsStyle()
        {
            StringBuilder bld = new StringBuilder();

            bld.Append("ssh1 ");
            SSH1DataWriter wr = new SSH1DataWriter();
            //RSA only for SSH1
            RSAPublicKey rsa = (RSAPublicKey)_hostkey;

            wr.Write(rsa.Exponent);
            wr.Write(rsa.Modulus);
            byte[] tmpdata = Base64.Encode(wr.ToByteArray());
            bld.Append(Encoding.UTF8.GetString(tmpdata, 0, tmpdata.Length));
            return(bld.ToString());
        }
예제 #4
0
        public void ListenForwardedPort(string allowed_host, int bind_port)
        {
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(bind_port);
            writer.Write(allowed_host);
            writer.Write(0);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_PORT_FORWARD_REQUEST, writer.ToByteArray());

            p.WriteTo(_stream, _tCipher);

            if (_shellID == -1)
            {
                ExecShell();
                _shellID = RegisterChannelEventReceiver(null, new SSH1DummyReceiver())._localID;
            }
        }
예제 #5
0
        public void SendIgnorableData(string msg)
        {
            SSH1DataWriter w = new SSH1DataWriter();

            w.Write(msg);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_IGNORE, w.ToByteArray());

            Transmit(p);
        }
예제 #6
0
        private void SendUserName(string username)
        {
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(username);
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_USER, writer.ToByteArray());

            SSH1Packet.WriteTo(_stream, _tCipher);
        }
예제 #7
0
        private void SendPlainPassword()
        {
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(_param.Password);
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_PASSWORD, writer.ToByteArray());

            SSH1Packet.WriteTo(_stream, _tCipher);
        }
예제 #8
0
        public SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
        {
            if (_shellID == -1)
            {
                ExecShell();
                _shellID = RegisterChannelEventReceiver(null, new SSH1DummyReceiver())._localID;
            }

            int local_id = this.RegisterChannelEventReceiver(null, receiver)._localID;

            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(local_id);             //channel id is fixed to 0
            writer.Write(remote_host);
            writer.Write(remote_port);
            //originator is specified only if SSH_PROTOFLAG_HOST_IN_FWD_OPEN is specified
            //writer.Write(originator_host);
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_PORT_OPEN, writer.ToByteArray());

            SSH1Packet.WriteTo(_stream, _tCipher);

            return(new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id));
        }
예제 #9
0
        public void Disconnect(string msg)
        {
            if (_closed)
            {
                return;
            }
            SSH1DataWriter w = new SSH1DataWriter();

            w.Write(msg);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_DISCONNECT, w.ToByteArray());

            p.WriteTo(_stream, _tCipher);
            _stream.Flush();
            _closed = true;
            _stream.Close();
        }
예제 #10
0
        /**
         * transmits channel data
         */
        public void Transmit(byte[] data)
        {
            SSH1DataWriter wr = new SSH1DataWriter();

            if (_type == ChannelType.Shell)
            {
                wr.WriteAsString(data);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_STDIN_DATA, wr.ToByteArray());
                Transmit(p);
            }
            else
            {
                wr.Write(_remoteID);
                wr.WriteAsString(data);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_DATA, wr.ToByteArray());
                Transmit(p);
            }
        }
예제 #11
0
        private void SendRequestPTY()
        {
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.Write(_param.TerminalName);
            writer.Write(_param.TerminalHeight);
            writer.Write(_param.TerminalWidth);
            writer.Write(_param.TerminalPixelWidth);
            writer.Write(_param.TerminalPixelHeight);
            writer.Write(new byte[1]);             //TTY_OP_END
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_REQUEST_PTY, writer.ToByteArray());

            SSH1Packet.WriteTo(_stream, _tCipher);
        }
예제 #12
0
        /**
         * closes this channel
         */
        public void Close()
        {
            if (_connection.IsClosed())
            {
                return;
            }

            if (_type == ChannelType.Shell)
            {
                SSH1DataWriter wr2 = new SSH1DataWriter();
                wr2.Write(_remoteID);
                SSH1Packet p2 = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_EOF, wr2.ToByteArray());
                Transmit(p2);
            }

            SSH1DataWriter wr = new SSH1DataWriter();

            wr.Write(_remoteID);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_CLOSE, wr.ToByteArray());

            Transmit(p);
        }
예제 #13
0
        //RSA authentication
        private void DoRSAChallengeResponse()
        {
            //read key
            SSH1UserAuthKey key = new SSH1UserAuthKey(_param.IdentityFile, _param.Password);
            SSH1DataWriter  w   = new SSH1DataWriter();

            w.Write(key.PublicModulus);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA, w.ToByteArray());

            p.WriteTo(_stream, _tCipher);

            p = ReceivePacket();
            if (p.Type == PacketType.SSH_SMSG_FAILURE)
            {
                throw new Exception(Strings.GetString("ServerRefusedRSA"));
            }
            else if (p.Type != PacketType.SSH_SMSG_AUTH_RSA_CHALLENGE)
            {
                throw new Exception(String.Format(Strings.GetString("UnexpectedResponse"), p.Type));
            }

            //creating challenge
            SSH1DataReader r         = new SSH1DataReader(p.Data);
            BigInteger     challenge = key.decryptChallenge(r.ReadMPInt());

            byte[] rawchallenge = RSAUtil.StripPKCS1Pad(challenge, 2).getBytes();

            //building response
            MemoryStream bos = new MemoryStream();

            bos.Write(rawchallenge, 0, rawchallenge.Length);             //!!mindtermでは頭が0かどうかで変なハンドリングがあった
            bos.Write(_sessionID, 0, _sessionID.Length);
            byte[] response = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5).HashData(bos.ToArray().AsBuffer()).ToArray();;

            w = new SSH1DataWriter();
            w.Write(response);
            p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA_RESPONSE, w.ToByteArray());
            p.WriteTo(_stream, _tCipher);
        }
예제 #14
0
 public void Disconnect(string msg)
 {
     if(_closed) return;
     SSH1DataWriter w = new SSH1DataWriter();
     w.Write(msg);
     SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_DISCONNECT, w.ToByteArray());
     p.WriteTo(_stream, _tCipher);
     _stream.Flush();
     _closed = true;
     _stream.Close();
 }
예제 #15
0
        public void ListenForwardedPort(string allowed_host, int bind_port)
        {
            SSH1DataWriter writer = new SSH1DataWriter();
            writer.Write(bind_port);
            writer.Write(allowed_host);
            writer.Write(0);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_PORT_FORWARD_REQUEST, writer.ToByteArray());
            p.WriteTo(_stream, _tCipher);

            if(_shellID==-1) {
                ExecShell();
                _shellID = RegisterChannelEventReceiver(null, new SSH1DummyReceiver())._localID;
            }
        }
예제 #16
0
 public void SendIgnorableData(string msg)
 {
     SSH1DataWriter w = new SSH1DataWriter();
     w.Write(msg);
     SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_IGNORE, w.ToByteArray());
     Transmit(p);
 }
예제 #17
0
        //RSA authentication
        private void DoRSAChallengeResponse()
        {
            //read key
            SSH1UserAuthKey key = new SSH1UserAuthKey(_param.IdentityFile, _param.Password);
            SSH1DataWriter w = new SSH1DataWriter();
            w.Write(key.PublicModulus);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA, w.ToByteArray());
            p.WriteTo(_stream, _tCipher);

            p = ReceivePacket();
            if(p.Type==PacketType.SSH_SMSG_FAILURE)
                throw new Exception(Strings.GetString("ServerRefusedRSA"));
            else if(p.Type!=PacketType.SSH_SMSG_AUTH_RSA_CHALLENGE)
                throw new Exception(String.Format(Strings.GetString("UnexpectedResponse"), p.Type));

            //creating challenge
            SSH1DataReader r = new SSH1DataReader(p.Data);
            BigInteger challenge = key.decryptChallenge(r.ReadMPInt());
            byte[] rawchallenge = RSAUtil.StripPKCS1Pad(challenge, 2).getBytes();

            //building response
            MemoryStream bos = new MemoryStream();
            bos.Write(rawchallenge, 0, rawchallenge.Length); //!!mindterm�ł͓����O���ǂ����ŕςȃn���h�����O��������
            bos.Write(_sessionID, 0, _sessionID.Length);
            byte[] response = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5).HashData(bos.ToArray().AsBuffer()).ToArray(); ;

            w = new SSH1DataWriter();
            w.Write(response);
            p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA_RESPONSE, w.ToByteArray());
            p.WriteTo(_stream, _tCipher);
        }
예제 #18
0
        private void ProcessPortforwardingRequest(ISSHConnectionEventReceiver receiver, SSH1Packet packet)
        {
            SSH1DataReader reader = new SSH1DataReader(packet.Data);
            int server_channel = reader.ReadInt32();
            byte[] tmpdata = reader.ReadString();
            string host = Encoding.UTF8.GetString(tmpdata,0, tmpdata.Length);
            int port = reader.ReadInt32();

            SSH1DataWriter writer = new SSH1DataWriter();
            PortForwardingCheckResult result = receiver.CheckPortForwardingRequest(host, port, "", 0);
            if(result.allowed) {
                int local_id = this.RegisterChannelEventReceiver(null, result.channel)._localID;
                _eventReceiver.EstablishPortforwarding(result.channel, new SSH1Channel(this, ChannelType.ForwardedRemoteToLocal, local_id, server_channel));

                writer.Write(server_channel);
                writer.Write(local_id);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, writer.ToByteArray());
                p.WriteTo(_stream, _tCipher);
            }
            else {
                writer.Write(server_channel);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE, writer.ToByteArray());
                p.WriteTo(_stream, _tCipher);
            }
        }
예제 #19
0
 private void SendPlainPassword()
 {
     SSH1DataWriter writer = new SSH1DataWriter();
     writer.Write(_param.Password);
     SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_PASSWORD, writer.ToByteArray());
     SSH1Packet.WriteTo(_stream, _tCipher);
 }
예제 #20
0
 private void SendRequestPTY()
 {
     SSH1DataWriter writer = new SSH1DataWriter();
     writer.Write(_param.TerminalName);
     writer.Write(_param.TerminalHeight);
     writer.Write(_param.TerminalWidth);
     writer.Write(_param.TerminalPixelWidth);
     writer.Write(_param.TerminalPixelHeight);
     writer.Write(new byte[1]); //TTY_OP_END
     SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_REQUEST_PTY, writer.ToByteArray());
     SSH1Packet.WriteTo(_stream, _tCipher);
 }
예제 #21
0
        private void SendSessionKey(byte[] session_key)
        {
            try
            {
                //step1 XOR with session_id
                byte[] working_data = new byte[session_key.Length];
                byte[] session_id = CalcSessionID();
                Array.Copy(session_key, 0, working_data, 0, session_key.Length);
                for(int i=0; i<session_id.Length; i++) working_data[i] ^= session_id[i];

                //step2 decrypts with RSA
                RSAPublicKey first_encryption;
                RSAPublicKey second_encryption;
                SSHServerInfo si = _cInfo._serverinfo;
                int first_key_bytelen, second_key_bytelen;
                if(si.server_key_bits < si.host_key_bits)
                {
                    first_encryption  = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus);
                    second_encryption = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus);
                    first_key_bytelen = (si.server_key_bits+7)/8;
                    second_key_bytelen = (si.host_key_bits+7)/8;
                }
                else
                {
                    first_encryption  = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus);
                    second_encryption = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus);
                    first_key_bytelen = (si.host_key_bits+7)/8;
                    second_key_bytelen = (si.server_key_bits+7)/8;
                }

                BigInteger first_result = RSAUtil.PKCS1PadType2(new BigInteger(working_data), first_key_bytelen, _param.Random).modPow(first_encryption.Exponent, first_encryption.Modulus);
                BigInteger second_result = RSAUtil.PKCS1PadType2(first_result, second_key_bytelen, _param.Random).modPow(second_encryption.Exponent, second_encryption.Modulus);

                //output
                SSH1DataWriter writer = new SSH1DataWriter();
                writer.Write((byte)_cInfo._algorithmForTransmittion);
                writer.Write(si.anti_spoofing_cookie);
                writer.Write(second_result);
                writer.Write(0); //protocol flags

                //send
                SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_SESSION_KEY, writer.ToByteArray());
                SSH1Packet.WriteTo(_stream);

                _sessionID = session_id;

            }
            catch(Exception e)
            {
                if(e is IOException)
                    throw (IOException)e;
                else
                {
                    string t = e.StackTrace;
                    throw new Exception(e.Message); //IOException�ȊO�݂͂�SSHException�ɂ��Ă��܂�
                }
            }
        }
예제 #22
0
        private void SendSessionKey(byte[] session_key)
        {
            try
            {
                //step1 XOR with session_id
                byte[] working_data = new byte[session_key.Length];
                byte[] session_id   = CalcSessionID();
                Array.Copy(session_key, 0, working_data, 0, session_key.Length);
                for (int i = 0; i < session_id.Length; i++)
                {
                    working_data[i] ^= session_id[i];
                }

                //step2 decrypts with RSA
                RSAPublicKey  first_encryption;
                RSAPublicKey  second_encryption;
                SSHServerInfo si = _cInfo._serverinfo;
                int           first_key_bytelen, second_key_bytelen;
                if (si.server_key_bits < si.host_key_bits)
                {
                    first_encryption   = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus);
                    second_encryption  = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus);
                    first_key_bytelen  = (si.server_key_bits + 7) / 8;
                    second_key_bytelen = (si.host_key_bits + 7) / 8;
                }
                else
                {
                    first_encryption   = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus);
                    second_encryption  = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus);
                    first_key_bytelen  = (si.host_key_bits + 7) / 8;
                    second_key_bytelen = (si.server_key_bits + 7) / 8;
                }

                BigInteger first_result  = RSAUtil.PKCS1PadType2(new BigInteger(working_data), first_key_bytelen, _param.Random).modPow(first_encryption.Exponent, first_encryption.Modulus);
                BigInteger second_result = RSAUtil.PKCS1PadType2(first_result, second_key_bytelen, _param.Random).modPow(second_encryption.Exponent, second_encryption.Modulus);

                //output
                SSH1DataWriter writer = new SSH1DataWriter();
                writer.Write((byte)_cInfo._algorithmForTransmittion);
                writer.Write(si.anti_spoofing_cookie);
                writer.Write(second_result);
                writer.Write(0);                 //protocol flags

                //send
                SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_SESSION_KEY, writer.ToByteArray());
                SSH1Packet.WriteTo(_stream);

                _sessionID = session_id;
            }
            catch (Exception e)
            {
                if (e is IOException)
                {
                    throw (IOException)e;
                }
                else
                {
                    string t = e.StackTrace;
                    throw new Exception(e.Message);                     //IOException以外はみなSSHExceptionにしてしまう
                }
            }
        }
예제 #23
0
 private void SendUserName(string username)
 {
     SSH1DataWriter writer = new SSH1DataWriter();
     writer.Write(username);
     SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_USER, writer.ToByteArray());
     SSH1Packet.WriteTo(_stream, _tCipher);
 }
예제 #24
0
        /**
         * closes this channel
         */
        public void Close()
        {
            if(_connection.IsClosed()) return;

            if(_type==ChannelType.Shell) {
                SSH1DataWriter wr2 = new SSH1DataWriter();
                wr2.Write(_remoteID);
                SSH1Packet p2 = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_EOF, wr2.ToByteArray());
                Transmit(p2);
            }

            SSH1DataWriter wr = new SSH1DataWriter();
            wr.Write(_remoteID);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_CLOSE, wr.ToByteArray());
            Transmit(p);
        }
예제 #25
0
 /**
  * resizes the size of terminal
  */
 public void ResizeTerminal(int width, int height, int pixel_width, int pixel_height)
 {
     SSH1DataWriter writer = new SSH1DataWriter();
     writer.Write(height);
     writer.Write(width);
     writer.Write(pixel_width);
     writer.Write(pixel_height);
     SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_WINDOW_SIZE, writer.ToByteArray());
     Transmit(p);
 }
예제 #26
0
        public SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
        {
            if(_shellID==-1) {
                ExecShell();
                _shellID = RegisterChannelEventReceiver(null, new SSH1DummyReceiver())._localID;
            }

            int local_id = this.RegisterChannelEventReceiver(null, receiver)._localID;

            SSH1DataWriter writer = new SSH1DataWriter();
            writer.Write(local_id); //channel id is fixed to 0
            writer.Write(remote_host);
            writer.Write(remote_port);
            //originator is specified only if SSH_PROTOFLAG_HOST_IN_FWD_OPEN is specified
            //writer.Write(originator_host);
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_PORT_OPEN, writer.ToByteArray());
            SSH1Packet.WriteTo(_stream, _tCipher);

            return new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id);
        }
예제 #27
0
 public string DumpHostKeyInKnownHostsStyle()
 {
     StringBuilder bld = new StringBuilder();
     bld.Append("ssh1 ");
     SSH1DataWriter wr = new SSH1DataWriter();
     //RSA only for SSH1
     RSAPublicKey rsa = (RSAPublicKey)_hostkey;
     wr.Write(rsa.Exponent);
     wr.Write(rsa.Modulus);
     byte[] tmpdata = Base64.Encode(wr.ToByteArray());
     bld.Append(Encoding.UTF8.GetString(tmpdata, 0 , tmpdata.Length));
     return bld.ToString();
 }
예제 #28
0
 /**
 * transmits channel data
 */
 public void Transmit(byte[] data, int offset, int length)
 {
     SSH1DataWriter wr = new SSH1DataWriter();
     if(_type==ChannelType.Shell) {
         wr.WriteAsString(data, offset, length);
         SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_STDIN_DATA, wr.ToByteArray());
         Transmit(p);
     }
     else {
         wr.Write(_remoteID);
         wr.WriteAsString(data, offset, length);
         SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_DATA, wr.ToByteArray());
         Transmit(p);
     }
 }