Getting started with Nmap - Part 6

In part 5, we learned of the various types of port-specific scanning techniques. In this post, we'll understand OS and version detection along with timing options that Nmap provides.

The process of OS detection is also known as TCP/IP fingerprinting. It is performed using a set of fingerprints that Nmap has collected for a particular OS. This set of fingerprints includes the type of responses that specific OSes have towards certain packets.

Disclaimer: In this entire tutorial series, I have used scanme.nmap.org as the target host and sometimes my local machine itself. scanme.nmap.org is a machine set up by Nmap developers for educational purposes, so it is legal to scan it a FEW TIMES A DAY. Scanning networks, in general is a cyber crime and may even lead to jail. Please take permission before scanning random networks.


Operating System Detection


Using this option, Nmap attempts to detect the OS running on the target machine.

nikhilh@ubuntu:~$ sudo nmap -O 127.0.0.1
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 16:52 PDT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000040s latency).
Not shown: 999 closed ports
PORT    STATE SERVICE
631/tcp open  ipp
Device type: general purpose
Running: Linux 3.X|4.X
OS CPE: cpe:/o:linux:linux_kernel:3 cpe:/o:linux:linux_kernel:4
OS details: Linux 3.8 - 4.14
Network Distance: 0 hops

OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 1.58 seconds

For OS detection to work accurately, Nmap requires at least 1 open and 1 closed port. When scanning multiple systems, it is advisable to use --osscan-limit to not OS scan hosts which do not meet this criteria.

nikhilh@ubuntu:~$ sudo nmap -d -O --osscan-limit 127.0.0.1
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 16:57 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Skipping OS Scan against localhost (127.0.0.1) due to absence of open (or perhaps closed) ports
Nmap scan report for localhost (127.0.0.1)
Host is up, received localhost-response (0.0000020s latency).
All 1000 scanned ports on localhost (127.0.0.1) are closed because of 1000 resets
Final times for host: srtt: 2 rttvar: 0  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-os-db nmap-payloads nmap-services.
OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 0.27 seconds
           Raw packets sent: 1000 (44.000KB) | Rcvd: 2000 (84.000KB)

To limit the maximum number of tries for Nmap to determine the OS, use the --max-os-tries <number> option.

Determine the Service Version


Using this option, Nmap attempts to detect the version of the service running on any open ports. By itself, this option will skip ports 9100 - 9107. These ports are usually associated with network printers which respond with garbage values when Nmap probes them for service version detection.

nikhilh@ubuntu:~$ sudo nmap -d -sV 127.0.0.1
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 06:53 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
NSE: Using Lua 5.3.
NSE: Arguments from CLI: 
NSE: Loaded 43 scripts for scanning.
mass_rdns: Using DNS server 127.0.1.1
Initiating SYN Stealth Scan at 06:53
...
...
Initiating Service scan at 06:53
Scanning 1 service on localhost (127.0.0.1)
Completed Service scan at 06:53, 6.02s elapsed (1 service on 1 host)
NSE: Script scanning 127.0.0.1.
NSE: Starting runlevel 1 (of 2) scan.
Initiating NSE at 06:53
NSE: Starting hnap-info against 127.0.0.1:631.
NSE: Starting vmware-version against 127.0.0.1:631.
NSE: Starting http-server-header against 127.0.0.1:631.
NSE: Starting http-trane-info against 127.0.0.1:631.
NSE: [hnap-info 127.0.0.1:631] HTTP: Host returns proper 404 result.
NSE: [vmware-version 127.0.0.1:631] Couldn't download file: /sdk
NSE: Finished vmware-version against 127.0.0.1:631.
NSE: [http-trane-info 127.0.0.1:631] HTTP: Host returns proper 404 result.
NSE: Finished hnap-info against 127.0.0.1:631.
NSE: Finished http-server-header against 127.0.0.1:631.
NSE: Finished http-trane-info against 127.0.0.1:631.
Completed NSE at 06:53, 0.02s elapsed
NSE: Starting runlevel 2 (of 2) scan.
Initiating NSE at 06:53
Completed NSE at 06:53, 0.00s elapsed
Nmap scan report for localhost (127.0.0.1)
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-service-probes nmap-services.
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 6.74 seconds
           Raw packets sent: 1000 (44.000KB) | Rcvd: 2001 (84.044KB)
  1. To prevent Nmap from skipping 9100-9107 ports, use the --all-ports option. 
  2. To probe the host more intensely for service versions, use the --version-intensity <intensity-number [0-9]>.
    1. Using a lower intensity could possibly lead Nmap to miss detecting versions which it could have with a higher intensity.

