首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSLStream读取无效数据+ KB3147458 SSLStream bug (?)

SSLStream读取无效数据+ KB3147458 SSLStream bug (?)
EN

Stack Overflow用户
提问于 2016-04-17 12:39:18
回答 2查看 1.6K关注 0票数 2

当远程客户端没有发送任何信息时,SSLStream返回一些数据时,我遇到了问题。当服务器正在侦听一个新命令时,我遇到了这个问题。如果服务器没有接收到新的请求,ReadMessage()函数应该会因为SSLStream的读取超时而捕获SSLStream。当第二次执行sslStream.Read()时出现问题,它似乎读取了客户端未发送的5个字节。因此,问题发生在以下顺序:

-> ReadMessage() -> sslstream.Read() ->超时异常按预期捕获

-> ReadMessage() -> sslstream.Read() ->超时异常未捕获,即使客户机没有发送任何内容,仍读取5个字节

-> ReadMessage() -> sslstream.Read() ->超时异常按预期捕获

-> ReadMessage() -> sslstream.Read() ->超时异常未捕获,5字节读取,即使客户端没有发送任何内容.

以此类推。

代码语言:javascript
复制
    public void ClientHandle(object obj)
    {
        nRetry = MAX_RETRIES;

        // Open connection with the client
        if (Open() == OPEN_SUCCESS)
        {
            String request = ReadMessage();
            String response = null;

            // while loop for the incoming commands from client
            while (!String.IsNullOrEmpty(request))
            {
                Console.WriteLine("[{0}] {1}", RemoteIPAddress, request);

                response = Execute(request);

                // If QUIT was received, close the connection with the client
                if (response.Equals(QUIT_RESPONSE))
                {
                    // Closing connection
                    Console.WriteLine("[{0}] {1}", RemoteIPAddress, response);

                    // Send QUIT_RESPONSE then return and close this thread
                    SendMessage(response);
                    break;
                }

                // If another command was received, send the response to the client
                if (!response.StartsWith("TIMEOUT"))
                {
                    // Reset nRetry
                    nRetry = MAX_RETRIES;

                    if (!SendMessage(response))
                    {
                        // Couldn't send message
                        Close();
                        break;
                    }
                }


                // Wait for new input request from client
                request = ReadMessage();

                // If nothing was received, SslStream timeout occurred
                if (String.IsNullOrEmpty(request))
                {
                    request = "TIMEOUT";
                    nRetry--;

                    if (nRetry == 0)
                    {
                        // Close everything
                        Console.WriteLine("Client is unreachable. Closing client connection.");
                        Close();
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            Console.WriteLine("Stopped");
        }
    }



    public String ReadMessage()
    {
        if (tcpClient != null)
        {
            int bytes = -1;
            byte[] buffer = new byte[MESSAGE_SIZE];

            try
            {
                bytes = sslStream.Read(buffer, 0, MESSAGE_SIZE);
            }
            catch (ObjectDisposedException)
            {
                // Streams were disposed
                return String.Empty;
            }
            catch (IOException)
            {
                return String.Empty;
            }
            catch (Exception)
            {
                // Some other exception occured
                return String.Empty;
            }

            if (bytes != MESSAGE_SIZE)
            {
                return String.Empty;
            }

            // Return string read from the stream
            return Encoding.Unicode.GetString(buffer, 0, MESSAGE_SIZE).Replace("\0", String.Empty);
        }

        return String.Empty;
    }


    public bool SendMessage(String message)
    {
        if (tcpClient != null)
        {
            byte[] data = CreateMessage(message);

            try
            {
                // Write command message to the stream and send it
                sslStream.Write(data, 0, MESSAGE_SIZE);
                sslStream.Flush();
            }
            catch (ObjectDisposedException)
            {
                // Streamers were disposed
                return false;
            }
            catch (IOException)
            {
                // Error while trying to access streams or connection timedout
                return false;
            }
            catch (Exception)
            {
                return false;
            }

            // Data sent successfully
            return true;
        }

        return false;
    }

   private byte[] CreateMessage(String message)
    {
        byte[] data = new byte[MESSAGE_SIZE];

        byte[] messageBytes = Encoding.Unicode.GetBytes(message);

        // Can't exceed MESSAGE_SIZE parameter (max message size in bytes)
        if (messageBytes.Length >= MESSAGE_SIZE)
        {
            throw new ArgumentOutOfRangeException("message", String.Format("Message string can't be longer than {0} bytes", MESSAGE_SIZE));
        }

        for (int i = 0; i < messageBytes.Length; i++)
        {
            data[i] = messageBytes[i];
        }
        for (int i = messageBytes.Length; i < MESSAGE_SIZE; i++)
        {
            data[i] = messageBytes[messageBytes.Length - 1];
        }

        return data;
    }

客户机还使用相同的ReadMessage()、SendMessage()和CreateMessage()函数向服务器发送消息。MESSAGE_SIZE常量也是相同的,它被设置为2048年。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-17 21:04:30

问题是,在超时之后,我再次使用了SSLStream。因此,我通过删除nRetry变量并设置一个更长的超时来解决这个问题。相关的MSDN文章指出,SSLStream将在超时异常(https://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx )之后返回垃圾。

SslStream假设调用方将在从内部流抛出超时时与任何其他IOException一起视为致命的超时。超时后重用SslStream实例将返回垃圾。在这种情况下,应用程序应该关闭SslStream并抛出异常。

另一个问题是Windows KB3147458 (Windows104月的更新)改变了读取函数的行为。它看起来像是改变了SSLStream实现中的一些东西,现在它以2部分的形式返回数据,1字节,每一次返回剩下的字节。实际上,MSDN文档并没有说Read()函数将在一步内返回所有请求的字节,所提供的示例使用do-while循环来读取确切的字节数。因此,我假设Read()函数不能保证一次读取所请求的确切字节数,可能需要更多的读迭代。

SSLstream工作正常,因此不会损坏。您只需注意并使用do-while循环,并检查所有字节是否正确读取。

我更改了代码,如下所示,以解决我所拥有的bug。

代码语言:javascript
复制
    public String ReadMessage()
    {
        if (tcpClient != null)
        {
            int bytes = -1, offset = 0;
            byte[] buffer = new byte[MESSAGE_SIZE];

            try
            {
                // perform multiple read iterations 
                // and check the number of bytes received
                while (offset < MESSAGE_SIZE)
                {
                    bytes = sslStream.Read(buffer, offset, MESSAGE_SIZE - offset);
                    offset += bytes;

                    if (bytes == 0)
                    {
                        return String.Empty;
                    }
                }
            }
            catch (Exception)
            {
                // Some exception occured
                return String.Empty;
            }

            if (offset != MESSAGE_SIZE)
            {
                return String.Empty;
            }

            // Return string read from the stream
            return Encoding.Unicode.GetString(buffer, 0, MESSAGE_SIZE).Replace("\0", String.Empty);
        }

        return String.Empty;
    }
票数 2
EN

Stack Overflow用户

发布于 2018-01-12 17:53:36

关于超时后在Read()上返回五个字节的SslStream,这是因为SslStream类没有很好地处理底层流中的任何IOException,正如前面提到的那样。

SslStream假设调用方将在从内部流抛出超时时与任何其他IOException一起视为致命的超时。超时后重用SslStream实例将返回垃圾。在这种情况下,应用程序应该关闭SslStream并抛出异常。

https://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx

但是,您可以通过创建一个位于Tcp NetworkStream和SslStream之间的包装类来解决这个问题,该类捕获并抑制无害的超时异常,并且(似乎)不会丢失功能。

它的完整代码在我在类似线程上的回答中,这里是https://stackoverflow.com/a/48231248/8915494

对于Read()方法,在每个Read()上只返回部分有效负载,您的答案已经正确地修复了这个问题。虽然这是SslStream的“最近”行为,但不幸的是,所有网络和所有代码都需要创建某种形式的缓冲区来存储片段,直到您拥有完整的数据包为止。例如,如果您的数据超过1500个字节(假设以太网传输,大多数以太网适配器的最大数据包大小),您很可能收到多个部分的数据,并且必须自己重新组装它。

希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36676593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档