Skip to content

Commit 03c9f34

Browse files
committed
Fix sign and width issues around max_ips_to_scan. Fixes nmap#2838. Fixes nmap#2836
1 parent e4a4b28 commit 03c9f34

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

NmapOps.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ class NmapOps {
226226
/* Gets the spoofed MAC address, but returns NULL if it hasn't been set */
227227
const u8 *spoofMACAddress() { return spoof_mac_set? spoof_mac : NULL; }
228228

229-
unsigned int max_ips_to_scan; // Used for Random input (-iR) to specify how
230-
// many IPs to try before stopping. 0 means unlimited.
229+
unsigned long max_ips_to_scan; // Used for Random input (-iR) to specify how
230+
// many IPs to try before stopping. 0 means unlimited if generate_random_ips is true
231231
int extra_payload_length; /* These two are for --data-length op */
232232
char *extra_payload;
233233
unsigned long host_timeout;

TargetGroup.cc

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,21 @@ class NetBlockRandomIPv4 : public NetBlock {
126126
public:
127127
NetBlockRandomIPv4();
128128

129-
void reject_last_host() { count++; }
130-
void set_num_random(int num) { count = num; }
129+
void reject_last_host() { if (!infinite) count++; }
130+
void set_num_random(unsigned long num) {
131+
if (num == 0)
132+
infinite = true;
133+
else
134+
count = num;
135+
}
131136
bool next(struct sockaddr_storage *ss, size_t *sslen);
132137
void apply_netmask(int bits) {}
133138
std::string str() const {return "Random IPv4 addresses";}
134139

135140
private:
136141
struct sockaddr_in base;
137-
int count;
142+
unsigned long count;
143+
bool infinite;
138144
};
139145

140146
class NetBlockIPv4Ranges : public NetBlock {
@@ -352,21 +358,25 @@ bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const {
352358
return false;
353359
}
354360

355-
NetBlockRandomIPv4::NetBlockRandomIPv4() : count(0) {
361+
NetBlockRandomIPv4::NetBlockRandomIPv4() : count(0), infinite(false) {
356362
memset(&base, 0, sizeof(base));
357363
base.sin_family = AF_INET;
358364
}
359365

360366
bool NetBlockRandomIPv4::next(struct sockaddr_storage *ss, size_t *sslen) {
361-
if (count <= 0) {
362-
return false;
367+
if (!infinite) {
368+
if (count > 0) {
369+
count--;
370+
}
371+
else {
372+
return false;
373+
}
363374
}
364375
do {
365376
base.sin_addr.s_addr = get_random_unique_u32();
366377
} while (ip_is_reserved(&base.sin_addr));
367378
memcpy(ss, &base, sizeof(base));
368379
*sslen = sizeof(base);
369-
count--;
370380
return true;
371381
}
372382

@@ -861,7 +871,7 @@ bool TargetGroup::load_expressions(HostGroupState *hs, int af) {
861871
return !netblocks.empty();
862872
}
863873

864-
void TargetGroup::generate_random_ips(int num_random) {
874+
void TargetGroup::generate_random_ips(unsigned long num_random) {
865875
NetBlockRandomIPv4 *nbrand = new NetBlockRandomIPv4();
866876
nbrand->set_num_random(num_random);
867877
netblocks.push_front(nbrand);

TargetGroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class TargetGroup {
9898
const std::list<struct sockaddr_storage> &get_unscanned_addrs(void) const;
9999
/* is the current expression a named host */
100100
int get_namedhost() const;
101-
void generate_random_ips(int num_random);
101+
void generate_random_ips(unsigned long num_random);
102102
void reject_last_host();
103103

104104
private:

targets.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ bool target_needs_new_hostgroup(Target **targets, int targets_sz, const Target *
285285
The target_expressions array MUST REMAIN VALID IN MEMORY as long as
286286
this class instance is used -- the array is NOT copied.
287287
*/
288-
HostGroupState::HostGroupState(int lookahead, int rnd, int num_random, int argc, const char **argv) {
288+
HostGroupState::HostGroupState(int lookahead, int rnd, unsigned long num_random, int argc, const char **argv) {
289289
assert(lookahead > 0);
290290
this->argc = argc;
291291
this->argv = argv;
@@ -296,7 +296,7 @@ HostGroupState::HostGroupState(int lookahead, int rnd, int num_random, int argc,
296296
current_batch_sz = 0;
297297
next_batch_no = 0;
298298
randomize = rnd;
299-
if (num_random > 0) {
299+
if (num_random >= 0) {
300300
current_group.generate_random_ips(num_random);
301301
}
302302
}
@@ -414,8 +414,8 @@ static Target *setup_target(const HostGroupState *hs,
414414
}
415415

416416
bool HostGroupState::get_next_host(struct sockaddr_storage *ss, size_t *sslen, struct addrset *exclude_group) {
417-
int num_queued = o.numhosts_scanned + current_batch_sz;
418-
if (o.max_ips_to_scan > 0 && num_queued >= (int)o.max_ips_to_scan) {
417+
unsigned long num_queued = o.numhosts_scanned + current_batch_sz;
418+
if (o.max_ips_to_scan > 0 && num_queued >= o.max_ips_to_scan) {
419419
return false;
420420
}
421421

targets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class HostGroupState {
7474
/* The maximum number of entries we want to allow storing in defer_buffer. */
7575
static const unsigned int DEFER_LIMIT = 64;
7676

77-
HostGroupState(int lookahead, int randomize, int num_random, int argc, const char *argv[]);
77+
HostGroupState(int lookahead, int randomize, unsigned long num_random, int argc, const char *argv[]);
7878
~HostGroupState();
7979
Target **hostbatch;
8080

0 commit comments

Comments
 (0)