From 16d23c07754d06b1c51e8c4f9db60666b3f93a4c Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 13:22:53 +0300 Subject: [PATCH 01/10] Adds trace key --- libnmap/parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libnmap/parser.py b/libnmap/parser.py index 21f6a12..1d40e07 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -305,6 +305,7 @@ def _parse_xml_host(cls, scanhost_data): "tcpsequence", "ipidsequence", "tcptssequence", + "trace" "times", ] for xh in xelement: From d21dc98019153768642b7ad9bc2c5bfae1753bcc Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 13:26:35 +0300 Subject: [PATCH 02/10] Small change --- libnmap/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnmap/parser.py b/libnmap/parser.py index 1d40e07..b99e6ae 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -305,7 +305,7 @@ def _parse_xml_host(cls, scanhost_data): "tcpsequence", "ipidsequence", "tcptssequence", - "trace" + "trace", "times", ] for xh in xelement: From f17139cde1d3c134303d480d5343e2658544eae7 Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 14:29:59 +0300 Subject: [PATCH 03/10] Adds trace parsing --- libnmap/parser.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/libnmap/parser.py b/libnmap/parser.py index b99e6ae..7907b5b 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -327,6 +327,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: @@ -681,6 +684,30 @@ 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) + + rdict = {} + for xmltag in xelement: + if xmltag.tag in ["port", "proto", "hop"]: + rdict[xmltag.tag] = cls.__format_attributes(xmltag) + else: + exmsg = "Unexcepted node in : {0}".format(xmltag.tag) + raise NmapParserException(exmsg) + + return rdict + @staticmethod def __format_element(elt_data): """ From 53072527065fa193c4417f91e08efc4da4b274f1 Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 15:26:26 +0300 Subject: [PATCH 04/10] Adds more fields to tarce --- libnmap/parser.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libnmap/parser.py b/libnmap/parser.py index 7907b5b..dd86884 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -675,6 +675,7 @@ def __parse_runstats(cls, scanrunstats_data): xelement = cls.__format_element(scanrunstats_data) rdict = {} + rdict["port"] for xmltag in xelement: if xmltag.tag in ["finished", "hosts"]: rdict[xmltag.tag] = cls.__format_attributes(xmltag) @@ -697,11 +698,15 @@ def __parse_trace(cls, scantrace_data): """ xelement = cls.__format_element(scantrace_data) + _trace_attrs = cls.__format_attributes(xelement) rdict = {} + rdict["port"] = _trace_attrs["port"] + rdict["proto"] = _trace_attrs["proto"] + rdict["hops"] = [] for xmltag in xelement: - if xmltag.tag in ["port", "proto", "hop"]: - rdict[xmltag.tag] = cls.__format_attributes(xmltag) + if xmltag.tag in ["hop"]: + rdict["hops"].append(cls.__format_attributes(xmltag)) else: exmsg = "Unexcepted node in : {0}".format(xmltag.tag) raise NmapParserException(exmsg) From 3a4fa75da4688f5beef06a6bc6f59f58c5d1537b Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 15:36:44 +0300 Subject: [PATCH 05/10] WIP: fix --- libnmap/parser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/libnmap/parser.py b/libnmap/parser.py index dd86884..718dac6 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -675,7 +675,6 @@ def __parse_runstats(cls, scanrunstats_data): xelement = cls.__format_element(scanrunstats_data) rdict = {} - rdict["port"] for xmltag in xelement: if xmltag.tag in ["finished", "hosts"]: rdict[xmltag.tag] = cls.__format_attributes(xmltag) From a4b355b7f5b10ebbc015e0748fedcfb1f6af743b Mon Sep 17 00:00:00 2001 From: dimeko Date: Wed, 10 Apr 2024 21:46:20 +0300 Subject: [PATCH 06/10] Adds conditions --- libnmap/parser.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libnmap/parser.py b/libnmap/parser.py index 718dac6..4bb9123 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -700,8 +700,12 @@ def __parse_trace(cls, scantrace_data): _trace_attrs = cls.__format_attributes(xelement) rdict = {} - rdict["port"] = _trace_attrs["port"] - rdict["proto"] = _trace_attrs["proto"] + if "proto" not in _trace_attrs: + rdict["port"] = _trace_attrs["port"] + + if "port" not in _trace_attrs: + rdict["port"] = _trace_attrs["proto"] + rdict["hops"] = [] for xmltag in xelement: if xmltag.tag in ["hop"]: From b9a42aa8bd5e446bb06460808f91c54e617efcdb Mon Sep 17 00:00:00 2001 From: dimeko Date: Tue, 16 Apr 2024 15:32:12 +0300 Subject: [PATCH 07/10] Fixes proto and port retrieval --- libnmap/parser.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libnmap/parser.py b/libnmap/parser.py index 4bb9123..52d78b7 100644 --- a/libnmap/parser.py +++ b/libnmap/parser.py @@ -700,11 +700,12 @@ def __parse_trace(cls, scantrace_data): _trace_attrs = cls.__format_attributes(xelement) rdict = {} - if "proto" not in _trace_attrs: - rdict["port"] = _trace_attrs["port"] - if "port" not in _trace_attrs: - rdict["port"] = _trace_attrs["proto"] + 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: From d6f484fc8a4bb766eec18013c01feea1ec93c9e7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:50:20 +0000 Subject: [PATCH 08/10] Bump black from 20.8b1 to 24.3.0 Bumps [black](https://github.com/psf/black) from 20.8b1 to 24.3.0. - [Release notes](https://github.com/psf/black/releases) - [Changelog](https://github.com/psf/black/blob/main/CHANGES.md) - [Commits](https://github.com/psf/black/commits/24.3.0) --- updated-dependencies: - dependency-name: black dependency-type: direct:development ... Signed-off-by: dependabot[bot] --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From ed0619e9bb9648ce99d52c447c5dde8b6f5449d1 Mon Sep 17 00:00:00 2001 From: Alexandre ZANNI <16578570+noraj@users.noreply.github.com> Date: Fri, 7 Jul 2023 14:33:53 +0200 Subject: [PATCH 09/10] fix python 2 compat fix #138 --- setup.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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", From 6c0ee1ab3c5962cc124f8c4c7d8f51f0591c989f Mon Sep 17 00:00:00 2001 From: Martin Schobert Date: Tue, 12 Dec 2023 17:02:37 +0100 Subject: [PATCH 10/10] Update parser.py documentation with a warning/clarification. --- libnmap/parser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libnmap/parser.py b/libnmap/parser.py index 52d78b7..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.