Esempio n. 1
0
        private void ExecShell()
        {
            //System.out.println("EXEC SHELL");
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_EXEC_SHELL);

            SSH1Packet.WriteTo(_stream, _tCipher);
        }
Esempio n. 2
0
        private void ProcessPortforwardingRequest(ISSHConnectionEventReceiver receiver, SSH1DataReader reader)
        {
            int    server_channel = reader.ReadInt32();
            string host           = Encoding.ASCII.GetString(reader.ReadString());
            int    port           = reader.ReadInt32();

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

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

                writer.WriteInt32(server_channel);
                writer.WriteInt32(local_id);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, writer.ToByteArray());
                p.WriteTo(_stream, _tCipher);
            }
            else
            {
                writer.WriteInt32(server_channel);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE, writer.ToByteArray());
                p.WriteTo(_stream, _tCipher);
            }
        }
Esempio n. 3
0
        //returns true if a new packet could be obtained
        private DataFragment ConstructPacket()
        {
            if (_writeOffset - _readOffset < 4)
            {
                return(null);
            }
            int packet_length  = SSHUtil.ReadInt32(_buffer, _readOffset);
            int padding_length = 8 - (packet_length % 8); //padding length
            int total          = packet_length + padding_length;

            if (_writeOffset - _readOffset < 4 + total)
            {
                return(null);
            }

            byte[] decrypted = new byte[total];
            if (_cipher != null)
            {
                _cipher.Decrypt(_buffer, _readOffset + 4, total, decrypted, 0);
            }
            else
            {
                Array.Copy(_buffer, _readOffset + 4, decrypted, 0, total);
            }
            _readOffset += 4 + total;

            SSH1Packet p = new SSH1Packet();

            return(ConstructAndCheck(decrypted, packet_length, padding_length, _checkMAC));
        }
Esempio n. 4
0
        public static SSH1Packet FromPlainPayload(PacketType type)
        {
            SSH1Packet p = new SSH1Packet();

            p._type = (byte)type;
            p._data = new byte[0];
            return(p);
        }
Esempio n. 5
0
        /**
         * creates a packet as the input of shell
         */
        static SSH1Packet AsStdinString(byte[] input)
        {
            SSH1DataWriter w = new SSH1DataWriter();

            w.WriteAsString(input);
            SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_STDIN_DATA, w.ToByteArray());

            return(p);
        }
Esempio n. 6
0
        public override void SendIgnorableData(string msg)
        {
            SSH1DataWriter w = new SSH1DataWriter();

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

            Transmit(p);
        }
Esempio n. 7
0
        private void SendUserName(string username)
        {
            SSH1DataWriter writer = new SSH1DataWriter();

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

            SSH1Packet.WriteTo(_stream, _tCipher);
            TraceTransmissionEvent(PacketType.SSH_CMSG_USER, "sent user name");
        }
Esempio n. 8
0
        private void SendPlainPassword()
        {
            SSH1DataWriter writer = new SSH1DataWriter();

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

            SSH1Packet.WriteTo(_stream, _tCipher);
            TraceTransmissionEvent(PacketType.SSH_CMSG_AUTH_PASSWORD, "sent password");
        }
Esempio n. 9
0
        private void SendExecCommand()
        {
            Debug.WriteLine("EXEC COMMAND");
            string         cmd    = _execCmd;
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.WriteString(cmd);
            SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_EXEC_CMD, writer.ToByteArray());

            SSH1Packet.WriteTo(_stream, _tCipher);
            TraceTransmissionEvent(PacketType.SSH_CMSG_EXEC_CMD, "exec command: cmd={0}", cmd);
        }
Esempio n. 10
0
        /**
         * resizes the size of terminal
         */
        public override void ResizeTerminal(int width, int height, int pixel_width, int pixel_height)
        {
            SSH1DataWriter writer = new SSH1DataWriter();

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

            Transmit(p);
        }
Esempio n. 11
0
        public override void Disconnect(string msg)
        {
            if (!this.IsOpen)
            {
                return;
            }
            SSH1DataWriter w = new SSH1DataWriter();

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

            p.WriteTo(_stream, _tCipher);
            base.Close();
        }
Esempio n. 12
0
        private void SendRequestPTY()
        {
            SSH1DataWriter writer = new SSH1DataWriter();

            writer.WriteString(_param.TerminalName);
            writer.WriteInt32(_param.TerminalHeight);
            writer.WriteInt32(_param.TerminalWidth);
            writer.WriteInt32(_param.TerminalPixelWidth);
            writer.WriteInt32(_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);
            TraceTransmissionEvent(PacketType.SSH_CMSG_REQUEST_PTY, "open shell: terminal={0} width={1} height={2}", _param.TerminalName, _param.TerminalWidth, _param.TerminalHeight);
        }
