Пример #1
0
        public Packet RecvPacket(ICryptoTransform decryptor)
        {
            if (decryptor == null)
            {
                Packet packet = new Packet();
                uint packet_length = reader.ReadUInt32();
                byte padding_length = reader.ReadByte();
                byte[] payload = reader.ReadBytes(packet_length - padding_length - 1);
                byte[] padding = reader.ReadBytes(padding_length);
                packet.SetPayload(payload);
                return packet;
            }
            else
            {
                int blocksize = Math.Max(decryptor.InputBlockSize, 8);
                MemoryStream ms_packet = new MemoryStream();
                NetworkByteWriter writer_packet = new NetworkByteWriter(ms_packet);

                byte[] buffer = new byte[blocksize];

                bool first = true;
                int more = 0;
                while (true)
                {
                    // read a block
                    int ret = reader.ReadBytes(buffer, 0, blocksize);
                    // must be a real block size;
                    if (ret != blocksize) return null;
                    decryptor.TransformBlock(buffer, 0, buffer.Length, buffer, 0);
                    writer_packet.WriteBytes(buffer);

                    if (first) // it's first time, need parse packet_length and padding_length
                    {
                        NetworkByteReader reader_buffer = new NetworkByteReader(new MemoryStream(buffer));
                        uint packet_length_t = reader_buffer.ReadUInt32();
                        first = false;

                        more = (int)packet_length_t + 4 - blocksize;
                        if (more % blocksize != 0) return null;
                    }
                    else
                    {
                        more -= blocksize;
                    }
                    if (more <= 0) break;
                }

                byte[] mac = reader.ReadBytes(20);

                ms_packet.Seek(0, SeekOrigin.Begin);
                NetworkByteReader reader_packet = new NetworkByteReader(ms_packet);

                Packet packet = new Packet();
                uint packet_length = reader_packet.ReadUInt32();
                byte padding_length = reader_packet.ReadByte();
                byte[] payload = reader_packet.ReadBytes(packet_length - padding_length - 1);
                byte[] padding = reader_packet.ReadBytes(padding_length);
                packet.SetPayload(payload);

                return packet;
            }
        }
Пример #2
0
 public bool Connect(string address, int port)
 {
     tcpclient = new TcpClient(address, port);
     NetworkStream ns = tcpclient.GetStream();
     reader = new NetworkByteReader(ns);
     writer = new NetworkByteWriter(ns);
     return false;
 }
Пример #3
0
        public void KeyVerify(string algorithm, HashAlgorithm hash)
        {
            MemoryStream cache = new MemoryStream();
            NetworkByteWriter nbr_cache = new NetworkByteWriter(cache);

            nbr_cache.WriteString(verify_v_c);
            nbr_cache.WriteString(verify_v_s);
            nbr_cache.WriteBlob(verify_i_c);
            nbr_cache.WriteBlob(verify_i_s);
            nbr_cache.WriteBlob(verify_k_s);
            nbr_cache.WriteMPInt(verify_e);
            nbr_cache.WriteMPInt(verify_f);
            nbr_cache.WriteMPInt(verify_k);
            nbr_cache.Flush();

            if (algorithm == "ssh-rsa")
            {
                verify_h = hash.ComputeHash(cache.ToArray());

                RSAParameters RSAKeyInfo = new RSAParameters();
                {
                    MemoryStream ms_tmp = new MemoryStream(verify_k_s);
                    NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                    string type = nbr_tmp.ReadString();
                    BigInteger rsa_e = nbr_tmp.ReadMPInt();
                    BigInteger rsa_n = nbr_tmp.ReadMPInt();
                    RSAKeyInfo.Modulus = NetworkByteUtils.BigIntegerToUnsignedArray(rsa_n);
                    RSAKeyInfo.Exponent = NetworkByteUtils.BigIntegerToUnsignedArray(rsa_e);
                }
                byte[] rsa_signature_blob;
                {
                    MemoryStream ms_tmp = new MemoryStream(verify_sig);
                    NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                    string type = nbr_tmp.ReadString();
                    rsa_signature_blob = nbr_tmp.ReadBlob();
                }

                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSA.ImportParameters(RSAKeyInfo);
                RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
                RSADeformatter.SetHashAlgorithm("SHA1");

                byte[] xx2 = SHA1.Create().ComputeHash(verify_h);
                bool verify = RSADeformatter.VerifySignature(xx2, rsa_signature_blob);
            }
        }
Пример #4
0
 public void DumpError(Packet packet)
 {
     MemoryStream ms = new MemoryStream(packet.GetPayload());
     NetworkByteReader nbr = new NetworkByteReader(ms);
     nbr.ReadByte();
     nbr.ReadUInt32();
     string xxx = nbr.ReadString();
 }
Пример #5
0
        public override void Parse()
        {
            MemoryStream ms = new MemoryStream(payload);
            NetworkByteReader nbr = new NetworkByteReader(ms);
            nbr.ReadByte();
            certificates = nbr.ReadBlob();

            {
                MemoryStream ms_tmp = new MemoryStream(certificates);
                NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                string type = nbr_tmp.ReadString();
                BigInteger rsa_e = nbr_tmp.ReadMPInt();
                BigInteger rsa_n = nbr_tmp.ReadMPInt();
            }

            f = nbr.ReadMPInt();
            signature = nbr.ReadBlob();
            {
                MemoryStream ms_tmp = new MemoryStream(signature);
                NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                string type = nbr_tmp.ReadString();
                byte[] rsa_signature_blob = nbr_tmp.ReadBlob();
            }
        }
Пример #6
0
 public override void Parse()
 {
     MemoryStream ms = new MemoryStream(payload);
     NetworkByteReader nbr = new NetworkByteReader(ms);
     nbr.ReadByte();
     cookie = nbr.ReadBytes(16);
     kex_algorithms = nbr.ReadNameList();
     server_host_key_algorithms = nbr.ReadNameList();
     encryption_algorithms_client_to_server = nbr.ReadNameList();
     encryption_algorithms_server_to_client = nbr.ReadNameList();
     mac_algorithms_client_to_server = nbr.ReadNameList();
     mac_algorithms_server_to_client = nbr.ReadNameList();
     compression_algorithms_client_to_server = nbr.ReadNameList();
     compression_algorithms_server_to_client = nbr.ReadNameList();
     languages_client_to_server = nbr.ReadNameList();
     languages_server_to_client = nbr.ReadNameList();
     first_kex_packet_follows = nbr.ReadBoolean();
     reserved = nbr.ReadUInt32();
 }
Пример #7
0
 public override void Parse()
 {
     MemoryStream ms = new MemoryStream(payload);
     NetworkByteReader nbr = new NetworkByteReader(ms);
     nbr.ReadByte();
     nbr.ReadUInt32();
     data = nbr.ReadString();
     Console.Write(data);
 }