TCP Port Scan with Nmap
How the Nmap Scanner works
Nmap is a very effective port scanner, known as the de-facto tool for finding open ports and services. Nmap performs several phases in order to achieve its purpose:
Nmap host discovery
The first phase of a port scan is host discovery. Here the scanner attempts to check if the target host is live before actually probing for open ports. This phase is needed mainly when scanning a large range of IP addresses in order to optimize the time for the whole scan. It does not make any sense to waste time probing for open ports on a 'dead' host (e.g. there is no server at a given IP).
However, this phase can sometimes lead to not finding some open ports because the host 'liveness' cannot be always correctly detected (e.g. because of firewalls which permit access only to a certain port and drop everything else). In this case you have the option "Don't ping host" (or Nmap -Pn) which skips the host discovery phase and just does the port scanning.
Open ports detection
In order to determine if a TCP port is open, Nmap takes advantage of the Three way handshake mechanism used by TCP to establish a connection between a client and a server.
There are two main methods for detecting open TCP ports:
Connect-Scan (Nmap -sT)
In this case Nmap does a full three-way handshake with the target server, establishing a full TCP connection. The sequence of packets for this type of scan is:
The advantage of this method is that it does not require root/administrator access on the client machine, while the disadvantage is that it is rather noisy and the server can log the connections attempted from other hosts.
SYN-Scan (Nmap -sS)
This is the default scanning method, also enabled in our scanner. In this method, Nmap does a half-open TCP connection, knowing that the port is open immediately after the server responds with SYN-ACK. The sequence of packets in this case is:
The advantage of this method is that it is stealthier than a Connect-Scan but it does require Nmap to run with root/administrator privileges (because it needs to create low-level raw sockets to send the individual packets, instead of leaving the kernel stack to do the connection).
Nmap service detection
After Nmap has found a list of ports, it can do a more in-depth check to determine the exact type of service that is running on that port, including its version. This is needed because it is possible for common services to run on non-standard ports (e.g. a web server running on port 32566). Service detection is enabled with the command
Nmap does service detection by sending a number of predefined probes for various protocols to the target port in order to see if it responds accordingly. For example, it sends:
- SSL CLIENT HELLO - to check for SSL services;
- HTTP GET request - to check for HTTP service;
- SIP OPTIONS - to check for SIP/RTSP protocol;
- and many others.
You can find more details about Nmap and its internal functionality in our blog post Inside Nmap, the world’s most famous port scanner.
A brief history of the Nmap Port Scanner
Nmap was first introduced in September 1997 in the article The Art of Scanning, in the well known Phrack Magazine, by its author - Fyodor (Gordon Lyon).
It has greatly evolved over time (the current version is 7.92) from a simple port scanner to a robust tool containing advanced fingerprinting capabilities and a complex scripting engine. Nmap currently has more than 500 scripts that can run after service detection has been disabled, covering aspects as advanced service discovery, brute-forcing and some vulnerability identification.