/*************************************************************************** * 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-2011 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-db 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 meant to clarify our * * interpretation of derived works with some common examples. Our * * interpretation applies only to Nmap--we don't speak for other people's * * GPL works. * * * * 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 docs/licenses/OpenSSL.txt 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 nmap-dev@insecure.org for possible incorporation into the main * * distribution. By sending these changes to Fyodor or one of the * * Insecure.Org development mailing lists, it is assumed 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 GNU * * General Public License v2.0 for more details at * * http://www.gnu.org/licenses/gpl-2.0.html , or in the COPYING file * * included with Nmap. * * * ***************************************************************************/ /* $Id$ */ #include "tcpip.h" #include "TargetGroup.h" #include "NmapOps.h" #include "nmap_error.h" #include "global_structures.h" #include "nbase_addrset.h" #include "libnetutil/netutil.h" extern NmapOps o; NewTargets *NewTargets::new_targets; TargetGroup::TargetGroup() { addrset_init(&target_addrset); this->addrset_finished = true; this->cur_elem = NULL; this->cur_elem_finished = true; this->last_host = false; } TargetGroup::~TargetGroup() { addrset_free(&target_addrset); } /* Add a new address block into the target_addrset. The specification can be 192.168.0.0/16 , 10.1.0-5.1-254 , or fe80::202:e3ff:fe14:1102 or something else. */ int TargetGroup::add_spec(const char *spec, int af, int dns) { int rc; rc = addrset_add_spec(&target_addrset, spec, af, dns, 0); if (rc == 0) return rc; this->addrset_finished = false; this->cur_elem = NULL; this->cur_elem_finished = true; return 1; } bool TargetGroup::cur_ip6_plusplus() { for (int i = 7; i >= 0; i--) { this->cur_ip6[i]++; if (this->cur_ip6[i] != 0) { break; } } struct sockaddr_storage cur_ss; struct sockaddr_in6 *cur_sin6 = (struct sockaddr_in6 *) &cur_ss; cur_sin6->sin6_family = AF_INET6; for (int i = 7; i >= 0; i--) { cur_sin6->sin6_addr.s6_addr16[i] = htons(this->cur_ip6[i]); } struct sockaddr_storage addr_ss; struct sockaddr_in6 *addr_sin6 = (struct sockaddr_in6 *) &addr_ss; addr_sin6->sin6_family = AF_INET6; addr_sin6->sin6_addr = this->cur_elem->u.ipv6.addr; uint16_t nbits; addr_mtob(&this->cur_elem->u.ipv6.mask, IP6_ADDR_LEN, &nbits); if (sockaddr_equal_netmask(&addr_ss, &cur_ss, nbits)) return true; return false; } bool TargetGroup::cur_ip4_plusplus() { int i; for (i = 3; i >= 0; i--) { if (this->cur_ip4[i] != this->end_ip4[i]) break; } if (i == -1) return false; /* cur_ip4++. */ for (i = 3; i >= 0; i--) { if (this->cur_ip4[i] < this->end_ip4[i]) { for (int j = this->cur_ip4[i] + 1; j <= this->end_ip4[i]; j++) { if (BIT_IS_SET(this->cur_elem->u.ipv4.bits[i], j)) { this->cur_ip4[i] = j; return true; } } } if (i > 0) this->cur_ip4[i] = this->begin_ip4[i]; } return false; } void TargetGroup::get_cur_host(struct sockaddr_storage *ss, size_t *sslen) { assert(ss); assert(sslen); assert(this->cur_elem); if (this->cur_elem->type == ADDRSET_TYPE_IPV4_BITVECTOR) { struct sockaddr_in *sin = (struct sockaddr_in *) ss; memset(sin, 0, sizeof(struct sockaddr_in)); sin->sin_family = AF_INET; *sslen = sizeof(struct sockaddr_in); #if HAVE_SOCKADDR_SA_LEN sin->sin_len = *sslen; #endif /* Set the IP to the current value of everything */ sin->sin_addr.s_addr = htonl(this->cur_ip4[0] << 24 | this->cur_ip4[1] << 16 | this->cur_ip4[2] << 8 | this->cur_ip4[3]); #ifdef HAVE_IPV6 } else if (this->cur_elem->type == ADDRSET_TYPE_IPV6_NETMASK) { struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss; memset(sin6, 0, sizeof(struct sockaddr_in6)); sin6->sin6_family = AF_INET6; *sslen = sizeof(struct sockaddr_in6); #if HAVE_SOCKADDR_SA_LEN sin6->sin_len = *sslen; #endif /* Set the IPv6 to the current value of everything */ for (int j=0; j<8; j++) { sin6->sin6_addr.s6_addr16[j] = htons(this->cur_ip6[j]); } #endif } } /* Returns the last given host, so that it will be given again next time get_next_host is called. Obviously, you should only call this if you have fetched at least 1 host since add_spec() was called */ void TargetGroup::return_last_host() { last_host = true; } /* Grab the next host from this addrset (if any) and updates its internal state to reflect that the IP was given out. Returns 0 and fills in ss if successful. ss must point to a pre-allocated sockaddr_storage structure */ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { if (last_host) { this->get_cur_host(ss, sslen); last_host = false; return 0; } if (this->addrset_finished) return -1; int i, j; if (!this->cur_elem_finished) { if (this->cur_elem->type == ADDRSET_TYPE_IPV4_BITVECTOR) { /* cur_ip4++. */ if (this->cur_ip4_plusplus() == false) this->cur_elem_finished = true; } else if (this->cur_elem->type == ADDRSET_TYPE_IPV6_NETMASK) { /* cur_ip6++. */ if (this->cur_ip6_plusplus() == false) this->cur_elem_finished = true; } } if (this->cur_elem_finished) { if (this->cur_elem == NULL) this->cur_elem = this->target_addrset.head; else this->cur_elem = this->cur_elem->next; if (this->cur_elem == NULL) { this->addrset_finished = true; return -1; } //Build the resolvedaddrs list from cur_elem->resolved_addrs_info. this->build_resolvedaddrs_list(); //Initiate address iterators. if (this->cur_elem->type == ADDRSET_TYPE_IPV4_BITVECTOR) { for (i = 0; i < 4; i++) { for (j = 0; j < 256; j++) { if (BIT_IS_SET(this->cur_elem->u.ipv4.bits[i], j)) { this->cur_ip4[i] = this->begin_ip4[i] = j; break; } } for (j = 255; j >= 0; j--) { if (BIT_IS_SET(this->cur_elem->u.ipv4.bits[i], j)) { this->end_ip4[i] = j; break; } } } this->cur_elem_finished = false; #ifdef HAVE_IPV6 } else if (this->cur_elem->type == ADDRSET_TYPE_IPV6_NETMASK) { for (i = 0; i < 8; i++) { this->cur_ip6[i] = ntohs(this->cur_elem->u.ipv6.addr.s6_addr16[i]); } this->cur_elem_finished = false; #endif } } get_cur_host(ss, sslen); return 0; } /* Returns true iff the given address is the one that was resolved to create the cur_elem. */ bool TargetGroup::is_resolved_address(const struct sockaddr_storage *ss) { if (this->cur_elem->resolvedname == NULL) return false; return sockaddr_storage_equal(&this->cur_elem->resolvedaddr, ss); } /* Return a string of the name or address that was resolved for this addrset element. */ char* TargetGroup::get_resolved_name(void) { return this->cur_elem->resolvedname; } /* Return true if the addrset is empty or all of the addresses have been iterated. */ bool TargetGroup::isaddrset_finished(void) { return this->addrset_finished; } /* Build the resolvedaddrs list from cur_elem->resolved_addrs_info. */ bool TargetGroup::build_resolvedaddrs_list() { struct addrinfo *addrs = this->cur_elem->resolved_addrs_info; struct addrinfo *addr; struct sockaddr_storage ss; if (addrs == NULL) return false; this->resolvedaddrs.clear(); for (addr = addrs; addr != NULL; addr = addr->ai_next) { ss.ss_family = AF_UNSPEC; if (addr->ai_addrlen > sizeof(ss)) { continue; } if (addr->ai_family == AF_INET) { memcpy(&ss, addr->ai_addr, addr->ai_addrlen); #ifdef HAVE_IPV6 } else if (addr->ai_family == AF_INET6) { memcpy(&ss, addr->ai_addr, addr->ai_addrlen); #endif } else { continue; } this->resolvedaddrs.push_back(ss); } return true; } /* Return the list of addresses that the name for this group resolved to, if it came from a name resolution. */ const std::list &TargetGroup::get_resolved_addrs() { return this->resolvedaddrs; } /* debug level for the adding target is: 3 */ NewTargets *NewTargets::get (void) { if (new_targets) return new_targets; new_targets = new NewTargets(); return new_targets; } NewTargets::NewTargets (void) { Initialize(); } void NewTargets::Initialize (void) { history.clear(); while(!queue.empty()) queue.pop(); } /* This private method is used to push new targets to the * queue. It returns the number of targets in the queue. */ unsigned long NewTargets::push (const char *target) { std::pair::iterator, bool> pair_iter; std::string tg(target); if (tg.length() > 0) { /* save targets in the scanned history here (NSE side). */ pair_iter = history.insert(tg); /* A new target */ if (pair_iter.second == true) { /* push target onto the queue for future scans */ queue.push(tg); if (o.debugging > 2) log_write(LOG_PLAIN, "New Targets: target %s pushed onto the queue.\n", tg.c_str()); } else { if (o.debugging > 2) log_write(LOG_PLAIN, "New Targets: target %s is already in the queue.\n", tg.c_str()); /* Return 1 when the target is already in the history cache, * this will prevent returning 0 when the target queue is * empty since no target was added. */ return 1; } } return queue.size(); } /* Reads a target from the queue and return it to be pushed * onto Nmap scan queue */ std::string NewTargets::read (void) { std::string str; /* check to see it there are targets in the queue */ if (!new_targets->queue.empty()) { str = new_targets->queue.front(); new_targets->queue.pop(); } return str; } void NewTargets::clear (void) { new_targets->history.clear(); } unsigned long NewTargets::get_number (void) { return new_targets->history.size(); } unsigned long NewTargets::get_scanned (void) { return new_targets->history.size() - new_targets->queue.size(); } unsigned long NewTargets::get_queued (void) { return new_targets->queue.size(); } /* This is the function that is used by nse_nmaplib.cc to add * new targets. * Returns the number of targets in the queue on success, or 0 on * failures or when the queue is empty. */ unsigned long NewTargets::insert (const char *target) { if (*target) { if (new_targets == NULL) { error("ERROR: to add targets run with -sC or --script options."); return 0; } if (o.current_scantype == SCRIPT_POST_SCAN) { error("ERROR: adding targets is disabled in the Post-scanning phase."); return 0; } if (strlen(target) >= 1024) { error("ERROR: new target is too long (>= 1024), failed to add it."); return 0; } } return new_targets->push(target); } /* Lookahead is the number of hosts that can be checked (such as ping scanned) in advance. Randomize causes each group of up to lookahead hosts to be internally shuffled around. The target_expressions array MUST REMAIN VALID IN MEMORY as long as this class instance is used -- the array is NOT copied. */ HostGroupState::HostGroupState(int lookahead, int rnd, char *expr[], int numexpr) { assert(lookahead > 0); hostbatch = (Target **) safe_zalloc(sizeof(Target *) * lookahead); max_batch_sz = lookahead; current_batch_sz = 0; next_batch_no = 0; randomize = rnd; target_expressions = expr; num_expressions = numexpr; next_expression = 0; } HostGroupState::~HostGroupState() { free(hostbatch); }