/*************************************************************************** * nmap.cc -- Currently handles some of Nmap's port scanning features as * * well as the command line user interface. Note that the actual main() * * function is in main.cc * * * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2012 Insecure.Com LLC. Nmap is * * also a registered trademark of Insecure.Com LLC. This program is free * * software; you may redistribute and/or modify it under the terms of the * * GNU General Public License as published by the Free Software * * Foundation; Version 2 with the clarifications and exceptions described * * below. This guarantees your right to use, modify, and redistribute * * this software under certain conditions. If you wish to embed Nmap * * technology into proprietary software, we sell alternative licenses * * (contact sales@insecure.com). Dozens of software vendors already * * license Nmap technology such as host discovery, port scanning, OS * * detection, version detection, and the Nmap Scripting Engine. * * * * Note that the GPL places important restrictions on "derived works", yet * * it does not provide a detailed definition of that term. To avoid * * misunderstandings, we interpret that term as broadly as copyright law * * allows. For example, we consider an application to constitute a * * "derivative work" for the purpose of this license if it does any of the * * following: * * o Integrates source code from Nmap * * o Reads or includes Nmap copyrighted data files, such as * * nmap-os-db or nmap-service-probes. * * o Executes Nmap and parses the results (as opposed to typical shell or * * execution-menu apps, which simply display raw Nmap output and so are * * not derivative works.) * * o Integrates/includes/aggregates Nmap into a proprietary executable * * installer, such as those produced by InstallShield. * * o Links to a library or executes a program that does any of the above * * * * The term "Nmap" should be taken to also include any portions or derived * * works of Nmap, as well as other software we distribute under this * * license such as Zenmap, Ncat, and Nping. This list is not exclusive, * * but is meant to clarify our interpretation of derived works with some * * common examples. Our interpretation applies only to Nmap--we don't * * speak for other people's GPL works. * * * * If you have any questions about the GPL licensing restrictions on using * * Nmap in non-GPL works, we would be happy to help. As mentioned above, * * we also offer alternative license to integrate Nmap into proprietary * * applications and appliances. These contracts have been sold to dozens * * of software vendors, and generally include a perpetual license as well * * as providing for priority support and updates. They also fund the * * continued development of Nmap. Please email sales@insecure.com for * * further information. * * * * As a special exception to the GPL terms, Insecure.Com LLC grants * * permission to link the code of this program with any version of the * * OpenSSL library which is distributed under a license identical to that * * listed in the included docs/licenses/OpenSSL.txt file, and distribute * * linked combinations including the two. You must obey the GNU GPL in all * * respects for all of the code used other than OpenSSL. If you modify * * this file, you may extend this exception to your version of the file, * * but you are not obligated to do so. * * * * If you received these files with a written license agreement or * * contract stating terms other than the terms above, then that * * alternative license agreement takes precedence over these comments. * * * * Source is provided to this software because we believe users have a * * right to know exactly what a program is going to do before they run it. * * This also allows you to audit the software for security holes (none * * have been found so far). * * * * Source code also allows you to port Nmap to new platforms, fix bugs, * * and add new features. You are highly encouraged to send your changes * * to nmap-dev@insecure.org for possible incorporation into the main * * distribution. By sending these changes to Fyodor or one of the * * Insecure.Org development mailing lists, or checking them into the Nmap * * source code repository, it is understood (unless you specify otherwise) * * that you are offering the Nmap Project (Insecure.Com LLC) the * * unlimited, non-exclusive right to reuse, modify, and relicense the * * code. Nmap will always be available Open Source, but this is important * * because the inability to relicense code has caused devastating problems * * for other Free Software projects (such as KDE and NASM). We also * * occasionally relicense the code to third parties as discussed above. * * If you wish to specify special license conditions of your * * contributions, just say so when you send them. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License v2.0 for more details at * * http://www.gnu.org/licenses/gpl-2.0.html , or in the COPYING file * * included with Nmap. * * * ***************************************************************************/ /* $Id$ */ #include "nmap.h" #include "osscan.h" #include "osscan2.h" #include "scan_engine.h" #include "idle_scan.h" #include "timing.h" #include "NmapOps.h" #include "MACLookup.h" #include "traceroute.h" #include "nmap_tty.h" #include "nmap_dns.h" #include "services.h" #include "protocols.h" #include "targets.h" #include "TargetGroup.h" #include "service_scan.h" #include "charpool.h" #include "nmap_error.h" #include "utils.h" #include "xml.h" #ifndef NOLUA #include "nse_main.h" #endif #ifdef WIN32 #include "winfix.h" #include #endif #if HAVE_OPENSSL #include #endif /* To get the version number only. */ #ifdef WIN32 #include "libdnet-stripped/include/dnet_winconfig.h" #else #include "libdnet-stripped/include/config.h" #endif #define DNET_VERSION VERSION using namespace std; /* global options */ extern char *optarg; extern int optind; extern NmapOps o; /* option structure */ static bool target_needs_new_hostgroup(std::vector &targets, const Target *target); static void display_nmap_version(); /* A mechanism to save argv[0] for code that requires that. */ static const char *program_name = NULL; void set_program_name(const char *name) { program_name = name; } static const char *get_program_name(void) { return program_name; } /* parse the --scanflags argument. It can be a number >=0 or a string consisting of TCP flag names like "URGPSHFIN". Returns -1 if the argument is invalid. */ static int parse_scanflags(char *arg) { int flagval = 0; char *end = NULL; if (isdigit((int) (unsigned char) arg[0])) { flagval = strtol(arg, &end, 0); if (*end || flagval < 0 || flagval > 255) return -1; } else { if (strcasestr(arg, "FIN")) { flagval |= TH_FIN; } if (strcasestr(arg, "SYN")) { flagval |= TH_SYN; } if (strcasestr(arg, "RST") || strcasestr(arg, "RESET")) { flagval |= TH_RST; } if (strcasestr(arg, "PSH") || strcasestr(arg, "PUSH")) { flagval |= TH_PUSH; } if (strcasestr(arg, "ACK")) { flagval |= TH_ACK; } if (strcasestr(arg, "URG")) { flagval |= TH_URG; } if (strcasestr(arg, "ECE")) { flagval |= TH_ECE; } if (strcasestr(arg, "CWR")) { flagval |= TH_CWR; } if (strcasestr(arg, "ALL")) { flagval = 255; } if (strcasestr(arg, "NONE")) { flagval = 0; } } return flagval; } /* parse a URL stype ftp string of the form user:pass@server:portno */ static int parse_bounce_argument(struct ftpinfo *ftp, char *url) { char *p = url,*q, *s; if ((q = strrchr(url, '@'))) { /* we have user and/or pass */ *q++ = '\0'; if ((s = strchr(p, ':'))) { /* we have user AND pass */ *s++ = '\0'; strncpy(ftp->pass, s, 255); } else { /* we ONLY have user */ log_write(LOG_STDOUT, "Assuming %s is a username, and using the default password: %s\n", p, ftp->pass); } strncpy(ftp->user, p, 63); } else { q = url; } /* q points to beginning of server name */ if ((s = strchr(q, ':'))) { /* we have portno */ *s++ = '\0'; ftp->port = atoi(s); } strncpy(ftp->server_name, q, MAXHOSTNAMELEN); ftp->user[63] = ftp->pass[255] = ftp->server_name[MAXHOSTNAMELEN] = 0; return 1; } static void printusage(int rc) { printf("%s %s ( %s )\n" "Usage: nmap [Scan Type(s)] [Options] {target specification}\n" "TARGET SPECIFICATION:\n" " Can pass hostnames, IP addresses, networks, etc.\n" " Ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; 10.0.0-255.1-254\n" " -iL : Input from list of hosts/networks\n" " -iR : Choose random targets\n" " --exclude : Exclude hosts/networks\n" " --excludefile : Exclude list from file\n" "HOST DISCOVERY:\n" " -sL: List Scan - simply list targets to scan\n" " -sn: Ping Scan - disable port scan\n" " -Pn: Treat all hosts as online -- skip host discovery\n" " -PS/PA/PU/PY[portlist]: TCP SYN/ACK, UDP or SCTP discovery to given ports\n" " -PE/PP/PM: ICMP echo, timestamp, and netmask request discovery probes\n" " -PO[protocol list]: IP Protocol Ping\n" " -n/-R: Never do DNS resolution/Always resolve [default: sometimes]\n" " --dns-servers : Specify custom DNS servers\n" " --system-dns: Use OS's DNS resolver\n" " --traceroute: Trace hop path to each host\n" "SCAN TECHNIQUES:\n" " -sS/sT/sA/sW/sM: TCP SYN/Connect()/ACK/Window/Maimon scans\n" " -sU: UDP Scan\n" " -sN/sF/sX: TCP Null, FIN, and Xmas scans\n" " --scanflags : Customize TCP scan flags\n" " -sI : Idle scan\n" " -sY/sZ: SCTP INIT/COOKIE-ECHO scans\n" " -sO: IP protocol scan\n" " -b : FTP bounce scan\n" "PORT SPECIFICATION AND SCAN ORDER:\n" " -p : Only scan specified ports\n" " Ex: -p22; -p1-65535; -p U:53,111,137,T:21-25,80,139,8080,S:9\n" " -F: Fast mode - Scan fewer ports than the default scan\n" " -r: Scan ports consecutively - don't randomize\n" " --top-ports : Scan most common ports\n" " --port-ratio : Scan ports more common than \n" "SERVICE/VERSION DETECTION:\n" " -sV: Probe open ports to determine service/version info\n" " --version-intensity : Set from 0 (light) to 9 (try all probes)\n" " --version-light: Limit to most likely probes (intensity 2)\n" " --version-all: Try every single probe (intensity 9)\n" " --version-trace: Show detailed version scan activity (for debugging)\n" #ifndef NOLUA "SCRIPT SCAN:\n" " -sC: equivalent to --script=default\n" " --script=: is a comma separated list of \n" " directories, script-files or script-categories\n" " --script-args=: provide arguments to scripts\n" " --script-args-file=filename: provide NSE script args in a file\n" " --script-trace: Show all data sent and received\n" " --script-updatedb: Update the script database.\n" " --script-help=: Show help about scripts.\n" " is a comma separted list of script-files or\n" " script-categories.\n" " -sCS: equivalent to --script-suggest=all\n" " --script-suggest=: is a comma separated list of \n" " directories, script-files or script-categories\n" #endif "OS DETECTION:\n" " -O: Enable OS detection\n" " --osscan-limit: Limit OS detection to promising targets\n" " --osscan-guess: Guess OS more aggressively\n" "TIMING AND PERFORMANCE:\n" " Options which take