targetgroup.cc

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

CC
516
字号
/*************************************************************************** * TargetGroup.cc -- The "TargetGroup" class holds a group of IP           * * addresses, such as those from a '/16' or '10.*.*.*' specification.  It  * * also has a trivial HostGroupState class which handles a bunch of        * * expressions that go into TargetGroup classes.                           * *                                                                         * ***********************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: TargetGroup.cc 6633 2007-12-22 06:32:03Z fyodor $ */#include "TargetGroup.h"#include "NmapOps.h"#include "nmap_error.h"extern NmapOps o;TargetGroup::TargetGroup() {  Initialize();}// Bring back (or start with) original statevoid TargetGroup::Initialize() {  targets_type = TYPE_NONE;  memset(addresses, 0, sizeof(addresses));  memset(current, 0, sizeof(current));  memset(last, 0, sizeof(last));  ipsleft = 0;}/* take the object back to the beginning without  (mdmcl) * reinitalizing the data structures */  int  TargetGroup::rewind() {  /* For netmasks we must set the current address to the   * starting address and calculate the ips by distance */  if (targets_type == IPV4_NETMASK) {      currentaddr = startaddr;      if (startaddr.s_addr <= endaddr.s_addr) { 	ipsleft = endaddr.s_addr - startaddr.s_addr + 1;	return 0;       }      else        assert(0);  }  /* For ranges, we easily set current to zero and calculate   * the ips by the number of values in the columns */  else if (targets_type == IPV4_RANGES) {    memset((char *)current, 0, sizeof(current));    ipsleft = (last[0] + 1) * (last[1] + 1) *      (last[2] + 1) * (last[3] + 1);    return 0;  }#if HAVE_IPV6  /* For IPV6 there is only one address, this function doesn't   * make much sence for IPv6 does it? */  else if (targets_type == IPV6_ADDRESS) {    ipsleft = 1;    return 0;  }#endif   /* If we got this far there must be an error, wrong type */  return -1;} /* Initializes (or reinitializes) the object with a new expression, such    as 192.168.0.0/16 , 10.1.0-5.1-254 , or fe80::202:e3ff:fe14:1102 .      Returns 0 for success */  int TargetGroup::parse_expr(const char * const target_expr, int af) {  int i=0,j=0,k=0;  int start, end;  char *r,*s, *target_net;  char *addy[5];  char *hostexp = strdup(target_expr);  struct hostent *target;  unsigned long longtmp;  int namedhost = 0;  if (targets_type != TYPE_NONE)    Initialize();  ipsleft = 0;  if (af == AF_INET) {      if (strchr(hostexp, ':'))      fatal("Invalid host expression: %s -- colons only allowed in IPv6 addresses, and then you need the -6 switch", hostexp);    /*struct in_addr current_in;*/    addy[0] = addy[1] = addy[2] = addy[3] = addy[4] = NULL;    addy[0] = r = hostexp;    /* First we break the expression up into the four parts of the IP address       + the optional '/mask' */    target_net = strtok(hostexp, "/");    s = strtok(NULL, "");    /* find the end of the token from hostexp */    netmask  = ( s ) ? atoi(s) : 32;    if ((int) netmask <= 0 || netmask > 32) {      error("Illegal netmask value (%d), must be /1 - /32 .  Assuming /32 (one host)", netmask);      netmask = 32;    }    for(i=0; *(hostexp + i); i++)       if (isupper((int) *(hostexp +i)) || islower((int) *(hostexp +i))) {	namedhost = 1;	break;      }    if (netmask != 32 || namedhost) {      targets_type = IPV4_NETMASK;      if (!inet_pton(AF_INET, target_net, &(startaddr))) {	if ((target = gethostbyname(target_net))) {          int count=0;	  memcpy(&(startaddr), target->h_addr_list[0], sizeof(struct in_addr));          while (target->h_addr_list[count]) count++;          if (count > 1)             error("Warning: Hostname %s resolves to %d IPs. Using %s.", target_net, count, inet_ntoa(*((struct in_addr *)target->h_addr_list[0])));	} else {	  error("Failed to resolve given hostname/IP: %s.  Note that you can't use '/mask' AND '1-4,7,100-' style IP ranges", target_net);	  free(hostexp);	  return 1;	}      }       longtmp = ntohl(startaddr.s_addr);      startaddr.s_addr = longtmp & (unsigned long) (0 - (1<<(32 - netmask)));      endaddr.s_addr = longtmp | (unsigned long)  ((1<<(32 - netmask)) - 1);      currentaddr = startaddr;      if (startaddr.s_addr <= endaddr.s_addr) { 	ipsleft = endaddr.s_addr - startaddr.s_addr + 1;	free(hostexp); 	return 0;       }      fprintf(stderr, "Host specification invalid");      free(hostexp);      return 1;    }    else {      targets_type = IPV4_RANGES;      i=0;      while(*++r) {	if (*r == '.' && ++i < 4) {	  *r = '\0';	  addy[i] = r + 1;	}	else if (*r != '*' && *r != ',' && *r != '-' && !isdigit((int)*r)) 	  fatal("Invalid character in  host specification.  Note in particular that square brackets [] are no longer allowed.  They were redundant and can simply be removed.");      }      if (i != 3) fatal("Invalid target host specification: %s", target_expr);            for(i=0; i < 4; i++) {	j=0;	do {	  s = strchr(addy[i],',');	  if (s) *s = '\0';	  if (*addy[i] == '*') { start = 0; end = 255; } 	  else if (*addy[i] == '-') {	    start = 0;	    if (!(addy[i] + 1)) end = 255;	    else end = atoi(addy[i]+ 1);	  }	  else {	    start = end = atoi(addy[i]);	    if ((r = strchr(addy[i],'-')) && *(r+1) ) end = atoi(r + 1);

⌨️ 快捷键说明

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