/
nessusscan.py
305 lines (220 loc) · 7.36 KB
/
nessusscan.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
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import json
import datetime
import time
import sys
import getpass
import os
import subprocess
from scan import Scan
verify = False
token = None
url = None
username = None
password = None
scans = []
outputdir = os.environ['HOME']+'/Desktop/audit-scans/'
if not os.path.exists(outputdir):
print 'creating output dir:'
print outputdir
os.makedirs(outputdir)
today = datetime.date.today()
day = str(today.day)
if(len(day) == 1): day = "0"+day
month = str(today.month)
if(len(month) == 1): month = "0"+month
timestamp = ( str(today.year) + '-' + str(month) + '-' + str(day))
def build_url(resource):
return '{0}{1}'.format(url, resource)
def connect(method, resource, data=None):
headers = {'X-Cookie': 'token={0}'.format(token),'content-type': 'application/json'}
data = json.dumps(data)
if method == 'POST':
r = requests.post(build_url(resource), data=data, headers=headers, verify=verify)
elif method == 'PUT':
r = requests.put(build_url(resource), data=data, headers=headers, verify=verify)
elif method == 'DELETE':
r = requests.delete(build_url(resource), data=data, headers=headers, verify=verify)
else:
r = requests.get(build_url(resource), params=data, headers=headers, verify=verify)
if r.status_code != 200:
e = r.json()
print 'HTTP ERROR when trying to access:' + str(resource)
return None
if 'download' in resource:
return r.content
else:
try:
return r.json()
except:
print 'an error occured'
def login(username, password):
login = {'username': username, 'password': password}
data = connect('POST', '/session', data=login)
return data['token']
def logout():
connect('DELETE', '/session')
def get_policies():
data = connect('GET', '/editor/policy/templates')
if data is None:
print 'failed to fetch policies, sleeping for 1Os and re-trying'
time.sleep(10)
get_policies()
else:
return dict((p['title'], p['uuid']) for p in data['templates'])
def get_history_ids(sid):
data = connect('GET', '/scans/{0}'.format(sid))
return dict((h['uuid'], h['history_id']) for h in data['history'])
def get_scan_history(sid, hid):
params = {'history_id': hid}
data = connect('GET', '/scans/{0}'.format(sid), params)
return data['info']
def get_scan_percentages(sid):
data = connect('GET', '/scans/{0}'.format(sid))
hostarray = []
for host in data['hosts']:
progress = str(host['progress'])
firstpart = progress.split('/')[0]
#print 'DEBUG: first part = ' + str(firstpart)
dividend = float(firstpart.split('-')[0])
#print 'DEBUG: dividend = ' + str(dividend)
divisor = float(firstpart.split('-')[1])
#print 'DEBUG: divisor = ' + str(divisor)
percentage = float(dividend/divisor)
percentage = percentage*100
#print 'DEBUG: percentage = ' + str(percentage)
precision = "%.0f" % percentage
hostarray.append ("\t\033[0m" + str(host['hostname']) + "\t " + str(precision) + "%")
return hostarray
def add(name, desc, targets, pid):
scan = {'uuid': pid,
'settings': {
'name': name,
'description': desc,
'text_targets': targets}
}
data = connect('POST', '/scans', data=scan)
return data['scan']
def update(scan_id, name, desc, targets, pid=None):
scan = {}
scan['settings'] = {}
scan['settings']['name'] = name
scan['settings']['desc'] = desc
scan['settings']['text_targets'] = targets
if pid is not None:
scan['uuid'] = pid
data = connect('PUT', '/scans/{0}'.format(scan_id), data=scan)
return data
def launch(sid):
data = connect('POST', '/scans/{0}/launch'.format(sid))
return data['scan_uuid']
def status(sid, hid):
stat = get_scan_history(sid, hid)
return stat['status']
def export_status(sid, fid):
data = connect('GET', '/scans/{0}/export/{1}/status'.format(sid, fid))
return data['status'] == 'ready'
def export(sid, hid):
data = {'history_id': hid,
'format': 'nessus'}
data = connect('POST', '/scans/{0}/export'.format(sid), data=data)
fid = data['file']
while export_status(sid, fid) is False:
time.sleep(5)
return fid
def download(sid, fid,name):
data = connect('GET', '/scans/{0}/export/{1}/download'.format(sid, fid))
filename = str(timestamp) + "-" + str(name) + ".nessus"
print("Saving scan results to: " + str(outputdir)+str(filename))
with open(outputdir+filename, 'w') as f:
f.write(data)
def delete(sid):
connect('DELETE', '/scans/{0}'.format(scan_id))
def history_delete(sid, hid):
connect('DELETE', '/scans/{0}/history/{1}'.format(sid, hid))
def create_scan(scans):
print ('\n')
print 'Adding new scan:\n'
policies = get_policies()
policy_id = policies['Basic Network Scan']
scan_name = raw_input("Scan name: ")
scan_targets = raw_input("Targets: ")
scan_description = raw_input("Description: ")
scan_data = add(scan_name, scan_description, scan_targets, policy_id)
scan_id = scan_data['id']
scan = Scan(scan_name,scan_description,scan_targets,scan_id)
scans.append(scan)
def show_scans(scans):
for scan in scans:
print 'Name: ' + str(scan.name) + ', Target(s): ' + str(scan.targets)
def launch_scans(scans):
for scan in scans:
print('Launching scan: ' + str(scan.name))
scan_uuid = launch(scan.id)
history_ids = get_history_ids(scan.id)
history_id = history_ids[scan_uuid]
scan.setHistory_id(history_id)
def incomplete(scans):
incomplete = True
for scan in scans:
if (status(scan.id, scan.history_id) != 'completed'):
incomplete = True
break
else:
incomplete = False
return incomplete
def view_status_scans(scans):
os.system('clear')
print ('Running Nessus scans...\n')
for scan in scans:
print scan.name + " = " + status(scan.id, scan.history_id)
scan_percentages = (get_scan_percentages(scan.id))
print ('\t\033[1m' +'HOST \t\t ' + '\033[1m'+ 'STATUS')
for host_percentage in scan_percentages:
print host_percentage
print ('\033[0m\n')
count = 20
while(count >=0):
sys.stdout.write("\rRefresh in:{0}>>".format(count))
sys.stdout.flush()
count -=1
time.sleep(1)
if __name__ == "__main__" :
print 'Welcome to the nessus scan tool'
#start nessus
print ("Attempting to start nessus...")
command="/etc/init.d/nessusd start"
proc = subprocess.Popen([command],stdout=subprocess.PIPE,shell=True)
out = proc.communicate()[0]
print str(out)
print 'Gathering variables:'
username = raw_input("Nessus Username: ")
password = getpass.getpass("Nessus Password: ")
url = raw_input("Nessus URL (blank for 'https://localhost:8834'): ")
if ( url == ''):
url = "https://localhost:8834"
print 'establishing connection...'
time.sleep(5)
print('Logging in...')
token = login(username, password)
#TODO: validate login result
create_scan(scans)
while True:
print 'current scans in queue: '
show_scans(scans)
cont = raw_input("Add another scan? (y/n): ")
if (cont == 'y'):
create_scan(scans)
else:
break
print ('The scans will be launched now.')
launch_scans(scans)
while (incomplete(scans)):
view_status_scans(scans)
print '\n'
print('Exporting the completed scans...')
for scan in scans:
file_id = export(scan.id, scan.history_id)
download(scan.id, file_id,scan.name)