libnmap.parser

Purpose of libnmap.parser

This modules enables you to parse nmap scans’ output. For now on, only XML parsing is supported. NmapParser is a factory which will return a NmapReport, NmapHost or NmapService object. All these objects’ API are documented.

The module is capable of parsing:

  • a complete nmap XML scan report
  • an incomplete/interrupted nmap XML scan report
  • partial nmap xml tags: <host>, <ports> and <port>

Input the above capabilities could be either a string or a file path.

Based on the provided data, NmapParse.parse() could return the following:

  • NmapReport object: in case a full nmap xml/dict report was prodivded
  • NmapHost object: in case a nmap xml <host> section was provided
  • NmapService object: in case a nmap xml <port> section was provided
  • Python dict with following keys: ports and extraports; python lists.

Using libnmap.parser module

NmapParser parse the whole data and returns nmap objects usable via their documented API.

The NmapParser should never be instanciated and only the following methods should be called:

  • NmapParser.parse(string)
  • NmapParser.parse_fromfile(file_path)
  • NmapParser.parse_fromstring(string)

All of the above methods can receive as input:

  • a full XML nmap scan result and returns a NmapReport object
  • a scanned host in XML (<host>…</host> tag) and will return a NmapHost object
  • a list of scanned services in XML (<ports>…</ports> tag) and will return a python array of NmapService objects
  • a scanned service in XML (<port>…</port> tag) and will return a NmapService object

Small example:

from libnmap.parser import NmapParser

nmap_report = NmapParser.parse_fromfile('libnmap/test/files/1_os_banner_scripts.xml')
print "Nmap scan summary: {0}".format(nmap_report.summary)

Basic usage from a processed scan:

from libnmap.process import NmapProcess
from libnmap.parser import NmapParser

nm = NmapProcess("127.0.0.1, scanme.nmap.org")
nm.run()

nmap_report = NmapParser.parse(nm.stdout)

for scanned_hosts in nmap_report.hosts:
    print scanned_hosts

For more details on using the results from NmapParser, refer to the API of class: NmapReport, NmapHost, NmapService.

NmapParser methods

class libnmap.parser.NmapParser[source]
classmethod parse(nmap_data=None, data_type='XML', incomplete=False)[source]

Generic class method of NmapParser class.

The data to be parsed does not need to be a complete nmap scan report. You can possibly give <hosts>…</hosts> or <port> XML tags.

Parameters:
  • nmap_data (string) – any portion of nmap scan result. nmap_data should always be a string representing a part or a complete nmap scan report.
  • data_type (string ("XML"|"JSON"|"YAML")) – specifies the type of data to be parsed.
  • incomplete (boolean) – enable you to parse interrupted nmap scans and/or incomplete nmap xml blocks by adding a </nmaprun> at the end of the scan.

As of today, only XML parsing is supported.

Returns:NmapObject (NmapHost, NmapService or NmapReport)
classmethod parse_fromdict(rdict)[source]

Strange method which transforms a python dict representation of a NmapReport and turns it into an NmapReport object. Needs to be reviewed and possibly removed.

Parameters:rdict (dict) – python dict representation of an NmapReport
Returns:NmapReport
classmethod parse_fromfile(nmap_report_path, data_type='XML', incomplete=False)[source]

Call generic cls.parse() method and ensure that a correct file path is given as argument. If not, an exception is raised.

Parameters:
  • nmap_data – Same as for parse(). Any portion of nmap scan reports could be passed as argument. Data type _must be a valid path to a file containing nmap scan results.
  • data_type – Specifies the type of serialization in the file.
  • incomplete (boolean) – enable you to parse interrupted nmap scans and/or incomplete nmap xml blocks by adding a </nmaprun> at the end of the scan.
Returns:

NmapObject

classmethod parse_fromstring(nmap_data, data_type='XML', incomplete=False)[source]

Call generic cls.parse() method and ensure that a string is passed on as argument. If not, an exception is raised.

Parameters:
  • nmap_data (string) – Same as for parse(), any portion of nmap scan. Reports could be passed as argument. Data type _must_ be a string.
  • data_type – Specifies the type of data passed on as argument.
  • incomplete (boolean) – enable you to parse interrupted nmap scans and/or incomplete nmap xml blocks by adding a </nmaprun> at the end of the scan.
Returns:

NmapObject