Search code examples
javaudpport-scanning

UDP port scanning Java finds only 1 open UDP port


I have an assigment about port scanning. I am scanning UDP ports of some IP addresses in Java.In my program (assuming everything is OK) I can only find one open UDP port. In the other hands port scanning over "nmap" I get 4 open UDP ports. Can somebody tell me why I can not find more than one ports via Java code? By the way I can find the true open port in my code.

int startPortRange=1;
    int stopPortRange=1024;
    InetAddress address = InetAddress.getByName("bigblackbox.cs.binghamton.edu");
    int counter=0;
    for(int i=startPortRange; i <=stopPortRange; i++)
    {
        counter++;      
       try{


            byte [] bytes = new byte[128];
            DatagramSocket ds = new DatagramSocket();
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
            ds.setSoTimeout(100);
            ds.connect(address, i);
            ds.send(dp);
            ds.isConnected();
            dp = new DatagramPacket(bytes, bytes.length);
            ds.receive(dp);
            ds.close();
            System.out.println("open");
            System.out.println(counter);
        }
        catch(InterruptedIOException e){
            //System.out.println("closed");
        }
        catch(IOException e){
            //System.out.println("closed");
        }       
    }

Output of above code is 135 open

When I make same operation in command line using nmap I get more open ports. I could not upload an image because I am a new user. Thank you enter image description here


Solution

  • It is impossible to provide a concrete answer, unless you provide at least:

    • The source code of your program.

    • An example of the (incorrect) output that you are getting.

    • The expected output for the same scenario.

    Without this information there is no way for us to tell you what is wrong. For all we know, it could even be a simple case of your program terminating prematurely after finding an open port. Or a case of the open port that was last found overwriting the entries of the previous ones before being displayed.

    In any case, it might be worthwhile to investigate what is being sent and received using a network sniffer, such as Wireshark. By comparing an nmap session with a session created by your program, you might be able to spot some significant difference that would help pinpoint the issue.

    EDIT:

    After having a look at your code and comparing with nmap, it seems that you are mistakenly handling the case of a SocketTimeoutException as a closed port, while it could simply be the port of a server that refuses to answer to the packet that you sent.

    EDIT 2:

    Here's the full story:

    When a port is properly closed, the server sends back an ICMP Destination Unreachable packet with the Port unreachable error code. Java interprets this error to an IOException that you correctly consider to indicate a closed port.

    An open port, on the other hand may result into two different responses from the server:

    • The server sends back a UDP packet, which is received by your program and definitely indicates an open port. DNS servers, for example, often respond with a Format error response. nmap shows these ports are open.

    • The server ignores your probe packet because it is malformed w.r.t. to the provided service. This results in a network timeout and a SocketTimeoutException in your program.

    Unfortunately there is no way to tell whether a network timeout is because an active server ignored a malformed probe packet or because a packet filter cut down the probe. This is why nmap displays ports that time out as open|filtered.