1 
2 /***************************************************************************
3  * ServiceGroup.h -- The "ServiceGroup" class holds lists for all          *
4  * services that are under active cracking or have been stalled for one    *
5  * reason or another. Information and options that apply to all services   *
6  * as a whole are also kept here.                                          *
7  *                                                                         *
8  ***********************IMPORTANT NMAP LICENSE TERMS************************
9  *                                                                         *
10  * The Nmap Security Scanner is (C) 1996-2019 Insecure.Com LLC ("The Nmap  *
11  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
12  * This program is free software; you may redistribute and/or modify it    *
13  * under the terms of the GNU General Public License as published by the   *
14  * Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE   *
15  * CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN.  This guarantees your   *
16  * right to use, modify, and redistribute this software under certain      *
17  * conditions.  If you wish to embed Nmap technology into proprietary      *
18  * software, we sell alternative licenses (contact sales@nmap.com).        *
19  * Dozens of software vendors already license Nmap technology such as      *
20  * host discovery, port scanning, OS detection, version detection, and     *
21  * the Nmap Scripting Engine.                                              *
22  *                                                                         *
23  * Note that the GPL places important restrictions on "derivative works",  *
24  * yet it does not provide a detailed definition of that term.  To avoid   *
25  * misunderstandings, we interpret that term as broadly as copyright law   *
26  * allows.  For example, we consider an application to constitute a        *
27  * derivative work for the purpose of this license if it does any of the   *
28  * following with any software or content covered by this license          *
29  * ("Covered Software"):                                                   *
30  *                                                                         *
31  * o Integrates source code from Covered Software.                         *
32  *                                                                         *
33  * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db   *
34  * or nmap-service-probes.                                                 *
35  *                                                                         *
36  * o Is designed specifically to execute Covered Software and parse the    *
37  * results (as opposed to typical shell or execution-menu apps, which will *
38  * execute anything you tell them to).                                     *
39  *                                                                         *
40  * o Includes Covered Software in a proprietary executable installer.  The *
41  * installers produced by InstallShield are an example of this.  Including *
42  * Nmap with other software in compressed or archival form does not        *
43  * trigger this provision, provided appropriate open source decompression  *
44  * or de-archiving software is widely available for no charge.  For the    *
45  * purposes of this license, an installer is considered to include Covered *
46  * Software even if it actually retrieves a copy of Covered Software from  *
47  * another source during runtime (such as by downloading it from the       *
48  * Internet).                                                              *
49  *                                                                         *
50  * o Links (statically or dynamically) to a library which does any of the  *
51  * above.                                                                  *
52  *                                                                         *
53  * o Executes a helper program, module, or script to do any of the above.  *
54  *                                                                         *
55  * This list is not exclusive, but is meant to clarify our interpretation  *
56  * of derived works with some common examples.  Other people may interpret *
57  * the plain GPL differently, so we consider this a special exception to   *
58  * the GPL that we apply to Covered Software.  Works which meet any of     *
59  * these conditions must conform to all of the terms of this license,      *
60  * particularly including the GPL Section 3 requirements of providing      *
61  * source code and allowing free redistribution of the work as a whole.    *
62  *                                                                         *
63  * As another special exception to the GPL terms, the Nmap Project grants  *
64  * permission to link the code of this program with any version of the     *
65  * OpenSSL library which is distributed under a license identical to that  *
66  * listed in the included docs/licenses/OpenSSL.txt file, and distribute   *
67  * linked combinations including the two.                                  *
68  *                                                                         *
69  * The Nmap Project has permission to redistribute Npcap, a packet         *
70  * capturing driver and library for the Microsoft Windows platform.        *
71  * Npcap is a separate work with it's own license rather than this Nmap    *
72  * license.  Since the Npcap license does not permit redistribution        *
73  * without special permission, our Nmap Windows binary packages which      *
74  * contain Npcap may not be redistributed without special permission.      *
75  *                                                                         *
76  * Any redistribution of Covered Software, including any derived works,    *
77  * must obey and carry forward all of the terms of this license, including *
78  * obeying all GPL rules and restrictions.  For example, source code of    *
79  * the whole work must be provided and free redistribution must be         *
80  * allowed.  All GPL references to "this License", are to be treated as    *
81  * including the terms and conditions of this license text as well.        *
82  *                                                                         *
83  * Because this license imposes special exceptions to the GPL, Covered     *
84  * Work may not be combined (even as part of a larger work) with plain GPL *
85  * software.  The terms, conditions, and exceptions of this license must   *
86  * be included as well.  This license is incompatible with some other open *
87  * source licenses as well.  In some cases we can relicense portions of    *
88  * Nmap or grant special permissions to use it in other open source        *
89  * software.  Please contact fyodor@nmap.org with any such requests.       *
90  * Similarly, we don't incorporate incompatible open source software into  *
91  * Covered Software without special permission from the copyright holders. *
92  *                                                                         *
93  * If you have any questions about the licensing restrictions on using     *
94  * Nmap in other works, we are happy to help.  As mentioned above, we also *
95  * offer an alternative license to integrate Nmap into proprietary         *
96  * applications and appliances.  These contracts have been sold to dozens  *
97  * of software vendors, and generally include a perpetual license as well  *
98  * as providing support and updates.  They also fund the continued         *
99  * development of Nmap.  Please email sales@nmap.com for further           *
100  * information.                                                            *
101  *                                                                         *
102  * If you have received a written license agreement or contract for        *
103  * Covered Software stating terms other than these, you may choose to use  *
104  * and redistribute Covered Software under those terms instead of these.   *
105  *                                                                         *
106  * Source is provided to this software because we believe users have a     *
107  * right to know exactly what a program is going to do before they run it. *
108  * This also allows you to audit the software for security holes.          *
109  *                                                                         *
110  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
111  * and add new features.  You are highly encouraged to send your changes   *
112  * to the dev@nmap.org mailing list for possible incorporation into the    *
113  * main distribution.  By sending these changes to Fyodor or one of the    *
114  * Insecure.Org development mailing lists, or checking them into the Nmap  *
115  * source code repository, it is understood (unless you specify            *
116  * otherwise) that you are offering the Nmap Project the unlimited,        *
117  * non-exclusive right to reuse, modify, and relicense the code.  Nmap     *
118  * will always be available Open Source, but this is important because     *
119  * the inability to relicense code has caused devastating problems for     *
120  * other Free Software projects (such as KDE and NASM).  We also           *
121  * occasionally relicense the code to third parties as discussed above.    *
122  * If you wish to specify special license conditions of your               *
123  * contributions, just say so when you send them.                          *
124  *                                                                         *
125  * This program is distributed in the hope that it will be useful, but     *
126  * WITHOUT ANY WARRANTY; without even the implied warranty of              *
127  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Nmap      *
128  * license file for more details (it's in a COPYING file included with     *
129  * Nmap, and also available from https://svn.nmap.org/nmap/COPYING)        *
130  *                                                                         *
131  ***************************************************************************/
132 
133 
134 #ifndef SERVICE_GROUP_H
135 #define SERVICE_GROUP_H
136 
137 #include "ncrack.h"
138 #include "Service.h"
139 #include <list>
140 
141 
142 class ServiceGroup {
143 	public:
144 		ServiceGroup();
145 		~ServiceGroup();
146 
147     /* ********************* Functions ******************* */
148 
149     /* Find and set minimum connection delay from all services */
150     void findMinDelay(void);
151 
152     /*
153      * Pushes service into one of the ServiceGroup lists.
154      * A Service might belong:
155      * a) to 'services_active' OR
156      * b) to 'services_finished' OR
157      * c) to any other combination of the rest of the lists
158      * A service might belong to more than one of the lists in case c) when
159      * for example it needs to wait both for the 'connection_delay' and the
160      * 'connection_limit'.
161      */
162     list <Service *>::iterator pushServiceToList(Service *serv,
163         list <Service *> *dst);
164 
165     /*
166      * Pops service from one of the ServiceGroup lists. This is the only way
167      * for a service to return back to 'services_active' and this happens if
168      * and only if it stops belonging to any other list (except
169      * 'services_finished' from which you are not allowed to remove a service
170      * once it moves there).
171      */
172     list <Service *>::iterator popServiceFromList(Service *serv,
173         list <Service *> *src);
174 
175     double getCompletionFraction(void);
176 
177     bool checkLinearPending(void);
178 
179     /* ********************* Members ********************* */
180 
181     /* All Services. This includes all active and inactive services.
182      * This list is useful for iterating through all services in one
183      * global place instead of searching for each one of them in
184      * separate lists. This list is *never* touched except at creation.
185      */
186     list<Service *> services_all;
187 
188     /* Services finished (successfully or not) */
189     list<Service *> services_finished;
190 
191     /*
192      * Service has its credential list finished, the pool is empty
193      * but there are pending connections still active
194      */
195     list<Service *> services_finishing;
196 
197     /*
198      * Services that temporarily cannot initiate another
199      * connection due to timing constraints (connection limit)
200      */
201     list<Service *> services_full;
202 
203     /*
204      * Services that have to wait a time of 'connection_delay'
205      * until initiating another connection
206      */
207     list<Service *> services_wait;
208 
209 
210     /*
211      * Services that have to wait until our pair pool has at least one element
212      * to grab a login pair from, since the main credential list (username or
213      * password depending on the mode of iteration) has already finished being
214      * iterated through.
215      */
216     list<Service *> services_pairfini;
217 
218     /* Services that can initiate more connections */
219     list<Service *> services_active;
220 
221     /* how many services we need to crack in total */
222     unsigned long total_services;
223 
224     long min_connection_delay;/* minimum connection delay from all services */
225     long active_connections;  /* total number of active connections */
226     long connection_limit;    /* maximum total number of active connections */
227 
228     /* how many connections have been initiated */
229     unsigned long connections_total;
230     unsigned long connections_timedout; /* how many connections have failed */
231 
232     /* how many connections prematurely closed */
233     unsigned long connections_closed;
234 
235     /* total credentials found */
236     unsigned long credentials_found;
237 
238     int num_hosts_timedout;  /* # of hosts timed out during (or before) scan */
239     list <Service *>::iterator last_accessed; /* last element accessed */
240     list <Service *>::iterator prev_modified; /* prev element modified */
241 
242     RateMeter auth_rate_meter;
243     ScanProgressMeter *SPM;
244 
245   private:
246 
247     /*
248      * Returns list's equivalent name. e.g for services_finished it will return
249      * a "FINISHED" string. We prefer capitals for debugging purposes. Caller
250      * must free the string after it finishes using it.
251      */
252     const char *list2name(list <Service *> *list);
253 
254     /*
255      * Set service's corresponding boolean indicating that it now
256      * belongs to the particular list.
257      * Returns true if operation is valid.
258      */
259     bool set_servlist(Service *serv, list <Service *> *list);
260 
261     /*
262      * Unset service's corresponding boolean indicating that it stops
263      * belonging to the particular list.
264      * Returns true if operation is valid.
265      */
266     bool unset_servlist(Service *serv, list <Service *> *list);
267 
268 };
269 
270 #endif
271