Nmap offers a lot of scanning options to specify the scanning technique. But if none were specified, nmap chooses a default scan type which depends solely on the privileges the user have. Here's a quick explanation:
Figure1
Discovering OPEN TCP Ports
Discovering OPEN UDP Ports (Much slower than TCP)
We do not receive any acknowledgment. Consequently, the timeout is much longer, making the whole UDP scan (-sU) much slower than the TCP scan (-sS).
Also We often do not get a response back because Nmap sends empty datagrams to the scanned UDP ports, and we do not receive any response. So we cannot determine if the UDP packet has arrived at all or not. If the UDP port is open, we only get a response if the application is configured to do so.
UDP scans possible outputs.
Service detection:
Primarily nmap prints out the banners of the scanned ports. If nmap cannot identify versions through banner grabbing, it performs a signature-based matching system which increases the duration.
Nmap can miss some information because sometimes it doesn't know how to handle some information.
It happens because, after a successful three-way handshake, the server often sends a banner for identification. This serves to let the client know which service it is working with. At the network level, this happens with a PSH flag in the TCP header. This mechanism is described in the first figure.
Banner grabbing for service detection:
Let's prepare the intercept network traffic using tcpdump
Now let's netcat to the target machine an look at the intercepted traffic in tcpdump output:
SYN sent.
SYN/ACK received.
ACK sent.
PSH/ACK received. (Exactly where the confusion happens.)
└──╼ $sudo nmap 10.10.10.10 -p- -sV
Starting Nmap 7.80 ( https://nmap.org ) at 2020-06-15 20:00 CEST
Nmap scan report for 10.129.2.28
Host is up (0.013s latency).
Not shown: 65525 closed ports
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
25/tcp open smtp Postfix smtpd
80/tcp open http Apache httpd 2.4.29 ((Ubuntu))
110/tcp open pop3 Dovecot pop3d
139/tcp filtered netbios-ssn
143/tcp open imap Dovecot imapd (Ubuntu)
445/tcp filtered microsoft-ds
993/tcp open ssl/imap Dovecot imapd (Ubuntu)
995/tcp open ssl/pop3 Dovecot pop3d
MAC Address: DE:AD:00:00:BE:EF (Intel Corporate)
Service Info: Host: inlane; OS: Linux; CPE: cpe:/o:linux:linux_kernel
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 91.73 seconds
└──╼ $ sudo nmap 10.10.10.10 -p- -sV -Pn -n --disable-arp-ping --packet-trace
Starting Nmap 7.80 ( https://nmap.org ) at 2020-06-16 20:10 CEST
<SNIP>
NSOCK INFO [0.4200s] nsock_trace_handler_callback() .. SNIP .. ESMTP
Postfix (Ubuntu)..
It detected that it is Ubuntu
Service scan match (Probe NULL matched with NULL line 3104): 10.129.2.28:25 is smtp. Version: |Postfix smtpd|||
NSOCK INFO [0.4200s] nsock_iod_delete(): nsock_iod_delete (IOD #1)
Nmap scan report for 10.129.2.28
Host is up (0.076s latency).
PORT STATE SERVICE VERSION
25/tcp open smtp Postfix smtpd
MAC Address: DE:AD:00:00:BE:EF (Intel Corporate)
Service Info: Host: inlane
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 0.47 seconds
└──╼ $ sudo tcpdump -i eth0 host 10.10.10.10 and 1.1.1.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
└──╼ $ nc -nv 10.10.10.10 25
Connection to 10.10.10.10 port 25 [tcp/*] succeeded!
220 inlane ESMTP Postfix (Ubuntu)