~ / archivo / stackoverflow / 41622536
Bizarre netcat behavior, random socket?
-
Stuart pregunta 2017-01-12 20:38
Running Debian Jessie Linux. In learning nmap, I was using nc (netcat) in listen mode so that nmap would find an open port. All done on a single host.
I got the nc syntax wrong, so the command was 'nc -l 3306' where it should have been 'nc -l -p 3306'. nmap then did not report 3306 open but it DID show SOME port open, say 40000.
So I did 'lsof | grep nc' and indeed nc had 40000, or some similar high port open, bizarre.
I then straced nc. With the bad syntax of 'nc -l 3306', the relevant system call sequence is socket,listen,accept. Note no bind call. The correct 'nc -l -p 3306' command produces the expected socket,bind,listen,accept.
So, my point is, you can open a socket for accepting without specifying a port! Obviously, you don't know WHICH port you will get, but you DO get one. Now, if user space (i.e. nc) isn't supplying ANY bind info, that implies to me that kernel space is picking the port ?? I first expected the port to come from undefined stack memory in the nc code, but if no bind call is done, that memory's values would not be transferred to the kernel at all.
Bizarre!
- HappyCactus 2017-01-12 20:48 The "strange" part (though it may be a by-design choice) is not the unbound port (linux selects a free port), but the unbound address. I suspect that when undefined, the linux stack acts like if ADDR_ANY has been bound.
- Barmar 2017-01-12 21:25 It's surpring that
listen()doesn't report an error when the socket isn't bound. But it sounds like it just defaults to what it would do if you'd calledbind()withport = 0, which finds an unused ephemeral port.
-
Not weird at all, that's how netcat (nc) works (in certain versions will change its options) please reefer to this document.
netcat -l -p port [-options] [hostname] [port]If you want to specify an specific port you must use "-p".
executing this:
netcat -l 3306is just the same as executing:
netcat -lwhich selects a random socket to listen.
Now, you are right, there's no bind call, but also other system calls are not present:
Calling with bad syntax
mortiz@florida:~/Documents/projects$ strace -c netcat -l 4000 % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 15.09 0.000040 8 5 read 10.94 0.000029 7 4 openat 10.19 0.000027 4 6 rt_sigaction 9.81 0.000026 26 1 stat 9.81 0.000026 8 3 brk 7.55 0.000020 5 4 close 7.55 0.000020 20 1 socket 6.04 0.000016 4 4 fstat 6.04 0.000016 16 1 alarm 5.28 0.000014 7 2 getpid 5.28 0.000014 7 2 setsockopt 4.53 0.000012 12 1 listen 1.89 0.000005 5 1 rt_sigprocmask 0.00 0.000000 0 7 mmap 0.00 0.000000 0 4 mprotect 0.00 0.000000 0 1 munmap 0.00 0.000000 0 1 1 access 0.00 0.000000 0 1 execve 0.00 0.000000 0 1 arch_prctl ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000265 50 1 totalCalling with the right syntax
mortiz@florida:~/Documents/projects$ strace -c netcat -l -p 4000 % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 33.81 0.000334 8 41 32 openat 21.76 0.000215 6 33 28 stat 8.10 0.000080 5 14 mmap 5.97 0.000059 5 11 close 5.77 0.000057 4 14 read 4.76 0.000047 15 3 munmap 4.05 0.000040 20 2 2 connect 3.95 0.000039 4 9 fstat 3.34 0.000033 5 6 rt_sigaction 2.83 0.000028 9 3 socket 2.73 0.000027 4 6 mprotect 1.21 0.000012 2 6 lseek 0.61 0.000006 3 2 getpid 0.51 0.000005 5 1 listen 0.30 0.000003 3 1 rt_sigprocmask 0.30 0.000003 3 1 alarm 0.00 0.000000 0 3 brk 0.00 0.000000 0 1 1 access 0.00 0.000000 0 1 bind 0.00 0.000000 0 2 setsockopt 0.00 0.000000 0 1 execve 0.00 0.000000 0 1 arch_prctl ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000988 162 63 totalCalling with right syntax (without specific port)
mortiz@florida:~/Documents/projects$ strace -c netcat -l % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 17.62 0.000040 6 6 rt_sigaction 17.18 0.000039 7 5 read 13.66 0.000031 7 4 openat 12.33 0.000028 28 1 listen 9.69 0.000022 22 1 socket 7.93 0.000018 4 4 close 7.49 0.000017 8 2 setsockopt 3.96 0.000009 2 4 fstat 3.52 0.000008 8 1 rt_sigprocmask 3.52 0.000008 8 1 alarm 3.08 0.000007 3 2 getpid 0.00 0.000000 0 1 stat 0.00 0.000000 0 7 mmap 0.00 0.000000 0 4 mprotect 0.00 0.000000 0 1 munmap 0.00 0.000000 0 3 brk 0.00 0.000000 0 1 1 access 0.00 0.000000 0 1 execve 0.00 0.000000 0 1 arch_prctl ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000227 50 1 totalSo we can see changing parameters obviously will change system calls (that has sense).
The fact that the port being assigned doesn't use
bind()could be related to the different netcat implementations as @UrielKatz mentioned in my comment.- Uriel Katz 2020-02-11 16:22 This is not correct, netcat has various versions, for example in ubuntu 18.04 you will be able to do nc -l 5000 and have it listen on port 5000 But inside a docker running ubuntu 16.04 it will listen to a random port indeed
- Miguel Ortiz yo 2020-02-12 13:31 Thanks @UrielKatz that has sense, I've updated the answer, I've checked other netcat | nc versions and yes, options are different as their system calls.