Skip to content

Commit cd3ffbd

Browse files
committed
temporary fix for issue40
1 parent ff63931 commit cd3ffbd

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

libnmap/objects/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
from libnmap.objects.host import NmapHost
44
from libnmap.objects.report import NmapReport
5-
from libnmap.objects.service import NmapService
5+
from libnmap.objects.service import NmapService, NmapExtraPort
66

7-
__all__ = ["NmapReport", "NmapHost", "NmapService"]
7+
__all__ = ['NmapReport', 'NmapHost', 'NmapService', 'NmapExtraPort']

libnmap/objects/host.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(
3838
self._status = status if status is not None else {}
3939
self._services = services if services is not None else []
4040
self._extras = extras if extras is not None else {}
41+
self._extraports = self._extras.get('extraports', None)
4142
self._osfingerprinted = False
4243
self.os = None
4344
if "os" in self._extras:
@@ -469,12 +470,13 @@ def extraports_state(self):
469470
470471
:return: dict with keys 'state' and 'count' or None
471472
"""
472-
_xtrports = self._extras.get("extraports", None)
473+
rval = None
474+
_xports = self._extras.get('extraports', None)
473475

474-
if _xtrports is None:
475-
return None
476+
if _xports is not None:
477+
rval = {'state': _xtrports['state'], 'count': _xtrports['count']}
476478

477-
return {"state": _xtrports["state"], "count": _xtrports["count"]}
479+
return rval
478480

479481
@property
480482
def extraports_reasons(self):

libnmap/objects/service.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,51 @@ def diff(self, other):
366366
:return: NmapDiff object
367367
"""
368368
return NmapDiff(self, other)
369+
370+
371+
class NmapExtraPort(object):
372+
"""
373+
NmapExtraPort is an object which documents unlisted ports/services
374+
which are possibly closed, filtered, ignored,...
375+
"""
376+
def __init__(self, xdict):
377+
"""
378+
Constructor
379+
:param xdict: python dict containing the following structure:
380+
{
381+
'state': <str>,
382+
'count': <int>,
383+
'reasons': [{'reason': <str>, 'count' <int>}]
384+
}
385+
"""
386+
self._count = xdict.get('count', 0)
387+
self._state = xdict.get('state', 'unknown')
388+
self._reasons = xdict.get('reasons', [])
389+
390+
@property
391+
def count(self):
392+
"""
393+
Accessor for the number of extraports
394+
395+
:return: int
396+
"""
397+
return int(self._count)
398+
399+
@property
400+
def state(self):
401+
"""
402+
Accessor for the state of extraports listed
403+
404+
:return: string
405+
"""
406+
return self._state
407+
408+
@property
409+
def reason(self):
410+
"""
411+
Return the first reason available for the extraport listed.
412+
413+
:return: dict, empty if no extraports reason available
414+
"""
415+
416+
return self.reasons[0] if len(self.reasons) else {}

libnmap/parser.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import xml.etree.cElementTree as ET
99
except ImportError:
1010
import xml.etree.ElementTree as ET
11-
1211
from xml.etree.ElementTree import iselement as et_iselement
13-
14-
from libnmap.objects import NmapHost, NmapReport, NmapService
12+
from libnmap.objects import NmapHost, NmapService, NmapReport, NmapExtraPort
1513

1614

1715
class NmapParser(object):
@@ -385,17 +383,18 @@ def _parse_xml_ports(cls, scanports_data):
385383

386384
xelement = cls.__format_element(scanports_data)
387385

388-
rdict = {"ports": [], "extraports": None}
386+
rdict = {'ports': [], 'extraports': []}
389387
for xservice in xelement:
390388
if xservice.tag == "port":
391389
nport = cls._parse_xml_port(xservice)
392390
rdict["ports"].append(nport)
393391
elif xservice.tag == "extraports":
394392
extraports = cls.__parse_extraports(xservice)
395-
rdict["extraports"] = extraports
396-
# else:
397-
# print "struct port unknown attr: %s value: %s" %
398-
# (h.tag, h.get(h.tag))
393+
rdict['extraports'].append(extraports)
394+
# DEBUG REMOVE ME
395+
else:
396+
print "struct port unknown attr: %s value: %s" %
397+
(h.tag, h.get(h.tag))
399398
return rdict
400399

401400
@classmethod
@@ -485,8 +484,9 @@ def __parse_extraports(cls, extraports_data):
485484
for xelt in xelement:
486485
if xelt.tag == "extrareasons":
487486
extrareasons_dict = cls.__format_attributes(xelt)
488-
rdict["reasons"].append(extrareasons_dict)
489-
return rdict
487+
rdict['reasons'].append(extrareasons_dict)
488+
robj = NmapExtraPort(rdict)
489+
return robj
490490

491491
@classmethod
492492
def __parse_script_table(cls, script_table):

0 commit comments

Comments
 (0)