Search code examples
javasocketsnetworkingnmap

Is there a way I can detect a port "version" with Java like in Zenmap?


I am making a small project with Java sockets, it's like a port scanner and I am wondering if I can detect a port "version" like with Zenmap. In case you're not familiar with what I'm talking about, if you scan a target with zenmap then go to "Ports / Hosts" and you get something like this.

I was wondering if I could get the port "version" information in Java.


Solution

  • When you get a connection, try reading the first line the server sends. Many applications identify themselves when the connection is established. This is especially true for ftp servers like your example.

    For example, this connects to a port on a server and reads up to 1000 bytes from what the server sent, which should be enough:

    Socket s = new Socket(hostname, port)
    InputStream in = s.getInputStream();
    byte[] bs = new byte[1000];
    int len = in.read(bs);
    s.close();
    

    You can then convert those bytes into a string:

    String serverInfo = new String(bs, 0, len);
    // I got "SSH-2.0-OpenSSH_5.3\r\n" in a test
    

    Not all protocols start with the server sending something so you should also set a timeout for reading from the socket.