-
Notifications
You must be signed in to change notification settings - Fork 9
/
python-nessus
491 lines (382 loc) · 18.5 KB
/
python-nessus
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/python
import sys
import time
import random
import json
try:
import requests
import argparse
from termcolor import colored
except:
print"Please install the dependencies first"
print"You can install them by typing pip install -r dependencies.txt\n"
exit()
usage = "\n\tpython-nessus [OPTIONS] -p ploicy_name [-t target| -T target_file]\n"
example="""For Example:
python-nessus -p 'Basic Network Scan' -t 192.168.15.24 -e csv -o report.csv
python-nessus -p 'Advanced Scan' -T targetFile.txt -e nessus --configure
python-nessus -l
SEE THE MAN PAGE at https://github.com/AdmiralGaust/python-nessus
"""
parser = argparse.ArgumentParser(usage=usage,epilog=example,formatter_class=argparse.RawDescriptionHelpFormatter)
group = parser.add_mutually_exclusive_group()
parser.add_argument('-l','--list-policies',dest="list_policies",action="store_true",help="Lists all policies")
parser.add_argument('-c','--configure',dest="configure",action='store_true',help="By specifying this flag, user will be able to configure the policy determined by -p flag before launching the scan.")
parser.add_argument('-e ',dest="export_format",help="Export the scan report in specified format.\nThe available formats are nessus,html,csv and db.")
group.add_argument('-t ',dest="target",help="Single target to launch scan against")
group.add_argument('-T ',dest="target_file",help="Specifies File containing the list of targets")
parser.add_argument("-p ",dest='policy_name',type=str,help='policy to use for nessus scan')
parser.add_argument('-o ',dest="output",type=str,help="File to output the result.")
parser.add_argument("-n ",dest='scan_name',type=str,help='Name to be used for the particular scan.If not specified, default value of "Scan Created by API Script" will be used.')
parser.add_argument("-d ",'--delete',dest='delete',action='store_true',help='Delete the scan after completion.This flag can be useful when user wants to delete the scan after successfully exporting the report.')
args = parser.parse_args()
#Prints help if no argument is passed
if not len(sys.argv)>1:
parser.print_help()
exit()
#Check the current version of python, if not python 2.x, exit.
if sys.version_info[0] != 2:
print('This script must be run with Python version 2.x')
exit()
#username and password for loging in
username = ""
password = ""
#Alter the url filled if Nessus is running on a remote machine
url = "https://localhost:8834"
#Make it true if you want to verify the SSL certificate
verify = False
#Disables Warning when not verifying SSL certs
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
#Checks if the target is specified or configure flag is set, when -p flag is used
if args.policy_name:
if not (args.target or args.target_file or args.configure):
print "usage: " + usage
print"python-nessus -h for more info"
exit()
#Checks if the policy to use is defined or not, when -t or -T flag is used
if args.target or args.target_file:
if not args.policy_name:
print "usage: " + usage
print"python-nessus -h for more info"
exit()
#Tries to open target file if -T flag is used
if args.target_file:
try:
with open(args.target_file) as t:
target=t.read()
except IOError:
print"Cannot open the file",args.target_file
print"Make sure you spelled it right\n"
exit()
#set target variable when -t flag is used
if args.target:
target=args.target
#if --configure flag is set, configure the policy before launching the scan
if args.configure:
if not args.policy_name:
print"Policy name not supplied"
print"python-nessus -h for more info"
exit()
def login():
"""Function to log the user in by sending GET request
with username and password to URI /session"""
res = requests.post(url + '/session',data={'username':username,'password':password},verify=verify)
if(res.status_code==200):
global token
token = res.json().get('token')
else:
print res.json()['error']
exit()
def logout():
"""Function to log the user out by sending DELETE
request to URI /session"""
requests.delete(url + '/session',headers=headers,verify=verify)
print""
exit()
def get_policies():
""" Function to retrieve policies:
This Function retrieves policies in two phases:
First it retrieves the user defined policies and then the predefined templates"""
policies = requests.get(url + '/policies',headers=headers,verify=verify) #Getting user defined policies
templates = requests.get(url+"/editor/policy/templates",headers=headers,verify=False) #Getting templates
if policies.status_code==403 or templates.status_code==403:
print"User don't have the permission to view the policy list"
print"You may try logging in again with privileged account"
if policies.status_code==200 and templates.status_code==200:
return policies.json()['policies'],templates.json()['templates'] #return json data as tuple
else:
print str(policies.json()['error'])
logout()
def policy_json_from_policy_name():
"""This function is used to get the json data from the policy name.
The function checks for the existence of policy name supplied with -p tag
and returns the complete json data of the file."""
policies = get_policies() #Getting policies as a tuple
for policy in policies[0]: #checks if the policy to use is predefined-template,if so,return the policy json
if args.policy_name==policy['name']:
return policy
for policy in policies[1]: #checks if the policy to use is user-defined, if so,return the policy json
if args.policy_name==policy['title']:
return policy
print"Cannot find the policy with name",args.policy_name
logout()
def delete_scan():
print"Are you sure you want to delete the scan(y/n): ",
answer = raw_input()
if answer!='y' or answer!='n':
while answer!='y' and answer!='n':
answer = raw_input("\nPress y or n : ")
if answer=='y':
print"Deleting the scan"
global scan_id
res = requests.delete(url + '/scans/{0}'.format(scan_id),headers=headers,verify=verify)
if res.status_code==200:
print"Scan deleted"
else:
print res.json()['error']
else:
print"Scan not deleted"
def configure_policy():
"""Function to configure policy settings.
This function can be useful when trying to configure the policy prior to launching the scan"""
policy = policy_json_from_policy_name() #getting policy json data from its name
try:
policy_id=policy['id']
except KeyError:
print"Only User-defined policies can be configured"
logout()
#Getting policy configurations
res = requests.get(url+'/policies/{0}'.format(policy['id']),verify=False,headers=headers)
#If response is not ok, print the error and logout
if res.status_code!=200:
print res.json()['error']
logout()
#If response is ok, ensure that the discovery mode is set to custom
if res.json()['settings']['discovery_mode']!='Custom':
payload = res.json()
payload['settings']['discovery_mode']="Custom"
payload=json.dumps(payload)
res = requests.put(url+'/policies/{0}'.format(policy['id']),verify=False,headers=headers,data=payload)
if res.status_code!=200:
print res.json()['error']
logout()
payload = requests.get(url+'/policies/{0}'.format(policy['id']),verify=False,headers=headers).json()
print"Trying to configure the policy"
try:
#Configure user-defined policy at run time
print colored("\nAnytime press Enter for default :\n",'green',attrs=['bold'])
print "Ping the remote host" + "(default is " + payload['settings']['ping_the_remote_host'] + ") : ",
temp = raw_input()
#when ping the remote host option is changed, we need to get the policy json data again to handle key error
#if ping_the_remote_host option is changed, getting the payload again
if not (temp=="" or payload['settings']['ping_the_remote_host']==temp):
payload['settings']['ping_the_remote_host']=temp
payload=json.dumps(payload)
res = requests.put(url+'/policies/{0}'.format(policy['id']),verify=False,headers=headers,data=payload)
payload = requests.get(url+'/policies/{0}'.format(policy['id']),verify=False,headers=headers).json()
if payload['settings']['ping_the_remote_host']=='yes': #if pinging the remote host is enabled, ask for various pinging option
print colored("\nPING METHODS : ",'blue',attrs=['bold'])
print "ICMP ping" + "(default " + payload['settings']['icmp_ping'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['icmp_ping']=temp
print "TCP ping" + "(default " + payload['settings']['tcp_ping'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['tcp_ping']=temp
print "UDP ping" + "(default " + payload['settings']['udp_ping'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['udp_ping']=temp
print "ARP ping" + "(default " + payload['settings']['arp_ping'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['arp_ping']=temp
#Taking portscan range from user
print colored("\nPORT SCAN RANGE",'blue',attrs=['bold'])
print "Port Scan range " + "(default is " + payload['settings']['portscan_range'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['portscan_range']=temp
#Determining which network port scanners to use
print colored("\nNETWORK PORT SCANNERS",'blue',attrs=['bold'])
print "TCP " + "(default " + payload['settings']['tcp_scanner'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['tcp_scanner']=temp
print "SYN " + "(default " + payload['settings']['syn_scanner'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['syn_scanner']=temp
print "UDP " + "(default " + payload['settings']['udp_scanner'] + ") : ",
temp = raw_input()
if temp!="":
payload['settings']['udp_scanner']=temp
#sending put request to /policies to update the changes requested
print"Updating the policy "
payload=json.dumps(payload)
res = requests.put(url+'/policies/{0}'.format(policy['id']),verify=False,headers=headers,data=payload)
if res.status_code==200:
print"Policy updated Successfully"
else:
print res.json()['error']
except KeyError:
print "key error code executed",
raw_input()
exit()
def create_scan(uuid,name,targets,id):
"""This function is to create and launch the scan.
It takes policy uuid,name for launching scan and targets """
print"Trying to create the scan"
payload = { "uuid" : uuid,
"settings" : {
"name" : name,
"text_targets" : targets,
"launch_now" : True,
}
}
if not id=="": #Used when policy id is supplied
payload["settings"]["policy_id"] = id
payload = json.dumps(payload)
new_scan = requests.post(url + '/scans',data=payload,headers=headers,verify=verify)
if new_scan.status_code==200:
print colored("scan created successfully",'blue',attrs=['bold'])
print"Launching the scan...."
print"Scan launched"
return new_scan.json()['scan']['id']
else:
print str(new_scan.json()['error'])
logout()
def show_status(scan_id):
print"\n" + colored('status : ','blue',attrs=['bold']) + colored('Running','green',attrs=['bold'])
while True:
status = requests.get(url + '/scans/' + str(scan_id),headers=headers,verify=verify)
status = status.json()['info']['status']
if status=='canceled':
print"Scan has been canceled"
print"Logging the user out"
logout()
if status=='paused':
print"Scan has been paused"
print"Logging the user out"
logout()
if status=='completed':
print colored("Scan completed",'yellow',attrs=['bold'])
break
if status=='running':
time.sleep(2)
def export_request(scan_id):
"""Function to request for the export of scan result.
It takes scan ID of the scan and tries to export the report in the specified format"""
print"Trying to export the report"
if args.export_format=='csv' or args.export_format=='nessus':
payload = { "format" : args.export_format }
elif args.export_format=='db':
payload = { "format" : args.export_format , "password":password}
elif args.export_format=='pdf' or args.export_format=='html':
payload = { "format":args.export_format, "chapters":"vuln_hosts_summary"}
else:
print"Unsupported format selected\nPlease select a valid format"
logout()
payload = json.dumps(payload)
res = requests.post(url + '/scans/' + str(scan_id) + '/export',data=payload,verify=verify,headers=headers)
if res.status_code==200:
file_id = res.json()['file']
print"Waiting for the report to be ready to download..."
time.sleep(2)
while export_status(scan_id,file_id) is False:
time.sleep(1)
export_download(scan_id,file_id)
else:
print res.json()['error']
print"Waiting for 10 seconds before retrying..."
time.sleep(10)
export_request(scan_id)
def export_status(scan_id,file_id):
"""This function checks the status of the export file.
It returns false until the report is ready for downloading"""
res = requests.get(url + '/scans/{0}/export/{1}/status'.format(scan_id,file_id),headers=headers,verify=verify)
return res.json()['status']=='ready'
def export_download(scan_id,file_id):
"""Function to download the report when it is ready"""
print"Report is ready to download"
print"Trying to download the report"
res = requests.get(url + '/scans/' + str(scan_id) + '/export/' + str(file_id) +'/download',headers=headers,verify=verify)
if res.status_code!=200:
print res.json()['error'] + '\nTrying again'
export_download(scan_id,file_id)
else:
print"Report downloaded"
print"Storing the report downloaded"
if args.output:
filename = args.output
else:
filename = 'nessus_{0}_{1}.{2}'.format(scan_id,file_id,args.export_format)
with open(filename,'wb') as f:
f.write(res.content)
print colored("Output stored to " + filename,'green',attrs=['bold'])
#################***********Main Function***************#################
if __name__=='__main__':
colors = ['grey','blue','green','red','yellow','cyan','magenta','white']
color = colors[random.randint(0,len(colors)-1)]
print"\n\n"
print " "*4 + colored(" #################################################",color,attrs=['bold'])
print " "*4 +colored(" # #",color,attrs=['bold'])
print " "*4 +colored(" # Nessus REST API script written in #",color,attrs=['bold'])
print " "*4 +colored(" # python. #",color,attrs=['bold'])
print " "*4 +colored(" # #",color,attrs=['bold'])
print " "*4 +colored(" # Copyright (C) 2017 Admiral Gaust #",color,attrs=['bold'])
print " "*4 +colored(" # Email: admiralgaust@gmail.com #",color,attrs=['bold'])
print " "*4 +colored(" # #",color,attrs=['bold'])
print " "*4 +colored(" ##################################################",color,attrs=['bold'])
print""
token = '' #token for maintaning session after login
login()
headers = {'X-Cookie': 'token=' + token, 'content-type': 'application/json'} # header containing token, it also specifies
# the type of data being sent is json formatted
#checks if the -l flag is set, if yes,list the policies and exit
if args.list_policies:
policies = get_policies()
if not policies[0]==None:
print"\n\n"
print"--------------------------"
print" USER DEFINED POLICIES "
print"--------------------------\n"
for policy in policies[0]:
print policy['name']
print"\n\n" + "--------------------------"
print " PRE-DEFINED TEMPLATES "
print "--------------------------\n"
for policy in policies[1]:
print policy['title']
logout()
#configure the policy before launching the scan, if --configure flag is set
if args.configure:
configure_policy()
#checks if target is specified or not while using -e flag
if args.export_format:
if not (args.target or args.target_file):
print"Please specify the target"
print"For more info type python-nessus -h"
exit()
#if target and policy name both are specified,launch the scan
if args.policy_name and (args.target or args.target_file):
name = "Scan created by API script"
if args.scan_name:
name = args.scan_name
policy = policy_json_from_policy_name()
try:
scan_id = create_scan(policy['template_uuid'],name,target,policy['id'])
except KeyError:
scan_id = create_scan(policy['uuid'],name,target,"")
show_status(scan_id)
#Try to export the scan report in specified format, when -e flag is set
if args.export_format:
export_request(scan_id)
#Delete the scan, after successfully exporting the report
if args.delete:
if not args.export_format:
print"You haven't exported the report yet"
delete_scan()
#Log the user out
logout()