Guessing an OS


On the off-chance that Nmap is unable to determine the OS, it has the ability to guess the OS using the information that it has.

(In the below example, --osscan-guess has not been utilized because Nmap couldn't find an unknown OS while scanning the host. The example below is just for demonstration purposes.)

nikhilh@ubuntu:~$ sudo nmap -d -O --osscan-guess scanme.nmap.org
[sudo] password for nikhilh: 
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 06:38 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
Initiating Ping Scan at 06:38
...
...
Overall sending rates: 75.94 packets / s, 3339.26 bytes / s.
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or (tcp and (src host 45.33.32.156)))
Initiating OS detection (try #1) against scanme.nmap.org (45.33.32.156)
OS detection timingRatio() == (1535031531.632 - 1535031531.128) * 1000 / 500 == 1.006
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.060s latency).
...
...
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: WAP|general purpose
Running: Actiontec embedded, Linux 2.4.X|3.X
OS CPE: cpe:/h:actiontec:mi424wr-gen3i cpe:/o:linux:linux_kernel cpe:/o:linux:linux_kernel:2.4.37 cpe:/o:linux:linux_kernel:3.2
OS details: Actiontec MI424WR-GEN3I WAP, DD-WRT v24-sp2 (Linux 2.4.37), Linux 3.2
TCP/IP fingerprint:
OS:SCAN(V=7.70%E=4%D=8/23%OT=22%CT=%CU=%PV=N%G=N%TM=5B7EB8ED%P=x86_64-unkno
OS:wn-linux-gnu)SEQ(SP=106%GCD=1%ISR=104%TS=U)OPS(O1=M5B4%O2=M5B4%O3=M5B4%O
OS:4=M5B4%O5=M5B4%O6=M5B4)WIN(W1=FAF0%W2=FAF0%W3=FAF0%W4=FAF0%W5=FAF0%W6=FA
OS:F0)ECN(R=Y%DF=N%TG=80%W=FAF0%O=M5B4%CC=N%Q=)T1(R=Y%DF=N%TG=80%S=O%A=S+%F
OS:=AS%RD=0%Q=)T2(R=N)T3(R=Y%DF=N%TG=80%W=FAF0%S=O%A=S+%F=AS%O=M5B4%RD=0%Q=
OS:)T4(R=Y%DF=N%TG=80%W=7FFF%S=A%A=Z%F=R%O=%RD=0%Q=)T6(R=Y%DF=N%TG=80%W=7FF
OS:F%S=A%A=Z%F=R%O=%RD=0%Q=)U1(R=N)IE(R=Y%DFI=N%TG=80%CD=S)

TCP Sequence Prediction: Difficulty=262 (Good luck!)
IP ID Sequence Generation: Busy server or unknown class
Final times for host: srtt: 59841 rttvar: 45038  to: 239993

Read from /usr/local/bin/../share/nmap: nmap-os-db nmap-payloads nmap-services.
OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 29.34 seconds
           Raw packets sent: 2054 (92.594KB) | Rcvd: 1042 (42.470KB)

The TCP/IP fingerprint generated in the output can be submitted to Nmap to help improve the accuracy of Nmap's OS detection feature.

Now, we'll start looking at the timing options. With these options you can manipulate the time between probes on a host, time between hosts and more.

Using Timing Templates


The easiest way to manipulate the timing options, albeit as a whole, is to use timing templates. They vary from 0-5 with 0 being the most slow and 5 being the fastest.
  1. T0: Paranoid
  2. T1: Sneaky
  3. T2: Polite
  4. T3: Normal (default timing template)
  5. T4: Aggressive
  6. T5: Insane
The more aggressive the scan, the more noticeable it is on the target system. In general, one would use slow scanning when trying to evade firewalls and fast scanning on speedy networks.

Below, I've used the fastest timing template:

