mirror of
https://github.com/nmap/nmap.git
synced 2026-11-19 14:10:39 +00:00
Fix parsing of multiple osclasses. Fixes #3560. Closes #3561.
Some checks failed
nmap multiplatform autobuilds / build (arm64, gcc, ubuntu-latest-gcc-arm64, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, freebsd-15-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-15-clang, macos-15) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-26-clang, macos-26) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, netbsd-10-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, openbsd-7-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, solaris-11-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, ubuntu-latest-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (egcc, openbsd-7-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, freebsd-15-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, netbsd-10-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, solaris-11-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, ubuntu-latest-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (msvc, windows-latest-msvc, windows-latest) (push) Has been cancelled
Some checks failed
nmap multiplatform autobuilds / build (arm64, gcc, ubuntu-latest-gcc-arm64, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, freebsd-15-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-15-clang, macos-15) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, macos-26-clang, macos-26) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, netbsd-10-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, openbsd-7-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, solaris-11-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (clang, ubuntu-latest-clang, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (egcc, openbsd-7-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, freebsd-15-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, netbsd-10-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, solaris-11-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (gcc, ubuntu-latest-gcc, ubuntu-latest) (push) Has been cancelled
nmap multiplatform autobuilds / build (msvc, windows-latest-msvc, windows-latest) (push) Has been cancelled
This commit is contained in:
parent
628feef6d6
commit
367d5296f8
3 changed files with 171 additions and 1 deletions
|
|
@ -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:
|
||||
|
|
|
|||
17
zenmap/test/xml_test_osclasses.xml
Normal file
17
zenmap/test/xml_test_osclasses.xml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<nmaprun scanner="nmap" version="7.992+SVN" start="1000000000" args="synthetic saved XML fixture" xmloutputversion="1.05">
|
||||
<scaninfo type="syn" protocol="tcp" numservices="1" services="80"/>
|
||||
<verbose level="0"/>
|
||||
<debugging level="0"/>
|
||||
<host>
|
||||
<status state="up" reason="user-set" reason_ttl="0"/>
|
||||
<address addr="192.0.2.1" addrtype="ipv4"/>
|
||||
<os>
|
||||
<osmatch name="OS-first" accuracy="100" line="1">
|
||||
<osclass type="general purpose" vendor="Vendor-A" osfamily="Family-A" osgen="A" accuracy="100"/>
|
||||
<osclass type="general purpose" vendor="Vendor-B" osfamily="Family-B" osgen="B" accuracy="100"/>
|
||||
</osmatch>
|
||||
</os>
|
||||
</host>
|
||||
<runstats><finished time="1000000001" elapsed="1" exit="success"/><hosts up="1" down="0" total="1"/></runstats>
|
||||
</nmaprun>
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue