Host Discovery
One of the very first steps in any network reconnaissance mission is to reduce a (sometimes huge) set of IP ranges into a list of active or interesting hosts. Scanning every port of every single IP address is slow and usually unnecessary. Of course what makes a host interesting depends greatly on the scan purposes. Network administrators may only be interested in hosts running a certain service, while security auditors may care about every single device with an IP address. An administrator may be comfortable using just an ICMP ping to locate hosts on his internal network, while an external penetration tester may use a diverse set of dozens of probes in an attempt to evade firewall restrictions.
Because host discovery needs are so diverse, Nmap offers a
wide variety of options for customizing the techniques used. Host
discovery is sometimes called ping scan, but it goes well beyond
the simple ICMP echo request packets associated with the
ubiquitous ping tool. Users can skip
the discovery step entirely with a list scan (-sL
) or
by disabling host discovery (-Pn
), or engage the network
with arbitrary combinations of multi-port TCP SYN/ACK, UDP, SCTP
INIT and ICMP probes. The goal of these probes is to solicit
responses which demonstrate that an IP address is actually active
(is being used by a host or network device). On many networks,
only a small percentage of IP addresses are active at any given
time. This is particularly common with private address space
such as 10.0.0.0/8. That network has 16 million IPs, but I have
seen it used by companies with less than a thousand machines. Host
discovery can find those machines in a sparsely allocated sea of
IP addresses.
If no host discovery options are given, Nmap sends an ICMP
echo request, a TCP SYN packet to port 443, a TCP ACK packet to
port 80, and an ICMP timestamp request. (For IPv6, the ICMP
timestamp request is omitted because it is not part of ICMPv6.)
These defaults are
equivalent to the -PE -PS443 -PA80 -PP
options.
The exceptions to this are the ARP
(for IPv4) and Neighbor
Discovery
(for IPv6)
scans which are used for any targets on a local ethernet network.
For unprivileged Unix shell
users, the default probes are a SYN packet to ports 80 and 443 using
the connect
system
call.
This host discovery is often sufficient when scanning local
networks, but a more comprehensive set of discovery probes is
recommended for security auditing.
The -P*
options (which select
ping types) can be combined. You can increase your odds of
penetrating strict firewalls by sending many probe types using
different TCP ports/flags and ICMP codes. Also note that
ARP/Neighbor Discovery
is done by default against
targets on a local Ethernet network even if you specify other
-P*
options, because it is almost always faster
and more effective.
By default, Nmap does host discovery and then performs a
port scan against each host it determines is online. This is true
even if you specify non-default host discovery types such as UDP
probes (-PU
). Read about the
-sn
option to learn how to perform
only host discovery, or use -Pn
to skip host
discovery and port scan all target addresses. The following options
control host discovery:
-
-sL
(List Scan) The list scan is a degenerate form of host discovery that simply lists each host of the network(s) specified, without sending any packets to the target hosts. By default, Nmap still does reverse-DNS resolution on the hosts to learn their names. It is often surprising how much useful information simple hostnames give out. For example,
fw.chi
is the name of one company's Chicago firewall. Nmap also reports the total number of IP addresses at the end. The list scan is a good sanity check to ensure that you have proper IP addresses for your targets. If the hosts sport domain names you do not recognize, it is worth investigating further to prevent scanning the wrong company's network.-
-sn
(No port scan) -
-Pn
(No ping) For machines on a local ethernet network, ARP scanning will still be performed (unless
--disable-arp-ping
or--send-ip
is specified) because Nmap needs MAC addresses to further scan target hosts. In previous versions of Nmap,-Pn
was-P0
and-PN
.-
-PS
(TCP SYN Ping)<port list>
This option sends an empty TCP packet with the SYN flag set. The default destination port is 80 (configurable at compile time by changing
DEFAULT_TCP_PROBE_PORT_SPEC
innmap.h
). Alternate ports can be specified as a parameter. The syntax is the same as for the-p
except that port type specifiers likeT:
are not allowed. Examples are-PS22
and-PS22-25,80,113,1050,35000
. Note that there can be no space between-PS
and the port list. If multiple probes are specified they will be sent in parallel.The SYN flag suggests to the remote system that you are attempting to establish a connection. Normally the destination port will be closed, and a RST (reset) packet sent back. If the port happens to be open, the target will take the second step of a TCP three-way-handshake by responding with a SYN/ACK TCP packet. The machine running Nmap then tears down the nascent connection by responding with a RST rather than sending an ACK packet which would complete the three-way-handshake and establish a full connection. The RST packet is sent by the kernel of the machine running Nmap in response to the unexpected SYN/ACK, not by Nmap itself.
On Unix boxes, only the privileged user
root
is generally able to send and receive raw TCP packets. For unprivileged users, a workaround is automatically employed whereby theconnect
system call is initiated against each target port. This has the effect of sending a SYN packet to the target host, in an attempt to establish a connection. Ifconnect
returns with a quick success or an ECONNREFUSED failure, the underlying TCP stack must have received a SYN/ACK or RST and the host is marked available. If the connection attempt is left hanging until a timeout is reached, the host is marked as down.-
-PA
(TCP ACK Ping)<port list>
The reason for offering both SYN and ACK ping probes is to maximize the chances of bypassing firewalls. Many administrators configure routers and other simple firewalls to block incoming SYN packets except for those destined for public services like the company web site or mail server. This prevents other incoming connections to the organization, while allowing users to make unobstructed outgoing connections to the Internet. This non-stateful approach takes up few resources on the firewall/router and is widely supported by hardware and software filters. The Linux Netfilter/iptables firewall software offers the
--syn
convenience option to implement this stateless approach. When stateless firewall rules such as this are in place, SYN ping probes (-PS
) are likely to be blocked when sent to closed target ports. In such cases, the ACK probe shines as it cuts right through these rules.-
-PU
(UDP Ping)<port list>
Another host discovery option is the UDP ping, which sends a UDP packet to the given ports. For most ports, the packet will be empty, though some use a protocol-specific payload that is more likely to elicit a response. The payloads are the same probes used in service and version detection and are defined in the
nmap-service-probes
file. Packet content can also be affected with the--data
,--data-string
, and--data-length
options.The port list takes the same format as with the previously discussed
-PS
and-PA
options. If no ports are specified, the default is 40125. This default can be configured at compile-time by changingDEFAULT_UDP_PROBE_PORT_SPEC
innmap.h
. A highly uncommon port is used by default because sending to open ports is often undesirable for this particular scan type.-
-PY
(SCTP INIT Ping)<port list>
This option sends an SCTP packet containing a minimal INIT chunk. The default destination port is 80 (configurable at compile time by changing
DEFAULT_SCTP_PROBE_PORT_SPEC
innmap.h
). Alternate ports can be specified as a parameter. The syntax is the same as for the-p
except that port type specifiers likeS:
are not allowed. Examples are-PY22
and-PY22,80,179,5060
. Note that there can be no space between-PY
and the port list. If multiple probes are specified they will be sent in parallel.The INIT chunk suggests to the remote system that you are attempting to establish an association. Normally the destination port will be closed, and an ABORT chunk will be sent back. If the port happens to be open, the target will take the second step of an SCTP four-way-handshake by responding with an INIT-ACK chunk. If the machine running Nmap has a functional SCTP stack, then it tears down the nascent association by responding with an ABORT chunk rather than sending a COOKIE-ECHO chunk which would be the next step in the four-way-handshake. The ABORT packet is sent by the kernel of the machine running Nmap in response to the unexpected INIT-ACK, not by Nmap itself.
On Unix boxes, only the privileged user
root
is generally able to send and receive raw SCTP packets. Using SCTP INIT Pings is currently not possible for unprivileged users.-
-PE
;-PP
;-PM
(ICMP Ping Types) In addition to the unusual TCP, UDP and SCTP host discovery types discussed previously, Nmap can send the standard packets sent by the ubiquitous ping program. Nmap sends an ICMP type 8 (echo request) packet to the target IP addresses, expecting a type 0 (echo reply) in return from available hosts. Unfortunately for network explorers, many hosts and firewalls now block these packets, rather than responding as required by RFC 1122. For this reason, ICMP-only scans are rarely reliable enough against unknown targets over the Internet. But for system administrators monitoring an internal network, they can be a practical and efficient approach. Use the
-PE
option to enable this echo request behavior.While echo request is the standard ICMP ping query, Nmap does not stop there. The ICMP standards (RFC 792 and RFC 950 ) also specify timestamp request, information request, and address mask request packets as codes 13, 15, and 17, respectively. While the ostensible purpose for these queries is to learn information such as address masks and current times, they can easily be used for host discovery. A system that replies is up and available. Nmap does not currently implement information request packets, as they are not widely supported. RFC 1122 insists that “a host SHOULD NOT implement these messages”. Timestamp and address mask queries can be sent with the
-PP
and-PM
options, respectively. A timestamp reply (ICMP code 14) or address mask reply (code 18) discloses that the host is available. These two queries can be valuable when administrators specifically block echo request packets while forgetting that other ICMP queries can be used for the same purpose.-
-PO
(IP Protocol Ping)<protocol list>
One of the newer host discovery options is the IP protocol ping, which sends IP packets with the specified protocol number set in their IP header. The protocol list takes the same format as do port lists in the previously discussed TCP, UDP and SCTP host discovery options. If no protocols are specified, the default is to send multiple IP packets for ICMP (protocol 1), IGMP (protocol 2), and IP-in-IP (protocol 4). The default protocols can be configured at compile-time by changing
DEFAULT_PROTO_PROBE_PORT_SPEC
innmap.h
. Note that for the ICMP, IGMP, TCP (protocol 6), UDP (protocol 17) and SCTP (protocol 132), the packets are sent with the proper protocol headers while other protocols are sent with no additional data beyond the IP header (unless any of--data
,--data-string
, or--data-length
options are specified).-
--disable-arp-ping
(No ARP or ND Ping) -
--discovery-ignore-rst
-
--traceroute
(Trace path to host)