class documentation

class NmapParser(object): (source)

View In Hierarchy

Undocumented

Class Method parse Generic class method of NmapParser class.
Class Method parse_fromdict 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.
Class Method parse_fromfile Call generic cls.parse() method and ensure that a correct file path is given as argument. If not, an exception is raised.
Class Method parse_fromstring Call generic cls.parse() method and ensure that a string is passed on as argument. If not, an exception is raised.
Class Method __parse_extraports Private method parsing the data from extra scanned ports. X extraports were in state "closed" server returned "conn-refused" tag: <extraports>
Class Method __parse_host_scripts Private method parsing the data from scripts affecting the target host. Contents of <hostscript> is returned as a list of dict.
Class Method __parse_hostnames Private method parsing the hostnames list within a <host> XML tag.
Class Method __parse_os_fingerprint Private method parsing the data from an OS fingerprint (-O). Contents of <os> is returned as a dict.
Class Method __parse_osclass This methods parses osclass data and returns a dict. Depending on the nmap xml version, osclass could contain a cpe dict.
Class Method __parse_osmatch This methods parses osmatch data and returns a dict. Depending on the nmap xml version, osmatch could contain an osclass dict.
Class Method __parse_runstats Private method parsing a portion of a nmap scan result. Receives a <runstats> XML tag.
Class Method __parse_scaninfo Private method parsing a portion of a nmap scan result. Receives a <scaninfo> XML tag.
Class Method __parse_script Private method parsing the data from NSE scripts output
Class Method __parse_script_table Private method parsing a table from NSE scripts output
Class Method __parse_service Parse <service> tag to manage CPE object
Class Method _parse_xml Protected class method used to process a specific data type. In this case: XML. This method is called by cls.parse class method and receives nmap scan results data (in XML).
Class Method _parse_xml_host Protected method parsing a portion of a nmap scan result. Receives a <host> XML tag representing a scanned host with its services.
Class Method _parse_xml_port Protected method parsing a scanned service from a targeted host. This protected method cannot be called directly. A <port/> tag can be directly passed to parse() and the below method will be called and return a NmapService object representing the state of the service.
Class Method _parse_xml_ports Protected method parsing the list of scanned services from a targeted host. This protected method cannot be called directly with a string. A <ports/> tag can be directly passed to parse() and the below method will be called and return a list of nmap scanned services.
Class Method _parse_xml_report This method parses out a full nmap scan report from its XML root node: <nmaprun>.
Static Method __format_attributes Private method which converts a single XML tag to a python dict. It also checks that the elt_data given as argument is of type xml.etree.ElementTree.Element
Static Method __format_element Private method which ensures that a XML portion to be parsed is of type xml.etree.ElementTree.Element. If elt_data is a string, then it is converted to an XML Element type.
@classmethod
def parse(cls, 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. :param nmap_data: any portion of nmap scan result. nmap_data should always be a string representing a part or a complete nmap scan report. :type nmap_data: string :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)

