/*************************************************************************** * 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-2013 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 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS * * AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, * * modify, and redistribute this software under certain conditions. If * * you wish to embeed Nmap technology into proprietary software, we sell * * alternative licenses (contact sales@nmap.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 "derivative 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 with any software or content covered by this license * * ("Covered Software"): * * * * o Integrates source code from Covered Software. * * * * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db * * or nmap-service-probes. * * * * o Is designed specifically to execute Covered Software and parse the * * results (as opposed to typical shell or execution-menu apps, which will * * execute anything you tell them to). * * * * o Includes Covered Software in a proprietary executable installer. The * * installers produced by InstallShield are an example of this. Including * * Nmap with other software in compressed or archival form does not * * trigger this provision, provided appropriate open source decompression * * or de-archiving software is widely available for no charge. For the * * purposes of this license, an installer is considered to include Covered * * Software even if it actually retrieves a copy of Covered Software from * * another source during runtime (such as by downloading it from the * * Internet). * * * * o Links (statically or dynamically) to a library which does any of the * * above. * * * * o Executes a helper program, module, or script to do any of the above. * * * * This list is not exclusive, but is meant to clarify our interpretation * * of derived works with some common examples. Other people may interpret * * the plain GPL differently, so we consider this a special exception to * * the GPL that we apply to Covered Software. Works which meet any of * * these conditions must conform to all of the terms of this license, * * particularly including the GPL Section 3 requirements of providing * * source code and allowing free redistribution of the work as a whole. * * * * As another 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. * * * * Any redistribution of Covered Software, including any derived works, * * must obey and carry forward all of the terms of this license, including * * obeying all GPL rules and restrictions. For example, source code of * * the whole work must be provided and free redistribution must be * * allowed. All GPL references to "this License", are to be treated as * * including the terms and conditions of this license text as well. * * * * Because this license imposes special exceptions to the GPL, Covered * * Work may not be combined (even as part of a larger work) with plain GPL * * software. The terms, conditions, and exceptions of this license must * * be included as well. This license is incompatible with some other open * * source licenses as well. In some cases we can relicense portions of * * Nmap or grant special permissions to use it in other open source * * software. Please contact fyodor@nmap.org with any such requests. * * Similarly, we don't incorporate incompatible open source software into * * Covered Software without special permission from the copyright holders. * * * * If you have any questions about the licensing restrictions on using * * Nmap in other works, are 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@nmap.com for further * * information. * * * * If you have received a written license agreement or contract for * * Covered Software stating terms other than these, you may choose to use * * and redistribute Covered Software under those terms instead of these. * * * * 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 the dev@nmap.org mailing list 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 Nmap * * license file for more details (it's in a COPYING file included with * * Nmap, and also available from https://svn.nmap.org/nmap/COPYING * * * ***************************************************************************/ /* $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 "nmap_ftp.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 /* 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; } 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-separated list of script-files or\n" " 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