nikhilh@ubuntu:~$ nmap -d -T5 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 13:15 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 250, min 50, max 300
  max-scan-delay: TCP 5, UDP 1000, SCTP 5
  parallelism: min 0, max 0
  max-retries: 2, host-timeout: 900000
  min-rate: 0, max-rate: 0
---------------------------------------------
Initiating Ping Scan at 13:15
Scanning scanme.nmap.org (45.33.32.156) [2 ports]
...
...
Final times for host: srtt: 77087 rttvar: 15669  to: 139763

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 6.70 seconds

When using the default timing template, I expect the time taken to scan to be more than 6.7s

nikhilh@ubuntu:~$ nmap -d -T3 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 13:14 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
Initiating Ping Scan at 13:14
Scanning scanme.nmap.org (45.33.32.156) [2 ports]
Completed Ping Scan at 13:14, 0.08s elapsed (1 total hosts)
...
...
Reason: 996 no-responses
PORT      STATE SERVICE    REASON
22/tcp    open  ssh        syn-ack
80/tcp    open  http       syn-ack
9929/tcp  open  nping-echo syn-ack
31337/tcp open  Elite      syn-ack
Final times for host: srtt: 80227 rttvar: 15726  to: 143131

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 8.80 seconds

Set the Minimum/Maximum Number of Parallel Probes


These options specify the minimum and the maximum number of parallel port scanning that can take place when scanning the target system. In general, these values are set by Nmap based on the network speed and quality.

It is important to note that setting these values too high may lead to inaccurate results.

nikhilh@ubuntu:~$ nmap -d --min-parallelism 5 --max-parallelism 10 127.0.0.1
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 13:19 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 5, max 10
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds

Set the Minimum/Maximum Hosts to Scan in Parallel


These options specify the number of hosts that can be scanned in parallel. Setting them too high may lead to inaccurate results.

nikhilh@ubuntu:~$ nmap -d --min-hostgroup 2 --max-hostgroup 3 scanme.nmap.org 127.0.0.1
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 13:26 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 2, max 3
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 2 IP addresses (2 hosts up) scanned in 21.75 seconds

Set the Minimum/Maximum Packet Rate

Nmap adjusts the packet rate based on network speed and stability. Using these options, the minimum and maximum packet rate can be set. It is important to note that having the minimum packet rate too high may lead to unreliable results.

nikhilh@ubuntu:~$ nmap -d --min-rate 700 --max-rate 1200 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 14:06 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 700, max-rate: 1200
---------------------------------------------
...
...
Final times for host: srtt: 76122 rttvar: 44945  to: 255902

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 3.35 seconds

Set the Minimum/Maximum Scan Delay


Using these options, we can set the time interval between probes. While this can increase scan time, it will also help in avoiding detection.

nikhilh@ubuntu:~$ nmap -d --scan-delay 10ms --max-scan-delay 100ms scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 14:12 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 100, UDP 100, SCTP 100
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 35.99 seconds

Set the Round Trip Timeout (RTT) - Minimum, Initial and Maximum


Nmap waits for a certain time interval for probe responses. If this timeout expires, it re-transmits packets. As per the default timing template, Nmap sets an initial RTT of 1000ms. Nmap dynamically adjusts the RTT based on network performance. The minimum and maximum RTT options set the range for this adjustment.

It is important to note that having a very low RTT may end up slowing scans. In such situations, Nmap will wait for too less time for the packet response and re-transmit packets more often.

nikhilh@ubuntu:~$ nmap -d --initial-rtt-timeout 900ms --max-rtt-timeout 1500ms --min-rtt-timeout 800ms scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 13:46 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 900, min 800, max 1500
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 49.77 seconds

Setting the Maximum Number of Probe Retries


As I said before, Nmap re-transmits packets if it doesn't receive a response the first time. This option set the maximum number of such retries.

nikhilh@ubuntu:~$ nmap -d --max-retries 3 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 13:53 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 3, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Final times for host: srtt: 80692 rttvar: 13467  to: 134560

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 32.14 seconds

Set Host Timeout


When Nmap encounters slow hosts, it waits for a specific interval of time before timing out. Using this option, we can set the time interval that Nmap waits for. Setting the timeout too low will interrupt the scanning process and lead to early termination.

nikhilh@ubuntu:~$ nmap -d --host-timeout 50s scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 14:16 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 50000
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 20.69 seconds


