From ccd55623c7bfc2eea8eb6fa7f7bc6cee3d64b02c Mon Sep 17 00:00:00 2001 From: david Date: Wed, 18 Mar 2009 00:09:14 +0000 Subject: [PATCH] Record OS information in Ndiff. --- ndiff/ndiff | 8 ++++++++ ndiff/ndifftest.py | 7 +++++++ ndiff/test-scans/os.xml | 27 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 ndiff/test-scans/os.xml diff --git a/ndiff/ndiff b/ndiff/ndiff index d929c8923..4df9b9433 100755 --- a/ndiff/ndiff +++ b/ndiff/ndiff @@ -119,6 +119,7 @@ class Host(object): self.addresses = [] self.hostnames = [] self.ports = PortDict() + self.os = [] def get_id(self): """Return an id that is used to determine if hosts are "the same" across @@ -730,6 +731,13 @@ class NmapContentHandler(xml.sax.handler.ContentHandler): self.current_port.service.product = attrs.get(u"product") self.current_port.service.version = attrs.get(u"version") self.current_port.service.extrainfo = attrs.get(u"extrainfo") + elif name == u"osmatch": + assert self.parent_element() == u"os" + assert self.current_host is not None + if not attrs.has_key(u"name"): + warn("osmatch element of host %s is missing the \"name\" attribute; skipping." % self.current_host.format_name()) + return + self.current_host.os.append(attrs[u"name"]) elif name == u"finished": assert self.parent_element() == u"runstats" if attrs.has_key(u"time"): diff --git a/ndiff/ndifftest.py b/ndiff/ndifftest.py index 202bdb357..8b6bd7ac7 100755 --- a/ndiff/ndifftest.py +++ b/ndiff/ndifftest.py @@ -487,6 +487,13 @@ class scan_test(unittest.TestCase): self.assertTrue(len(host.hostnames) == 1) self.assertTrue(host.hostnames[0] == u"scanme.nmap.org") + def test_os(self): + """Test that OS information is recorded.""" + scan = Scan() + scan.load_from_file("test-scans/os.xml") + host = scan.hosts[0] + self.assertTrue(len(host.os) > 0) + # This test is commented out because Nmap XML doesn't store any information # about down hosts, not even the fact that they are down. Recovering the list of # scanned hosts to infer which ones are down would involve parsing the targets diff --git a/ndiff/test-scans/os.xml b/ndiff/test-scans/os.xml new file mode 100644 index 000000000..ebccf5f35 --- /dev/null +++ b/ndiff/test-scans/os.xml @@ -0,0 +1,27 @@ + + + + + + + + +
+ + + + + + + + + + + + + + + + + +