/*************************************************************************** * 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-2020 Insecure.Com LLC ("The Nmap * * Project"). Nmap is also a registered trademark of the Nmap Project. * * * * This program is distributed under the terms of the Nmap Public Source * * License (NPSL). The exact license text applying to a particular Nmap * * release or source code control revision is contained in the LICENSE * * file distributed with that version of Nmap or source code control * * revision. More Nmap copyright/legal information is available from * * https://nmap.org/book/man-legal.html, and further information on the * * NPSL license itself can be found at https://nmap.org/npsl. This header * * summarizes some key points from the Nmap license, but is no substitute * * for the actual license text. * * * * Nmap is generally free for end users to download and use themselves, * * including commercial use. It is available from https://nmap.org. * * * * The Nmap license generally prohibits companies from using and * * redistributing Nmap in commercial products, but we sell a special Nmap * * OEM Edition with a more permissive license and special features for * * this purpose. See https://nmap.org/oem * * * * If you have received a written Nmap license agreement or contract * * stating terms other than these (such as an Nmap OEM license), you may * * choose to use and redistribute Nmap under those terms instead. * * * * The official Nmap Windows builds include the Npcap software * * (https://npcap.org) for packet capture and transmission. It is under * * separate license terms which forbid redistribution without special * * permission. So the official Nmap Windows builds may not be * * redistributed without special permission (such as an Nmap OEM * * license). * * * * 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. * * * * Source code also allows you to port Nmap to new platforms, fix bugs, * * and add new features. You are highly encouraged to submit your * * changes as a Github PR or by email to the dev@nmap.org mailing list * * for possible incorporation into the main distribution. Unless you * * specify otherwise, it is understood that you are offering us very * * broad rights to use your submissions as described in the Nmap Public * * Source License Contributor Agreement. This is important because we * * fund the project by selling licenses with various terms, and also * * because the inability to relicense code has caused devastating * * problems for other Free Software projects (such as KDE and NASM). * * * * The free version of Nmap 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. Warranties, * * indemnification and commercial support are all available through the * * Npcap OEM program--see https://nmap.org/oem. * * * ***************************************************************************/ /* $Id$ */ #ifdef WIN32 #include "winfix.h" /* This name collides in the following include. */ #undef PS_NONE #include #endif #include "nmap.h" #include "osscan.h" #include "scan_engine.h" #include "FPEngine.h" #include "idle_scan.h" #include "NmapOps.h" #include "MACLookup.h" #include "traceroute.h" #include "nmap_tty.h" #include "nmap_ftp.h" #include "services.h" #include "targets.h" #include "tcpip.h" #include "NewTargets.h" #include "Target.h" #include "service_scan.h" #include "charpool.h" #include "nmap_error.h" #include "utils.h" #include "xml.h" #include "scan_lists.h" #ifndef NOLUA #include "nse_main.h" #endif #ifdef HAVE_SIGNAL #include #endif #include #ifdef HAVE_PWD_H #include #endif #ifndef IPPROTO_SCTP #include "libnetutil/netutil.h" #endif #if HAVE_OPENSSL #include #include #endif #if HAVE_LIBSSH2 #include #endif #if HAVE_LIBZ #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 #ifdef LINUX /* Check for Windows Subsystem for Linux (WSL) */ #include #endif #include #include #include /* global options */ extern char *optarg; extern int optind; extern NmapOps o; /* option structure */ 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() { 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" " --exclude-ports : Exclude the specified ports from scanning\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