Set the Packet Time-To-Live (TTL)


In situations where the target system is present on a slow network, packets sent to it may time out because their TTL value is too low. Using this option, we can manipulate that value.

nikhilh@ubuntu:~$ nmap -d --ttl 20s scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 14:21 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 30.38 seconds

Although the TTL value is specified in seconds, it is equivalent to hop count which decreases by 1 every hop.

Defeat Reset Rate Limits


We know that rate limiting is applied to Type 3 ICMP Port Unreachable messages on UDP ports. If similar rate limiting is present on RST packets, this option can be used. It is important to note that this option works only with TCP SYN scan that we studied before in Part 4.

nikhilh@ubuntu:~$ sudo nmap -d --defeat-rst-ratelimit -sS scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-23 14:26 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
Initiating Ping Scan at 14:26
...
...
22/tcp    open   ssh        syn-ack ttl 128
80/tcp    open   http       syn-ack ttl 128
9929/tcp  open   nping-echo syn-ack ttl 128
10012/tcp closed unknown    reset ttl 128
31337/tcp open   Elite      syn-ack ttl 128
Final times for host: srtt: 113005 rttvar: 214217  to: 969873

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 24.05 seconds
           Raw packets sent: 2012 (88.456KB) | Rcvd: 1703 (68.124KB)

That's it for part 6! This post has a lot of information that you must remember, especially because these options will be helpful in evading and avoiding detection by IDS and firewalls. If you have any questions, leave them in the comments below and I'll get back to you as soon as I can!

In the next post, we'll take a look at the various firewall and IDS evasion techniques that are built into Nmap.

Getting started with Nmap - Part 5

In part 4, we looked at the various scanning techniques that are available in Nmap. In this post, we'll look at the various port-scan specific options that Nmap provides.

Disclaimer: In this entire tutorial series, I have used scanme.nmap.org as the target host and sometimes my local machine itself. scanme.nmap.org is a machine set up by Nmap developers for educational purposes, so it is legal to scan it a FEW TIMES A DAY. Scanning networks, in general is a cyber crime and may even lead to jail. Please take permission before scanning random networks.

Scan specific ports


So far, we've been looking at options where, by default the top 1000 ports were scanned. There will be situations where you would need to scan a port that is not among those 1000. This option will be helpful in that situation.

nikhilh@ubuntu:~$ nmap -d -p80,443 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 12:25 PDT
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
...
...
PORT    STATE    SERVICE REASON
80/tcp  open     http    syn-ack
443/tcp filtered https   no-response
Final times for host: srtt: 81264 rttvar: 61024  to: 325360

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 1.84 seconds

Scan ports by name


If you have forgotten the port number for a particular service, that is not a problem. Instead of the port number, you can provide the name of the service that runs on that port number.

nikhilh@ubuntu:~$ nmap -d -pimap,smtp scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 13:23 PDT
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received syn-ack (0.082s latency).
Scanned at 2018-08-22 13:23:47 PDT for 2s

PORT    STATE    SERVICE REASON
25/tcp  filtered smtp    no-response
143/tcp filtered imap    no-response
Final times for host: srtt: 81738 rttvar: 81738  to: 408690

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 1.96 seconds

Scan ports by protocol


If you want to scan port x with TCP and port y with UDP, it is possible through this option. It important to use both -sS (or any other TCP scanning technique) and -sU for this to work.

nikhilh@ubuntu:~$ sudo nmap -d -sU -sS -p T:80,U:67 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 15:21 PDT
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Scanned at 2018-08-22 15:21:07 PDT for 0s

PORT   STATE         SERVICE REASON
80/tcp open          http    syn-ack ttl 128
67/udp open|filtered dhcps   no-response
Final times for host: srtt: 10394 rttvar: 24115  to: 106854

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 0.35 seconds
           Raw packets sent: 7 (252B) | Rcvd: 3 (112B)

Scanning all ports


If you have the need to scan ALL 65535 ports, then this option will be helpful. Keep in mind that it will take a while to execute. I have never had the need to use this option so far. The command is as below:

nikhilh@ubuntu:~$ nmap -d -p- scanme.nmap.org

Scan top ports


