nmap.cc

来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,556 行 · 第 1/5 页

CC
1,556
字号
/*************************************************************************** * 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-2006 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, and version detection.                                       * *                                                                         * * 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 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-fingerprints 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.  This list is not exclusive, but is just meant to        * * clarify our interpretation of derived works with some common examples.  * * These restrictions only apply when you actually redistribute Nmap.  For * * example, nothing stops you from writing and selling a proprietary       * * front-end to Nmap.  Just distribute it by itself, and point people to   * * http://insecure.org/nmap/ to download Nmap.                             * *                                                                         * * We don't consider these to be added restrictions on top of the GPL, but * * just a clarification of how we interpret "derived works" as it applies  * * to our GPL-licensed Nmap product.  This is similar to the way Linus     * * Torvalds has announced his interpretation of how "derived works"        * * applies to Linux kernel modules.  Our interpretation refers only to     * * Nmap - we don't speak for any other GPL products.                       * *                                                                         * * 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 as well as helping to     * * fund the continued development of Nmap technology.  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 Copying.OpenSSL 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 fyodor@insecure.org for possible incorporation into the main         * * distribution.  By sending these changes to Fyodor or one the            * * Insecure.Org development mailing lists, it is assumed that you are      * * offering Fyodor and 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 for more details at                              * * http://www.gnu.org/copyleft/gpl.html , or in the COPYING file included  * * with Nmap.                                                              * *                                                                         * ***************************************************************************//* $Id: nmap.cc 4069 2006-10-14 06:02:43Z fyodor $ */#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 "nmap_tty.h"#include "nmap_dns.h"#ifdef WIN32#include "winfix.h"#endifusing namespace std;/* global options */extern char *optarg;extern int optind;extern NmapOps o;  /* option structure *//* 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(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;    }   }  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(char *name, 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 <inputfilename>: Input from list of hosts/networks\n"       "  -iR <num hosts>: Choose random targets\n"       "  --exclude <host1[,host2][,host3],...>: Exclude hosts/networks\n"       "  --excludefile <exclude_file>: Exclude list from file\n"       "HOST DISCOVERY:\n"       "  -sL: List Scan - simply list targets to scan\n"       "  -sP: Ping Scan - go no further than determining if host is online\n"       "  -P0: Treat all hosts as online -- skip host discovery\n"       "  -PS/PA/PU [portlist]: TCP SYN/ACK or UDP discovery to given ports\n"       "  -PE/PP/PM: ICMP echo, timestamp, and netmask request discovery probes\n"       "  -n/-R: Never do DNS resolution/Always resolve [default: sometimes]\n"       "  --dns-servers <serv1[,serv2],...>: Specify custom DNS servers\n"       "  --system-dns: Use OS's DNS resolver\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 <flags>: Customize TCP scan flags\n"       "  -sI <zombie host[:probeport]>: Idlescan\n"       "  -sO: IP protocol scan\n"       "  -b <ftp relay host>: FTP bounce scan\n"       "PORT SPECIFICATION AND SCAN ORDER:\n"       "  -p <port ranges>: Only scan specified ports\n"       "    Ex: -p22; -p1-65535; -p U:53,111,137,T:21-25,80,139,8080\n"       "  -F: Fast - Scan only the ports listed in the nmap-services file)\n"       "  -r: Scan ports consecutively - don't randomize\n"       "SERVICE/VERSION DETECTION:\n"       "  -sV: Probe open ports to determine service/version info\n"       "  --version-intensity <level>: 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"       "OS DETECTION:\n"       "  -O: Enable OS detection (try 2nd generation w/fallback to 1st)\n"       "  -O2: Only use the new OS detection system (no fallback)\n"       "  -O1: Only use the old (1st generation) OS detection system\n"       "  --osscan-limit: Limit OS detection to promising targets\n"       "  --osscan-guess: Guess OS more aggressively\n"       "TIMING AND PERFORMANCE:\n"       "  Options which take <time> are in milliseconds, unless you append 's'\n"       "  (seconds), 'm' (minutes), or 'h' (hours) to the value (e.g. 30m).\n"       "  -T[0-5]: Set timing template (higher is faster)\n"       "  --min-hostgroup/max-hostgroup <size>: Parallel host scan group sizes\n"       "  --min-parallelism/max-parallelism <time>: Probe parallelization\n"       "  --min-rtt-timeout/max-rtt-timeout/initial-rtt-timeout <time>: Specifies\n"       "      probe round trip time.\n"       "  --max-retries <tries>: Caps number of port scan probe retransmissions.\n"       "  --host-timeout <time>: Give up on target after this long\n"       "  --scan-delay/--max-scan-delay <time>: Adjust delay between probes\n"       "FIREWALL/IDS EVASION AND SPOOFING:\n"       "  -f; --mtu <val>: fragment packets (optionally w/given MTU)\n"       "  -D <decoy1,decoy2[,ME],...>: Cloak a scan with decoys\n"       "  -S <IP_Address>: Spoof source address\n"       "  -e <iface>: Use specified interface\n"       "  -g/--source-port <portnum>: Use given port number\n"       "  --data-length <num>: Append random data to sent packets\n"       "  --ip-options <options>: Send packets with specified ip options\n"       "  --ttl <val>: Set IP time-to-live field\n"       "  --spoof-mac <mac address/prefix/vendor name>: Spoof your MAC address\n"       "  --badsum: Send packets with a bogus TCP/UDP checksum\n"       "OUTPUT:\n"       "  -oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,\n"       "     and Grepable format, respectively, to the given filename.\n"       "  -oA <basename>: Output in the three major formats at once\n"       "  -v: Increase verbosity level (use twice for more effect)\n"       "  -d[level]: Set or increase debugging level (Up to 9 is meaningful)\n"       "  --open: Only show open (or possibly open) ports\n"       "  --packet-trace: Show all packets sent and received\n"       "  --iflist: Print host interfaces and routes (for debugging)\n"       "  --log-errors: Log errors/warnings to the normal-format output file\n"       "  --append-output: Append to rather than clobber specified output files\n"       "  --resume <filename>: Resume an aborted scan\n"       "  --stylesheet <path/URL>: XSL stylesheet to transform XML output to HTML\n"       "  --webxml: Reference stylesheet from Insecure.Org for more portable XML\n"       "  --no-stylesheet: Prevent associating of XSL stylesheet w/XML output\n"       "MISC:\n"       "  -6: Enable IPv6 scanning\n"       "  -A: Enables OS detection and Version detection\n"       "  --datadir <dirname>: Specify custom Nmap data file location\n"       "  --send-eth/--send-ip: Send using raw ethernet frames or IP packets\n"       "  --privileged: Assume that the user is fully privileged\n"       "  --unprivileged: Assume the user lacks raw socket privileges\n"       "  -V: Print version number\n"       "  -h: Print this help summary page.\n"       "EXAMPLES:\n"       "  nmap -v -A scanme.nmap.org\n"       "  nmap -v -sP 192.168.0.0/16 10.0.0.0/8\n"       "  nmap -v -iR 10000 -P0 -p 80\n"       "SEE THE MAN PAGE FOR MANY MORE OPTIONS, DESCRIPTIONS, AND EXAMPLES\n", NMAP_NAME, NMAP_VERSION, NMAP_URL);  exit(rc);}/** * Returns 1 if this is a reserved IP address, where "reserved" means * either a private address, non-routable address, or even a non-reserved * but unassigned address which has an extremely high probability of being * black-holed. * * We try to optimize speed when ordering the tests. This optimization * assumes that all byte values are equally likely in the input. * * Warning: This function could easily become outdated if the IANA * starts to assign some more IPv4 ranges to RIPE, etc. as they have * started doing this year (2001), for example 80.0.0.0/4 used to be * completely unassigned until they gave 80.0.0.0/7 to RIPE in April * 2001 (www.junk.org is an example of a new address in this range). * * Check <http://www.iana.org/assignments/ipv4-address-space> for * the most recent assigments and * <http://www.cymru.com/Documents/bogon-bn-nonagg.txt> for bogon * netblocks. */static int ip_is_reserved(struct in_addr *ip){  char *ipc = (char *) &(ip->s_addr);  unsigned char i1 = ipc[0], i2 = ipc[1], i3 = ipc[2], i4 = ipc[3];  /* do all the /7's and /8's with a big switch statement, hopefully the

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?