Projects STRLCPY flan Commits c675f2e5
🤬
  • report builders for json and html

  • Loading...
  • sw committed 4 years ago
    c675f2e5
    1 parent 8ba0a59e
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    contrib/report_builders/html_report_builder.py
     1 +import os
     2 +from typing import Any
     3 + 
     4 +from jinja2 import Template, FileSystemLoader, Environment
     5 + 
     6 +from contrib.descriptions import VulnDescriptionProvider
     7 +from contrib.report_builders import JsonReportBuilder
     8 + 
     9 + 
     10 +class JinjaHtmlReportBuilder(JsonReportBuilder):
     11 + def __init__(self, description_provider: VulnDescriptionProvider):
     12 + super().__init__(description_provider)
     13 + self.template_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
     14 + self.template_name = 'jinja2_report.html'
     15 + self._read_template() # type: Template
     16 + 
     17 + def build(self) -> Any:
     18 + return self._template.render(data=self._buffer)
     19 + 
     20 + def _read_template(self):
     21 + template_loader = FileSystemLoader(searchpath=self.template_path)
     22 + template_env = Environment(loader=template_loader, autoescape=True)
     23 + self._template = template_env.get_template(self.template_name)
     24 + 
  • ■ ■ ■ ■ ■ ■
    contrib/report_builders/json_report_builder.py
     1 +import json
     2 +from typing import Any, Dict, List
     3 + 
     4 +from contrib.descriptions import VulnDescriptionProvider
     5 +from contrib.internal_types import ScanResult
     6 +from contrib.report_builders import ReportBuilder
     7 + 
     8 + 
     9 +class JsonReportBuilder(ReportBuilder):
     10 + def __init__(self, description_provider: VulnDescriptionProvider):
     11 + self.description_provider = description_provider
     12 + self._buffer = {'ips': [], 'vulnerable': {}, 'not_vulnerable': {}}
     13 + 
     14 + def init_report(self, start_date: str, nmap_command: str):
     15 + self._buffer['start_date'] = start_date
     16 + self._buffer['nmap_command'] = nmap_command
     17 + 
     18 + def build(self) -> Any:
     19 + return json.dumps(self._buffer)
     20 + 
     21 + def add_vulnerable_services(self, scan_results: Dict[str, ScanResult]):
     22 + for app_name, result in scan_results.items():
     23 + self._buffer['vulnerable'][app_name] = {
     24 + 'vulnerabilities': [],
     25 + 'locations': self._serialize_locations(result.locations)
     26 + }
     27 + 
     28 + for v in result.vulns:
     29 + data = v.to_dict()
     30 + description = self.description_provider.get_description(v.name, v.vuln_type)
     31 + data['description'], data['url'] = description.text, description.url
     32 + self._buffer['vulnerable'][app_name]['vulnerabilities'].append(data)
     33 + 
     34 + def add_non_vulnerable_services(self, scan_results: Dict[str, ScanResult]):
     35 + for app_name, result in scan_results.items():
     36 + self._buffer['not_vulnerable'][app_name] = {
     37 + 'locations': self._serialize_locations(result.locations)
     38 + }
     39 + 
     40 + def add_ip_address(self, ip: str):
     41 + self._buffer['ips'].append(ip)
     42 + 
     43 + @staticmethod
     44 + def _serialize_locations(locations: Dict[str, List[str]]):
     45 + return {loc: [int(port) for port in ports] for loc, ports in locations.items()}
     46 + 
Please wait...
Page is in error, reload to recover