Previously, we have seen the top 1000 ports being scanned. With the -F option, Nmap scans the top 100 ports. But what if you need to scan the top 10 ports? Or top 200 ports? Then this option would be helpful.

nikhilh@ubuntu:~$ nmap -d --top-ports 10 scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 13:34 PDT
PORTS: Using top 10 ports found open (TCP:10, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
PORT     STATE    SERVICE       REASON
21/tcp   filtered ftp           no-response
22/tcp   open     ssh           syn-ack
23/tcp   filtered telnet        no-response
25/tcp   filtered smtp          no-response
80/tcp   open     http          syn-ack
110/tcp  filtered pop3          no-response
139/tcp  filtered netbios-ssn   no-response
443/tcp  filtered https         no-response
445/tcp  filtered microsoft-ds  no-response
3389/tcp filtered ms-wbt-server no-response
Final times for host: srtt: 82117 rttvar: 35374  to: 223613

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 1.75 seconds

Scanning ports sequentially


Scanning ports sequentially is usually not recommended because it is very obvious that an entity is trying to scan the system. But if there is a requirement, then this is the option to use.

nikhilh@ubuntu:~$ nmap -d -r scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 13:40 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Not shown: 998 filtered ports
Reason: 998 no-responses
PORT     STATE SERVICE    REASON
22/tcp   open  ssh        syn-ack
9929/tcp open  nping-echo syn-ack
Final times for host: srtt: 82645 rttvar: 15487  to: 144593

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 21.56 seconds

Scan and show only open ports


When this option is used, Nmap will only display open ports. All others, which include closed, filtered, unfiltered, open|filtered, closed|filtered will not be displayed.

nikhilh@ubuntu:~$ nmap -d --open scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 14:55 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Reason: 997 no-responses
Some closed ports may be reported as filtered due to --defeat-rst-ratelimit
PORT     STATE SERVICE    REASON
22/tcp   open  ssh        syn-ack
80/tcp   open  http       syn-ack
9929/tcp open  nping-echo syn-ack
Final times for host: srtt: 83157 rttvar: 12869  to: 134633

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 28.22 seconds

That's it for part 5! Like part 4, I believe this post's content was straight forward as well. I hope everything was simple to understand. If there are any questions, please leave them in the comments below and I'll get back to you as soon as I can!

In the next part, we'll be looking at OS and service version detection along with timing options that Nmap offers.

Getting started with Nmap - Part 4

In part 3 of this tutorial series, we looked at various host discovery options that Nmap provides. Nmap first pings the target, checks its status and will proceed to scan it only if the host is alive (if -Pn option is not used). In this part, we'll look at the various scanning options that Nmap provides.

Disclaimer: In this entire tutorial series, I have used scanme.nmap.org as the target host and sometimes my local machine itself. scanme.nmap.org is a machine set up by Nmap developers for educational purposes, so it is legal to scan it a FEW TIMES A DAY. Scanning networks, in general is a cyber crime and may even lead to jail. Please take permission before scanning random networks.


Fast Scan


When the fast scan option is used, Nmap only scans the top 100 most commonly used ports rather than the top 1000.

nikhilh@ubuntu:~$ nmap -F -d scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 05:43 PDT
PORTS: Using top 100 ports found open (TCP:100, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
  max-scan-delay: TCP 1000, UDP 1000, SCTP 1000
  parallelism: min 0, max 0
  max-retries: 10, host-timeout: 0
  min-rate: 0, max-rate: 0
---------------------------------------------
Initiating Ping Scan at 05:43
...
...
22/tcp    open  ssh        syn-ack
80/tcp    open  http       syn-ack
Final times for host: srtt: 81393 rttvar: 34707  to: 220221

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 3.01 seconds

TCP SYN Scan


In part 2, I mentioned TCP Stealth scan. TCP SYN scan is the official name for TCP Stealth scan. This requires root access to execute and is the default scanning technique when Nmap is run as root as well.

nikhilh@ubuntu:~$ sudo nmap -sS -d scanme.nmap.org
[sudo] password for nikhilh: 
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 05:53 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
...
...
Initiating SYN Stealth Scan at 05:53
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or ((tcp or udp or sctp) and (src host 45.33.32.156)))
Discovered open port 22/tcp on 45.33.32.156
Discovered open port 80/tcp on 45.33.32.156
Discovered open port 31337/tcp on 45.33.32.156
Completed SYN Stealth Scan at 05:53, 31.55s elapsed (1000 total ports)
...
...
Nmap done: 1 IP address (1 host up) scanned in 31.61 seconds
           Raw packets sent: 2018 (88.700KB) | Rcvd: 1032 (41.280KB)

TCP Connect Scan


We already know that TCP Connect scan is the default scanning technique when Nmap is run with regular user privileges.

nikhilh@ubuntu:~$ nmap -d -sT scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 06:02 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Initiating Connect Scan at 06:02
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Discovered open port 22/tcp on 45.33.32.156
Discovered open port 80/tcp on 45.33.32.156
Discovered open port 31337/tcp on 45.33.32.156
...
...
Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 50.72 seconds

TCP Connect scan is less reliable than TCP SYN scan because Connect scan uses the localhost's TCP/IP stack. It does not generate its own raw packets.

TCP ACK Scan


In this type of scan, Nmap generates TCP ACK packets and sends them to the target host. This type of scan is useful when you want to check if a system is protected by a firewall.
  1. If the system is protected, Nmap would receive no responses and the ports are marked filtered
  2. If unprotected, Nmap would receive RST packets and mark the ports as unfiltered as in the case below.
nikhilh@ubuntu:~$ sudo nmap -d -sA scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 06:15 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Initiating ACK Scan at 06:15
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or ((tcp or udp or sctp) and (src host 45.33.32.156)))
Completed ACK Scan at 06:15, 0.03s elapsed (1000 total ports)
Overall sending rates: 34491.08 packets / s, 1379643.36 bytes / s.
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.000025s latency).
All 1000 scanned ports on scanme.nmap.org (45.33.32.156) are unfiltered because of 1000 resets
Final times for host: srtt: 25 rttvar: 7  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds
           Raw packets sent: 1004 (40.152KB) | Rcvd: 1001 (40.040KB)

