home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / nmap.h < prev    next >
C/C++ Source or Header  |  2002-02-25  |  15KB  |  455 lines

  1. /***********************************************************************/
  2. /* nmap.h -- Currently handles the bulk of Nmap's port scanning        */
  3. /* features as well as the command line user interface.  At some point */
  4. /* I hope to move the port scanning & related support functions to     */
  5. /* another file.                                                       */
  6. /*                                                                     */
  7. /***********************************************************************/
  8. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  9. /*  program is free software; you can redistribute it and/or modify    */
  10. /*  it under the terms of the GNU General Public License as published  */
  11. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  12. /*  right to use, modify, and redistribute this software under certain */
  13. /*  conditions.  If this license is unacceptable to you, we may be     */
  14. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  15. /*                                                                     */
  16. /*  If you received these files with a written license agreement       */
  17. /*  stating terms other than the (GPL) terms above, then that          */
  18. /*  alternative license agreement takes precendence over this comment. */
  19. /*                                                                     */
  20. /*  Source is provided to this software because we believe users have  */
  21. /*  a right to know exactly what a program is going to do before they  */
  22. /*  run it.  This also allows you to audit the software for security   */
  23. /*  holes (none have been found so far).                               */
  24. /*                                                                     */
  25. /*  Source code also allows you to port Nmap to new platforms, fix     */
  26. /*  bugs, and add new features.  You are highly encouraged to send     */
  27. /*  your changes to fyodor@insecure.org for possible incorporation     */
  28. /*  into the main distribution.  By sending these changes to Fyodor or */
  29. /*  one the insecure.org development mailing lists, it is assumed that */
  30. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  31. /*  reuse, modify, and relicense the code.  This is important because  */
  32. /*  the inability to relicense code has caused devastating problems    */
  33. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  34. /*  will always be available Open Source.  If you wish to specify      */
  35. /*  special license conditions of your contributions, just say so      */
  36. /*  when you send them.                                                */
  37. /*                                                                     */
  38. /*  This program is distributed in the hope that it will be useful,    */
  39. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  40. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  41. /*  General Public License for more details (                          */
  42. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  43. /*                                                                     */
  44. /***********************************************************************/
  45.  
  46. /* $Id: nmap.h,v 1.76 2001/10/14 09:38:26 fyodor Exp $ */
  47.  
  48. #ifndef NMAP_H
  49. #define NMAP_H
  50.  
  51. /************************INCLUDES**********************************/
  52.  
  53. #ifdef WIN32
  54. #include "mswin32\winclude.h"
  55. #endif
  56.  
  57. #ifdef HAVE_CONFIG_H
  58. #include "config.h"
  59. #else
  60. #ifdef WIN32
  61. #include "nmap_winconfig.h"
  62. #endif /* WIN32 */
  63. #endif /* HAVE_CONFIG_H */
  64.  
  65. #include <nbase.h>
  66.  
  67. #if HAVE_UNISTD_H
  68. #include <unistd.h>
  69. #endif
  70.  
  71. #ifdef STDC_HEADERS
  72. #include <stdlib.h>
  73. #else
  74. void *malloc();
  75. void *realloc();
  76. #endif
  77.  
  78. #if STDC_HEADERS || HAVE_STRING_H
  79. #include <string.h>
  80. #if !STDC_HEADERS && HAVE_MEMORY_H
  81. #include <memory.h>
  82. #endif
  83. #endif
  84. #if HAVE_STRINGS_H
  85. #include <strings.h>
  86. #endif
  87.  
  88. #ifdef HAVE_BSTRING_H
  89. #include <bstring.h>
  90. #endif
  91.  
  92. #include <ctype.h>
  93. #include <sys/types.h>
  94.  
  95. #ifdef __EMX__
  96. #include <sys/socket.h>
  97. #define INCL_DOS
  98. #include <os2.h>
  99. #define usleep(a) DosSleep(a * 1000)
  100. #define Strncpy strncpy
  101. #endif
  102.  
  103. #ifndef WIN32    //    from nmapNT -- seems to work
  104. #include <sys/wait.h>
  105. #endif /* !WIN32 */
  106.  
  107. #ifdef HAVE_SYS_PARAM_H   
  108. #include <sys/param.h> /* Defines MAXHOSTNAMELEN on BSD*/
  109. #endif
  110.  
  111. /* Linux uses these defines in netinet/ip.h and netinet/tcp.h to
  112.    use the correct struct ip and struct tcphdr */
  113. #ifndef __FAVOR_BSD
  114. #define __FAVOR_BSD
  115. #endif
  116. #ifndef __USE_BSD
  117. #define __USE_BSD
  118. #endif
  119. #ifndef __BSD_SOURCE
  120. #define __BSD_SOURCE
  121. #endif
  122.  
  123. /* BSDI needs this to insure the correct struct ip */
  124. #undef _IP_VHL
  125.  
  126. #if HAVE_STRINGS_H
  127. #include <strings.h>
  128. #endif
  129.  
  130. #include <stdio.h>
  131.  
  132. #if HAVE_RPC_TYPES_H
  133. #include <rpc/types.h>
  134. #endif
  135.  
  136. #if HAVE_SYS_SOCKET_H
  137. #include <sys/socket.h>
  138. #endif
  139.  
  140. #include <sys/stat.h>
  141.  
  142. #if HAVE_NETINET_IN_H
  143. #include <netinet/in.h>
  144. #endif
  145.  
  146. #include <errno.h>
  147.  
  148. #if HAVE_NETDB_H
  149. #include <netdb.h>
  150. #endif
  151.  
  152. #if TIME_WITH_SYS_TIME
  153. # include <sys/time.h>
  154. # include <time.h>
  155. #else
  156. # if HAVE_SYS_TIME_H
  157. #  include <sys/time.h>
  158. # else
  159. #  include <time.h>
  160. # endif
  161. #endif
  162.  
  163. #include <fcntl.h>
  164. #include <stdarg.h>
  165.  
  166. #ifdef HAVE_PWD_H
  167. #include <pwd.h>
  168. #endif
  169.  
  170. #ifndef NETINET_IN_SYSTEM_H  /* why the HELL does OpenBSD not do this? */
  171. #include <netinet/in_systm.h> /* defines n_long needed for netinet/ip.h */
  172. #define NETINET_IN_SYSTEM_H
  173. #endif
  174. #ifndef NETINET_IP_H  /* why the HELL does OpenBSD not do this? */
  175. #include <netinet/ip.h> 
  176. #define NETINET_IP_H
  177. #endif
  178. #include <netinet/ip_icmp.h> 
  179.  
  180. #if HAVE_ARPA_INET_H
  181. #include <arpa/inet.h>
  182. #endif
  183.  
  184. #include <math.h>
  185. #include <assert.h>
  186. #ifndef __FAVOR_BSD
  187. #define __FAVOR_BSD
  188. #endif
  189. #ifndef NETINET_TCP_H  /* why the HELL does OpenBSD not do this? */
  190. #include <netinet/tcp.h>          /*#include <netinet/ip_tcp.h>*/
  191. #define NETINET_TCP_H
  192. #endif
  193.  
  194. #if HAVE_SYS_RESOURCE_H
  195. #include <sys/resource.h>
  196. #endif
  197.  
  198. /*#include <net/if_arp.h> *//* defines struct arphdr needed for if_ether.h */
  199. #ifndef NET_IF_H  /* why the HELL does OpenBSD not do this? */
  200. #include <net/if.h>
  201. #define NET_IF_H
  202. #endif
  203. #if HAVE_NETINET_IF_ETHER_H 
  204. #ifndef NETINET_IF_ETHER_H
  205. #include <netinet/if_ether.h>
  206. #define NETINET_IF_ETHER_H
  207. #endif /* NETINET_IF_ETHER_H */
  208. #endif /* HAVE_NETINET_IF_ETHER_H */
  209.  
  210. /*******  DEFINES  ************/
  211.  
  212. /* User configurable #defines: */
  213. #ifndef VERSION
  214. #define VERSION "1.60-Beta"
  215. #endif
  216. #ifndef DEBUGGING
  217. #define DEBUGGING 0
  218. #endif
  219. /* Default number of ports in parallel.  Doesn't always involve actual 
  220.    sockets.  Can also adjust with the -M command line option.  */
  221. #define MAX_SOCKETS 36 
  222. /* As an optimisation we limit the maximum value of MAX_SOCKETS to a very
  223.    high value (avoids dynamic memmory allocation */
  224. #define MAX_SOCKETS_ALLOWED 1025
  225. /* How many hosts do we ping in parallel to see if they are up? */
  226. #define LOOKAHEAD 25
  227. /* If reads of a UDP port keep returning EAGAIN (errno 13), do we want to 
  228.    count the port as valid? */
  229. #define RISKY_UDP_SCAN 0
  230. /* How many syn packets do we send to TCP sequence a host? */
  231. #define NUM_SEQ_SAMPLES 6
  232.  /* This ideally should be a port that isn't in use for any protocol on our machine or on the target */ 
  233. #define MAGIC_PORT 49724
  234. /* How many udp sends without a ICMP port unreachable error does it take before we consider the port open? */
  235. #define UDP_MAX_PORT_RETRIES 4
  236.  /*How many seconds before we give up on a host being alive? */
  237.  
  238. #define FAKE_ARGV "pine" /* What ps and w should show if you use -q */
  239. /* How do we want to log into ftp sites for */ 
  240. #define FTPUSER "anonymous"
  241. #define FTPPASS "-wwwuser@"
  242. #define FTP_RETRIES 2 /* How many times should we relogin if we lose control
  243.                          connection? */
  244. #define MAX_TIMEOUTS MAX_SOCKETS   /* How many timed out connection attempts 
  245.                       in a row before we decide the host is 
  246.                       dead? */
  247. #define DEFAULT_TCP_PROBE_PORT 80 /* The port TCP probes go to if unspecified
  248.                       by user -- uber hackers change this
  249.                       to 113 */
  250.  
  251. #define MAX_DECOYS 128 /* How many decoys are allowed? */
  252.  
  253. #ifndef MAX_RTT_TIMEOUT
  254. #define MAX_RTT_TIMEOUT 10000 /* Never allow more than 10 secs for packet round
  255.                  trip */
  256. #endif
  257.  
  258. #ifndef MIN_RTT_TIMEOUT
  259. #define MIN_RTT_TIMEOUT 300 /* We will always wait at least 300 ms for a response */
  260. #endif
  261.  
  262. #define INITIAL_RTT_TIMEOUT 6000 /* Allow 6 seconds at first for packet responses */
  263. #define HOST_TIMEOUT    0 /* By default allow unlimited time to scan each host */
  264.  
  265. /* If nmap is called with one of the names below, it will start up in interactive mode -- alternatively, you can rename Nmap any of the following names to have it start up interactivey by default.  */
  266. #define INTERACTIVE_NAMES { "BitchX", "Calendar", "X", "awk", "bash", "bash2", "calendar", "cat", "csh", "elm", "emacs", "ftp", "fvwm", "g++", "gcc", "gimp", "httpd", "irc", "man", "mutt", "nc", "ncftp", "netscape", "perl", "pine", "ping", "sleep", "slirp", "ssh", "sshd", "startx", "tcsh", "telnet", "telnetd", "tia", "top", "vi", "vim", "xdvi", "xemacs", "xterm", "xv" }
  267.  
  268. /* Number of hosts we pre-ping and then scan.  We do a lot more if
  269.    randomize_hosts is set.  Every one you add to this leads to ~1K of
  270.    extra always-resident memory in nmap */
  271. #define HOST_GROUP_SZ 256
  272.  
  273. /* DO NOT change stuff after this point */
  274. #define UC(b)   (((int)b)&0xff)
  275. #define SA    struct sockaddr  /*Ubertechnique from R. Stevens */
  276. /*#define fatal(x) { fprintf(stderr, "%s\n", x); exit(-1); }
  277.   #define error(x) fprintf(stderr, "%s\n", x);*/
  278. /* hoststruct->flags stuff */
  279. #define HOST_UP 1
  280. #define HOST_DOWN 2 
  281. #define HOST_FIREWALLED 4 
  282. #define HOST_BROADCAST 8 /* use the wierd_responses member of hoststruct instead */
  283.  
  284. #define PINGTYPE_UNKNOWN 0
  285. #define PINGTYPE_NONE 1
  286. #define PINGTYPE_ICMP 2
  287. #define PINGTYPE_TCP  4
  288. #define PINGTYPE_TCP_USE_ACK 8
  289. #define PINGTYPE_TCP_USE_SYN 16
  290. #define PINGTYPE_RAWTCP 32
  291. #define PINGTYPE_CONNECTTCP 64
  292.  
  293. /* TCP/IP ISN sequence prediction classes */
  294. #define SEQ_UNKNOWN 0
  295. #define SEQ_64K 1
  296. #define SEQ_TD 2
  297. #define SEQ_RI 4
  298. #define SEQ_TR 8
  299. #define SEQ_i800 16
  300. #define SEQ_CONSTANT 32
  301.  
  302. /* TCP Timestamp Sequence */
  303. #define TS_SEQ_UNKNOWN 0
  304. #define TS_SEQ_ZERO 1 /* At least one of the timestamps we received back was 0 */
  305. #define TS_SEQ_2HZ 2
  306. #define TS_SEQ_100HZ 3
  307. #define TS_SEQ_1000HZ 4
  308. #define TS_SEQ_UNSUPPORTED 5 /* System didn't send back a timestamp */
  309.  
  310. #define IPID_SEQ_UNKNOWN 0
  311. #define IPID_SEQ_INCR 1  /* simple increment by one each time */
  312. #define IPID_SEQ_BROKEN_INCR 2 /* Stupid MS -- forgot htons() so it 
  313.                                   counts by 256 on little-endian platforms */
  314. #define IPID_SEQ_RPI 3 /* Goes up each time but by a "random" positive 
  315.                           increment */
  316. #define IPID_SEQ_RD 4 /* Appears to select IPID using a "random" distributions (meaning it can go up or down) */
  317. #define IPID_SEQ_CONSTANT 5 /* Contains 1 or more sequential duplicates */
  318. #define IPID_SEQ_ZERO 6 /* Every packet that comes back has an IP.ID of 0 (eg Linux 2.4 does this) */
  319. #ifndef MAXHOSTNAMELEN
  320. #define MAXHOSTNAMELEN 64
  321. #endif
  322.  
  323. #ifndef BSDFIX
  324. #if FREEBSD || BSDI || NETBSD
  325. #define BSDFIX(x) x
  326. #define BSDUFIX(x) x
  327. #else
  328. #define BSDFIX(x) htons(x)
  329. #define BSDUFIX(x) ntohs(x)
  330. #endif
  331. #endif /* BSDFIX */
  332.  
  333. /* Funny story about this one in /usr/include/apache/ap_config.h */
  334. #if defined(AIX)
  335.   #if AIX >= 42
  336.   #define NET_SIZE_T size_t
  337.   #endif
  338. #elif defined(LINUX)
  339.   #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 0))
  340.   #define NET_SIZE_T socklen_t
  341.   #endif
  342. #elif defined(SEQUENT)
  343.   #if SEQUENT < 44
  344.     #define NO_KILLPG 1
  345.     #define NET_SIZE_T int
  346.   #endif
  347.   #if SEQUENT >= 44
  348.     #undef NO_KILLPG
  349.     #define NET_SIZE_T size_t
  350.   #endif
  351. #elif defined(SVR4)
  352.   #define NET_SIZE_T size_t
  353. #elif defined(UW)
  354.   #define NET_SIZE_T size_t
  355. #elif defined(__FreeBSD__)
  356.   /* XXX: Apache didn't have this one,
  357.           so watch it be wrong :)... */
  358.   #define NET_SIZE_T size_t
  359. #elif defined(OS390)
  360.   #define NET_SIZE_T size_t
  361. #endif
  362.  
  363. #ifndef NET_SIZE_T
  364.   #define NET_SIZE_T int
  365. #endif
  366.  
  367. /********************** LOCAL INCLUDES *****************************/
  368.  
  369.  
  370. #include "output.h"
  371. #include "portlist.h"
  372. #include "tcpip.h"
  373. #include "global_structures.h"
  374. #include "nmap_error.h"
  375. #include "utils.h"
  376. #include "services.h"
  377. #include "protocols.h"
  378. #include "nmap_rpc.h"
  379. #include "targets.h"
  380.  
  381.  
  382. /***********************STRUCTURES**********************************/
  383.  
  384. /* Moved to global_structures.h */
  385.  
  386. /***********************PROTOTYPES**********************************/
  387.  
  388. /* print usage information and exit */
  389. void printusage(char *name, int rc);
  390. /* print Interactive usage information */
  391. void printinteractiveusage();
  392.  
  393. /* Scan helper functions */
  394. unsigned long calculate_sleep(struct in_addr target);
  395. int check_ident_port(struct in_addr target);
  396.  
  397. int ftp_anon_connect(struct ftpinfo *ftp);
  398.  
  399. /* port manipulators */
  400. struct scan_lists *getpts(char *expr); /* someone stole the name getports()! */
  401. int getidentinfoz(struct in_addr target, u16 localport, u16 remoteport,
  402.           char *owner, int ownersz);
  403.  
  404. /* socket manipulation functions */
  405. void init_socket(int sd);
  406. int recvtime(int sd, char *buf, int len, int seconds);
  407.  
  408. /* RAW packet building/dissasembling stuff */
  409. int isup(struct in_addr target);
  410. int listen_icmp(int icmpsock, unsigned short outports[],
  411.         unsigned short numtries[], int *num_out,
  412.         struct in_addr target, portlist *ports);
  413.  
  414. /* Renamed main so that interactive mode could preprocess when neccessary */
  415. int nmap_main(int argc, char *argv[]);
  416.  
  417. /* general helper functions */
  418. void *safe_malloc(int size);
  419. char *grab_next_host_spec(FILE *inputfd, int argc, char **fakeargv);
  420. int parse_targets(struct targets *targets, char *h);
  421. void options_init();
  422. char *statenum2str(int state);
  423. char *scantype2str(stype scantype);
  424. void sigdie(int signo);
  425. void reaper(int signo);
  426. char *seqreport(struct seq_info *seq);
  427. char *seqclass2ascii(int clas);
  428. char *ipidclass2ascii(int seqclass);
  429. char *tsseqclass2ascii(int seqclass);
  430.  
  431. /* Convert a TCP sequence prediction difficulty index like 1264386
  432.    into a difficulty string like "Worthy Challenge */
  433. const char *seqidx2difficultystr(unsigned long idx);
  434. int nmap_fetchfile(char *filename_returned, int bufferlen, char *file);
  435. int fileexistsandisreadable(char *pathname);
  436. int check_firewallmode(struct hoststruct *target, struct scanstats *ss);
  437. int gather_logfile_resumption_state(char *fname, int *myargc, char ***myargv);
  438.  
  439. /* From glibc 2.0.6 because Solaris doesn't seem to have this function */
  440. #ifndef HAVE_INET_ATON
  441. int inet_aton(register const char *, struct in_addr *);
  442. #endif
  443.  
  444. /* Sets a pcap filter function -- makes SOCK_RAW reads easier */
  445. #ifndef WINIP_H
  446. typedef int (*PFILTERFN)(const char *packet, int len); /* 1 to keep */
  447. void set_pcap_filter(struct hoststruct *target, pcap_t *pd, PFILTERFN filter, char *bpf, ...);
  448. #endif
  449.  
  450. int flt_icmptcp(const char *packet, int len);
  451. int flt_icmptcp_2port(const char *packet, int len);
  452. int flt_icmptcp_5port(const char *packet, int len);
  453.  
  454. #endif /* NMAP_H */
  455.