home *** CD-ROM | disk | FTP | other *** search
/ Steganos Hacker Tools / SHT151.iso / programme / scanner / nmapNTsp1 / Win_2000.exe / nmapNT-src / docs / nmap.deprecated.txt < prev    next >
Encoding:
Text File  |  2000-05-08  |  20.6 KB  |  372 lines

  1.                                .oO Phrack 51 Oo.
  2.  
  3.                             Volume Seven, Issue Fifty One
  4.                                      xx of xx                     
  5.  
  6.                             The Art of Port Scanning
  7.                            by Fyodor (fyodor@dhp.com)
  8.  
  9.  
  10.  
  11. [ Abstract ]
  12.  
  13. This paper details many of the techniques used to determine what ports (or
  14. similar protocol abstraction) of a host are listening for connections.  These 
  15. ports represent potential communication channels.  Mapping their existence
  16. facilitates the exchange of information with the host, and thus it is quite 
  17. useful for anyone wishing to explore their networked environment, including 
  18. hackers.  Despite what you have heard from the media, the Internet is NOT
  19. all about TCP port 80.  Anyone who relies exclusively on the WWW for
  20. information gathering is likely to gain the same level of proficiency as your 
  21. average AOLer, who does the same.  This paper is also meant to serve as an
  22. introduction to and ancillary documentation for a coding project I have been 
  23. working on.  It is a full featured, robust port scanner which (I hope) solves 
  24. some of the problems I have encountered when dealing with other scanners and 
  25. when working to scan massive networks.  The tool, nmap, supports the following:
  26.  
  27.     - vanilla TCP connect() scanning, 
  28.     - TCP SYN (half open) scanning,
  29.     - TCP FIN (stealth) scanning, 
  30.     - TCP ftp proxy (bounce attack) scanning
  31.     - SYN/FIN scanning using IP fragments (bypasses packet filters),
  32.     - UDP recvfrom() scanning, 
  33.     - UDP raw ICMP port unreachable scanning, 
  34.     - ICMP scanning (ping-sweep), and
  35.     - reverse-ident scanning.
  36.  
  37. The freely distributable source code is appended to this paper.
  38.  
  39.  
  40.  
  41. [ Introduction ]
  42.  
  43. Scanning, as a method for discovering exploitable communication channels, has
  44. been around for ages.  The idea is to probe as many listeners as possible, and
  45. keep track of the ones which are receptive or useful to your particular need.
  46. Much of the field of advertising is based on this paradigm, and the "to current
  47. resident" brute force style of bulk mail is an almost perfect parallel to what
  48. we will discuss.  Just stick a message in every mailbox and wait for the
  49. responses to trickle back.
  50.  
  51. Scanning entered the h/p world along with the phone systems.  Here we have this
  52. tremendous global telecommunications network, all reachable through codes on 
  53. our telephone.  Millions of numbers are reachable locally, yet we may only
  54. be interested in 0.5% of these numbers, perhaps those that answer with a 
  55. carrier.
  56.  
  57. The logical solution to finding those numbers that interest us is to try them
  58. all.  Thus the field of "wardialing" arose.  Excellent programs like Toneloc
  59. were developed to facilitate the probing of entire exchanges and more.  The
  60. basic idea is simple.  If you dial a number and your modem gives you a CONNECT,
  61. you record it.  Otherwise the computer hangs up and tirelessly dials the next 
  62. one.
  63.  
  64. While wardialing is still useful, we are now finding that many of the computers
  65. we wish to communicate with are connected through networks such as the Internet
  66. rather than analog phone dialups.  Scanning these machines involves the same
  67. brute force technique.  We send a blizzard of packets for various protocols,
  68. and we deduce which services are listening from the responses we receive (or
  69. don't receive).
  70.  
  71.  
  72.  
  73. [ Techniques ]
  74.  
  75. Over time, a number of techniques have been developed for surveying the
  76. protocols and ports on which a target machine is listening.  They all offer
  77. different benefits and problems.  Here is a line up of the most common:
  78.  
  79. - TCP connect() scanning : This is the most basic form of tcp scanning.  The
  80. connect() system call provided by your operating system is used to open a
  81. connection to every interesting port on the machine.  If the port is listening,
  82. connect() will succeed, otherwise the port isn't reachable.  One strong
  83. advantage to this technique is that you don't need any special privileges.  Any
  84. user on most UNIX boxes is free to use this call.  Another advantage is speed.
  85. While making a separate connect() call for every targeted port in a linear
  86. fashion would take ages over a slow connection, you can hasten the scan by
  87. using many sockets in parallel.  Using non-blocking I/O allows you to set a low
  88. time-out period and watch all the sockets at once.  This is the fastest
  89. scanning method supported by nmap, and is available with the -t (TCP) option.
  90. The big downside is that this sort of scan is easily detectable and filterable.
  91. The target hosts logs will show a bunch of connection and error messages for
  92. the services which take the connection and then have it immediately shutdown.
  93.  
  94.  
  95. - TCP SYN scanning : This technique is often referred to as "half-open"
  96. scanning, because you don't open a full TCP connection.  You send a SYN packet,
  97. as if you are going to open a real connection and wait for a response.  A
  98. SYN|ACK indicates the port is listening.  A RST is indicative of a non-
  99. listener.  If a SYN|ACK is received, you immediately send a RST to tear down
  100. the connection (actually the kernel does this for us).  The primary advantage
  101. to this scanning technique is that fewer sites will log it.  Unfortunately you
  102. need root privileges to build these custom SYN packets.  SYN scanning is the -s
  103. option of nmap.
  104.  
  105.  
  106. - TCP FIN scanning : There are times when even SYN scanning isn't clandestine
  107. enough.  Some firewalls and packet filters watch for SYNs to an unallowed port,
  108. and programs like synlogger and courtney are available to detect these scans.
  109. FIN packets, on the other hand, may be able to pass through unmolested.  This
  110. scanning technique was featured in detail by Uriel Maimon in Phrack 49, article
  111. 15.  The idea is that closed ports tend to reply to your FIN packet with the
  112. proper RST.  Open ports, on the other hand, tend to ignore the packet in
  113. question.  This is a bug in TCP implementations and so it isn't 100% reliable
  114. (some systems, notably Micro$oft boxes, seem to be immune).  It works well on
  115. most other systems I've tried.  FIN scanning is the -U (Uriel) option of nmap.
  116.  
  117.  
  118. - Fragmentation scanning : This is not a new scanning method in and of itself,
  119. but a modification of other techniques.  Instead of just sending the probe
  120. packet, you break it into a couple of small IP fragments.  You are splitting 
  121. up the TCP header over several packets to make it harder for packet filters 
  122. and so forth to detect what you are doing.  Be careful with this!  Some
  123. programs have trouble handling these tiny packets.  My favorite sniffer
  124. segmentation faulted immediately upon receiving the first 36-byte fragment.  
  125. After that comes a 24 byte one!  While this method won't get by packet filters
  126. and firewalls that queue all IP fragments (like the CONFIG_IP_ALWAYS_DEFRAG 
  127. option in Linux), a lot of networks can't afford the performance hit this
  128. causes.  This feature is rather unique to scanners (at least I haven't seen 
  129. any others that do this).  Thanks to daemon9 for suggesting it.  The -f
  130. instructs the specified SYN or FIN scan to use tiny fragmented packets.
  131.  
  132.  
  133. - TCP reverse ident scanning : As noted by Dave Goldsmith in a 1996 Bugtraq
  134. post, the ident protocol (rfc1413) allows for the disclosure of the username of
  135. the owner of any process connected via TCP, even if that process didn't 
  136. initiate the connection.  So you can, for example, connect to the http port 
  137. and then use identd to find out whether the server is running as root.  This 
  138. can only be done with a full TCP connection to the target port (ie the -t
  139. option).  nmap's -i option queries identd for the owner of all listen()ing
  140. ports.
  141.  
  142.  
  143. - FTP bounce attack : An interesting "feature" of the ftp protocol (RFC 959) is
  144. support for "proxy" ftp connections.  In other words, I should be able to
  145. connect from evil.com to the FTP server-PI (protocol interpreter) of target.com
  146. to establish the control communication connection.  Then I should be able to
  147. request that the server-PI initiate an active server-DTP (data transfer
  148. process) to send a file ANYWHERE on the internet!  Presumably to a User-DTP,
  149. although the rfc specifically states that asking one server to send a file to
  150. another is OK.  Now this may have worked well in 1985, when the rfc was
  151. written.  But nowadays, we can't have people hijacking ftp servers and
  152. requesting that data be spit out to arbitrary points on the internet.  As
  153. *Hobbit* wrote back in 1995, this protocol flaw "can be used to post virtually
  154. untraceable mail and news, hammer on servers at various sites, fill up disks,
  155. try to hop firewalls, and generally be annoying and hard to track down at the
  156. same time."  What we will exploit this for is to (surprise, surprise) scan TCP
  157. ports from a "proxy" ftp server.  Thus you could connect to an ftp server
  158. behind a firwall, and then scan ports that are more likely to be blocked (139
  159. is a good one).  If the ftp server allows reading from and writing to a
  160. directory (such as /incoming), you can send arbitrary data to ports that you do
  161. find open.
  162.  
  163. For port scanning, our technique is to use the PORT command to declare that
  164. our passive "User-DTP" is listening on the target box at a certain port number.
  165.  Then we try to LIST the current directory, and the result is sent over the
  166. Server-DTP channel.  If our target host is listening on the specified port, the
  167. transfer will be successful (generating a 150 and a 226 response).  Otherwise
  168. we will get "425 Can't build data connection: Connection refused."  Then we
  169. issue another PORT command to try the next port on the target host.  The
  170. advantages to this approach are obvious (harder to trace, potential to bypass
  171. firewalls).  The main disadvantages are that it is slow, and that some FTP
  172. servers have finally got a clue and disabled the proxy "feature".  For what it
  173. is worth, here is a list of benners from sites where it does/doesn't work:
  174.  
  175. *Bounce attacks worked:*
  176.  
  177. 220 xxxxxxx.com FTP server (Version wu-2.4(3) Wed Dec 14 ...) ready.
  178. 220 xxx.xxx.xxx.edu FTP server ready.
  179. 220 xx.Telcom.xxxx.EDU FTP server (Version wu-2.4(3) Tue Jun 11 ...) ready.
  180. 220 lem FTP server (SunOS 4.1) ready.
  181. 220 xxx.xxx.es FTP server (Version wu-2.4(11) Sat Apr 27 ...) ready.
  182. 220 elios FTP server (SunOS 4.1) ready
  183.  
  184. *Bounce attack failed:*
  185.  
  186. 220 wcarchive.cdrom.com FTP server (Version DG-2.0.39 Sun May 4 ...) ready.
  187. 220 xxx.xx.xxxxx.EDU Version wu-2.4.2-academ[BETA-12](1) Fri Feb 7
  188. 220 ftp Microsoft FTP Service (Version 3.0).
  189. 220 xxx FTP server (Version wu-2.4.2-academ[BETA-11](1) Tue Sep 3 ...) ready.
  190. 220 xxx.unc.edu FTP server (Version wu-2.4.2-academ[BETA-13](6) ...) ready.
  191.  
  192. The 'x's are partly there to protect those guilty of running a flawed server,
  193. but mostly just to make the lines fit in 80 columns.  Same thing with the
  194. ellipse points.  The bounce attack is avalable with the -b <proxy_server>
  195. option of nmap.  proxy_server can be specified in standard URL format,
  196. username:password@server:port , with everything but server being optional.
  197.  
  198.  
  199. - UDP ICMP port unreachable scanning : This scanning method varies from the
  200. above in that we are using the UDP protocol instead of TCP.  While this
  201. protocol is simpler, scanning it is actually significantly more difficult.
  202. This is because open ports don't have to send an acknowledgement in response to
  203. our probe, and closed ports aren't even required to send an error packet.
  204. Fortunately, most hosts do send an ICMP_PORT_UNREACH error when you send a
  205. packet to a closed UDP port.  Thus you can find out if a port is NOT open, and
  206. by exclusion determine which ports which are.  Neither UDP packets, nor the
  207. ICMP errors are guaranteed to arrive, so UDP scanners of this sort must also
  208. implement retransmission of packets that appear to be lost (or you will get a
  209. bunch of false positives).  Also, this scanning technique is slow because of
  210. compensation for machines that took RFC 1812 section 4.3.2.8 to heart and limit
  211. ICMP error message rate.  For example, the Linux kernel (in net/ipv4/icmp.h)
  212. limits destination unreachable message generation to 80 per 4 seconds, with a
  213. 1/4 second penalty if that is exceeded.  At some point I will add a better
  214. algorithm to nmap for detecting this.  Also, you will need to be root for
  215. access to the raw ICMP socket necessary for reading the port unreachable.  The
  216. -u (UDP) option of nmap implements this scanning method for root users.
  217.  
  218. Some people think UDP scanning is lame and pointless.  I usually remind them of
  219. the recent Solaris rcpbind hole.  Rcpbind can be found hiding on an
  220. undocumented UDP port somewhere above 32770.  So it doesn't matter that 111 is
  221. blocked by the firewall.  But can you find which of the more than 30,000 high
  222. ports it is listening on?  With a UDP scanner you can!
  223.  
  224.  
  225. - UDP recvfrom() and write() scanning : While non-root users can't read
  226. port unreachable errors directly, Linux is cool enough to inform the user
  227. indirectly when they have been received.  For example a second write()
  228. call to a closed port will usually fail.  A lot of scanners such as netcat
  229. and Pluvius' pscan.c do this.  I have also noticed that recvfrom() on
  230. non-blocking UDP sockets usually return EAGAIN ("Try Again", errno 13) if
  231. the ICMP error hasn't been received, and ECONNREFUSED ("Connection refused", 
  232. errno 111) if it has.  This is the technique used for determining open ports 
  233. when non-root users use -u (UDP).  Root users can also use the -l (lamer
  234. UDP scan) options to force this, but it is a really dumb idea.
  235.  
  236.  
  237. - ICMP echo scanning : This isn't really port scanning, since ICMP doesn't have
  238. a port abstraction.  But it is sometimes useful to determine what hosts in a
  239. network are up by pinging them all.  the -P option does this.  Also you might
  240. want to adjust the PING_TIMEOUT #define if you are scanning a large
  241. network. nmap supports a host/bitmask notation to make this sort of thing
  242. easier.  For example 'nmap -P cert.org/24 152.148.0.0/16' would scan CERT's
  243. class C network and whatever class B entity 152.148.* represents.  Host/26 is
  244. useful for 6-bit subnets within an organization.
  245.  
  246.  
  247.  
  248. [ Features ]
  249.  
  250. Prior to writing nmap, I spent a lot of time with other scanners exploring the
  251. Internet and various private networks (note the avoidance of the "intranet"
  252. buzzword).  I have used many of the top scanners available today, including
  253. strobe by Julian Assange, netcat by *Hobbit*, stcp by Uriel Maimon, pscan by
  254. Pluvius, ident-scan by Dave Goldsmith, and the SATAN tcp/udp scanners by
  255. Wietse Venema.  These are all excellent scanners!  In fact, I ended up hacking
  256. most of them to support the best features of the others.  Finally I decided
  257. to write a whole new scanner, rather than rely on hacked versions of a dozen
  258. different scanners in my /usr/local/sbin.  While I wrote all the code, nmap
  259. uses a lot of good ideas from its predecessors.  I also incorporated some new
  260. stuff like fragmentation scanning and options which were on my "wish list" for
  261. other scanners.  Here are some of the (IMHO) useful features of nmap:
  262.  
  263. - dynamic delay time calculations: Some scanners require that you supply a
  264. delay time between sending packets.  Well how should I know what to use?
  265. Sure, I can ping them, but that is a pain, and plus the response time of many
  266. hosts changes dramatically when they are being flooded with requests.  nmap
  267. tries to determine the best delay time for you.  It also tries to keep track
  268. of packet retransmissions, etc. so that it can modify this delay time during
  269. the course of the scan.  For root users, the primary technique for finding an
  270. initial delay is to time the internal "ping" function.  For non-root users, it
  271. times an attempted connect() to a closed port on the target.  It can also pick
  272. a reasonable default value.  Again, people who want to specify a delay
  273. themselves can do so with -w (wait), but you shouldn't have to.
  274.  
  275. - retransmission: Some scanners just send out all the query packets, and
  276. collect the responses.  But this can lead to false positives or negatives in
  277. the case where packets are dropped.  This is especially important for
  278. "negative" style scans like UDP and FIN, where what you are looking for is a
  279. port that does NOT respond.  In most cases, nmap implements a configurable
  280. number of retransmissions for ports that don't respond.
  281.  
  282. - parallel port scanning:  Some scanners simply scan ports linearly, one at a
  283. time, until they do all 65535.  This actually works for TCP on a very fast 
  284. local network, but the speed of this is not at all acceptable on a wide area 
  285. network like the Internet.  nmap uses non-blocking i/o and parallel scanning 
  286. in all TCP and UDP modes.  The number of scans in parallel is configurable
  287. with the -M (Max sockets) option.  On a very fast network you will actually 
  288. decrease performance if you do more than 18 or so.  On slow networks, high
  289. values increase performance dramatically.
  290.  
  291. - Flexible port specification:  I don't always want to just scan all 65535
  292. ports.  Also, the scanners which only allow you to scan ports 1 - N sometimes 
  293. fall short of my need.  The -p option allows you to specify an arbitrary
  294. number of ports and ranges for scanning.  For example, '-p 21-25,80,113,
  295. 60000-' does what you would expect (a trailing hyphen means up to 65536, a
  296. leading hyphen means 1 through).  You can also use the -F (fast) option, which
  297. scans all the ports registered in your /etc/services (a la strobe).
  298.  
  299. - Flexible target specification:  I often want to scan more then one host,
  300. and I certainly don't want to list every single host on a large network to
  301. scan.  Everything that isn't an option (or option argument) in nmap is
  302. treated as a target host.  As mentioned before, you can optionally append
  303. /mask to a hostname or IP address in order to scan all hosts with the same
  304. initial <mask> bits of the 32 bit IP address.
  305.  
  306. - detection of down hosts:  Some scanners allow you to scan large networks, but
  307. they waste a huge amount of time scanning 65535 ports of a dead host!  By
  308. default, nmap pings each host to make sure it is up before wasting time on it.
  309. It is also capable of bailing on hosts which seem down based on strange port
  310. scanning errors.  It is also meant to be tolerant of people who accidently scan
  311. network addresses, broadcast addresses, etc.
  312.  
  313. - detection of your IP address: For some reason, a lot of scanners ask you to
  314. type in your IP address as one of the parameters.  Jeez, I don't want to have
  315. to 'ifconfig' and figure out my current address every time I scan.  Of course,
  316. this is better then the scanners I've seen which require recompilation every
  317. time you change your address!  nmap first tries to detect your address during
  318. the ping stage.  It uses the address that the echo response is received on, as
  319. that is the interface it should almost always be routed through.  If it can't
  320. do this (like if you don't have host pinging enabled), nmap tries to detect
  321. your primary interface and uses that address.  You can also use -S to specify
  322. it directly, but you shouldn't have to (unless you want to make it look like
  323. someone ELSE is SYN or FIN scanning a host.
  324.  
  325.  
  326. Some other, more minor options:
  327.  
  328.  -v (verbose): This is highly recommended for interactive use.  Among other
  329. useful messages, you will see ports come up as they are found, rather than
  330. having to wait for the sorted summary list.
  331.  
  332.  -r (randomize): This will randomize the order in which the target host's
  333. ports are scanned.
  334.  
  335.  -q (quash argv): This changes argv[0] to FAKE_ARGV ("pine" by default).
  336. It also eliminates all other arguments, so you won't look too suspicious in
  337. 'w' or 'ps' listings.
  338.  
  339.  -h for an options summary.
  340.  
  341. Also look for http://www.dhp.com/~fyodor/nmap/, which is the web site I plan to
  342. put future versions and more information on.  In fact, you would be well
  343. advised to check there right now.
  344.  
  345.  
  346. [ Greets ]
  347.  
  348. Of course this paper would not be complete without a shout out to all the 
  349. people who made it possible.
  350.  
  351. * Congratulations to the people at Phrack for getting this thing going again!
  352. * Greets to the whole dc-stuff crew.
  353. * Greets to the STUPH, Turntec, L0pht, TACD, the Guild, cDc, and all the other
  354.   groups who help keep the scene alive.
  355. * Shout out to _eci for disclosing the coolest Windows bug in recent history.
  356. * Thanks to the Data Haven Project (dhp.com) admins for providing such great
  357.   service for $10/month.  
  358. * And a special shout out goes to all my friends.  You know who
  359.   you are and some of you (wisely) stay out of the spotlight, so I'll keep you
  360.   anonymous ... except of course for Ken and Jay, and Avenger, Grog, Cash
  361.   Monies, Ethernet Kid, Zos, JuICe, Mother Prednisone, and Karen.
  362.  
  363.  
  364. And finally, we get to ...
  365.  
  366.  
  367. [ The code ]
  368.  
  369. This should compile fine on any Linux box with 'gcc -O6 -o nmap nmap.c -lm'.
  370. It is distrubuted under the terms of the GNU GENERAL PUBLIC LICENSE.  If you
  371. have problems or comments, feel free to mail me (fyodor@dhp.com).
  372.