1 /***************************************************************************
2 * NewTargets.h -- The "NewTargets" class allows NSE scripts to add new *
3 * targets to the scan queue. *
4 ***********************IMPORTANT NMAP LICENSE TERMS************************
5 * *
6 * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap *
7 * Project"). Nmap is also a registered trademark of the Nmap Project. *
8 * *
9 * This program is distributed under the terms of the Nmap Public Source *
10 * License (NPSL). The exact license text applying to a particular Nmap *
11 * release or source code control revision is contained in the LICENSE *
12 * file distributed with that version of Nmap or source code control *
13 * revision. More Nmap copyright/legal information is available from *
14 * https://nmap.org/book/man-legal.html, and further information on the *
15 * NPSL license itself can be found at https://nmap.org/npsl. This header *
16 * summarizes some key points from the Nmap license, but is no substitute *
17 * for the actual license text. *
18 * *
19 * Nmap is generally free for end users to download and use themselves, *
20 * including commercial use. It is available from https://nmap.org. *
21 * *
22 * The Nmap license generally prohibits companies from using and *
23 * redistributing Nmap in commercial products, but we sell a special Nmap *
24 * OEM Edition with a more permissive license and special features for *
25 * this purpose. See https://nmap.org/oem *
26 * *
27 * If you have received a written Nmap license agreement or contract *
28 * stating terms other than these (such as an Nmap OEM license), you may *
29 * choose to use and redistribute Nmap under those terms instead. *
30 * *
31 * The official Nmap Windows builds include the Npcap software *
32 * (https://npcap.org) for packet capture and transmission. It is under *
33 * separate license terms which forbid redistribution without special *
34 * permission. So the official Nmap Windows builds may not be *
35 * redistributed without special permission (such as an Nmap OEM *
36 * license). *
37 * *
38 * Source is provided to this software because we believe users have a *
39 * right to know exactly what a program is going to do before they run it. *
40 * This also allows you to audit the software for security holes. *
41 * *
42 * Source code also allows you to port Nmap to new platforms, fix bugs, *
43 * and add new features. You are highly encouraged to submit your *
44 * changes as a Github PR or by email to the dev@nmap.org mailing list *
45 * for possible incorporation into the main distribution. Unless you *
46 * specify otherwise, it is understood that you are offering us very *
47 * broad rights to use your submissions as described in the Nmap Public *
48 * Source License Contributor Agreement. This is important because we *
49 * fund the project by selling licenses with various terms, and also *
50 * because the inability to relicense code has caused devastating *
51 * problems for other Free Software projects (such as KDE and NASM). *
52 * *
53 * The free version of Nmap is distributed in the hope that it will be *
54 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of *
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, *
56 * indemnification and commercial support are all available through the *
57 * Npcap OEM program--see https://nmap.org/oem. *
58 * *
59 ***************************************************************************/
60
61 /* $Id: NewTargets.cc 38078 2020-10-02 16:12:22Z dmiller $ */
62
63 #include "NewTargets.h"
64 #include "NmapOps.h"
65 #include "output.h"
66 #include "nmap_error.h"
67
68 extern NmapOps o; /* option structure */
69 NewTargets *NewTargets::new_targets;
70
71 /* debug level for the adding target is: 3 */
get(void)72 NewTargets *NewTargets::get (void) {
73 if (new_targets)
74 return new_targets;
75 new_targets = new NewTargets();
76 return new_targets;
77 }
78
NewTargets(void)79 NewTargets::NewTargets (void) {
80 Initialize();
81 }
82
Initialize(void)83 void NewTargets::Initialize (void) {
84 history.clear();
85 while (!queue.empty())
86 queue.pop();
87 }
88
89 /* This private method is used to push new targets to the
90 * queue. It returns the number of targets in the queue. */
push(const char * target)91 unsigned long NewTargets::push (const char *target) {
92 std::pair<std::set<std::string>::iterator, bool> pair_iter;
93 std::string tg(target);
94
95 if (tg.length() > 0) {
96 /* save targets in the scanned history here (NSE side). */
97 pair_iter = history.insert(tg);
98
99 /* A new target */
100 if (pair_iter.second == true) {
101 /* push target onto the queue for future scans */
102 queue.push(tg);
103
104 if (o.debugging > 2)
105 log_write(LOG_PLAIN, "New Targets: target %s pushed onto the queue.\n", tg.c_str());
106 } else {
107 if (o.debugging > 2)
108 log_write(LOG_PLAIN, "New Targets: target %s is already in the queue.\n", tg.c_str());
109 /* Return 1 when the target is already in the history cache,
110 * this will prevent returning 0 when the target queue is
111 * empty since no target was added. */
112 return 1;
113 }
114 }
115
116 return queue.size();
117 }
118
119 /* Reads a target from the queue and return it to be pushed
120 * onto Nmap scan queue */
read(void)121 std::string NewTargets::read (void) {
122 std::string str;
123
124 /* check to see it there are targets in the queue */
125 if (!new_targets->queue.empty()) {
126 str = new_targets->queue.front();
127 new_targets->queue.pop();
128 }
129
130 return str;
131 }
132
clear(void)133 void NewTargets::clear (void) {
134 new_targets->history.clear();
135 }
136
get_number(void)137 unsigned long NewTargets::get_number (void) {
138 return new_targets->history.size();
139 }
140
get_scanned(void)141 unsigned long NewTargets::get_scanned (void) {
142 return new_targets->history.size() - new_targets->queue.size();
143 }
144
get_queued(void)145 unsigned long NewTargets::get_queued (void) {
146 return new_targets->queue.size();
147 }
148
149 /* This is the function that is used by nse_nmaplib.cc to add
150 * new targets.
151 * Returns the number of targets in the queue on success, or 0 on
152 * failures or when the queue is empty. */
insert(const char * target)153 unsigned long NewTargets::insert (const char *target) {
154 if (*target) {
155 if (new_targets == NULL) {
156 error("ERROR: to add targets run with -sC or --script options.");
157 return 0;
158 }
159 if (o.current_scantype == SCRIPT_POST_SCAN) {
160 error("ERROR: adding targets is disabled in the Post-scanning phase.");
161 return 0;
162 }
163 if (strlen(target) >= 1024) {
164 error("ERROR: new target is too long (>= 1024), failed to add it.");
165 return 0;
166 }
167 }
168
169 return new_targets->push(target);
170 }
171