I’ve attempted to write some code to export the scan reports as a csv file:
from gvm.connections import UnixSocketConnection
from gvm.protocols.latest import Gmp, ReportFormatType
import xml.etree.ElementTree as Et
USERNAME = 'user'
PASSWORD = 'blah'
CSV_FORMAT = ReportFormatType.CSV_RESULTS
CONNECTION = UnixSocketConnection()
def process_all_reports(xml_data: str) -> list:
try:
xml_root = Et.fromstring(xml_data)
return list(set(report_data.get('id') for report_data in xml_root.findall('report')))
except Et.ParseError as e:
print(f"Parsing failed!\nDetails: {e}")
return []
def get_csv_report() -> None:
"""
Logs into GVM to pull reports and saves them as CSV files.
:return: None
"""
try:
with Gmp(CONNECTION) as gmp:
gmp.authenticate(username=USERNAME, password=PASSWORD)
# logger.info("Authentication successful.")
print("Auth Success")
# Get all reports
raw_reports = gmp.get_reports()
report_id_list = process_all_reports(raw_reports)
print(f"got {len(report_id_list)} reports.\nReports: {report_id_list}")
# logger.info(f"Gathered {len(reports)} reports.")
# download all reports in csv format
for report_id in report_id_list:
try:
report_data = gmp.get_report(report_id, ignore_pagination=True, report_format_id=CSV_FORMAT)
try:
with open(f'report_{report_id}.csv', 'w') as report_file:
print(f"Data check:{report_id}: {report_data}")
report_file.write(report_data)
report_file.close()
print(f"saved report {report_id} as CSV file")
except Exception as e:
print(f"Error attempting to write files!\nDetails: {e}")
# logger.info(f"saved report {report_id} as CSV file")
except Exception as e:
print(f"Saving report {report_id} failed!\nDetails: {e}")
# logger.exception(f"Saving report {report_id} failed!\nDetails: {e}")
pass
except Exception as e:
print(f"An error occurred when starting to extract reports!\nDetails: {e}")
# logger.exception(f"An error occurred when starting to extract reports!\nDetails: {e}")
pass
# Call the function to test
get_csv_report()
While there is another issue with the code not saving the files, my most pressing issue is that the output of the get_report method seems to produce base64 inside of a xml tree. Unfortunately, there doesn’t appear to be any tags wrapping the base64 string, so I am at a loss how I would be able to extract the string to transform it into something that could be saved as a csv file.
How can I accomplish this? Are there any docs that give a short how to?