62

So I can use this netcat command to check if a UDP port is open:

$  nc -vz -u 10.1.0.100 53
Connection to 10.1.0.100 53 port [udp/domain] succeeded!

Unlike TCP, UDP is connectionless (fire and forget). So at a high level does anyone know how netcat knows the UDP port is open? Does it ask for a reply or something like that?

4 Answers 4

29

In fact, it doesn't. You can check by doing:

$ nc -vz -u 8.8.8.8 53 
Connection to 8.8.8.8 53 port [udp/domain] succeeded!
$ nc -vz -u 8.8.8.8 54
Connection to 8.8.8.8 54 port [udp/*] succeeded!
$ nc -vz -u 8.8.8.8 59
Connection to 8.8.8.8 59 port [udp/*] succeeded!
$ 

So with UDP, it's not something you can really check unless it will give you information back.

18

Judging by the specific output Connection to Connection to 10.1.0.100 53 port [udp/domain] succeeded! you are using openbsd-netcat.

Looking at the code for that the test is to bind to the UDP socket, i.e. there is an open connection:

              if (vflag || zflag) {
                            /* For UDP, make sure we are connected. */
                            if (uflag) {
                                    if (udptest(s) == -1) {
                                            ret = 1;
                                            continue;
                                    }
                            }

                            /* Don't look up port if -n. */
                            if (nflag)
                                    sv = NULL;
                            else {
                                    sv = getservbyport(
                                        ntohs(atoi(portlist[i])),
                                        uflag ? "udp" : "tcp");
                            }

                            fprintf(stderr,
                                "Connection to %s %s port [%s/%s] "
                                "succeeded!\n", host, portlist[i],
                                uflag ? "udp" : "tcp",
                                sv ? sv->s_name : "*");

udptest issues around 3 writes to the open socket. There is a note that this doesn't work for IPv6 and fails after around 100 ports checked.

So while the other suggestion may be valid, I don't think that's happening in this particular case.

3
  • so udptest is the function I am looking for and it answers my question. From the link you provided "* udptest() * Do a few writes to see if the UDP port is there" Oct 13, 2015 at 15:11
  • Yes, I just checked that and saw the writes as well. Revised answer.
    – rocky
    Oct 13, 2015 at 15:13
  • 2
    @PatrickMcMahon - if this answers your question, then accept it by clicking on the large tick.
    – cas
    Oct 14, 2015 at 0:29
14

Well I have different opinion:

a:~# nc -luk 10.12.0.12 667 // listen on UDP port 667
b:~# nc -uv 10.12.0.12 667  // check if port is open 
nc: 10.12.0.12 (10.12.0.12) 667 [667] open
I love stackexchange // send a message
a:~# nc -luk 10.12.0.12 667
I love stackexchange // receive the message.

So based on that, you can check if the connection between a and b on that udp port is possible. Later on you can continue checking using tcpdump.

3
  • 1
    Ah yes I like this. Its a good way to test if you have access to both nodes. I guess when I was asking the question I was steering towards "how to test a black box" or probing an external node. The results of doing just that, netcat cannot guarantee. Jun 14, 2017 at 15:58
  • Could you please expand on the IP address 10.12.0.12 ? Why is it used on both computers? Jun 18, 2018 at 17:22
  • -k option is not necessary when you are dealing with UDP
    – YON
    Nov 13, 2019 at 6:27
9

There is an ICMP message to signalize that a port, even an UDP one, is closed. So if a host sends this message then the port can be assumed to be closed.

https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#Destination_unreachable

2
  • 4
    Note that because UDP is connectionless, you cannot reliably distinguish an open port from a firewalled port from a lost packet.
    – Mark
    Oct 13, 2015 at 23:06
  • Very true. And theoretically it would also be possible that you get the packet and the port is not really closed.
    – phk
    Oct 13, 2015 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.