Lightweight Vulnerability Scanner for Resourced-constrained Organizations

getreports.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import csv
  2. import time
  3. import os
  4. from gvm.connections import TLSConnection
  5. from gvm.protocols.gmp import Gmp
  6. from gvm.transforms import EtreeTransform
  7. # Set up data directory
  8. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  9. DATA_DIR = os.path.abspath(os.path.join(BASE_DIR, "..", "data"))
  10. os.makedirs(DATA_DIR, exist_ok=True)
  11. TASK_ID_CSV = os.path.join(DATA_DIR, "task_id.csv")
  12. REPORT_ID_CSV = os.path.join(DATA_DIR, "report_id.csv")
  13. OPENVAS_HOST = "localhost"
  14. OPENVAS_PORT = 9390
  15. USERNAME = "admin"
  16. PASSWORD = "admin"
  17. def wait_for_task_and_get_report(gmp, task_id):
  18. while True:
  19. response = gmp.get_task(task_id=task_id)
  20. task = response.find("task")
  21. if task is None:
  22. print(f"Task with ID {task_id} not found. Skipping.")
  23. return None
  24. status = task.find("status").text
  25. progress = task.find("progress").text
  26. print(f"Task {task_id} - Status: {status}, Progress: {progress}%")
  27. if status == "Done":
  28. report_elem = task.find("last_report/report")
  29. if report_elem is not None:
  30. report_id = report_elem.get("id")
  31. print(f"[✓] Task {task_id} finished. Report ID: {report_id}")
  32. return report_id
  33. else:
  34. print(f"[✗] Task {task_id} is done, but no report found.")
  35. return None
  36. time.sleep(10)
  37. def main():
  38. connection = TLSConnection(hostname=OPENVAS_HOST, port=OPENVAS_PORT)
  39. with Gmp(connection=connection, transform=EtreeTransform()) as gmp:
  40. gmp.authenticate(username=USERNAME, password=PASSWORD)
  41. print("Authenticated with OpenVAS")
  42. try:
  43. with open(TASK_ID_CSV, newline="") as csvfile:
  44. reader = csv.DictReader(csvfile)
  45. task_ids = [row["Task ID"] for row in reader]
  46. with open(REPORT_ID_CSV, "w", newline="") as outfile:
  47. writer = csv.writer(outfile)
  48. writer.writerow(["Task ID", "Report ID"]) # Header
  49. for task_id in task_ids:
  50. print(f"Waiting for report from Task ID: {task_id}")
  51. report_id = wait_for_task_and_get_report(gmp, task_id)
  52. if report_id:
  53. writer.writerow([task_id, report_id])
  54. except FileNotFoundError:
  55. print(f"File not found: {TASK_ID_CSV}. Please run taskmaker.py first.")
  56. except KeyError:
  57. print("Error: CSV must contain a 'Task ID' column.")
  58. if __name__ == "__main__":
  59. main()