1 
2 /***************************************************************************
3  * TargetGroup.h -- The "TargetGroup" class holds a group of IP addresses, *
4  * such as those from a '/16' or '10.*.*.*' specification. It is a         *
5  * stripped version of the equivalent class of Nmap.                       *
6  *                                                                         *
7  ***********************IMPORTANT NMAP LICENSE TERMS************************
8  *                                                                         *
9  * The Nmap Security Scanner is (C) 1996-2019 Insecure.Com LLC ("The Nmap  *
10  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
11  * This program is free software; you may redistribute and/or modify it    *
12  * under the terms of the GNU General Public License as published by the   *
13  * Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE   *
14  * CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN.  This guarantees your   *
15  * right to use, modify, and redistribute this software under certain      *
16  * conditions.  If you wish to embed Nmap technology into proprietary      *
17  * software, we sell alternative licenses (contact sales@nmap.com).        *
18  * Dozens of software vendors already license Nmap technology such as      *
19  * host discovery, port scanning, OS detection, version detection, and     *
20  * the Nmap Scripting Engine.                                              *
21  *                                                                         *
22  * Note that the GPL places important restrictions on "derivative works",  *
23  * yet it does not provide a detailed definition of that term.  To avoid   *
24  * misunderstandings, we interpret that term as broadly as copyright law   *
25  * allows.  For example, we consider an application to constitute a        *
26  * derivative work for the purpose of this license if it does any of the   *
27  * following with any software or content covered by this license          *
28  * ("Covered Software"):                                                   *
29  *                                                                         *
30  * o Integrates source code from Covered Software.                         *
31  *                                                                         *
32  * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db   *
33  * or nmap-service-probes.                                                 *
34  *                                                                         *
35  * o Is designed specifically to execute Covered Software and parse the    *
36  * results (as opposed to typical shell or execution-menu apps, which will *
37  * execute anything you tell them to).                                     *
38  *                                                                         *
39  * o Includes Covered Software in a proprietary executable installer.  The *
40  * installers produced by InstallShield are an example of this.  Including *
41  * Nmap with other software in compressed or archival form does not        *
42  * trigger this provision, provided appropriate open source decompression  *
43  * or de-archiving software is widely available for no charge.  For the    *
44  * purposes of this license, an installer is considered to include Covered *
45  * Software even if it actually retrieves a copy of Covered Software from  *
46  * another source during runtime (such as by downloading it from the       *
47  * Internet).                                                              *
48  *                                                                         *
49  * o Links (statically or dynamically) to a library which does any of the  *
50  * above.                                                                  *
51  *                                                                         *
52  * o Executes a helper program, module, or script to do any of the above.  *
53  *                                                                         *
54  * This list is not exclusive, but is meant to clarify our interpretation  *
55  * of derived works with some common examples.  Other people may interpret *
56  * the plain GPL differently, so we consider this a special exception to   *
57  * the GPL that we apply to Covered Software.  Works which meet any of     *
58  * these conditions must conform to all of the terms of this license,      *
59  * particularly including the GPL Section 3 requirements of providing      *
60  * source code and allowing free redistribution of the work as a whole.    *
61  *                                                                         *
62  * As another special exception to the GPL terms, the Nmap Project grants  *
63  * permission to link the code of this program with any version of the     *
64  * OpenSSL library which is distributed under a license identical to that  *
65  * listed in the included docs/licenses/OpenSSL.txt file, and distribute   *
66  * linked combinations including the two.                                  *
67  *                                                                         *
68  * The Nmap Project has permission to redistribute Npcap, a packet         *
69  * capturing driver and library for the Microsoft Windows platform.        *
70  * Npcap is a separate work with it's own license rather than this Nmap    *
71  * license.  Since the Npcap license does not permit redistribution        *
72  * without special permission, our Nmap Windows binary packages which      *
73  * contain Npcap may not be redistributed without special permission.      *
74  *                                                                         *
75  * Any redistribution of Covered Software, including any derived works,    *
76  * must obey and carry forward all of the terms of this license, including *
77  * obeying all GPL rules and restrictions.  For example, source code of    *
78  * the whole work must be provided and free redistribution must be         *
79  * allowed.  All GPL references to "this License", are to be treated as    *
80  * including the terms and conditions of this license text as well.        *
81  *                                                                         *
82  * Because this license imposes special exceptions to the GPL, Covered     *
83  * Work may not be combined (even as part of a larger work) with plain GPL *
84  * software.  The terms, conditions, and exceptions of this license must   *
85  * be included as well.  This license is incompatible with some other open *
86  * source licenses as well.  In some cases we can relicense portions of    *
87  * Nmap or grant special permissions to use it in other open source        *
88  * software.  Please contact fyodor@nmap.org with any such requests.       *
89  * Similarly, we don't incorporate incompatible open source software into  *
90  * Covered Software without special permission from the copyright holders. *
91  *                                                                         *
92  * If you have any questions about the licensing restrictions on using     *
93  * Nmap in other works, we are happy to help.  As mentioned above, we also *
94  * offer an alternative license to integrate Nmap into proprietary         *
95  * applications and appliances.  These contracts have been sold to dozens  *
96  * of software vendors, and generally include a perpetual license as well  *
97  * as providing support and updates.  They also fund the continued         *
98  * development of Nmap.  Please email sales@nmap.com for further           *
99  * information.                                                            *
100  *                                                                         *
101  * If you have received a written license agreement or contract for        *
102  * Covered Software stating terms other than these, you may choose to use  *
103  * and redistribute Covered Software under those terms instead of these.   *
104  *                                                                         *
105  * Source is provided to this software because we believe users have a     *
106  * right to know exactly what a program is going to do before they run it. *
107  * This also allows you to audit the software for security holes.          *
108  *                                                                         *
109  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
110  * and add new features.  You are highly encouraged to send your changes   *
111  * to the dev@nmap.org mailing list for possible incorporation into the    *
112  * main distribution.  By sending these changes to Fyodor or one of the    *
113  * Insecure.Org development mailing lists, or checking them into the Nmap  *
114  * source code repository, it is understood (unless you specify            *
115  * otherwise) that you are offering the Nmap Project the unlimited,        *
116  * non-exclusive right to reuse, modify, and relicense the code.  Nmap     *
117  * will always be available Open Source, but this is important because     *
118  * the inability to relicense code has caused devastating problems for     *
119  * other Free Software projects (such as KDE and NASM).  We also           *
120  * occasionally relicense the code to third parties as discussed above.    *
121  * If you wish to specify special license conditions of your               *
122  * contributions, just say so when you send them.                          *
123  *                                                                         *
124  * This program is distributed in the hope that it will be useful, but     *
125  * WITHOUT ANY WARRANTY; without even the implied warranty of              *
126  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Nmap      *
127  * license file for more details (it's in a COPYING file included with     *
128  * Nmap, and also available from https://svn.nmap.org/nmap/COPYING)        *
129  *                                                                         *
130  ***************************************************************************/
131 
132 /* $Id: TargetGroup.h 12955 2009-04-15 00:37:03Z fyodor $ */
133 
134 #ifndef TARGETGROUP_H
135 #define TARGETGROUP_H
136 
137 #include "ncrack.h"
138 #include "Target.h"
139 #include "global_structures.h"
140 
141 #include <vector>
142 
143 class TargetGroup {
144  public:
145   /* used by get_target_types */
146   enum _targets_types { TYPE_NONE, IPV4_NETMASK, IPV4_RANGES, IPV6_ADDRESS };
147   /* used as input to skip range */
148   enum _octet_nums { FIRST_OCTET, SECOND_OCTET, THIRD_OCTET };
149   TargetGroup();
150 
151  /* Initializes (or reinitializes) the object with a new expression,
152     such as 192.168.0.0/16 , 10.1.0-5.1-254 , or
153     fe80::202:e3ff:fe14:1102 .  The af parameter is AF_INET or
154     AF_INET6 Returns 0 for success */
155   int parse_expr(const char * const target_expr, int af);
156   /* Reset the object without reinitializing it */
157   int rewind();
158   /* Grab the next host from this expression (if any).  Returns 0 and
159      fills in ss if successful.  ss must point to a pre-allocated
160      sockaddr_storage structure */
161   int get_next_host(struct sockaddr_storage *ss, size_t *sslen);
162   /* Returns the last given host, so that it will be given again next
163      time get_next_host is called.  Obviously, you should only call
164      this if you have fetched at least 1 host since parse_expr() was
165      called */
166   int return_last_host();
167   /* return the target type */
get_targets_type()168   char get_targets_type() {return targets_type;};
169   /* get the netmask */
get_mask()170   int get_mask() {return netmask;};
171   /* is the current expression a named host */
get_namedhost()172   int get_namedhost() {return namedhost;};
173   /* Skip an octet in the range array */
174   int skip_range(_octet_nums octet);
175 
176   vector <service_lookup *>services;
177 
178  private:
179   enum _targets_types targets_type;
180   void Initialize();
181 
182 #if HAVE_IPV6
183   struct sockaddr_in6 ip6;
184 #endif
185 
186   /* These 4 are used for the '/mask' style of specifying target
187      net (IPV4_NETMASK) */
188   u32 netmask;
189   struct in_addr startaddr;
190   struct in_addr currentaddr;
191   struct in_addr endaddr;
192 
193   // These three are for the '138.[1-7,16,91-95,200-].12.1' style (IPV4_RANGES)
194   u8 addresses[4][256];
195   unsigned int current[4];
196   u8 last[4];
197 
198 /* Number of IPs left in this structure -- set to 0 if
199       the fields are not valid */
200   unsigned long long ipsleft;
201 
202   // is the current target expression a named host
203   int namedhost;
204 };
205 
206 
207 #endif /* TARGETGROUP_H */
208