Nav apraksta

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import csv
  2. from gvm.connections import TLSConnection
  3. from gvm.protocols.gmp import Gmp
  4. from gvm.transforms import EtreeTransform
  5. # OpenVAS Configuration
  6. OPENVAS_HOST = "localhost"
  7. OPENVAS_PORT = 9390
  8. USERNAME = "admin"
  9. PASSWORD = "admin"
  10. # CSV files
  11. TARGET_ID_CSV = "target_id.csv"
  12. TASK_ID_CSV = "task_id.csv"
  13. def get_scan_config_id(gmp):
  14. """
  15. Retrieve the ID of the 'Full and Fast' scan configuration.
  16. """
  17. scan_configs = gmp.get_scan_configs()
  18. for config in scan_configs.findall("config"):
  19. if config.find("name").text == "Full and fast":
  20. config_id = config.get("id")
  21. print(f"'Full and Fast' scan configuration found with ID: {config_id}")
  22. return config_id
  23. print("Failed to find 'Full and Fast' scan configuration.")
  24. return None
  25. def get_scanner_id(gmp):
  26. """
  27. Retrieve the ID of the default scanner.
  28. """
  29. scanners = gmp.get_scanners()
  30. for scanner in scanners.findall("scanner"):
  31. if "OpenVAS Default" in scanner.find("name").text:
  32. scanner_id = scanner.get("id")
  33. print(f"'OpenVAS Default' scanner found with ID: {scanner_id}")
  34. return scanner_id
  35. print("Failed to find 'OpenVAS Default' scanner.")
  36. return None
  37. def read_csv_to_target_list(csv_file):
  38. """
  39. Read the CSV file and extract target IDs into a list.
  40. """
  41. target_list = []
  42. with open(csv_file, newline="") as csvfile:
  43. reader = csv.DictReader(csvfile)
  44. for row in reader:
  45. target_list.append(row["Target ID"])
  46. return target_list
  47. def save_task_id_to_csv(target_id, task_id):
  48. """
  49. Save the task ID to the task_id.csv file.
  50. """
  51. with open(TASK_ID_CSV, "a", newline="") as csvfile:
  52. writer = csv.writer(csvfile)
  53. writer.writerow([target_id, task_id])
  54. print(f"Saved task ID {task_id} for target ID {target_id} to {TASK_ID_CSV}")
  55. def create_task(gmp, task_name, target_id, scan_config_id, scanner_id):
  56. """
  57. Create a task in OpenVAS.
  58. """
  59. response = gmp.create_task(
  60. name=task_name,
  61. config_id=scan_config_id,
  62. target_id=target_id,
  63. scanner_id=scanner_id
  64. )
  65. task_id = response.get("id")
  66. if task_id:
  67. print(f"Created task '{task_name}' for target ID '{target_id}' with ID: {task_id}")
  68. else:
  69. print(f"Failed to create task for target ID {target_id}. Check logs.")
  70. return task_id
  71. def main():
  72. connection = TLSConnection(hostname=OPENVAS_HOST, port=OPENVAS_PORT)
  73. with Gmp(connection=connection, transform=EtreeTransform()) as gmp:
  74. # Authenticate with OpenVAS
  75. gmp.authenticate(username=USERNAME, password=PASSWORD)
  76. print("Authenticated with OpenVAS")
  77. # Get the scan configuration ID
  78. scan_config_id = get_scan_config_id(gmp)
  79. if not scan_config_id:
  80. print("Failed to retrieve a valid scan configuration. Exiting.")
  81. return
  82. # Get the scanner ID
  83. scanner_id = get_scanner_id(gmp)
  84. if not scanner_id:
  85. print("Failed to retrieve a valid scanner. Exiting.")
  86. return
  87. # Read target IDs from the target_id.csv file
  88. target_list = read_csv_to_target_list(TARGET_ID_CSV)
  89. if not target_list:
  90. print("No target IDs found in the CSV file. Exiting.")
  91. return
  92. print(f"Found {len(target_list)} targets to create tasks for.")
  93. # Process each target
  94. for target_id in target_list:
  95. print(f"Processing target ID: {target_id}")
  96. # Create task for the target
  97. task_name = f"Task for Target {target_id}"
  98. task_id = create_task(gmp, task_name, target_id, scan_config_id, scanner_id)
  99. if task_id:
  100. save_task_id_to_csv(target_id, task_id)
  101. else:
  102. print(f"Failed to create task for target ID {target_id}. Continuing.")
  103. if __name__ == "__main__":
  104. # Ensure the task_id.csv file exists with headers
  105. try:
  106. with open(TASK_ID_CSV, "x", newline="") as csvfile:
  107. writer = csv.writer(csvfile)
  108. writer.writerow(["Target ID", "Task ID"]) # Write headers if the file doesn't exist
  109. except FileExistsError:
  110. pass # File already exists, no need to create
  111. main()