简体   繁体   English

Java:安全套接字接受

[英]Java: Secure socket accepting

I got this multi-threaded server application that someone else wrote. 我得到了别人编写的这个多线程服务器应用程序。 When it is going to accept a Socket-object with it's ServerSocket-object, it's running trough a method called "acceptSocketSafe". 当它要与它的ServerSocket对象一起接受一个Socket对象时,它正在通过一种名为“ acceptSocketSafe”的方法运行。

Here is a snippet of the program where I have included the parts of code needed: 这是该程序的一个片段,其中包括了所需的代码部分:

public Socket acceptSocketSafe(ServerSocket x)  {
    boolean socketFound = false;
    Socket socket = null;

    do  {
        try {
            socket = x.accept();
            int i = socket.getInputStream().read();

            if ((i & 0xFF) == 14)   {
                socketFound = true;
            }
        } catch (Exception e)   {

        }
    } while (!socketFound);

    return socket;
}

What I don't understand is how the method "acceptSocketSafe" makes the socket acception safer than how I would have done it (the simple, regular way). 我不了解的是,“ acceptSocketSafe”方法如何使套接字接受比我本来会做的更安全(简单,常规的方式)。 (I believe it has something with excluding connections with bad intentions (DDoS, for example)). (我相信它可以排除出于恶意的连接(例如DDoS))。

Thank you for any explanation of this method! 感谢您对此方法的任何解释!

It doesn't make it safer at all. 它根本没有使其更安全。 It makes it worse. 这使情况变得更糟。

This code does client I/O on the accepting thread. 该代码在接受线程上执行客户端I / O。 That means that all a malevolent client has to do to mount a DOS attack is to connect and send nothing. 这意味着恶意客户端进行DOS攻击所需要做的就是连接并不发送任何内容。 Then no other client can be accepted until that client either sends something or closes the connection. 然后,其他任何客户端都无法接受,直到该客户端发送内容或关闭连接。

As for what it does, it just rejects client connections that don't start with a 14 byte. 至于它的作用,它只是拒绝不以14字节开头的客户端连接。 It's a pretty weak test: 1 in 256 random attempts will pass. 这是一个非常弱的测试:256次随机尝试中有1次会通过。 It would be better accomplished by proper error checking in the application protocol. 通过在应用程序协议中进行适当的错误检查,可以更好地实现。 You still have to do that anyway so there is no actual advantage at all. 无论如何,您仍然必须这样做,因此根本没有实际优势。

This code also leaks rejected sockets. 此代码还会泄漏拒绝的套接字。

Throw it away. 把它扔掉。

This is security by obscurity. 这是默默无闻的安全。 The socket is accepted anyway, only that it checks that the client sends 0x0E (14) as the first byte. 无论如何,套接字都被接受,只是它会检查客户端是否将0x0E(14)作为第一个字节发送。 If it doesn't, it throws (without closing the accepted socket btw.). 如果不是,它将抛出(不关闭接受的套接字顺便说一句)。

This could still DDoS'ed by just not sending anything after connecting... 这仍然可以通过在连接后不发送任何内容来进行DDoS ...

Edit: Looking at it closer, it doesn't even need to be a distributed attack. 编辑:更仔细地看,它甚至不需要是分布式攻击。 A single client just not sending any byte will block the accept loop entirely, mission accomplished. 一个不发送任何字节的客户端将完全阻塞接受循环,完成任务。 Whoever wrote it didn't know what he was doing. 谁写的都不知道他在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM