From 8769ab35f3f99aa7567e763cd3ffba2c2a2c490f Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 25 Jun 2026 21:58:48 +0000 Subject: [PATCH] Correctly handle root domain label. Credit: Himanshu Anand --- libnetutil/massdns.cc | 9 +++++++-- tests/nmap_dns_test.cc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/libnetutil/massdns.cc b/libnetutil/massdns.cc index ea0e96d2b..8308ecadf 100644 --- a/libnetutil/massdns.cc +++ b/libnetutil/massdns.cc @@ -1974,8 +1974,13 @@ size_t DNS::Factory::parseDomainName(std::string &name, const u8 *buf, size_t of max_offset++; } - std::string::iterator it = name.end()-1; - if( *it == '.') name.erase(it); + if (name.empty()) { + name = "."; + } + else { + std::string::iterator it = name.end()-1; + if( *it == '.') name.erase(it); + } return max_offset - offset; } diff --git a/tests/nmap_dns_test.cc b/tests/nmap_dns_test.cc index b1d858c76..876c43984 100644 --- a/tests/nmap_dns_test.cc +++ b/tests/nmap_dns_test.cc @@ -216,6 +216,41 @@ o.debugging = 1; plen = p.parseFromBuffer(inval_answer, sizeof(inval_answer)); TEST_INCR(plen == 0, ret, tot); + const u8 ptr_answer1[] = { 0x12, 0x34, // ID + 0x81, 0x80, // Flags + 0x00, 0x01, // Questions count + 0x00, 0x01, // Answers RRs count + 0x00, 0x00, // Authorities RRs count + 0x00, 0x00, // Additionals RRs count + 0x00, // Label length + 0x00, 0x0c, // PTR + 0x00, 0x01, // CLASS_IN + 0x00, // label length + 0x00, 0x0c, // PTR + 0x00, 0x01, // CLASS_IN + 0x00, 0x01, 0x51, 0x78, // TTL 86392 + 0x00, 0x01, // Record Length + 0x00, // Label length + }; + + plen = p.parseFromBuffer(ptr_answer1, sizeof(ptr_answer1)); + TEST_INCR(plen == sizeof(ptr_answer1), ret, tot); + TEST_INCR(p.id == 0x1234, ret, tot); + TEST_INCR(p.flags == 0x8180, ret, tot); + TEST_INCR(p.queries.size() == 1, ret, tot); + TEST_INCR(p.answers.size() == 1, ret, tot); + + q = &*p.queries.begin(); + TEST_INCR(q->name == ".", ret, tot); + TEST_INCR(q->record_class == DNS::CLASS_IN, ret, tot); + TEST_INCR(q->record_type == DNS::PTR, ret, tot); + + a = &*p.answers.begin(); + TEST_INCR(a->name == ".", ret, tot); + TEST_INCR(a->record_class == DNS::CLASS_IN, ret, tot); + TEST_INCR(a->record_type == DNS::PTR, ret, tot); + TEST_INCR(a->length == 0x01, ret, tot); + TEST_INCR(a->ttl == 86392, ret, tot); if(ret) std::cout << "Testing nmap_dns finished with errors" << std::endl; else std::cout << "Testing nmap_dns finished without errors" << std::endl;