使用BufferedOutputStream / BufferedInputStream的套接字随机接收伪造数据

时间:2013-06-19 07:33:43

标签: java sockets tcp stream

我有一个使用BufferedOutputStream / BufferedInputStream发送/接收数据的客户端/服务器应用程序。沟通协议如下:

  1. 发送部分:

    • 第一个字节是要执行的操作
    • 接下来的4个字节是消息的长度
    • next x bytes(x =消息长度)是消息本身
  2. 收到部分:

    • 读取第一个字节以获取操作
    • 读取接下来的4个字节以获取消息长度
    • 读取x(在prev步骤中获得)字节以获取消息
  3. 现在问题是有时当我在服务器部分发送消息长度(例如:23045)时,我收到它时会得到一个巨大的int(例如:123106847)。

    一个重要的线索是,当消息超过多个字符(在我的情况下> 10K)时,这种情况发生,如果我发送了一条较小的消息(例如4-5k),一切都按预期工作

    客户端发送部分(outputStream / inputStream是BufferedXXXStream类型):

        private String getResponseFromServer( NormalizerActionEnum action, String message) throws IOException{
    
            writeByte( action.id());
            writeString( message);
            flush(;
    
            return read();
        }
    
        private String read() throws IOException{
            byte[] msgLen = new byte[4];
            inputStream.read(msgLen);
            int len = ByteBuffer.wrap(msgLen).getInt();
            byte[] bytes = new byte[len];
            inputStream.read(bytes);
    
            return new String(bytes);
        }
    
        private void writeByte( byte msg) throws IOException{
            outputStream.write(msg);
        }
    
        private void writeString( String msg) throws IOException{
    
            byte[] msgLen = ByteBuffer.allocate(4).putInt(msg.length()).array();
    
            outputStream.write(msgLen);
            outputStream.write(msg.getBytes());
        }
    
        private void flush() throws IOException{
            outputStream.flush();
        }
    

    服务器部分(_input / _output是BufferedXXXStream类型)

    private byte readByte() throws IOException, InterruptedException {
        int b =  _input.read();
        while(b==-1){
            Thread.sleep(1);
            b = _input.read();
        }
    
        return (byte) b;
    }
    
    private String readString() throws IOException, InterruptedException {
        byte[] msgLen = new byte[4];
        int s = _input.read(msgLen);
        while(s==-1){
            Thread.sleep(1);
            s = _input.read(msgLen);
        }   
    
        int len = ByteBuffer.wrap(msgLen).getInt();     
        byte[] bytes = new byte[len];
        s = _input.read(bytes);
        while(s==-1){
            Thread.sleep(1);
            s = _input.read(bytes);
        }
    
        return new String(bytes);
    }
    
    private void writeString(String message) throws IOException {
        byte[] msgLen = ByteBuffer.allocate(4).putInt(message.length()).array();
        _output.write(msgLen);
        _output.write(message.getBytes());
        _output.flush();
    }
    
    ....
    
    byte cmd = readByte();
    String message = readString();
    

    任何帮助将不胜感激。如果您需要其他详细信息,请通知我。

    更新:由于来自 Jon Skeet EJP 的评论,我实现了服务器上的读取部分有一些毫无意义的操作,但让我放在一边我终于得到了问题所在:关键是我保持流量为应用程序的全长打开,前几次发送消息我可以在服务器端读取它但是 Jon Skeet 指出数据不会一次全部到达所以当我尝试读取消息长度时我实际上是在阅读消息本身,这就是我伪造消息长度的原因。

    〜而不是发送数据长度,然后立即读取它我发送它没有长度,我一次读取一个字节,直到字符串完成正常工作

    private String readString() throws IOException, InterruptedException {
        StringBuilder sb = new StringBuilder();
        byte[] bytes = new byte[100];
        int s = 0;
        int index=0;
        while(true){
            s = _input.read();
            if(s == 10){
                break;
            }
            bytes[index++] = (byte) (s);
            if(index == bytes.length){
                sb.append(new String(bytes));
                bytes = new byte[100];
                index=0;
            }           
        }
        if(index > 0){
            sb.append(new String(Arrays.copyOfRange(bytes, 0, index)));
        }
    
        return sb.toString();
    }
    

2 个答案:

答案 0 :(得分:4)

看看这个:

byte[] bytes = new byte[len];
s = _input.read(bytes);
while(s==-1){
    Thread.sleep(1);
    s = _input.read(bytes);
}

return new String(bytes);

首先,循环是没有意义的:唯一的时间read将返回-1,如果它已关闭,在这种情况下循环不会对你有帮助。

其次,你忽略了数据进入多个块的可能性。您假设如果您已经设法获取任何数据,那么您已经所有数据。相反,你应该循环这样的东西:

int bytesRead = 0;
while (bytesRead < bytes.length) {
    int chunk = _input.read(bytes, bytesRead, bytes.length - bytesRead);
    if (chunk == -1) {
        throw new IOException("Didn't get as much data as we should have");
    }
    bytesRead += chunk;
}

请注意,您的所有其他 InputStream.read调用也假设您已设法读取数据,而且您确实已经读取所有数据需要。

哦,你正在使用平台默认编码在二进制数据和文本数据之间进行转换 - 这不是一个好主意。

你有没有理由不使用DataInputStreamDataOutputStream?目前你正在重新发明轮子,并且这样做有虫子。

答案 1 :(得分:1)

您发送的代码有问题:

byte[] msgLen = ByteBuffer.allocate(4).putInt(message.length()).array();
_output.write(msgLen);
_output.write(message.getBytes());

您发送字符数作为消息长度,但之后将消息转换为字节。根据平台编码,String.getBytes()可以提供比字符更多的字节。

你应该从不假设String.length()与String.getBytes()有任何的关系。这些是不同的概念,不应该混在一起。