Nmap

Nmap Phases

The following provides the order in which Nmap operates. Phases are used, and certain phases utilize information gathered from previous phases. Many phases are optional and not performed by default, such as verion and OS detection. Understanding the phases and the consequences of performing them can help perform better scans.

  1. Target Enumeration resolves input to a list of IPv4 or IPv6 addresses provided by the user.
  2. Host Discovery interacts with targets to determine if they are up. This step is optional, and there are many methods to choose from (i.e. ICMP, ARP, TCP).
  3. Reverse DNS Resolution iterates over the list of targets to get DNS names via DNS servers. This step is optional, and can be set to resolve unresponsive hosts as well.
  4. Port Scanning iterates over the targets and performs scanning techniques based on user-specified requests.
  5. Version Detection utilizes techniques to determine versions of software, these techniques include banner fetching, fingerprinting, etc.
  6. OS Detection uses thousands of stored OS responses to help determine what OS is running on the remote system.
  7. Traceroute traces hops to your target and prints the routes out beautifully.
  8. Script Scanning utilizes the Nmap scripting Engine (NSE) allowing users to specify scripts to run against targets, even exploits.
  9. Output collects info gathered and outputs it to a file or stdout. Different ouput formats are available.

Port States

Nmap provides granular port states, 6 in total. These states reflect how Nmap sees the ports. Scanning techniques can be used to determine actual states of ports when firewalls are in place.

Target Enumeration

Enumeration is the management of address inputs for Nmap, it can be a hostname, hostname followed by the subnet, ip address / range of ip's via subnet, and comma-seperated values. Provided are flags that relate to user-specified enumeration and their effects.

Host Discovery

Discovery is the initial stage regarding interacting with targets. This is the stage where Nmap determines what hosts are up and the user can specify different methods for identifying responsive targets. For example, on a LAN -PR flag is used by default, utilizing layer 2 ARP requests to resolve hosts.

DNS Resolution

The DNS Resolution stage is where users can alter how Nmap handles DNS. Reverse lookups can be performed for all hosts, up or down, or the stage can be completely skipped. Multiple DNS servers can be used to balance requests across servers making your actions more difficult to detect.

Port Scanning

Nmap provides many options for how users can scan ports. Some options are provided below, by default the SYN scan is used. You can try different scanning options to see if targets return different results based on the response, and different scans can be used to bypass firewalls if they are configured a certain way.

Version Detection

Nmap utilizes databases containing many expected probe responses based on services and their versions, this allows the version scan to provide more information about services running on ports. It will also attempt to gather further information about ports in states that do not provide info (i.e. open|filtered).

OS Detection

Nmap utilizes databases containing many expected probe responses based on Operating Systems. There are thousands of OS fingerprints in the database, if you encounter an OS that is not in the database Nmap will notify you and request you add the info to the database, assuming you know exactly the OS and version running on the target. Fingerprints include OS details, vendor name, OS version, and the device type (i.e. general purpose, router, switch, etc.).

Script Scanning

Nmap provides many scripts created by the community over the years. Some scripts can be very obtrusive and even exploit vulnerabilities on targets so use with caution. Nmap scripts are organized via categories and the default category is applied when the user does not specify, obtrusive/exploitative scripts do not fall under default.

Output

Users can specify how output data is displayed, as well as save the output to several different files. Output in xml format is useful when being ingested by other programs such as metasploit.

Nmap Scripting Engine

Nmap Example Script

local comm = require "comm"
local shortport = require "shortport"
local oops = require "oops"

description = [[
Retrieves the day and time from the Daytime service.
]]

---
-- @output
-- PORT   STATE SERVICE
-- 13/tcp open  daytime
-- |_daytime: Wed Mar 31 14:48:58 MDT 2010

author = "Diman Todorov"

license = "Same as Nmap--See https://nmap.org/book/man-legal.html"

categories = {"discovery", "safe"}


portrule = shortport.port_or_service(13, "daytime", {"tcp", "udp"})

action = function(host, port)
  return oops.output(comm.exchange(host, port, "dummy", {lines=1}))
end

Speed Up your UDP Scans

Version detection (-sV) helps determine open or filtered for UDP scans, set --version-intensity 0, has a dramatic impact.

Limit scans to popular ports, run the full port scan in the background because it will take a while.

When scanning multiple hosts, set the --min-hostgroup flag to a higher number, this increases the scan rate. Ex. scanning 4096 IPs, set the host group to 512 (number is divisible).

Set the -v flag for verbosity to provide estimated completion times.

Useful Commands

References