The Art of Port Scanning

by Fyodor <fyodor@dhp.com>

(Last significant update: Sat Sep 6 03:24:53 GMT 1997)


Abstract

This paper details many of the techniques used to determine what ports (orsimilar protocol abstraction) of a host are listening for connections. These ports represent potential communication channels. Mapping their existencefacilitates the exchange of information with the host, and thus it is quite useful for anyone wishing to explore their networked environment, including hackers. Despite what you have heard from the media, the Internet is NOTall about TCP port 80. Anyone who relies exclusively on the WWW forinformation gathering is likely to gain the same level of proficiency as your average AOLer, who does the same. This paper is also meant to serve as anintroduction to and ancillary documentation for a coding project I have been working on. It is a full featured, robust port scanner which (I hope) solves some of the problems I have encountered when dealing with other scanners and when working to scan massive networks. The tool, nmap, supports the following:



The freely distributable source code is appended to this paper.

Introduction

Scanning, as a method for discovering exploitable communication channels, hasbeen around for ages. The idea is to probe as many listeners as possible, andkeep track of the ones that are receptive or useful to your particular need.Much of the field of advertising is based on this paradigm, and the "to currentresident" brute force style of bulk mail is an almost perfect parallel to whatwe will discuss. Just stick a message in every mailbox and wait for theresponses to trickle back.

Scanning entered the h/p world along with the phone systems. Herewe have this tremendous global telecommunications network, allreachable through codes on our telephone. Millions of numbers arereachable locally, yet we may only be interested in 0.5% of thesenumbers, perhaps those that answer with a carrier.

The logical solution to finding those numbers that interest us isto try them all. Thus the field of "wardialing" arose. Excellentprograms like Toneloc were developed to facilitate the probing ofentire exchanges and more. The basic idea is simple. If you dial anumber and your modem gives you a CONNECT, you record it. Otherwisethe computer hangs up and tirelessly dials the next one.

While wardialing is still useful, we are now finding that many ofthe computers we wish to communicate with are connected throughnetworks such as the Internet rather than analog phone dialups.Scanning these machines involves the same brute force technique. Wesend a blizzard of packets for various protocols, and we deduce whichservices are listening from the responses we receive (or don'treceive).

Techniques

Over time, a number of techniques have been developed for surveying theprotocols and ports on which a target machine is listening. They all offerdifferent benefits and problems. Here is a line up of the most common:



Features

Prior to writing nmap, I spent a lot of time with other scannersexploring the Internet and various private networks (note theavoidance of the "intranet" buzzword). I have used many of the topscanners available today, including strobe by Julian Assange, netcatby *Hobbit*, stcp by Uriel Maimon, pscan by Pluvius, ident-scan byDave Goldsmith, and the SATAN tcp/udp scanners by Wietse Venema.These are all excellent scanners! In fact, I ended up hacking most ofthem to support the best features of the others. Finally I decided towrite a whole new scanner, rather than rely on hacked versions of adozen different scanners in my /usr/local/sbin. While I wrote all thecode, nmap uses a lot of good ideas from its predecessors. I alsoincorporated some new stuff like fragmentation scanning and optionsthat were on my "wish list" for other scanners. Here are some of the(IMHO) useful features of nmap:

Some other, more minor options:
 -v (verbose): This is highly recommended for interactive use.  Among otheruseful messages, you will see ports come up as they are found, rather thanhaving to wait for the sorted summary list. -r (randomize): This will randomize the order in which the target host'sports are scanned. -q (quash argv): This changes argv[0] to FAKE_ARGV ("pine" by default).It also eliminates all other arguments, so you won't look too suspicious in'w' or 'ps' listings. -h for an options summary. -R show and resolve all hosts, even down ones.
Also look for http://www.dhp.com/~fyodor/nmap/
,which is the web site I plan to put future versions and moreinformation on. In fact, you would be well advised to check thereright now. (If that isn't where you are reading this).
ExampleUsage


To launch a stealth scan of the entire class 'B' networks 166.66.0.0 and166.67.0.0 for the popularly exploitable imapd daemon:
# nmap -Up 143 166.66.0.0/16 166.67.0.0/16
To do a standard tcp scan on the reserved ports of host<target>:
> nmap target
To check the class 'C' network on which warez.com sits for popularservices (via fragmented SIN scan):
# nmap -fsp 21,22,23,25,80,110 warez.com/24
To scan the same network for all the services in your /etc/servicesvia (very fast) tcp scan:
> nmap -F warez.com/24
To scan secret.pathetic.net using the ftp bounce attack off offtp.pathetic.net:
> nmap -Db ftp.pathetic.net secret.pathetic.net
To find hosts that are up in the the adjacent class C's 193.14.12,.13, .14, .15, ... , .30:
> nmap -P '193.14.[12-30].*'
If you don't want to have to quote it to avoid shell interpretation,this does the same thing:
> nmap -P 193.14.12-30.0-255