Esempio n. 13
0
        public override void ListenForwardedPort(string allowed_host, int bind_port)
        {
            SSH1DataWriter writer = new SSH1DataWriter();

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

            p.WriteTo(_stream, _tCipher);
            TraceTransmissionEvent(PacketType.SSH_CMSG_PORT_FORWARD_REQUEST, "start to listening to remote port: host={0} port={1}", allowed_host, bind_port);

            if (_shellID == -1)
            {
                ExecShell();
                _shellID = _channel_collection.RegisterChannelEventReceiver(null, new SSH1DummyReceiver()).LocalID;
            }
        }
Esempio n. 14
0
        /**
         * transmits channel data
         */
        public override 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.WriteInt32(_remoteID);
                wr.WriteAsString(data, offset, length);
                SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_MSG_CHANNEL_DATA, wr.ToByteArray());
                Transmit(p);
            }
        }
Esempio n. 15
0
        //RSA authentication
        private void DoRSAChallengeResponse()
        {
            //read key
            SSH1UserAuthKey key = new SSH1UserAuthKey(_param.IdentityFile, _param.Password);
            SSH1DataWriter  w   = new SSH1DataWriter();

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

            p.WriteTo(_stream, _tCipher);
            TraceTransmissionEvent(PacketType.SSH_CMSG_AUTH_RSA, "RSA challenge-reponse");

            DataFragment   response = ReceivePacket();
            SSH1DataReader reader   = new SSH1DataReader(response);
            PacketType     pt       = reader.ReadPacketType();

            if (pt == PacketType.SSH_SMSG_FAILURE)
            {
                throw new SSHException(Strings.GetString("ServerRefusedRSA"));
            }
            else if (pt != PacketType.SSH_SMSG_AUTH_RSA_CHALLENGE)
            {
                throw new SSHException(String.Format(Strings.GetString("UnexpectedResponse"), pt));
            }
            TraceReceptionEvent(PacketType.SSH_SMSG_AUTH_RSA_CHALLENGE, "received challenge");

            //creating challenge
            BigInteger challenge = key.decryptChallenge(reader.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[] reply = new MD5CryptoServiceProvider().ComputeHash(bos.ToArray());

            w = new SSH1DataWriter();
            w.Write(reply);
            p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA_RESPONSE, w.ToByteArray());
            p.WriteTo(_stream, _tCipher);
            TraceReceptionEvent(PacketType.SSH_CMSG_AUTH_RSA_RESPONSE, "received response");
        }
Esempio n. 16
0
        /// <summary>
        /// Builds SSH_MSG_PORT_OPEN packet
        /// </summary>
        protected override Tuple <SSH1Packet, string, object[]> BuildOpenPacket()
        {
            var packet =
                new SSH1Packet(SSH1PacketType.SSH_MSG_PORT_OPEN)
                .WriteUInt32(LocalChannel)
                .WriteString(_remoteHost)
                .WriteUInt32(_remotePort);

            // Note:
            //  "originator" is specified only if both sides specified
            //  SSH_PROTOFLAG_HOST_IN_FWD_OPEN in the protocol flags.
            //  currently 0 is used as the protocol flags.
            //
            //  .WriteString(_originatorIp + ":" + _originatorPort.ToString(NumberFormatInfo.InvariantInfo))

            return(Tuple.Create(
                       packet,
                       "CH[0] port open : remoteHost={1} remotePort={2}",
                       new object[] { LocalChannel, _remoteHost, _remotePort }
                       ));
        }
Esempio n. 17
0
        /**
         * closes this channel
         */
        public override void Close()
        {
            if (!_connection.IsOpen)
            {
                return;
            }

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

            SSH1DataWriter wr = new SSH1DataWriter();

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

            Transmit(p);
        }
Esempio n. 18
0
        public override SSHChannel ForwardPort(ISSHChannelEventReceiver receiver, string remote_host, int remote_port, string originator_host, int originator_port)
        {
            if (_shellID == -1)
            {
                ExecShell();
                _shellID = _channel_collection.RegisterChannelEventReceiver(null, new SSH1DummyReceiver()).LocalID;
            }

            int local_id = _channel_collection.RegisterChannelEventReceiver(null, receiver).LocalID;

            SSH1DataWriter writer = new SSH1DataWriter();

            writer.WriteInt32(local_id); //channel id is fixed to 0
            writer.WriteString(remote_host);
            writer.WriteInt32(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);
            TraceTransmissionEvent(PacketType.SSH_MSG_PORT_OPEN, "open forwarded port: host={0} port={1}", remote_host, remote_port);

            return(new SSH1Channel(this, ChannelType.ForwardedLocalToRemote, local_id));
        }
