Skip to content

Commit

Permalink
few facelift and botox injections for libnmap
Browse files Browse the repository at this point in the history
  • Loading branch information
savon-noir committed Apr 6, 2014
1 parent fba3026 commit 3119f4e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,12 @@
v0.4.5, 06/04/2014 -- minor fixes and botox injections
- Added "incomplete" argument in NmapReport.parse()
in order to enable parsing of incomplete or interrupted
nmap scans. Could be useful to use with a background
scan to parse incomplete data blocks from callback
function (thanks @Sibwara for the idea).
- Fixed bug when NmapReport.summary is empty
- added NmapReport.endtimestr
- remplaced ElementTree by cElementTree (performance)
v0.4.4, 04/04/2014 -- Bugs and features requests
- added support for tunnel attribute from <service> tag
- added support for servicefp (service fingerprint) in
Expand Down
6 changes: 2 additions & 4 deletions TODO
@@ -1,12 +1,10 @@
- review OS fingerprint API and add NmapOSFP class
- add unit test for os fingerprint class
- complete unit tests
- complete unit tests with coverall support
- review and improve threading for NmapProcess
- XML interface to support both ElementTree and lxml APIs
- support for python3
- support for windows and cywin
- support for windows
- Add new plugins to support import/export from mysql, couchdb, csv
- support iterators for NmapReport::hosts
- add unittest for udp scans, ping sweeping
- add support for 'resume' capability (see nmap --resume)
- add support for "not shown ports" (extra ports) in NmapHost (via NmapParser)
21 changes: 21 additions & 0 deletions libnmap/objects/report.py
Expand Up @@ -153,6 +153,21 @@ def endtime(self):
pass
return rval

@property
def endtimestr(self):
"""
Accessor returning a human readable time string
of when the scan ended.
:return: string
"""
rval = ''
try:
rval = self._runstats['finished']['timestr']
except(KeyError, TypeError, ValueError):
pass
return rval

@property
def summary(self):
"""
Expand All @@ -167,6 +182,12 @@ def summary(self):
except(KeyError, TypeError):
pass

if len(rval) == 0:
rval = ("Nmap ended at {0} ; {1} IP addresses ({2} hosts up)"
" scanned in {3} seconds".format(self.endtimestr,
self.hosts_total,
self.hosts_up,
self.elapsed))
return rval

@property
Expand Down
23 changes: 19 additions & 4 deletions libnmap/parser.py
@@ -1,11 +1,14 @@
#!/usr/bin/env python
import xml.etree.ElementTree as ET
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
from libnmap.objects import NmapHost, NmapService, NmapReport


class NmapParser(object):
@classmethod
def parse(cls, nmap_data=None, data_type='XML'):
def parse(cls, nmap_data=None, data_type='XML', incomplete=False):
"""
Generic class method of NmapParser class.
The data to be parsed does not need to be a complete nmap
Expand All @@ -22,22 +25,27 @@ def parse(cls, nmap_data=None, data_type='XML'):
:param data_type: specifies the type of data to be parsed.
:type data_type: string ("XML"|"JSON"|"YAML").
: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.
:type incomplete: boolean
As of today, only XML parsing is supported.
:return: NmapObject (NmapHost, NmapService or NmapReport)
"""

nmapobj = None
if data_type == "XML":
nmapobj = cls._parse_xml(nmap_data)
nmapobj = cls._parse_xml(nmap_data, incomplete)
else:
raise NmapParserException("Unknown data type provided. "
"Please check documentation for "
"supported data types.")
return nmapobj

@classmethod
def _parse_xml(cls, nmap_data=None):
def _parse_xml(cls, nmap_data=None, incomplete=False):
"""
Protected class method used to process a specific data type.
In this case: XML. This method is called by cls.parse class
Expand All @@ -58,6 +66,11 @@ def _parse_xml(cls, nmap_data=None):
4. a list of hosts: <hosts/> tag (TODO)
5. a list of ports: <ports/> tag
: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.
:type incomplete: boolean
:return: NmapObject (NmapHost, NmapService or NmapReport)
or a list of NmapObject
"""
Expand All @@ -69,6 +82,8 @@ def _parse_xml(cls, nmap_data=None):
raise NmapParserException("wrong nmap_data type given as "
"argument: cannot parse data")

if incomplete:
nmap_data += "</nmaprun>"
try:
root = ET.fromstring(nmap_data)
except:
Expand Down

0 comments on commit 3119f4e

Please sign in to comment.