Skip to content

Commit

Permalink
Report IPv4 and IPv6 addresses resolved for each host
Browse files Browse the repository at this point in the history
Previously, we would only request IPv4 addresses by default, or only
IPv6 when scanning with -6. Now, we'll request both (by not passing an
address family in the hints to getaddrinfo) and report them all in the
"Other addresses" line. This should encourage more users to scan with
-6. Additionally, it allows us to catch and report attempts to scan
IPv6-only hosts via IPv4, where previously we would just say "Failed to
resolve"

Closes #76
  • Loading branch information
bonsaiviking committed Mar 11, 2015
1 parent fbbb64a commit c0628fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
32 changes: 29 additions & 3 deletions TargetGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,20 @@ NetBlock *NetBlock::parse_expr(const char *target_expr, int af) {
return NULL;
}

/* Returns the first address which matches the address family af */
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 = addrs->begin(), end = addrs->end(); it != end; ++it) {
if (it->ss_family == af) {
return &*it;
}
}
return NULL;
}

bool NetBlock::is_resolved_address(const struct sockaddr_storage *ss) const {
if (this->resolvedaddrs.empty())
return false;
return sockaddr_storage_equal(&*this->resolvedaddrs.begin(), ss);
return sockaddr_storage_equal(first_af_address(&this->resolvedaddrs, ss->ss_family), ss);
}

NetBlockIPv4Ranges::NetBlockIPv4Ranges() {
Expand Down Expand Up @@ -610,10 +620,11 @@ NetBlock *NetBlockHostname::resolve() const {
struct addrinfo *addrs, *addr;
std::list<struct sockaddr_storage> resolvedaddrs;
NetBlock *netblock;
const struct sockaddr_storage *sp = NULL;
struct sockaddr_storage ss;
size_t sslen;

addrs = resolve_all(this->hostname.c_str(), this->af);
addrs = resolve_all(this->hostname.c_str(), AF_UNSPEC);
for (addr = addrs; addr != NULL; addr = addr->ai_next) {
if (addr->ai_addrlen < sizeof(ss)) {
memcpy(&ss, addr->ai_addr, addr->ai_addrlen);
Expand All @@ -626,7 +637,22 @@ NetBlock *NetBlockHostname::resolve() const {
if (resolvedaddrs.empty())
return NULL;

ss = *resolvedaddrs.begin();
sp = first_af_address(&resolvedaddrs, this->af);
if (sp == NULL or sp->ss_family != this->af) {
switch (this->af) {
case AF_INET:
error("Warning: Hostname %s resolves, but not to any IPv4 address. Try scanning with -6", this->hostname.c_str());
break;
case AF_INET6:
error("Warning: Hostname %s resolves, but not to any IPv6 address. Try scanning without -6", this->hostname.c_str());
break;
default:
error("Warning: Unknown address family: %d", this->af);
break;
}
return NULL;
}
ss = *sp;
sslen = sizeof(ss);

if (resolvedaddrs.size() > 1 && o.verbose > 1) {
Expand Down
13 changes: 8 additions & 5 deletions output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1426,14 +1426,17 @@ void write_host_header(Target *currenths) {
write_host_status(currenths);
if (currenths->TargetName() != NULL
&& currenths->resolved_addrs.size() > 1) {
std::list<struct sockaddr_storage>::iterator it;
const struct sockaddr_storage *hs_ss = currenths->TargetSockAddr();

log_write(LOG_PLAIN, "Other addresses for %s (not scanned):",
currenths->TargetName());
it = currenths->resolved_addrs.begin();
it++;
for (; it != currenths->resolved_addrs.end(); it++)
log_write(LOG_PLAIN, " %s", inet_ntop_ez(&*it, sizeof(*it)));
for (std::list<struct sockaddr_storage>::const_iterator it = currenths->resolved_addrs.begin(), end = currenths->resolved_addrs.end();
it != end; 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, "\n");
}
/* Print reverse DNS if it differs. */
Expand Down

0 comments on commit c0628fd

Please sign in to comment.