Lightweight Vulnerability Scanner for Resourced-constrained Organizations

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import csv
  2. from gvm.connections import TLSConnection
  3. from gvm.protocols.gmp import Gmp
  4. from gvm.transforms import EtreeTransform
  5. import os
  6. # Set up data directory
  7. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  8. DATA_DIR = os.path.abspath(os.path.join(BASE_DIR, "..", "data"))
  9. os.makedirs(DATA_DIR, exist_ok=True)
  10. # OpenVAS Configuration
  11. OPENVAS_HOST = "localhost"
  12. OPENVAS_PORT = 9390
  13. USERNAME = "admin"
  14. PASSWORD = "admin"
  15. # CSV file
  16. TASK_ID_CSV = os.path.join(DATA_DIR, "task_id.csv")
  17. def start_task(gmp, task_id):
  18. """
  19. Start a task in OpenVAS.
  20. """
  21. try:
  22. gmp.start_task(task_id=task_id)
  23. print(f"Started task with ID: {task_id}")
  24. except Exception as e:
  25. print(f"Failed to start task with ID {task_id}. Error: {e}")
  26. def read_csv_to_task_list(csv_file):
  27. """
  28. Read the CSV file and extract task IDs into a list.
  29. """
  30. task_list = []
  31. with open(csv_file, newline="") as csvfile:
  32. reader = csv.DictReader(csvfile)
  33. for row in reader:
  34. task_list.append(row["Task ID"])
  35. return task_list
  36. def main():
  37. connection = TLSConnection(hostname=OPENVAS_HOST, port=OPENVAS_PORT)
  38. with Gmp(connection=connection, transform=EtreeTransform()) as gmp:
  39. # Authenticate with OpenVAS
  40. gmp.authenticate(username=USERNAME, password=PASSWORD)
  41. print("Authenticated with OpenVAS")
  42. # Read task IDs from the task_id.csv file
  43. task_list = read_csv_to_task_list(TASK_ID_CSV)
  44. if not task_list:
  45. print("No task IDs found in the CSV file. Exiting.")
  46. return
  47. print(f"Found {len(task_list)} tasks to start.")
  48. # Start each task
  49. for task_id in task_list:
  50. start_task(gmp, task_id)
  51. if __name__ == "__main__":
  52. main()