|
@@ -0,0 +1,154 @@
|
|
1
|
+import csv
|
|
2
|
+from gvm.connections import TLSConnection
|
|
3
|
+from gvm.protocols.gmp import Gmp
|
|
4
|
+from gvm.transforms import EtreeTransform
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+# OpenVAS Configuration
|
|
8
|
+OPENVAS_HOST = "localhost"
|
|
9
|
+OPENVAS_PORT = 9390
|
|
10
|
+USERNAME = "admin"
|
|
11
|
+PASSWORD = "admin"
|
|
12
|
+
|
|
13
|
+# CSV files
|
|
14
|
+ACTIVE_HOSTS_CSV = "active_hosts.csv"
|
|
15
|
+TARGET_ID_CSV = "target_id.csv"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+def get_port_list_id(gmp):
|
|
19
|
+ """
|
|
20
|
+ Retrieve a valid port list ID.
|
|
21
|
+ """
|
|
22
|
+ port_lists = gmp.get_port_lists()
|
|
23
|
+ default_port_list_id = None
|
|
24
|
+ for port_list in port_lists.findall("port_list"):
|
|
25
|
+ name = port_list.find("name").text
|
|
26
|
+ port_list_id = port_list.get("id")
|
|
27
|
+ print(f"Port List: {name} (ID: {port_list_id})")
|
|
28
|
+ if "OpenVAS Default" in name:
|
|
29
|
+ default_port_list_id = port_list_id
|
|
30
|
+
|
|
31
|
+ # If "OpenVAS Default" is not found, use the first available port list
|
|
32
|
+ if not default_port_list_id and port_lists:
|
|
33
|
+ default_port_list_id = port_lists.find("port_list").get("id")
|
|
34
|
+ print(f"'OpenVAS Default' not found. Using the first available port list with ID: {default_port_list_id}")
|
|
35
|
+
|
|
36
|
+ return default_port_list_id
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+def target_exists(gmp, ip):
|
|
40
|
+ """
|
|
41
|
+ Check if a target already exists for the given IP and return its target_id if it does.
|
|
42
|
+ """
|
|
43
|
+ targets = gmp.get_targets()
|
|
44
|
+ for target in targets.findall("target"):
|
|
45
|
+ if target.find("hosts").text == ip:
|
|
46
|
+ target_id = target.get("id")
|
|
47
|
+ print(f"Target for IP {ip} already exists with ID: {target_id}")
|
|
48
|
+ return target_id
|
|
49
|
+ return None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+def create_target(gmp, name, ip, port_list_id):
|
|
53
|
+ """
|
|
54
|
+ Create a target in OpenVAS.
|
|
55
|
+ """
|
|
56
|
+ response = gmp.create_target(name=name, hosts=ip, port_list_id=port_list_id)
|
|
57
|
+ target_id = response.get("id")
|
|
58
|
+ if target_id:
|
|
59
|
+ print(f"Created target '{name}' for IP '{ip}' with ID: {target_id}")
|
|
60
|
+ else:
|
|
61
|
+ print(f"Failed to create target for IP {ip}. Check logs.")
|
|
62
|
+ return target_id
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+def save_target_id_to_csv(ip, target_id):
|
|
66
|
+ """
|
|
67
|
+ Save the target ID to the target_id.csv file if it's not already there.
|
|
68
|
+ """
|
|
69
|
+ existing_data = []
|
|
70
|
+ try:
|
|
71
|
+ # Read existing data from the CSV
|
|
72
|
+ with open(TARGET_ID_CSV, "r", newline="") as csvfile:
|
|
73
|
+ reader = csv.reader(csvfile)
|
|
74
|
+ existing_data = list(reader)
|
|
75
|
+ except FileNotFoundError:
|
|
76
|
+ # If the file doesn't exist, it will be created later
|
|
77
|
+ pass
|
|
78
|
+
|
|
79
|
+ # Check if the IP is already in the CSV
|
|
80
|
+ for row in existing_data:
|
|
81
|
+ if row[0] == ip:
|
|
82
|
+ print(f"IP {ip} already recorded in {TARGET_ID_CSV}")
|
|
83
|
+ return
|
|
84
|
+
|
|
85
|
+ # Append the new target ID to the CSV
|
|
86
|
+ with open(TARGET_ID_CSV, "a", newline="") as csvfile:
|
|
87
|
+ writer = csv.writer(csvfile)
|
|
88
|
+ writer.writerow([ip, target_id])
|
|
89
|
+ print(f"Saved target ID {target_id} for IP {ip} to {TARGET_ID_CSV}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+def read_csv_to_ip_list(csv_file):
|
|
93
|
+ """
|
|
94
|
+ Read the CSV file and extract IPs into a list.
|
|
95
|
+ """
|
|
96
|
+ ip_list = []
|
|
97
|
+ with open(csv_file, newline="") as csvfile:
|
|
98
|
+ reader = csv.DictReader(csvfile)
|
|
99
|
+ for row in reader:
|
|
100
|
+ ip_list.append(row["IP"])
|
|
101
|
+ return ip_list
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+def main():
|
|
105
|
+ connection = TLSConnection(hostname=OPENVAS_HOST, port=OPENVAS_PORT)
|
|
106
|
+ with Gmp(connection=connection, transform=EtreeTransform()) as gmp:
|
|
107
|
+ # Authenticate with OpenVAS
|
|
108
|
+ gmp.authenticate(username=USERNAME, password=PASSWORD)
|
|
109
|
+ print("Authenticated with OpenVAS")
|
|
110
|
+
|
|
111
|
+ # Get a valid port list ID
|
|
112
|
+ port_list_id = get_port_list_id(gmp)
|
|
113
|
+ if not port_list_id:
|
|
114
|
+ print("Failed to retrieve a valid port list. Exiting.")
|
|
115
|
+ return
|
|
116
|
+
|
|
117
|
+ # Read IPs from the active hosts CSV
|
|
118
|
+ ip_list = read_csv_to_ip_list(ACTIVE_HOSTS_CSV)
|
|
119
|
+ if not ip_list:
|
|
120
|
+ print("No IPs found in the CSV file. Exiting.")
|
|
121
|
+ return
|
|
122
|
+
|
|
123
|
+ print(f"Found {len(ip_list)} active hosts.")
|
|
124
|
+
|
|
125
|
+ # Process each IP
|
|
126
|
+ for ip in ip_list:
|
|
127
|
+ print(f"Processing IP: {ip}")
|
|
128
|
+
|
|
129
|
+ # Check if target already exists
|
|
130
|
+ target_id = target_exists(gmp, ip)
|
|
131
|
+ if target_id:
|
|
132
|
+ # Save the existing target ID to the CSV if not already recorded
|
|
133
|
+ save_target_id_to_csv(ip, target_id)
|
|
134
|
+ continue
|
|
135
|
+
|
|
136
|
+ # Create target if it doesn't exist
|
|
137
|
+ target_name = f"Target for {ip}"
|
|
138
|
+ target_id = create_target(gmp, target_name, [ip], port_list_id)
|
|
139
|
+ if target_id:
|
|
140
|
+ save_target_id_to_csv(ip, target_id)
|
|
141
|
+ else:
|
|
142
|
+ print(f"Failed to create target for IP {ip}. Continuing.")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+if __name__ == "__main__":
|
|
146
|
+ # Ensure the target_id.csv file exists with headers
|
|
147
|
+ try:
|
|
148
|
+ with open(TARGET_ID_CSV, "x", newline="") as csvfile:
|
|
149
|
+ writer = csv.writer(csvfile)
|
|
150
|
+ writer.writerow(["IP", "Target ID"]) # Write headers if the file doesn't exist
|
|
151
|
+ except FileExistsError:
|
|
152
|
+ pass # File already exists, no need to create
|
|
153
|
+
|
|
154
|
+ main()
|