Esempio n. 19
0
 private void Transmit(SSH1Packet p)
 {
     _connection.Transmit(p);
 }
Esempio n. 20
0
 private void Transmit(SSH1Packet p)
 {
     _connection.Transmit(p);
 }
Esempio n. 21
0
 internal void Transmit(SSH1Packet p)
 {
     lock (_transmitSync) {
         _stream.Write(p.GetImage(_cipher));
     }
 }
Esempio n. 22
0
 /// <summary>
 /// Sends a packet.
 /// </summary>
 /// <param name="packet">packet object</param>
 protected void Transmit(SSH1Packet packet)
 {
     _packetSender.Send(packet);
 }
Esempio n. 23
0
 private void TransmitWithoutEncryption(SSH1Packet p)
 {
     lock (_transmitSync) {
         _stream.Write(p.GetImage());
     }
 }
Esempio n. 24
0
 public static SSH1Packet FromPlainPayload(PacketType type)
 {
     SSH1Packet p = new SSH1Packet();
     p._type = (byte)type;
     p._data = new byte[0];
     return p;
 }
Esempio n. 25
0
        /// <summary>
        /// Builds SSH_MSG_PORT_OPEN packet
        /// </summary>
        protected override Tuple<SSH1Packet, string, object[]> BuildOpenPacket()
        {
            var packet =
                new SSH1Packet(SSH1PacketType.SSH_MSG_PORT_OPEN)
                    .WriteUInt32(LocalChannel)
                    .WriteString(_remoteHost)
                    .WriteUInt32(_remotePort);
            // Note:
            //  "originator" is specified only if both sides specified
            //  SSH_PROTOFLAG_HOST_IN_FWD_OPEN in the protocol flags.
            //  currently 0 is used as the protocol flags.
            //
            //  .WriteString(_originatorIp + ":" + _originatorPort.ToString(NumberFormatInfo.InvariantInfo))

            return Tuple.Create(
                packet,
                "CH[0] port open : remoteHost={1} remotePort={2}",
                new object[] { LocalChannel, _remoteHost, _remotePort }
            );
        }
Esempio n. 26
0
        //returns true if a new packet could be obtained
        private DataFragment ConstructPacket()
        {
            if (_writeOffset - _readOffset < 4)
                return null;
            int packet_length = SSHUtil.ReadInt32(_buffer, _readOffset);
            int padding_length = 8 - (packet_length % 8); //padding length
            int total = packet_length + padding_length;
            if (_writeOffset - _readOffset < 4 + total)
                return null;

            byte[] decrypted = new byte[total];
            if (_cipher != null)
                _cipher.Decrypt(_buffer, _readOffset + 4, total, decrypted, 0);
            else
                Array.Copy(_buffer, _readOffset + 4, decrypted, 0, total);
            _readOffset += 4 + total;

            SSH1Packet p = new SSH1Packet();
            return ConstructAndCheck(decrypted, packet_length, padding_length, _checkMAC);
        }
Esempio n. 27
0
 internal void Transmit(SSH1Packet p)
 {
     lock (this) {
         p.WriteTo(_stream, _tCipher);
     }
 }
Esempio n. 28
0
 /// <summary>
 /// Sends a packet.
 /// </summary>
 /// <param name="packet">packet object</param>
 protected void Transmit(SSH1Packet packet)
 {
     _packetSender.Send(packet);
 }
Esempio n. 29
0
 internal void Transmit(SSH1Packet p)
 {
     lock (this) {
         p.WriteTo(_stream, _tCipher);
     }
 }
Esempio n. 30
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;
                }

                Rng        rng           = RngManager.GetSecureRng();
                BigInteger first_result  = RSAUtil.PKCS1PadType2(new BigInteger(working_data), first_key_bytelen, rng).modPow(first_encryption.Exponent, first_encryption.Modulus);
                BigInteger second_result = RSAUtil.PKCS1PadType2(first_result, second_key_bytelen, rng).modPow(second_encryption.Exponent, second_encryption.Modulus);

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

                //send
                TraceTransmissionEvent(PacketType.SSH_CMSG_SESSION_KEY, "sent encrypted session-keys");
                SSH1Packet packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_SESSION_KEY, writer.ToByteArray());
                packet.WriteTo(_stream);

                _sessionID = session_id;
            }
            catch (Exception e) {
                if (e is IOException)
                {
                    throw (IOException)e;
                }
                else
                {
                    string t = e.StackTrace;
                    throw new SSHException(e.Message); //IOException以外はみなSSHExceptionにしてしまう
                }
            }
        }