TCP Null Scan


Null scan is a type of scan where all the TCP flags (SYN, ACK, FIN, RST, URG, PSH, ECE and CWR) are set to NULL. Many systems do not respond to such flags but a badly configured one, will.
  1. If a port is open, it will not understand what to do with such packets and it will discard it.
  2. If a port is closed, Nmap will receive a RST response.
nikhilh@ubuntu:~$ sudo nmap -d -sN scanme.nmap.org
[sudo] password for nikhilh: 
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 06:40 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
  rtt-timeouts: init 1000, min 100, max 10000
...
...
Initiating NULL Scan at 06:40
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or ((tcp or udp or sctp) and (src host 45.33.32.156)))
Completed NULL Scan at 06:40, 4.03s elapsed (1000 total ports)
Overall sending rates: 496.45 packets / s, 19858.06 bytes / s.
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.00039s latency).
All 1000 scanned ports on scanme.nmap.org (45.33.32.156) are open|filtered because of 1000 no-responses
Final times for host: srtt: 389 rttvar: 2213  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 4.09 seconds
           Raw packets sent: 2007 (80.272KB) | Rcvd: 5 (188B)

TCP FIN Scan


Like TCP NULL scan, not all systems respond to TCP FIN scan. In this type of scan, the FIN flag in the TCP header is set.
  1. If a port is open, the packet will be discarded silently and there will be no response.
  2. If a port is closed, it will generate an error message.
nikhilh@ubuntu:~$ sudo nmap -d -sF scanme.nmap.org
[sudo] password for nikhilh: 
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 08:48 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
...
...
Initiating FIN Scan at 08:48
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or ((tcp or udp or sctp) and (src host 45.33.32.156)))
Completed FIN Scan at 08:48, 4.03s elapsed (1000 total ports)
Overall sending rates: 497.08 packets / s, 19883.05 bytes / s.
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.00044s latency).
All 1000 scanned ports on scanme.nmap.org (45.33.32.156) are open|filtered because of 1000 no-responses
Final times for host: srtt: 444 rttvar: 2246  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 4.21 seconds
           Raw packets sent: 2007 (80.272KB) | Rcvd: 5 (188B)

XMAS Scan


In this type of scan, TCP flags - URG, FIN and PSH are set. It is called the XMAS scan because so many TCP flags have been set that the header looked like it was "lit up like a Christmas tree". Not all systems respond to this type of scan.

