scan_engine.cc

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

CC
1,755
字号
/*************************************************************************** * scan_engine.cc -- Includes much of the "engine" functions for scanning, * * such as pos_scan and ultra_scan.  It also includes dependant functions  * * such as those for collecting SYN/connect scan responses.                * *                                                                         * ***********************IMPORTANT NMAP LICENSE TERMS************************ *                                                                         * * The Nmap Security Scanner is (C) 1996-2008 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: scan_engine.cc 6633 2007-12-22 06:32:03Z fyodor $ */#ifdef WIN32#include "nmap_winconfig.h"#endif#include "portreasons.h"#include <dnet.h>#include "scan_engine.h"#include "timing.h"#include "NmapOps.h"#include "nmap_tty.h"#include "nmap_rpc.h"#include "Target.h"#include "targets.h"#include "utils.h"#include <list>using namespace std;extern NmapOps o;class UltraScanInfo;struct ultra_scan_performance_vars {  int low_cwnd;  /* The lowest cwnd (congestion window) allowed */  int host_initial_cwnd; /* Initial congestion window for ind. hosts */  int group_initial_cwnd; /* Initial congestion window for all hosts as a group */  int max_cwnd; /* I should never have more than this many probes		   outstanding */  int quick_incr; /* How many probes are incremented for each response		     in quick start mode */  int cc_incr; /* How many probes are incremented per (roughly) rtt in 		  congestion control mode */  int cc_scale_max; /* The maximum scaling factor for congestion window                       increments. */  int initial_ccthresh;  /* When a successful ping response comes back, it counts as this many     "normal" responses, because the fact that pings are neccessary means     we aren't getting much input. */  int ping_magnifier;/* Try to send a scanping if no response has been received from a target host   in this many usecs */  int pingtime;   double group_drop_cwnd_divisor; /* all-host group cwnd divided by this				     value if any packet drop occurs */  double group_drop_ccthresh_divisor; /* used to drop the group ccthresh when					 any drop occurs */  double host_drop_ccthresh_divisor; /* used to drop the host ccthresh when					 any drop occurs */  int tryno_cap; /* The maximum trynumber (starts at zero) allowed */};/* Some of the algorithms used here are TCP congestion control   techniques from RFC2581. */struct ultra_timing_vals {  double cwnd; /* Congestion window - in probes */  int ccthresh; /* The threshold after which mode is changed from QUICK_START		   to CONGESTION_CONTROL */  int num_updates; /* Number of updates to this utv (generally packet receipts ) */  /* Last time values were adjusted for a drop (you usually only want     to adjust again based on probes sent after that adjustment so a     sudden batch of drops doesn't destroy timing.  Init to now */  struct timeval last_drop; };struct probespec_tcpdata {  u16 dport;  u8 flags;};struct probespec_udpdata {  u16 dport;};struct probespec_icmpdata {  u8 type;  u8 code;};#define PS_NONE 0#define PS_TCP 1#define PS_UDP 2#define PS_PROTO 3#define PS_ICMP 4#define PS_ARP 5#define PS_CONNECTTCP 6static const char *pspectype2ascii(int type) {  switch(type) {  case PS_NONE:    return "NONE";  case PS_TCP:    return "TCP";  case PS_UDP:    return "UDP";  case PS_PROTO:    return "IP Proto";  case PS_ICMP:    return "ICMP";  case PS_ARP:    return "ARP";  case PS_CONNECTTCP:    return "connect";  default:    fatal("%s: Unknown type: %d", __func__, type);  }  return ""; // Unreached}/* The size of this structure is critical, since there can be tens of   thousands of them stored together ... */typedef struct probespec {  /* To save space, I changed this from private enum (took 4 bytes) to     u8 that uses #defines above */  u8 type;  u8 proto; /* If not PS_ARP -- Protocol number ... eg IPPROTO_TCP, etc. */  union {    struct probespec_tcpdata tcp; /* If type is PS_TCP or PS_CONNECTTCP. */    struct probespec_udpdata udp; /* PS_UDP */    struct probespec_icmpdata icmp; /* PS_ICMP */    /* Nothing needed for PS_ARP, since src mac and target IP are       avail from target structure anyway */  } pd;} probespec;class ConnectProbe {public:  ConnectProbe();  ~ConnectProbe();  int sd; /* Socket descriptor used for connection.  -1 if not valid. */};struct IPExtraProbeData_tcp {  u16 sport;  u32 seq; /* host byte order (like the other fields */};struct IPExtraProbeData_udp {  u16 sport;};struct IPExtraProbeData {  u16 ipid; /* host byte order */  union {    struct IPExtraProbeData_tcp tcp;    struct IPExtraProbeData_udp udp;  } pd;};/* At least for now, I'll just use this like a struct and access   all the data members directly */class UltraProbe {public:  UltraProbe();  ~UltraProbe();  enum UPType { UP_UNSET, UP_IP, UP_CONNECT, UP_RPC, UP_ARP } type; /* The type of probe this is */  /* Sets this UltraProbe as type UP_IP and creates & initializes the     internal IPProbe.  The relevent probespec is necessary for setIP     because pspec.type is ambiguous with just the ippacket (e.g. a     tcp packet could be PS_PROTO or PS_TCP). */  void setIP(u8 *ippacket, u32 iplen, const probespec *pspec);  /* Sets this UltraProbe as type UP_CONNECT, preparing to connect to given   port number*/  void setConnect(u16 portno);  /* Pass an arp packet, including ethernet header. Must be 42bytes */  void setARP(u8 *arppkt, u32 arplen);  // The 4 accessors below all return in HOST BYTE ORDER// source port used if TCP or UDP  u16 sport() {    return (mypspec.proto == IPPROTO_TCP)? probes.IP.pd.tcp.sport : probes.IP.pd.udp.sport; }  // destination port used if TCP or UDP  u16 dport() {     return (mypspec.proto == IPPROTO_TCP)? mypspec.pd.tcp.dport : mypspec.pd.udp.dport; }  u16 ipid() { return probes.IP.ipid; }  u32 tcpseq(); // TCP sequence number if protocol is TCP  /* Number, such as IPPROTO_TCP, IPPROTO_UDP, etc. */  u8 protocol() { return mypspec.proto; }  ConnectProbe *CP() { return probes.CP; } // if type == UP_CONNECT  // Arpprobe removed because not used.  //  ArpProbe *AP() { return probes.AP; } // if UP_ARP  // Returns the protocol number, such as IPPROTO_TCP, or IPPROTO_UDP, by   // reading the appropriate fields of the probespec./* Get general details about the probe */  const probespec *pspec() { return &mypspec; }  /* Returns true if the given tryno and pingseq match those within this     probe. */  bool check_tryno_pingseq(unsigned int tryno, unsigned int pingseq) {    return pingseq == 0 && tryno >= this->tryno || pingseq > 0 && pingseq == this->pingseq;  }  u8 tryno; /* Try (retransmission) number of this probe */  u8 pingseq; /* 0 if this is not a scanping. Otherwise a posative ping seq#. *//* If true, probe is considered no longer active due to timeout, but it   may be kept around a while, just in case a reply comes late */  bool timedout;/* A packet may be timedout for a while before being retransmitted due to   packet sending rate limitations */  bool retransmitted;   struct timeval sent;  /* Time the previous probe was sent, if this is a retransmit (tryno > 0) */  struct timeval prevSent;   bool isPing() { return pingseq > 0; }private:  probespec mypspec; /* Filled in by the appropriate set* function */  union {    IPExtraProbeData IP;    ConnectProbe *CP;    //    ArpProbe *AP;  } probes;  void *internalProbe;};/* Global info for the connect scan */class ConnectScanInfo {public:  ConnectScanInfo();  ~ConnectScanInfo();/* Watch a socket descriptor (add to fd_sets and maxValidSD).  Returns   true if the SD was absent from the list, false if you tried to   watch an SD that was already being watched. */  bool watchSD(int sd);   /* Clear SD from the fd_sets and maxValidSD.  Returns true if the SD   was in the list, false if you tried to clear an sd that wasn't   there in the first place. */  bool clearSD(int sd);   int maxValidSD; /* The maximum socket descriptor in any of the fd_sets */  fd_set fds_read;  fd_set fds_write;  fd_set fds_except;  int numSDs; /* Number of socket descriptors being watched */  int maxSocketsAllowed; /* No more than this many sockets may be created @once */};class HostScanStats;/* These are ultra_scan() statistics for the whole group of Targets */class GroupScanStats {public:  struct timeval timeout; /* The time at which we abort the scan */  /* Most recent host tested for sendability */  struct sockaddr_storage latestip;   GroupScanStats(UltraScanInfo *UltraSI);  ~GroupScanStats();  /* Returns true if the GLOBAL system says that sending is OK. */  bool sendOK();   /* Total # of probes outstanding (active) for all Hosts */

⌨️ 快捷键说明

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