1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Keep resolved, scanned addresses separate from unscanned addresses.

This commit is contained in:
dmiller
2017-08-07 14:25:13 +00:00
parent 527c653a0b
commit a66e75173e
5 changed files with 32 additions and 35 deletions

View File

@@ -329,9 +329,8 @@ class Target {
std::list <TracerouteHop> traceroute_hops; std::list <TracerouteHop> traceroute_hops;
/* If the address for this target came from a DNS lookup, the list of /* If the address for this target came from a DNS lookup, the list of
resultant addresses (sometimes there are more than one). The address resultant addresses (sometimes there are more than one) that were not scanned. */
actually used is always the first element in this list. */ std::list<struct sockaddr_storage> unscanned_addrs;
std::list<struct sockaddr_storage> resolved_addrs;
#ifndef NOLUA #ifndef NOLUA
ScriptResults scriptResults; ScriptResults scriptResults;

View File

@@ -162,6 +162,7 @@ public:
virtual ~NetBlock() {} virtual ~NetBlock() {}
std::string hostname; std::string hostname;
std::list<struct sockaddr_storage> resolvedaddrs; std::list<struct sockaddr_storage> resolvedaddrs;
std::list<struct sockaddr_storage> unscanned_addrs;
/* Parses an expression such as 192.168.0.0/16, 10.1.0-5.1-254, or /* Parses an expression such as 192.168.0.0/16, 10.1.0-5.1-254, or
fe80::202:e3ff:fe14:1102/112 and returns a newly allocated NetBlock. The af fe80::202:e3ff:fe14:1102/112 and returns a newly allocated NetBlock. The af
@@ -380,20 +381,13 @@ bail:
return NULL; return NULL;
} }
/* Returns the first address which matches the address family af */ bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const {
static const struct sockaddr_storage *first_af_address(const std::list<struct sockaddr_storage> *addrs, int af) { for (std::list<struct sockaddr_storage>::const_iterator it = this->resolvedaddrs.begin(), end = this->resolvedaddrs.end(); it != end; ++it) {
for (std::list<struct sockaddr_storage>::const_iterator it = addrs->begin(), end = addrs->end(); it != end; ++it) { if (sockaddr_storage_equal(&*it, ss)) {
if (it->ss_family == af) { return true;
return &*it;
} }
} }
return NULL; return false;
}
bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const {
if (this->resolvedaddrs.empty())
return false;
return sockaddr_storage_equal(first_af_address(&this->resolvedaddrs, ss->ss_family), ss);
} }
NetBlockIPv4Ranges::NetBlockIPv4Ranges() { NetBlockIPv4Ranges::NetBlockIPv4Ranges() {
@@ -721,6 +715,7 @@ std::string NetBlockIPv6Netmask::str() const {
NetBlock *NetBlockHostname::resolve() { NetBlock *NetBlockHostname::resolve() {
struct addrinfo *addrs, *addr; struct addrinfo *addrs, *addr;
std::list<struct sockaddr_storage> resolvedaddrs; std::list<struct sockaddr_storage> resolvedaddrs;
std::list<struct sockaddr_storage> unscanned_addrs;
NetBlock *netblock; NetBlock *netblock;
const struct sockaddr_storage *sp = NULL; const struct sockaddr_storage *sp = NULL;
struct sockaddr_storage ss; struct sockaddr_storage ss;
@@ -730,17 +725,22 @@ NetBlock *NetBlockHostname::resolve() {
for (addr = addrs; addr != NULL; addr = addr->ai_next) { for (addr = addrs; addr != NULL; addr = addr->ai_next) {
if (addr->ai_addrlen < sizeof(ss)) { if (addr->ai_addrlen < sizeof(ss)) {
memcpy(&ss, addr->ai_addr, addr->ai_addrlen); memcpy(&ss, addr->ai_addr, addr->ai_addrlen);
resolvedaddrs.push_back(ss); if (sp == NULL && addr->ai_family == this->af) {
resolvedaddrs.push_back(ss);
sp = &resolvedaddrs.back();
}
else {
unscanned_addrs.push_back(ss);
}
} }
} }
if (addrs != NULL) if (addrs != NULL)
freeaddrinfo(addrs); freeaddrinfo(addrs);
if (resolvedaddrs.empty()) if (resolvedaddrs.empty() && unscanned_addrs.empty())
return NULL; return NULL;
sp = first_af_address(&resolvedaddrs, this->af); if (sp == NULL) {
if (sp == NULL || sp->ss_family != this->af) {
switch (this->af) { switch (this->af) {
case AF_INET: case AF_INET:
error("Warning: Hostname %s resolves, but not to any IPv4 address. Try scanning with -6", this->hostname.c_str()); error("Warning: Hostname %s resolves, but not to any IPv4 address. Try scanning with -6", this->hostname.c_str());
@@ -757,9 +757,9 @@ NetBlock *NetBlockHostname::resolve() {
ss = *sp; ss = *sp;
sslen = sizeof(ss); sslen = sizeof(ss);
if (resolvedaddrs.size() > 1 && o.verbose > 1) { if (!unscanned_addrs.empty() > 1 && o.verbose > 1) {
error("Warning: Hostname %s resolves to %lu IPs. Using %s.", this->hostname.c_str(), error("Warning: Hostname %s resolves to %lu IPs. Using %s.", this->hostname.c_str(),
(unsigned long) resolvedaddrs.size(), inet_ntop_ez(&ss, sslen)); (unsigned long) unscanned_addrs.size() + resolvedaddrs.size(), inet_ntop_ez(&ss, sslen));
} }
netblock = NULL; netblock = NULL;
@@ -782,6 +782,7 @@ NetBlock *NetBlockHostname::resolve() {
netblock->hostname = this->hostname; netblock->hostname = this->hostname;
netblock->resolvedaddrs = resolvedaddrs; netblock->resolvedaddrs = resolvedaddrs;
netblock->unscanned_addrs = unscanned_addrs;
netblock->apply_netmask(this->bits); netblock->apply_netmask(this->bits);
return netblock; return netblock;
@@ -873,10 +874,10 @@ const char *TargetGroup::get_resolved_name(void) const {
return this->netblock->hostname.c_str(); return this->netblock->hostname.c_str();
} }
/* Return the list of addresses that the name for this group resolved to, if /* Return the list of addresses that the name for this group resolved to, but
it came from a name resolution. */ which were not scanned, if it came from a name resolution. */
const std::list<struct sockaddr_storage> &TargetGroup::get_resolved_addrs(void) const { const std::list<struct sockaddr_storage> &TargetGroup::get_unscanned_addrs(void) const {
return this->netblock->resolvedaddrs; return this->netblock->unscanned_addrs;
} }
/* is the current expression a named host */ /* is the current expression a named host */

View File

@@ -164,9 +164,9 @@ public:
bool is_resolved_address(const struct sockaddr_storage *ss) const; bool is_resolved_address(const struct sockaddr_storage *ss) const;
/* Return a string of the name or address that was resolved for this group. */ /* Return a string of the name or address that was resolved for this group. */
const char *get_resolved_name(void) const; const char *get_resolved_name(void) const;
/* Return the list of addresses that the name for this group resolved to, if /* Return the list of addresses that the name for this group resolved to, but
it came from a name resolution. */ which were not scanned, if it came from a name resolution. */
const std::list<struct sockaddr_storage> &get_resolved_addrs(void) const; const std::list<struct sockaddr_storage> &get_unscanned_addrs(void) const;
/* is the current expression a named host */ /* is the current expression a named host */
int get_namedhost() const; int get_namedhost() const;
}; };

View File

@@ -1443,17 +1443,14 @@ void write_host_header(Target *currenths) {
} }
write_host_status(currenths); write_host_status(currenths);
if (currenths->TargetName() != NULL if (currenths->TargetName() != NULL
&& currenths->resolved_addrs.size() > 1) { && !currenths->unscanned_addrs.empty()) {
const struct sockaddr_storage *hs_ss = currenths->TargetSockAddr();
log_write(LOG_PLAIN, "Other addresses for %s (not scanned):", log_write(LOG_PLAIN, "Other addresses for %s (not scanned):",
currenths->TargetName()); currenths->TargetName());
for (std::list<struct sockaddr_storage>::const_iterator it = currenths->resolved_addrs.begin(), end = currenths->resolved_addrs.end(); for (std::list<struct sockaddr_storage>::const_iterator it = currenths->unscanned_addrs.begin(), end = currenths->unscanned_addrs.end();
it != end; it++) { it != end; it++) {
struct sockaddr_storage ss = *it; struct sockaddr_storage ss = *it;
if (!sockaddr_storage_equal(&ss, hs_ss)) { log_write(LOG_PLAIN, " %s", inet_ntop_ez(&ss, sizeof(ss)));
log_write(LOG_PLAIN, " %s", inet_ntop_ez(&ss, sizeof(ss)));
}
} }
log_write(LOG_PLAIN, "\n"); log_write(LOG_PLAIN, "\n");
} }

View File

@@ -445,7 +445,7 @@ static Target *setup_target(const HostGroupState *hs,
if (hs->current_group.is_resolved_address(ss)) { if (hs->current_group.is_resolved_address(ss)) {
if (hs->current_group.get_namedhost()) if (hs->current_group.get_namedhost())
t->setTargetName(hs->current_group.get_resolved_name()); t->setTargetName(hs->current_group.get_resolved_name());
t->resolved_addrs = hs->current_group.get_resolved_addrs(); t->unscanned_addrs = hs->current_group.get_unscanned_addrs();
} }
/* We figure out the source IP/device IFF /* We figure out the source IP/device IFF