Systems that conform to RFC 793 are especially at risk.
  1. Ports that do not respond to such packets are deemed open.
  2. If the port replies with a RST, it is deemed closed.
nikhilh@ubuntu:~$ sudo nmap -d -sX scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 08:56 PDT
PORTS: Using top 1000 ports found open (TCP:1000, UDP:0, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
...
...
Initiating XMAS Scan at 08:56
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or ((tcp or udp or sctp) and (src host 45.33.32.156)))
Completed XMAS Scan at 08:56, 4.04s elapsed (1000 total ports)
Overall sending rates: 495.34 packets / s, 19813.80 bytes / s.
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.00029s latency).
All 1000 scanned ports on scanme.nmap.org (45.33.32.156) are open|filtered because of 1000 no-responses
Final times for host: srtt: 290 rttvar: 2164  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 4.11 seconds
           Raw packets sent: 2007 (80.272KB) | Rcvd: 5 (188B)

UDP Scan


On systems, there are ports (DHCP ports 67 and 68, for example) that specifically use UDP rather than TCP. States of such ports will be missed if you use only TCP scanning techniques.
  1. If a port is open/filtered/allocated, Nmap receives no response.
  2. If a port is closed, Nmap receives a Type 3 ICMP response - Port Unreachable. Many systems employ rate limiting which increases the time required to solicit a response from the target.
nikhilh@ubuntu:~$ sudo nmap -sU -d scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 09:04 PDT
PORTS: Using top 1000 ports found open (TCP:0, UDP:1000, SCTP:0)
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
...
...
Initiating UDP Scan at 09:04
Scanning scanme.nmap.org (45.33.32.156) [1000 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or ((tcp or udp or sctp) and (src host 45.33.32.156)))
Completed UDP Scan at 09:04, 4.04s elapsed (1000 total ports)
Overall sending rates: 496.33 packets / s, 14365.54 bytes / s.
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.00036s latency).
All 1000 scanned ports on scanme.nmap.org (45.33.32.156) are open|filtered because of 1000 no-responses
Final times for host: srtt: 360 rttvar: 2245  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-services.
Nmap done: 1 IP address (1 host up) scanned in 4.09 seconds
           Raw packets sent: 2007 (58.126KB) | Rcvd: 5 (188B)

IP Protocol Scan


This type of scan is useful in determining what protocols are supported on the target system. You can use this information to decide which types of scan to use (UDP scan, TCP SYN scan, etc).

nikhilh@ubuntu:~$ sudo nmap -sO -d scanme.nmap.org
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-22 09:14 PDT
--------------- Timing report ---------------
  hostgroups: min 1, max 100000
...
...
Initiating IPProto Scan at 09:14
Scanning scanme.nmap.org (45.33.32.156) [256 ports]
Packet capture filter (device eth0): dst host 192.168.182.136 and (icmp or icmp6 or (src host 45.33.32.156))
Discovered open port 6/ip on 45.33.32.156
Discovered open port 1/ip on 45.33.32.156
Completed IPProto Scan at 09:14, 1.21s elapsed (256 total ports)
Overall sending rates: 212.82 packets / s, 4325.74 bytes / s.
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up, received reset ttl 128 (0.010s latency).
Scanned at 2018-08-22 09:14:47 PDT for 1s
Not shown: 252 filtered protocols
Reason: 252 proto-unreaches
PROTOCOL STATE         SERVICE REASON
1        open          icmp    echo-reply ttl 128
6        open          tcp     proto-response ttl 128
17       open|filtered udp     no-response
47       open|filtered gre     no-response
Final times for host: srtt: 10311 rttvar: 20137  to: 100000

Read from /usr/local/bin/../share/nmap: nmap-payloads nmap-protocols.
Nmap done: 1 IP address (1 host up) scanned in 1.23 seconds
           Raw packets sent: 262 (5.396KB) | Rcvd: 256 (12.272KB)

That's it for part 4! This post, I feel had fairly straight forward content and probably wouldn't have caused a lot of stress while learning. If you have any questions, leave them in the comments below and I'll get back to you as soon as I can!

In the next post, we'll look specifically at port scanning options. For example: which ports to scan, which protocols to use while scanning said ports and more.

Popular posts