@classmethod
def parse_fromdict(cls, 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. :param rdict: python dict representation of an NmapReport :type rdict: dict :return: NmapReport

@classmethod
def parse_fromfile(cls, 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. :param 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. :param data_type: Specifies the type of serialization in the file. :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

@classmethod
def parse_fromstring(cls, 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. :param nmap_data: Same as for parse(), any portion of nmap scan. Reports could be passed as argument. Data type _must_ be a string. :type nmap_data: string :param data_type: Specifies the type of data passed on as argument. :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

@classmethod
def __parse_extraports(cls, extraports_data): (source)

Private method parsing the data from extra scanned ports. X extraports were in state "closed" server returned "conn-refused" tag: <extraports> :param extraports_data: XML data for extraports :type extraports_data: xml.ElementTree.Element or a string :return: python dict with following keys: state, count, reason

@classmethod
def __parse_host_scripts(cls, scripts_data): (source)

Private method parsing the data from scripts affecting the target host. Contents of <hostscript> is returned as a list of dict. :param scripts_data: portion of XML describing the results of the scripts data :type scripts_data: xml.ElementTree.Element or a string :return: python list holding scripts output in a dict

@classmethod
def __parse_hostnames(cls, scanhostnames_data): (source)

Private method parsing the hostnames list within a <host> XML tag. :param scanhostnames_data: <hostnames> XML tag from a nmap scan :type scanhostnames_data: xml.ElementTree.Element or a string :return: list of hostnames

@classmethod
def __parse_os_fingerprint(cls, os_data): (source)

Private method parsing the data from an OS fingerprint (-O). Contents of <os> is returned as a dict. :param os_data: portion of XML describing the results of the os fingerprinting attempt :type os_data: xml.ElementTree.Element or a string :return: python dict representing the XML os tag

@classmethod
def __parse_osclass(cls, osclass_data): (source)

This methods parses osclass data and returns a dict. Depending on the nmap xml version, osclass could contain a cpe dict. :param osclass_data: <osclass> XML tag from a nmap scan :type osclass_data: xml.ElementTree.Element or a string :return: python dict representing the XML osclass tag

@classmethod
def __parse_osmatch(cls, osmatch_data): (source)

This methods parses osmatch data and returns a dict. Depending on the nmap xml version, osmatch could contain an osclass dict. :param osmatch_data: <osmatch> XML tag from a nmap scan :type osmatch_data: xml.ElementTree.Element or a string :return: python dict representing the XML osmatch tag

@classmethod
def __parse_runstats(cls, scanrunstats_data): (source)

Private method parsing a portion of a nmap scan result. Receives a <runstats> XML tag. :param scanrunstats_data: <runstats> XML tag from a nmap scan :type scanrunstats_data: xml.ElementTree.Element or a string :return: python dict representing the XML runstats tag

@classmethod
def __parse_scaninfo(cls, scaninfo_data): (source)

Private method parsing a portion of a nmap scan result. Receives a <scaninfo> XML tag. :param scaninfo_data: <scaninfo> XML tag from a nmap scan :type scaninfo_data: xml.ElementTree.Element or a string :return: python dict representing the XML scaninfo tag

@classmethod
def __parse_script(cls, script_data): (source)

Private method parsing the data from NSE scripts output :param script_data: portion of XML describing the results of the script data :type script_data: xml.ElementTree.Element or a string :return: python dict holding scripts output

@classmethod
def __parse_script_table(cls, script_table): (source)

Private method parsing a table from NSE scripts output :param sccript_table: poertion of XML containing the table :type script_table: xml.ElementTree.Element :return: python dict of table structure

@classmethod
def __parse_service(cls, xserv): (source)

Parse <service> tag to manage CPE object

@classmethod
def _parse_xml(cls, nmap_data=None, incomplete=False): (source)

Protected class method used to process a specific data type. In this case: XML. This method is called by cls.parse class method and receives nmap scan results data (in XML). :param nmap_data: any portion of nmap scan result can be given as argument. nmap_data should always be a string representing a part or a complete nmap scan report. :type nmap_data: string This method checks which portion of a nmap scan is given as argument. It could be: 1. a full nmap scan report; 2. a scanned host: <host> tag in a nmap scan report 3. a scanned service: <port> tag 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

@classmethod
def _parse_xml_host(cls, scanhost_data): (source)

Protected method parsing a portion of a nmap scan result. Receives a <host> XML tag representing a scanned host with its services. :param scaninfo_data: <host> XML tag from a nmap scan :type scaninfo_data: xml.ElementTree.Element or a string :return: NmapHost object

@classmethod
def _parse_xml_port(cls, scanport_data): (source)

Protected method parsing a scanned service from a targeted host. This protected method cannot be called directly. A <port/> tag can be directly passed to parse() and the below method will be called and return a NmapService object representing the state of the service. :param scanport_data: <port> XML tag from a nmap scan :type scanport_data: xml.ElementTree.Element or a string :return: NmapService

@classmethod
def _parse_xml_ports(cls, scanports_data): (source)

Protected method parsing the list of scanned services from a targeted host. This protected method cannot be called directly with a string. A <ports/> tag can be directly passed to parse() and the below method will be called and return a list of nmap scanned services. :param scanports_data: <ports> XML tag from a nmap scan :type scanports_data: xml.ElementTree.Element or a string :return: list of NmapService

@classmethod
def _parse_xml_report(cls, root=None): (source)

This method parses out a full nmap scan report from its XML root node: <nmaprun>. :param root: Element from xml.ElementTree (top of XML the document) :type root: Element :return: NmapReport object

@staticmethod
def __format_attributes(elt_data): (source)

Private method which converts a single XML tag to a python dict. It also checks that the elt_data given as argument is of type xml.etree.ElementTree.Element :param elt_data: XML Element to be parsed or string to be converted to a XML Element :return: Element

@staticmethod
def __format_element(elt_data): (source)

Private method which ensures that a XML portion to be parsed is of type xml.etree.ElementTree.Element. If elt_data is a string, then it is converted to an XML Element type. :param elt_data: XML Element to be parsed or string to be converted to a XML Element :return: Element