diff --git a/libnmap/parser.py b/libnmap/parser.py index 21f6a12..67ff616 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -34,7 +34,9 @@ def parse(cls, nmap_data=None, data_type="XML", incomplete=False): :param incomplete: enable you to parse interrupted nmap scans \ and/or incomplete nmap xml blocks by adding a at \ - the end of the scan. + the end of the scan. Be aware that this flag does not work for \ + already valid XML files, because adding an XML tag will \ + invalidate the XML. :type incomplete: boolean As of today, only XML parsing is supported. @@ -305,6 +307,7 @@ def _parse_xml_host(cls, scanhost_data): "tcpsequence", "ipidsequence", "tcptssequence", + "trace", "times", ] for xh in xelement: @@ -326,6 +329,9 @@ def _parse_xml_host(cls, scanhost_data): elif xh.tag == "hostscript": _host_scripts = cls.__parse_host_scripts(xh) _host_extras.update({"hostscript": _host_scripts}) + elif xh.tag == "trace": + _trace = cls.__parse_trace(xh) + _host_extras.update({"trace": _trace}) elif xh.tag in extra_tags: _host_extras[xh.tag] = cls.__format_attributes(xh) # else: @@ -680,6 +686,39 @@ def __parse_runstats(cls, scanrunstats_data): return rdict + @classmethod + def __parse_trace(cls, scantrace_data): + """ + Private method parsing a portion of a nmap scan result. + Receives a XML tag. + + :param scantrace_data: XML tag from a nmap scan + :type scantrace_data: xml.ElementTree.Element or a string + + :return: python dict representing the XML trace tag + """ + + xelement = cls.__format_element(scantrace_data) + _trace_attrs = cls.__format_attributes(xelement) + + rdict = {} + + if "proto" in _trace_attrs: + rdict["proto"] = _trace_attrs["proto"] + + if "port" in _trace_attrs: + rdict["port"] = _trace_attrs["port"] + + rdict["hops"] = [] + for xmltag in xelement: + if xmltag.tag in ["hop"]: + rdict["hops"].append(cls.__format_attributes(xmltag)) + else: + exmsg = "Unexcepted node in : {0}".format(xmltag.tag) + raise NmapParserException(exmsg) + + return rdict + @staticmethod def __format_element(elt_data): """ diff --git a/requirements-dev.txt b/requirements-dev.txt index 2fbfb2f..56c9a07 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,4 @@ -black==20.8b1 +black==24.3.0 defusedxml==0.6.0 isort==5.6.4 pre-commit diff --git a/setup.py b/setup.py index d6427c6..a2ca71f 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,13 @@ # -*- coding: utf-8 -*- from distutils.core import setup +import sys -with open("README.rst", encoding="utf-8") as rfile: - long_description = rfile.read() +if sys.version_info >= (3,0): + with open("README.rst", encoding="utf-8") as rfile: + long_description = rfile.read() +else: # encoding not compatible with python2 + with open("README.rst") as rfile: + long_description = rfile.read() setup( name="python-libnmap",