From 367d5296f85e337bf406bc22df94ece6b874504a Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 22 Oct 2026 15:35:55 +0000 Subject: [PATCH] Fix parsing of multiple osclasses. Fixes #3560. Closes #3561. --- CHANGELOG | 4 + zenmap/test/xml_test_osclasses.xml | 17 ++++ zenmap/zenmapCore/NmapParser.py | 151 ++++++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 zenmap/test/xml_test_osclasses.xml diff --git a/CHANGELOG b/CHANGELOG index 441c846e5..7ee6bc2d4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,10 @@ o [NSE][GH#3557] Fixed a regression which prevented most scripts in the version o [Ncat] Updated Ncat's ca-bundle.crt. +o [Zenmap][GH#3561] Fix Zenmap's handling of multiple osclass elements in Nmap + XML. Previously, each subsequent osclass in a host would overwrite the + previous osclass. [Derek Martin] + Nmap 7.992 [2026-10-15] o Upgraded included libraries: diff --git a/zenmap/test/xml_test_osclasses.xml b/zenmap/test/xml_test_osclasses.xml new file mode 100644 index 000000000..0b6ee4b86 --- /dev/null +++ b/zenmap/test/xml_test_osclasses.xml @@ -0,0 +1,17 @@ + + + + + + + +
+ + + + + + + + + diff --git a/zenmap/zenmapCore/NmapParser.py b/zenmap/zenmapCore/NmapParser.py index e7c185401..ddd017f76 100644 --- a/zenmap/zenmapCore/NmapParser.py +++ b/zenmap/zenmapCore/NmapParser.py @@ -60,6 +60,8 @@ import locale import time import socket import copy +import unittest +import os from io import StringIO @@ -72,6 +74,7 @@ from xml.sax import SAXException from xml.sax.handler import ContentHandler, EntityResolver from xml.sax.saxutils import XMLGenerator from xml.sax.xmlreader import AttributesImpl as Attributes +import xml.etree.ElementTree as ET import zenmapCore.I18N # lgtm[py/unused-import] from zenmapCore.NmapOptions import NmapOptions, join_quoted @@ -940,12 +943,12 @@ class NmapParserSAX(ParserBasics, ContentHandler): self.in_os = True self.list_portused = [] self.list_osmatch = [] + self.list_osclass = [] elif self.in_host and self.in_os and name == "osmatch": self._parse_host_osmatch(attrs) elif self.in_host and self.in_os and name == "portused": self._parse_host_portused(attrs) elif self.in_host and self.in_os and name == "osclass": - self.list_osclass = [] self._parse_host_osclass(attrs) elif self.in_host and name == "uptime": self._parse_host_uptime(attrs) @@ -1315,6 +1318,152 @@ def nmap_parser_sax(): NmapParser = nmap_parser_sax +class NmapParserTest(unittest.TestCase): + def setUp(self): + self.xml = self.read_fixture("xml_test_osclasses.xml") + + @staticmethod + def read_fixture(name): + filename = os.path.join( + os.path.dirname(__file__), os.pardir, "test", name) + with open(filename, "r", encoding="utf-8") as source: + return source.read() + + def set_matches(self, groups): + root = ET.fromstring(self.xml) + os_element = root.find("./host/os") + template = os_element.find("./osmatch/osclass") + for match in list(os_element): + os_element.remove(match) + for index, group in enumerate(groups): + match = ET.SubElement( + os_element, "osmatch", + name="OS-%d" % index, accuracy="100", line="1") + for label in group: + osclass = copy.deepcopy(template) + osclass.set("vendor", "Vendor-%s" % label) + osclass.set("osfamily", "Family-%s" % label) + osclass.set("osgen", label) + match.append(osclass) + self.xml = ET.tostring(root, encoding="unicode") + + @staticmethod + def xml_matches(xml): + matches = [] + for element in ET.fromstring(xml).findall("./host/os/osmatch"): + match = dict(element.attrib) + match["osclasses"] = [ + dict(osclass.attrib) + for osclass in element.findall("osclass")] + matches.append(match) + return matches + + def assert_matches(self, matches, expected): + self.assertEqual(matches, expected) + # Each match owns its list, including empty matches. + for index, match in enumerate(matches): + for other in matches[index + 1:]: + self.assertIsNot(match["osclasses"], other["osclasses"]) + + def check_roundtrip(self): + expected = self.xml_matches(self.xml) + scan = NmapParser() + scan.parse(StringIO(self.xml)) + self.assert_matches(scan.hosts[0].osmatches, expected) + + saved = StringIO() + scan.write_xml(saved) + self.assertEqual(self.xml_matches(saved.getvalue()), expected) + + reparsed = NmapParser() + reparsed.parse(StringIO(saved.getvalue())) + self.assert_matches(reparsed.hosts[0].osmatches, expected) + + def test_single_class(self): + self.set_matches([["A"]]) + self.check_roundtrip() + + def test_multiple_classes(self): + self.check_roundtrip() + + def test_consecutive_matches(self): + self.set_matches([["A"], ["B"]]) + self.check_roundtrip() + + def test_consecutive_multiple_classes(self): + self.set_matches([["A", "B"], ["C", "D"]]) + self.check_roundtrip() + + def test_empty_match(self): + self.set_matches([[]]) + self.check_roundtrip() + + def test_empty_then_populated_match(self): + self.set_matches([[], ["A"]]) + self.check_roundtrip() + + def test_populated_empty_populated_matches(self): + self.set_matches([["A"], [], ["B"]]) + self.check_roundtrip() + + def test_no_matches(self): + self.set_matches([]) + self.check_roundtrip() + + def test_legacy_direct_classes(self): + scan = NmapParser() + scan.parse(StringIO(self.read_fixture("xml_test11.xml"))) + linux_classes = [ + dict(type="general purpose", vendor="Linux", + osfamily="Linux", osgen=gen, accuracy="100") + for gen in ("2.4.X", "2.5.X", "2.6.X")] + linux_match = dict( + name="Linux 2.4.18 - 2.6.7", accuracy="100", line="", + osclasses=linux_classes) + expected = [ + [linux_match], + [dict(name="Alcatel Speed Touch *DSL modem/router", + accuracy="100", line="", osclasses=[ + dict(type="broadband router", vendor="Alcatel", + osfamily="embedded", osgen="", + accuracy="100")])], + [linux_match], + [dict(name="Linux 2.5.25 - 2.6.3 or Gentoo 1.2 " + "Linux 2.4.19 rc1-rc7)", + accuracy="100", line="", osclasses=linux_classes), + dict(name="Linux 2.6.3 - 2.6.8", accuracy="100", line="", + osclasses=[])]] + self.assertEqual([host.osmatches for host in scan.hosts], expected) + + saved = StringIO() + scan.write_xml(saved) + # Legacy direct classes are serialized inside the first match. + root = ET.fromstring(saved.getvalue()) + self.assertEqual(root.findall("./host/os/osclass"), []) + self.assertEqual( + self.xml_matches(saved.getvalue()), + [match for matches in expected for match in matches]) + reparsed = NmapParser() + reparsed.parse(StringIO(saved.getvalue())) + self.assertEqual( + [host.osmatches for host in reparsed.hosts], expected) + + def test_existing_xml_fixtures_roundtrip(self): + for number in range(1, 18): + name = "xml_test%d.xml" % number + with self.subTest(fixture=name): + scan = NmapParser() + scan.parse(StringIO(self.read_fixture(name))) + expected = copy.deepcopy( + [host.osmatches for host in scan.hosts]) + saved = StringIO() + scan.write_xml(saved) + reparsed = NmapParser() + reparsed.parse(StringIO(saved.getvalue())) + self.assertEqual( + [host.osmatches for host in reparsed.hosts], expected) + + if __name__ == '__main__': import sys