diff --git a/Target.cc b/Target.cc index 227b4c0f1..5174e35fe 100644 --- a/Target.cc +++ b/Target.cc @@ -346,6 +346,16 @@ void Target::setTargetName(const char *name) { } } +void Target::addTargetNameAlias(const char *name) { + if (name) + target_aliases.push_back(std::string(name)); +} + +const std::vector &Target::getTargetNameAliases() const { + return target_aliases; +} + + /* Generates a printable string consisting of the host's IP address and hostname (if available). Eg "www.insecure.org (64.71.184.53)" or "fe80::202:e3ff:fe14:1102". The name is diff --git a/Target.h b/Target.h index e73aea32e..730dabc1b 100644 --- a/Target.h +++ b/Target.h @@ -184,6 +184,11 @@ class Target { order */ void setTargetName(const char *name); + /* Add an additional user-supplied name that resolved to the same IP as this + target (recorded when --unique suppresses duplicate scanning). */ + void addTargetNameAlias(const char *name); + /* Returns the list of additional user-supplied names for this target. */ + const std::vector &getTargetNameAliases() const; /* If the host is directly connected on a network, set and retrieve that information here. directlyConnected() will abort if it hasn't @@ -262,6 +267,8 @@ class Target { struct timeout_info to; char *hostname; // Null if unable to resolve or unset char * targetname; // The name of the target host given on the command line if it is a named host + std::vector target_aliases; /* Additional user-supplied names that resolved to this + same IP, collected when --unique suppresses duplicate scanning. */ struct probespec traceroute_probespec; std::list traceroute_hops; diff --git a/output.cc b/output.cc index 995949f92..36be0a23d 100644 --- a/output.cc +++ b/output.cc @@ -1271,7 +1271,7 @@ static void write_xml_initial_hostinfo(const Target *currenths, print_MAC_XML_Info(currenths); /* Output a hostnames element whenever we have a name to write or the target is up. */ - if (currenths->TargetName() != NULL || *currenths->HostName() || strcmp(status, "up") == 0) { + if (currenths->TargetName() != NULL || *currenths->HostName() || strcmp(status, "up") == 0 || !currenths->getTargetNameAliases().empty()) { xml_start_tag("hostnames"); xml_newline(); if (currenths->TargetName() != NULL) { @@ -1281,6 +1281,15 @@ static void write_xml_initial_hostinfo(const Target *currenths, xml_close_empty_tag(); xml_newline(); } + /* Print additional names collected when --unique suppressed duplicate scanning. */ + for (std::vector::const_iterator alias = currenths->getTargetNameAliases().begin(); + alias != currenths->getTargetNameAliases().end(); ++alias) { + xml_open_start_tag("hostname"); + xml_attribute("name", "%s", alias->c_str()); + xml_attribute("type", "user"); + xml_close_empty_tag(); + xml_newline(); + } if (*currenths->HostName()) { xml_open_start_tag("hostname"); xml_attribute("name", "%s", currenths->HostName()); diff --git a/targets.cc b/targets.cc index 80427eb31..7255bbc01 100644 --- a/targets.cc +++ b/targets.cc @@ -473,6 +473,27 @@ bool HostGroupState::get_next_host(struct sockaddr_storage *ss, size_t *sslen, s return false; } } + /* This IP is already in the exclude list (a --unique duplicate). + At this point *ss holds the excluded IP. If it came from a named + host, attach that name as an alias on the existing Target in the + current batch so it appears in XML output. + + Note: Nmap's streaming XML architecture finalizes and closes blocks + after each batch. Therefore, it is impossible to attach aliases to duplicates + that occur in a later batch than the original target without buffering + + all XML in memory. We restrict our search to the current batch. */ + if (o.unique && current_group.get_namedhost() && current_group.is_resolved_address(ss)) { + const char *skipped_name = current_group.get_resolved_name(); + if (skipped_name) { + for (int i = 0; i < current_batch_sz; i++) { + if (sockaddr_storage_cmp(hostbatch[i]->TargetSockAddr(), ss) == 0) { + hostbatch[i]->addTargetNameAlias(skipped_name); + break; + } + } + } + } } while (true); return true; diff --git a/targets.h b/targets.h index 835d0dff2..37d92bde7 100644 --- a/targets.h +++ b/targets.h @@ -66,6 +66,7 @@ #include "TargetGroup.h" #include +#include #include class Target;