Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion libnmap/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 </nmaprun> 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.
Expand Down Expand Up @@ -305,6 +307,7 @@ def _parse_xml_host(cls, scanhost_data):
"tcpsequence",
"ipidsequence",
"tcptssequence",
"trace",
"times",
]
for xh in xelement:
Expand All @@ -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:
Expand Down Expand Up @@ -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 <trace> XML tag.

:param scantrace_data: <trace> 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 <trace>: {0}".format(xmltag.tag)
raise NmapParserException(exmsg)

return rdict

@staticmethod
def __format_element(elt_data):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black==20.8b1
black==24.3.0
defusedxml==0.6.0
isort==5.6.4
pre-commit
Expand Down
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Loading