Basic Parsers

synopsis:Define the basic parser classes that will parse the data from the report file.

AbstractParser

class ptp.libptp.parser.AbstractParser(pathname='./', filename='*', light=False, first=True)[source]

Abstract representation of a parser.

Note

This class will be extended for each pentesting tool. That way, each tool will add its own parsing specificities.

__init__(pathname='./', filename='*', light=False, first=True)[source]

Initialize AbstractParser.

Parameters:
  • pathname (str) – Path to the report directory.
  • filename (str) – Regex matching the report file.
  • light (bool) – True to only parse the ranking of the findings from the report.
  • first (bool) – Only process first file (True) or each file that matched (False).
static _recursive_find(pathname='./', file_regex='*', first=True)[source]

Retrieve the full path to the report file(s).

Parameters:
  • pathname (str) – The root directory where to start searching.
  • file_regex (re) – The regex that will be matched against all files from within pathname.
  • first (bool) – Stop the search as soon as a file has been matched.
Returns:

A list of path to the matched files that have been found.

Return type:

list

Note

The search occurs starting from pathname as the root directory.

classmethod check_version(metadata, key='version')[source]

Checks the version in the metadata against the supported one(s).

Parameters:
  • metadata (dict) – The metadata in which to find the version.
  • key (str) – The metadata key containing the version value.
Returns:

True if it can parse the report, False otherwise.

Return type:

bool

classmethod handle_file(pathname='./', filename='*', first=True)[source]

Process the report file.

Parameters:
  • pathname (str) – Path to the report directory.
  • filename (str) – Regex matching the report file.
  • first (bool) – Stop the search as soon as a file has been matched.
Raises:

NotImplementedError because this is an abstract method.

classmethod is_mine()[source]

Check if it can handle the report file.

Raises:NotImplementedError because this is an abstract method.
light = None

bool – Should fully parse the report or not.

metadata = None

dict – Dict of the metadata found in the report.

parse_metadata()[source]

Parse the metadata of a report.

Raises:NotImplementedError because this is an abstract method.
parse_report()[source]

Parse the results of a report file.

Raises:NotImplementedError because this is an abstract method.
pathname = None

str – Path to the report directory.

stream = None

type – i/o stream of the report.

vulns = None

list – List of dict of the results found in the report.

XMLParser

class ptp.libptp.parser.XMLParser(pathname='./', filename='*.xml', **kwargs)[source]

Specialized parser for XML files.

Define the special handle_file() function in order to process the XML report file.

__init__(pathname='./', filename='*.xml', **kwargs)[source]

Initialize XMLParser.

classmethod handle_file(pathname='./', filename='*.xml', first=True)[source]

Return the root node of the XML file.

Parameters:
  • pathname (str) – Path to the report directory.
  • filename (str) – Regex matching the report file.
  • first (bool) – Stop the search as soon as a file has been matched.
Raises:
  • IOError – when the report file cannot be found.
  • TypeError – when the report file has not the right extension.
  • lxml.etree.XMLSyntaxError – when Lxml cannot parse the XML file.
Returns:

handle on the root node element from the XML file.

Return type:

lxml.etree._Element

FileParser

class ptp.libptp.parser.FileParser(pathname='./', filename='*', light=False, first=True)[source]

Specialized parser for generic report file(s).

Define the special handle_file() function in order to process the generic report file.

classmethod handle_file(pathname='./', filename='*', first=True)[source]

Return a string of the content of the file.

Parameters:
  • pathname (str) – Path to the report directory.
  • filename (str) – Regex matching the report file.
  • first (bool) – Stop the search as soon as a file has been matched.
Raises:
  • IOError – when the report file cannot be found or an error occurs when opening/reading.
  • OSError – when an error occurs when opening/reading the report file.
Returns:

data from the file.

Return type:

str

LineParser

class ptp.libptp.parser.LineParser(pathname='./', filename='*', light=False, first=True)[source]

Specialized parser for generic report files.

Define the special handle_file() function in order to process the generic report file.

Note

Contrary to FileParser, this class reads the file line by line and return a list, instead of a str.

classmethod handle_file(pathname='./', filename='*', skip_empty=True, first=True)[source]

Return a list of the lines of the file.

Parameters:
  • pathname (str) – Path to the report directory.
  • filename (str) – Regex matching the report file.
  • first (bool) – Stop the search as soon as a file has been matched.
  • skip_empty (bool) – skip the empty lines that can occur in the file if True, otherwise keep them.
Raises:
  • IOError – when the report file cannot be found or an error occurs when opening/reading.
  • OSError – when an error occurs when opening/reading the report file.
Returns:

all the data from the file, line by line.

Return type:

list