-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaspromGUI.py
More file actions
406 lines (324 loc) · 11.3 KB
/
aspromGUI.py
File metadata and controls
406 lines (324 loc) · 11.3 KB
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
'''
Created on Oct 19, 2014
@author stefankn
@namespace asprom.aspromGUI
Main Script for the asprom GUI. This script presents a webserver socket to
which client browsers can connect to.
Also, it orchestrates URL calls between the model, view and controller classes.
'''
from bottle import (route, run, static_file, abort, redirect, template,
post, request, hook, response)
from inc.asprom import (AspromModel, AspromScheduleModel, Controller, Machine, initDB,
closeDB, Cfg)
# Variable definitions
## relative path to static files
sr = 'static/'
## main model
M = None
## schedule model
SM = None
localconf = Cfg()
#
# hooks
#
@hook('before_request')
def before_request():
'''
before each dynamic request, create DB connection and Model instances.
'''
username = None
try:
username = request.get_header("X-Forwarded-User", request.auth[0])
except:
pass
p = request.path
if p.startswith('/' + sr):
return
global M, SM
try:
initDB(localconf)
M = AspromModel(username=username)
SM = AspromScheduleModel(user=True)
except:
raise
#
# routes
#
@route('/')
def serve_homepage():
'''
HTTP Redirect to http:///alerts-exposed.
'''
redirect('/alerts-exposed')
# main views
@route('/alerts-exposed')
def serve_alertsexposed():
'''
Presents view: http:///alerts-exposed.
'''
return template('views/alerts-exposed')
@route('/alerts-closed')
def serve_alertsclosed():
'''
Presents view: http:///alerts-closed.
'''
return template('views/alerts-closed')
@route('/baseline')
def serve_baseline():
'''
Presents view: http:///baseline.
'''
return template('views/baseline')
@route('/posture')
def serve_forensic():
'''
Presents view: http:///posture.
'''
return template('views/posture')
@route('/schedule')
def serve_schedule():
'''
Presents view: http:///schedule.
'''
return template('views/schedule')
@route('/log')
def serve_log():
'''
Presents view: http:///log.
'''
return M.getLastLog(10)
# dialog views
@route('/dia/editjob/<jobid:re:[0-9a-f]{8}-[0-9a-f]{4}-\
[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>')
def serve_editjob_view(jobid):
'''
Presents view: http:///dia/editjob.
This is meant to be used as a dialog popup in the schedule view.
On this dialog, the parameters of an existing job can be edited.
@param jobid the job ID to be edited.
'''
j = SM.getScheduleEntryByID(jobid)
return template('views/editjob', jobid=jobid, initial=j['when'], iprange=j
['iprange'], portrange=j['ports'], extraparams=j['params'])
@route('/dia/addjob')
def serve_addjob_view():
'''
Presents view: http:///dia/addjob.
This is meant to be used as a dialog popup in the schedule view.
On this dialog, the parameters of a new job can be entered.
'''
from uuid import uuid4
return template('views/addjob', jobid=str(uuid4()), initial='0 1 * * *',
iprange='192.168.0.0/24', portrange='0-1024',
extraparams='-sV')
# JSON Views
@route('/json/<filename:path>')
def returnjson(filename):
'''
Presents all json views: http:///json/*.
These are used by the tables embedded in the main html views.
The data is aquired using ajax calls.
The data is pulled from the model in dict format and then converted
to json.
@param filename the json view to be shown. can be any of
alerts-exposed, alerts-closed, baseline, posture or schedule.
'''
if filename == 'alerts-exposed':
return M.tojson(M.getAlertsExposed())
elif filename == 'alerts-closed':
return M.tojson(M.getAlertsClosed())
elif filename == 'baseline':
return M.tojson(M.getNeatline())
elif filename == 'posture':
return M.tojson(M.getForensic())
elif filename == 'schedule':
return M.tojson(SM.getSchedule())
else:
abort(404, "undefined json")
closeDB()
# JSON Views
@route('/plain/<filename:path>')
def returnplain(filename):
'''
Presents all plaintext views: http:///plain/*.
These are used by other scripts, like markusk's openvas-config-script
@param filename the plaintext view to be shown.
'''
response.content_type = 'text/plain'
if filename == 'scanned-ranges':
return SM.getScannedRanges()
else:
abort(404, "undefined url")
closeDB()
# controller
# rescan
@route('/controller/rescanjob/<jobid:re:[0-9a-f]{8}-[0-9a-f]{4}-\
[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>')
def serve_rescanController(jobid):
'''
Activates controller: http:///controller/rescanjob/<jobid>.
Instructs the controller to perform a forensic rescan of
the job with id <jobid> now.
@param jobid the job ID to be scanned.
'''
rv = Controller.rescanJob(jobid)
closeDB()
return rv
@route('/controller/rescanmachine/<host:re:[0-9]+>')
@route('/controller/rescanmachine/<host:re:[0-9]+>/<port:re:[0-9]+>')
def serve_rescanMachine(host, port=None):
'''
Activates controller: http:///controller/rescanmachine/<hostid>[/<portid>].
Instructs the controller to perform a forensic rescan of the machine
with id <hostid> now.
If <portid> is present, only this single port is being rescanned.
@param host the host ID to be scanned.
@param port the port to be rescanned on the specified machine.
'''
assert host.isdigit()
rv = Controller.rescanMachine(int(host), int(port) if port else None)
closeDB()
return rv
@route('/controller/rescanservice/<serviceid:re:[0-9]+>')
def serve_rescanService(serviceid):
'''
Activates controller: http:///controller/rescanservice/<serviceid>.
Instructs the controller to perform a forensic rescan of the service
with id <serviceid> now.
@param serviceid The Service ID to be scanned.
'''
assert serviceid.isdigit()
rv = Controller.rescanService(int(serviceid))
closeDB()
return rv
@route('/controller/deletemachine/<machineid:re:[0-9]+>')
def serve_deleteMachine(machineid):
'''
Activates controller: http:///controller/deletemachine/<machineid>.
Deletes the machine and all its associated services from inventory.
@param machineid The Machine ID to be deleted.
'''
assert machineid.isdigit()
machine = Machine(int(machineid))
# Delete all services first
for service in machine.getServices():
service.delete()
# Then delete the machine
machine.delete()
closeDB()
return "ok"
@route('/controller/deletejob/<jobid:re:[0-9a-f]{8}-[0-9a-f]{4}-\
[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>')
def serve_deleteJob(jobid):
'''
Activates controller: http:///controller/deletejob/<jobid>.
Instructs the controller to delete the job with id <jobid>.
@param jobid The Job ID to be scanned.
'''
rv = SM.deleteJob(jobid)
closeDB()
return rv
# flipcrit
@route('/controller/flipcrit/<page:re:(exposed|closed)>/<serviceid:re:[0-9]+>')
def serve_flipCrit(page, serviceid):
'''
Activates controller:
http:///controller/flipcrit/<exposed|closed>/<serviceid>.
Instructs the controller to flip the criticality of service <serviceid> on
the alerts-<exposed|closed> view.
Flipping sets the service criticality to WARNING if it was CRITICAL before
and the other way round.
@param page Either "exposed" or "closed". Denominates the view
on which the criticality of the service should be flipped.
@param serviceid The Service whose criticality should be flipped.
'''
exposed = True if page == "exposed" else False
Controller.flipCrit(serviceid, exposed)
closeDB()
# approve
@post('/controller/approve')
def serve_approve():
'''
Activates controller: http:///controller/approve.
The arguments are to be passed by using the HTTP POST method.
Using this method, a service can be approved to the baseline.
@param pk The Service ID to be approved.
@param value a business justification for the service to be approved.
'''
serviceid = request.forms.get('pk')
justification = request.forms.get('value')
Controller.approve(int(serviceid), justification, M.username)
closeDB()
# remove
@post('/controller/remove')
def serve_remove():
'''
Activates controller: http:///controller/remove.
The arguments are to be passed by using the HTTP POST method.
Using this method, a service can be removed from the baseline.
@param pk The Service ID to be removed.
@param value a business justification for the service to be removed.
'''
serviceid = request.forms.get('pk')
justification = request.forms.get('value')
Controller.remove(int(serviceid), justification, M.username)
closeDB()
# edit job
@post('/controller/editjob/<jobid:re:[0-9a-f]{8}-[0-9a-f]{4}-\
[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>')
def serve_changejob(jobid):
'''
Activates controller: http:///controller/editjob/<jobid>.
This method tells the controller to set or change the parameters
of the specified job.
@param jobid the job to be edited.
The following arguments are to be passed by using the HTTP POST method.
@param cronval the cron schedule string.
@param iprange a CIDR range, single IP or hostname.
@param portrange a single port or port range in the format
<startport>-<endport> to be scanned.
@param extraparams extra command line parameters for nmap.
'''
rv = SM.changeJob(jobid=jobid,
cronval=request.forms.get('cronval'),
iprange=request.forms.get('iprange'),
portrange=request.forms.get('portrange'),
extraparams=request.forms.get('extraparams')
)
closeDB()
return rv
# edit job
@post('/controller/addjob/<jobid:re:[0-9a-f]{8}-[0-9a-f]{4}-\
[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>')
def serve_addjob(jobid):
'''
Activates controller: http:///controller/addjob/<jobid>.
This method tells the controller to set the parameters of the specified job
and add it to crontab.
@param jobid the job to be edited.
The following arguments are to be passed by using the HTTP POST method.
@param cronval the cron schedule string.
@param iprange a CIDR range, single IP or hostname.
@param portrange a single port or port range in the format
<startport>-<endport> to be scanned.
@param extraparams extra command line parameters for nmap.
'''
rv = SM.addJob(jobid=jobid,
cronval=request.forms.get('cronval'),
iprange=request.forms.get('iprange'),
portrange=request.forms.get('portrange'),
extraparams=request.forms.get('extraparams')
)
closeDB()
return rv
# static files
@route('/' + sr + '<filename:path>')
def static(filename):
'''
returns static files from the path defined by variable SR.
@param filename path to the static file relative to the SR directory.
'''
return static_file(filename, root=sr)
# run the service!
run(host=localconf['server']['listen'], port=localconf['server']['port'],
debug=localconf['server']['debug'], server='paste')