-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvasscan.py
387 lines (271 loc) · 10.1 KB
/
vasscan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
# Author: KittyTechnoProgrammer (aka: KittyTechno)
# vasscan.py attempts to ease the scanning of targets using openvas via the command line, and can save the reports
# It also gives you addition information such as the Report ID to download into metasploit using the openvas plugin.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from threading import Semaphore
from functools import partial
from openvas_lib import VulnscanManager, VulnscanException
from sys import stdout
from colored import fg, bg, attr
from progress.bar import ChargingBar
from argparse import ArgumentParser
from time import sleep
from xml.etree import ElementTree
from IPy import IP
import openvas_lib
import os
import base64
lastprog = 0
# function for getting arguments
def get_args():
args = ArgumentParser()
args.add_argument("-s", dest="server", help="Specify the ip of the openVAS server")
args.add_argument("-p", dest="profile", help="Specify profile to scan target(s) with | Needs to be in quotation marks", required = False)
args.add_argument("-u", dest="username", help="Specify the username to connect with", required = False)
args.add_argument("--pass", dest="password", help="Specify the password to the username", required = False)
args.add_argument("-t", dest="target", help="Specify the target(s) to scan", required = False)
args.add_argument("-oX", dest="save_xml", help="Specify the path of the xml to save the report", required = False)
args.add_argument("-oH", dest="save_html", help="Specify the path of the html file to save the report", required = False)
args.add_argument("-oA", dest="save_all", help="Specify the path of the xml and html files to save the report", required = False)
# return the parsed args
return args.parse_args()
# function for printing a loading prompt
def print_loading(msg):
stdout.write("[ {}{}*{} ] {}".format(
attr(1), fg(12), attr(0),
msg
))
stdout.flush()
# function for printing an okay prompt
def print_ok(msg):
stdout.write("[ {}{}+{} ] {}".format(
attr(1), fg(10), attr(0),
msg
))
stdout.flush()
# function for printing notifications
def print_notify(msg):
stdout.write("[ {}{}!{} ] {}".format(
attr(1), fg(11), attr(0),
msg
))
stdout.flush()
# function for printing an error prompt
def print_error(msg):
stdout.write("[ {}{}-{} ] {}".format(
attr(1), fg(9), attr(0),
msg
))
stdout.flush()
# function for printing status of the scan
def print_status(msg):
global pbar
global lastprog
p = int("{:.0f}".format(msg), 10)
pbar.next(p - lastprog)
# print("P : {} | lastprog : {}".format(p, lastprog))
if lastprog < p:
lastprog = p
# stdout.write("\033[K")
# print_loading("Scan Progress : {:.0f}%\r".format(msg))
# # stdout.write("\033[K")
# stdout.flush()
# function for printing profiles of scans
def print_profiles():
print("1). empty")
print("2). Host Discovery")
print("3). Full and fast ultimate")
print("4). Full and very deep")
print("5). Full and very deep ultimate")
print("6). Discovery")
print("7). System Discovery")
print("8). Full and fast")
# function for launching the scan
def launch_scanner(server, username, password, target, profile, args):
global pbar
try:
sem = Semaphore(0)
# configure the scan
print_loading("Connecting to openVAS\r")
manager = VulnscanManager(server, username, password)
stdout.write("\033[K")
print_ok("Connected to openVAS\n")
# print_loading("Launching Scan\r")
stdout.flush()
pbar = ChargingBar("Scanning", max=100, suffix='%(percent)d%%')
pbar.next(n=0)
# pbar.next()
scan_id, target_id = manager.launch_scan(target,
profile = profile,
callback_end = partial(lambda x: x.release(), sem),
callback_progress=print_status)
# wait
sem.acquire()
sleep(2)
pbar.finish()
stdout.write("\033[A")
stdout.write("\033[K")
stdout.flush()
# print()
# stdout.write("\033[K")
# stdout.flush()
print_ok("Finished Scan\n")
# print_loading("Getting Report ID")
report_id = manager.get_report_id(scan_id)
# check if it has been specified to save the file as an xml
if args.save_xml or args.save_all:
print_loading("Getting XML Report ")
report = manager.get_report_xml(report_id)
# print(report)
stdout.write("\r\033[K")
stdout.flush()
print_ok("Loaded XML Report\n")
name = args.save_xml
if not name.lower().endswith(".xml"):
name += ".xml"
print_loading("Attempting to save XML Report ")
with open(name, "wb") as f:
f.write(ElementTree.tostring(report, encoding="utf-8", method="xml"))
stdout.write("\r\033[K")
stdout.flush()
print_ok("Saved XML Report as : {}\n".format(name))
# check if it has been specified to save the file as an html
if args.save_html or args.save_all:
print_loading("Getting HTML Report ")
report = manager.get_report_html(report_id)
html_text = report.find("report").text
if not html_text:
html_text = report.find("report").find("report_format").tail
stdout.write("\r\033[K")
stdout.flush()
print_ok("Loaded HTML Report\n")
name = args.save_html
if not name.lower().endswith(".html"):
name += ".html"
print_loading("Attempting to save HTML Report ")
with open(name, "wb") as f:
f.write(base64.b64decode(html_text))
stdout.write("\r\033[K")
stdout.flush()
print_ok("Saved HTML Report as : {}\n".format(name))
print_ok("Scan ID : {}\n".format(scan_id))
print_ok("Target ID : {}\n".format(target_id))
print_ok("Report ID : {}\n".format(report_id))
# finished scan
print_ok("Finished\n")
except Exception as e:
print_error(e)
os._exit(-1)
def choose_profile():
print_profiles()
while True:
str_choice = input("(p: to print profiles) > ")
if str_choice.lower() == "p":
print_profiles()
continue
choice = int(str_choice, 10)
if choice >= 9 or choice <= 0:
print_error("Need a valid choice")
continue
if choice == 1:
return "empty"
elif choice == 2:
return "Host Discover"
elif choice == 3:
return "Full and fast ultimate"
elif choice == 4:
return "Full and very deep"
elif choice == 5:
return "Full and very deep ultimate"
elif choice == 6:
return "Discovery"
elif choice == 7:
return "System Discovery"
elif choice == 8:
return "Full and fast"
else:
print_error("Need a valid choice")
continue
# function for getting a username to connect to openvas with
def get_username():
while True:
user = input("openVAS username > ")
if user == "":
continue
else:
return user
# function for getting a server ip of openvas
def get_server():
while True:
try:
ip = input("openVAS server IP > ")
if ip.lower() == "localhost":
ip = "127.0.0.1"
IP(ip)
return ip
except ValueError:
print_error("Need valid server IP\n")
def get_target():
while True:
try:
ip = input("openVAS server IP > ")
if ip == "localhost":
ip = "127.0.0.1"
IP(ip)
return ip
except ValueError:
print_error("Need valid server IP\n")
# function for getting a password to use for the username
# def get_password():
# while True:
# password = input("password > ")
# # if password == "":
def main():
# get command line arguments
args = get_args()
if args.server:
server = args.server
else:
server = get_server()
print()
# check if username is given. If not then get it
if args.username:
username = args.username
else:
# print_notify("No user from command line. Please specify username\n")
username = get_username()
print()
if args.password:
password = args.password
else:
# print_notify("No password specified from command line. Please specify password\n")
password = input("password > ")
print()
# check if targets and profile are given. If not then get them
if args.target:
target = args.target
else:
# print_notify("No target(s) specified from command line. Please specify target(s)\n")
target = input("Target(s) > ")
print()
# check if a profile has been choosen to scan with
if args.profile:
profile = args.profile
else:
# print_notify("No profile specified from command line. Please specify profile\n")
profile = choose_profile()
print()
# launch the scan
launch_scanner(server, username, password, target, profile, args)
if __name__ == "__main__":
main()