Nmap Tutorial PDF
Nmap Tutorial PDF
Andrew J. Bennieston
http://www.nmap-tutorial.com
Contents
1 Introduction 4
2 Disclaimer 4
13 OS Fingerprinting 13
14 Outputting Logs 13
2
16 Typical Scanning Session 14
3
1 Introduction
Nmap is a free, open-source port scanner available for both UNIX and Windows.
It has an optional graphical front-end, NmapFE, and supports a wide variety of
scan types, each one with different benefits and drawbacks.
This article describes some of these scan types, explaining their relative ben-
efits and just how they actually work. It also offers tips about which types of
scan would be best against which types of host.
The article assumes you have Nmap installed (or that you know how to install
it. Instructions are available on the Nmap website, http://www.insecure.org/
nmap/install/inst-source.html ), and that you have the required privileges
to run the scans detailed (many scans require root or Administrator privileges).
A frequently asked questions section has been added since the first version of
this article, and this is included as the last section in this version. This is a fully
revised and updated version of this tutorial, re-typed and converted to a TeX
format, allowing more output formats to be utilised. At the time of writing, the
latest Nmap version was 4.11.
2 Disclaimer
This information is provided to assist users of Nmap in scanning their own net-
works, or networks for which they have been given permission to scan, in order
to determine the security of such networks. it is not intended to assist with scan-
ning remote sites with the intention of breaking into or exploiting services on
those sites, or for imformation gathering purposes beyond those allowed by law.
I hereby disclaim any responsibility for actions taken based upon the information
in this article, and urge all who seek information towards a destructive end to
reconsider their life, and do something constructive instead.
4
to every port in turn, and notes whether or not the connection succeeded. Once
the scan is completed, ports to which a connection could be established are listed
as open, the rest are said to be closed.
This method of scanning is very effective, and provides a clear picture of the
ports you can and cannot access. If a connect() scan lists a port as open, you
can definitely connect to it - that is what the scanning computer just did! There
is, however, a major drawback to this kind of scan; it is very easy to detect on
the system being scanned. If a firewall or intrusion detection system is running
on the victim, attempts to connect() to every port on the system will almost
always trigger a warning. Indeed, with modern firewalls, an attempt to connect
to a single port which has been blocked or has not been specifically opened will
usually result in the connection attempt being logged. Additionally, most servers
will log connections and their source IP, so it would be easy to detect the source
of a TCP connect() scan.
For this reason, the TCP Stealth Scan was developed.
5
to tear down the connection before it can be established fully; often preventing
the connection attempt appearing in application logs. If the port is closed, an
RST will be sent. If it is filtered, the SYN packet will have been dropped and
no response will be sent. In this way, Nmap can detect three port states - open,
closed and filtered. Filtered ports may require further probing since they could
be subject to firewall rules which render them open to some IPs or conditions,
and closed to others.
Modern firewalls and Intrusion Detection Systems can detect SYN scans, but
in combination with other features of Nmap, it is possible to create a virtually
undetectable SYN scan by altering timing and other options (explained later).
6
9 22/tcp open ssh
10 631/tcp open ipp
11 6000/tcp open X11
12
13 Nmap finished: 1 IP address (1 host up) scanned in 0.207
14 seconds
15 [chaos]# nmap -sF 127.0.0.1
16
17 Starting Nmap 4.01 at 2006-07-06 17:23 BST
18 Interesting ports on chaos (127.0.0.1):
19 (The 1668 ports scanned but not shown below are in state:
20 closed)
21 PORT STATE SERVICE
22 21/tcp open|filtered ftp
23 22/tcp open|filtered ssh
24 631/tcp open|filtered ipp
25 6000/tcp open|filtered X11
26
27 Nmap finished: 1 IP address (1 host up) scanned in 1.284
28 seconds
7
6 UDP Scan [-sU]
Scanning for open UDP ports is done with the -sU option. With this scan type,
Nmap sends 0-byte UDP packets to each target port on the victim. Receipt of
an ICMP Port Unreachable message signifies the port is closed, otherwise it is
assumed open.
One major problem with this technique is that, when a firewall blocks out-
going ICMP Port Unreachable messages, the port will appear open. These false-
positives are hard to distinguish from real open ports.
Another disadvantage with UDP scanning is the speed at which it can be
performed. Most operating systems limit the number of ICMP Port Unreachable
messages which can be generated in a certain time period, thus slowing the speed
of a UDP scan. Nmap adjusts its scan speed accordingly to avoid flooding a
network with useless packets. An interesting point to note here is that Microsoft
do not limit the Port Unreachable error generation frequency, and thus it is easy
to scan a Windows machines 65,535 UDP Ports in very little time!!
UDP Scanning is not usually useful for most types of attack, but it can reveal
information about services or trojans which rely on UDP, for example SNMP,
NFS, the Back Orifice trojan backdoor and many other exploitable services.
Most modern services utilise TCP, and thus UDP scanning is not usually
included in a pre-attack information gathering exercise unless a TCP scan or
other sources indicate that it would be worth the time taken to perform a UDP
scan.
8
5 (The 251 protocols scanned but not shown below are
6 in state: closed)
7 PROTOCOL STATE SERVICE
8 1 open icmp
9 2 open|filtered igmp
10 6 open tcp
11 17 open udp
12 255 open|filtered unknown
13
14 Nmap finished: 1 IP address (1 host up) scanned in
15 1.259 seconds
9
This scan type requires certain things of the zombie. The IPID sequence
generation must be predictable (single-step increments, for example). The host
must also have low traffic so that it is unlikely for other packets to hit the zombie
whilst Nmap is carrying out its scan (as these will artificially inflate the IPID
number!). Cheap routers or MS Windows boxes make good zombie hosts. Most
operating systems use randomised sequence numbers (see the OS Fingerprinting
section for details on how to check a targets sequence generation type).
The idle scan can also be used to determine IP trust based relationships
between hosts (e.g. a firewall may allow a certain host to connect to port x, but
not other hosts). This scan type can help to determine which hosts have access
to such a system.
For more information about this scan type, read http://www.insecure.org/
nmap/idlescan.html
10
11 Window Scan, RPC Scan, List Scan [-sW,
-sR, -sL]
The TCP Window scan is similar to the ACK scan but can sometimes detect
open ports as well as filtered/unfiltered ports. This is due to anomalies in TCP
Window size reporting by some operating systems (see the Nmap manual for a
list, or the nmap-hackers mailing list for the full list of susceptible OS).
RPC Scans can be used in conjunction with other scan types to try to deter-
mine if an open TCP or UDP port is an RPC service, and if so, which program,
and version numbers are running on it. Decoys are not supported with RPC
scans (see section on Timing and Hiding Scans, below).
List scanning simply prints a list of IPs and names (DNS resolution will be
used unless the -n option is passed to Nmap) without actually pinging or scanning
the hosts.
12.2 Decoys
The -D option allows you to specify Decoys. This option makes it look like those
decoys are scanning the target network. It does not hide your own IP, but it
makes your IP one of a torrent of others supposedly scanning the victim at the
11
same time. This not only makes the scan look more scary, but reduces the chance
of you being traced from your scan (difficult to tell which system is the real
source).
12.5 Fragmenting
The -f option splits the IP packet into tiny fragments when used with -sS, -sF,
-sX or -sN. This makes it more difficult for a firewall or packet filter to determine
the packet type. Note that many modern packet filters and firewalls (including
iptables) feature optional defragmenters for such fragmented packets, and will
thus reassemble the packet to check its type before sending it on. Less complex
firewalls will not be able to cope with fragmented packets this small and will most
likely let the OS reassemble them and send them to the port they were intended
to reach. Using this option could crash some less stable software and hardware
since packet sizes get pretty small with this option!
12
12.6 Idle Scanning
See the section on -sI for information about idle scans.
13 OS Fingerprinting
The -O option turns on Nmaps OS fingerprinting system. Used alongside the
-v verbosity options, you can gain information about the remote operating sys-
tem and about its TCP Sequenmce Number generation (useful for planning Idle
scans).
An article on OS detection is available at http://www.insecure.org/nmap/
nmap-fingerprinting-article.html
14 Outputting Logs
Logging in Nmap can be provided by the -oN, -oX or -oG options. Each one
is followed by the name of the logfile. -oN outputs a human readable log, -oX
outputs an XML log and -oG outputs a grepable log. The -oA option outputs in
all 3 formats, and -oS outputs in a format Im sure none of you would ever want
to use (try it; youll see what I mean!)
The append-output option appends scan results to the output files you spec-
ified instead of overwriting their contents.
15.3 Resuming
Scans cancelled with Ctrl+C can be resumed with the resume logfilename
option. The logfile must be a Normal or Grepable logfile (-oN or -oG).
13
15.4 Reading Targets From A File
-iL inputfilename reads targets from inputfilename rather than from the command-
line.
The file should contain a hostlist or list of network expressions separated by
spaces, tabs or newlines. Using a hyphen as inputfile makes Nmap read from
standard input.
15.6 Time-To-Live
The -ttl value option sets the IPv4 packets time-to-live. The usefulness of this is
in mapping paths through networks and determining ACLs on firewalls (setting
the ttl to one past the packet filter can help to determine information about the
filtering rules themselves). Repeated Nmap scans to a single port using differing
ttl values will emulate a traceroute style network path map (Try it, its great
fun for a while, until you get bored and realise traceroute does it all for you
automatically!).
14
Now were going to take a look at 10.0.0.1 and 10.0.0.2, both listed as Netgear
in the ping sweep. These IPs are good criteria for routers (in fact I know that
10.0.0.1 is a router and 10.0.0.2 is a wireless access point, since its my network,
but lets see what Nmap makes of it...)
Well scan 10.0.0.1 using a SYN scan [-sS] and -A to enable OS fingerprinting
and version detection.
The only open port is 80/tcp - in this case, the web admin interface for the
router. OS fingerprinting guessed it was a Netgear Wireless Access Point - in fact
this is a Netgear (wired) ADSL router. As it said, though, there were insufficient
responses for TCP sequencing to accurately detect the OS.
Now well do the same for 10.0.0.2...
15
11 Device type: general purpose
12 Running: Linux 2.4.X|2.5.X
13 OS details: Linux 2.4.0 - 2.5.20
14 Uptime 14.141 days (since Fri Jun 30 11:03:05 2006)
15
16 Nmap finished: 1 IP address (1 host up) scanned in 9.636
17 seconds
Interestingly, the OS detection here listed Linux, and the version detection
was able to detect the httpd running. The accuracy of this is uncertain, this
is a Netgear home wireless access point, so it could be running some embedded
Linux!
Now well move on to 10.0.0.4 and 10.0.0.5, these are likely to be normal
computers running on the network...
16
29 TCP Sequence Prediction: Class=random positive increments
30 Difficulty=4732564 (Good luck!)
31 IPID Sequence Generation: All zeros
32 Service Info: OS: Unix
33
34 Nmap finished: 1 IP address (1 host up) scanned in 8.333 seconds
35 Raw packets sent: 1687 (74.7KB) | Rcvd: 3382 (143KB)
From this, we can deduce that 10.0.0.4 is a Linux system (in fact, the one Im
typing this tutorial on!) running a 2.4 to 2.6 kernel (Actually, Slackware Linux
10.2 on a 2.6.19.9 kernel) with open ports 21/tcp, 22/tcp, 631/tcp and 6000/tcp.
All but 6000 have version information listed. The scan found the IPID sequence
to be all zeros, which makes it useless for idle scanning, and the TCP Sequence
prediction as random positive integers. The -v option is needed to get Nmap to
print the IPID information out!
Now, onto 10.0.0.5...
17
No open ports, and Nmap couldnt detect the OS. This suggests that it is
a firewalled or otherwise protected system, with no services running (and yet it
responded to ping sweeps).
We now have rather more information about this network than we did when
we started, and can guess at several other things based on these results. Using
that information, and the more advanced Nmap scans, we can obtain further scan
results which will help to plan an attack, or to fix weaknesses, in this network.
18
Note also, that decoys will not prevent your IP showing entirely; it just lists
the others as well. A particularly well designed IDS may even be able to figure
out which is the real source of the scans.
Where speed of scan isnt essential, the -P0 option is a good idea. Nmap
gains timing information from pinging the host, and can often complete its scans
faster with this information, but the ping packets will be sent to the victim from
your IP, and any IDS worth its CPU cycles will pick up on the pattern of a
few pings followed by connects to a variety of ports. -P0 also allows scanning of
hosts which do not respond to pings (i.e. if ICMP is blocked by a firewall or by
in-kernel settings).
I mentioned timing in the above paragraph. You can use the -T timing option
to slow the scan down. The slower a scan is, the less likely it is to be detected by
an IDS. There are bound to be occasional random connects occurring, people type
an IP in wrong or try to connect and their computer crashes half way through
the connect. These things happen, and unless an IDS is configured extremely
strictly, they generally arent reported (at least, not in the main alert logs, they
may be logged if logging of all traffic is enabled, but typically these kind of logs
are only checked if theres evidence of something going on). Setting the timing to
-T 0 or -T 1 (Paranoid or Sneaky) should help avoid detection. As mentioned in
my main tutorial, you can also set timing options for each aspect of a scan,
Timings for individual aspects of a scan can also be set using the
host timeout, max rtt timeout, min rtt timeout, initial rtt timeout,
max parallelism, min parallelism, and scan delay options. See the
Nmap manual for details.
The final note I will add to this answer is that use of the Idle scan method
(-sI) means that not a single packet is sent to the victim from your IP (provided
you also use the -P0 option to turn off pings). This is the ultimate in stealth as
there is absolutely no way the victim can determine that your IP is responsible
for the scan (short of obtaining log information from the host you used as part
of your idle scan).
19
17.3 Will -sN -sX and -sF work against any host, or just
Windows hosts?
-sN -sX and -sF scans will work against any host, but Windows computers do
not respond correctly to them, so scanning a Windows machine with these scans
results in all ports appearing closed. Scanning a *nix or other system should work
just fine, though. As I said in the main tutorial, -sX -sF and -sN are commonly
used to determine if youre scanning a Windows host or not, without using the
-O fingerprinting option.
The Nmap manual page should help to determine which scans work alongside
which options, and on which target systems they are most effective.
17.4 How do I find a dummy host for the Idle Scan (-sI)?
You simply have to scan for hosts using sequential IPID sequences, these are
(often) suitable for use as a dummy host for the -sI Idle Scan.
20