From 373a704c58a9d3dc7120cd457d67f99ebb50f234 Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 13:22:53 +0300 Subject: [PATCH 1/7] 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 11c826945f1be151a1714aa87887da26a41aec5c Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 13:26:35 +0300 Subject: [PATCH 2/7] 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 a6a0de9a2609129806bf2361bf164f3eb4d93685 Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 14:29:59 +0300 Subject: [PATCH 3/7] 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 5cf77ed6062f15ab3301c993fe8edab840e881de Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 15:26:26 +0300 Subject: [PATCH 4/7] 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 5fe7654df36f49e1b998517bcdb1e95ce58045b2 Mon Sep 17 00:00:00 2001 From: dimeko Date: Thu, 4 Apr 2024 15:36:44 +0300 Subject: [PATCH 5/7] 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 bdf35ffcdc54b0b7e67d0d82048a07e96309bd81 Mon Sep 17 00:00:00 2001 From: dimeko Date: Wed, 10 Apr 2024 21:46:20 +0300 Subject: [PATCH 6/7] 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 af3dd817d15213b180ad81d827d55e6585b86855 Mon Sep 17 00:00:00 2001 From: dimeko Date: Tue, 16 Apr 2024 15:32:12 +0300 Subject: [PATCH 7/7] 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: