4

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

Raedwald
  • 46,613
  • 43
  • 151
  • 237
snvngrc
  • 177
  • 2
  • 4
  • 12

1 Answers1

4

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.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • Sorry about that. I am new in here. – snvngrc Mar 02 '12 at 01:18
  • I changed packet size from 128 to 1024 but it didn't work. I changed timeout time in the case of a late answer but no difference again. – snvngrc Mar 02 '12 at 01:37
  • @user1243987: Timeouts in UDP are a bit of a gray area. You cannot definitely say that a port is closed if the connection times out. See my edit for more information... – thkala Mar 02 '12 at 01:49
  • I added an image that I used nmap. There are two open ports but I can't find none of them. I only find 135. – snvngrc Mar 02 '12 at 01:53
  • @user1243987: I assume that `nmap` is run on the same computer as your Java program, right? – thkala Mar 02 '12 at 01:57
  • After modifying your program to report `SocketTimeoutException` as `open|filtered`, the results seem to match `nmap` in my local network. Do you have a host that I could try to scan with both, whose owners would not mind a portscan? Apparently there is a firewall between me and the host you are using... – thkala Mar 02 '12 at 02:03
  • Can you send me the modified code? I don't know any host other than that. Sorry. – snvngrc Mar 02 '12 at 02:07
  • Just insert one more `catch` block for that exception before all other `catch` blocks and print the port as `open|filtered`... – thkala Mar 02 '12 at 02:10
  • I get 1023 open/filtered 1 open ports now. – snvngrc Mar 02 '12 at 02:14
  • I think your timeout is a bit on the low side... try something in the 3-5 second range. – thkala Mar 02 '12 at 02:17
  • I changed timeout from 100 to 5000 but no difference. – snvngrc Mar 02 '12 at 02:21
  • Hmm, it seems `nmap` makes more than one attempts with various packet sizes and other variations. You will be able to see that if you try a packet sniffer. That's why your program does not catch everything... – thkala Mar 02 '12 at 02:27
  • Ok but I didn't understand that how it works same as nmap in your computer? – snvngrc Mar 02 '12 at 02:30
  • Not only that, `nmap` seems to be trying *valid* packets for ports that belong to well-known protocols. That would be quite difficult to match... – thkala Mar 02 '12 at 02:30
  • My services are different and the firewalls are less restrictive within my local network. I have been testing with other networks and the results are not the same... – thkala Mar 02 '12 at 02:32
  • ok. then I can assume that my code is working good. I do not need to change it? – snvngrc Mar 02 '12 at 02:34
  • @user1243987: Depends what you need it for :-) – thkala Mar 02 '12 at 02:37
  • I need open TCP and UDP ports to detect OS of a remote host. – snvngrc Mar 02 '12 at 02:41
  • Anyway, thank you for all of your answers. I am appreciated :) – snvngrc Mar 02 '12 at 03:07