-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebserver.py
305 lines (245 loc) · 10.4 KB
/
webserver.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
#!/usr/bin/env python
# webserver.py - web app to display/edit halloffame.json
# should be able to launch the vulnerability checking
"""
Web app to display/edit the halloffame.json file
Author: Emilien LE JAMTEL
CERT-EU - version 1.0
30/05/2016
"""
import sys
#reload(sys)
#sys.setdefaultencoding('utf-8')
import json
import cgi
import datetime
from flask import Flask
from flask import render_template
from flask import request
from flask import url_for
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
from hofscanner import checkvuln
########## Navbar
nav = Nav()
@nav.navigation()
def mynavbar():
return Navbar(
'Hall of Fame',
View('All vulnerabilities', 'index'),
View('Not scanned vulnerabilities', 'display_notscan'),
View('New vulnerability', 'new_vuln'),
)
######### End Navbar
## init
json_file='halloffame.json'
app = Flask(__name__)
Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
app.debug = True
nav.init_app(app)
#app.run(debug=True)
#sorted_halloffame = load_sorted_hof()
##### Index page (hall of Fame)
@app.route('/')
def index():
return display_hof()
@app.route('/hof')
def display_hof():
sorted_halloffame = load_sorted_hof()
return render_template('hof.html', hof_vulns=sorted_halloffame)
##### Vulnerabilities per constituent/reporter/DO ######
@app.route('/constituent/<constituent>')
def display_constituent(constituent):
sorted_halloffame = load_sorted_hof()
constituent_halloffame = []
for i in range (len(sorted_halloffame)):
if sorted_halloffame[i]["constituent"] == constituent:
constituent_halloffame.append(sorted_halloffame[i])
return render_template('hof.html', hof_vulns=constituent_halloffame)
@app.route('/reporter/<reporter>')
def display_reporter(reporter):
sorted_halloffame = load_sorted_hof()
reporter_halloffame = []
for i in range (len(sorted_halloffame)):
if sorted_halloffame[i]["reporter"] == reporter:
reporter_halloffame.append(sorted_halloffame[i])
return render_template('hof.html', hof_vulns=reporter_halloffame)
@app.route('/DO/<dutyoff>')
def display_dutyoff(dutyoff):
sorted_halloffame = load_sorted_hof()
dutyoff_halloffame = []
for i in range (len(sorted_halloffame)):
if sorted_halloffame[i]["DO"] == dutyoff:
dutyoff_halloffame.append(sorted_halloffame[i])
return render_template('hof.html', hof_vulns=dutyoff_halloffame)
@app.route('/notscan/')
def display_notscan():
sorted_halloffame = load_sorted_hof()
notscan_halloffame = []
for i in range (len(sorted_halloffame)):
if sorted_halloffame[i]["scanable"] == 'no':
notscan_halloffame.append(sorted_halloffame[i])
return render_template('hof.html', hof_vulns=notscan_halloffame)
@app.route('/constituent_cleaned/<constituent>')
def display_constituent_cleaned(constituent):
sorted_halloffame = load_sorted_hof()
constituent_cleaned_halloffame = []
for i in range (len(sorted_halloffame)):
if sorted_halloffame[i]["constituent"] == constituent:
constituent_cleaned_halloffame.append(sorted_halloffame[i])
return render_template('hof-clean.html', hof_vulns=constituent_cleaned_halloffame)
#### Modifying JSON file (add) ######
@app.route('/new_vuln/')
def new_vuln():
return render_template('new_vuln.html')
@app.route('/create_vuln/', methods=['POST'])
def create_vuln():
halloffame = load_hof()
last_id = get_last_id()
new_id = last_id+1
# creating the new entry
# data value is buggy for now. Should find a way to receive POST data to build a proper dictionary for data value
new_vuln = {"DO":request.form['DO'],
"constituent":request.form['constituent'],
"reporter":request.form['reporter'],
"report_date":request.form['report_date'],
"Incident":request.form['incident_number'],
"type":request.form['vuln_type'],
"method":request.form['method'],
"url":request.form['url'],
"data":'',
"check_string":request.form['check_string'],
"scanable":request.form['scanable'],
"published":request.form['published'],
"test_type":request.form['test_type'],
"patched":"no",
"patched_date":"",
"last_test":"",
"id":new_id
}
post_data = request.form['post_data']
if post_data != '':
data = {}
for counter in range(1,int(post_data)+1):
data[request.form['key'+str(counter)]]=request.form['value'+str(counter)]
new_vuln["data"]=data
#adding the entry to the list
halloffame.append(new_vuln)
#writing on the JSON file
with open('halloffame.json', 'w') as data:
data.write(json.dumps(halloffame, indent=4))
### if ok, displaying success page
return view_vuln(new_id)
# return render_template('done.html')
## function to display/edit all values from a hof entry
# ## incident_halloffame should be a list a dictionnaries, each dict being a vuln, all will have common incident number
# return render_template('view_vuln.html', hof_vulns=incident_halloffame)
#@app.route('/view_incident/<incident_number>')
#def view_incident(incident_number):
# halloffame = load_hof()
#
# #looking for Incident with the specified Incident Number (should be uniq in the future release)
# list_incident = []
# for i in range (len(halloffame)):
# if halloffame[i]["Incident"] == incident_number:
# list_incident.append(halloffame[i])
#
# #list_incident is the list of dictionnaries (based on JSON file)
# return render_template('view_vuln.html', hof_vulns=list_incident)
@app.route('/view_vuln/<vuln_id>')
def view_vuln(vuln_id):
halloffame = load_hof()
#looking for Incident with the specified Incident Number (should be uniq in the future release)
list_incident = []
for i in range (len(halloffame)):
if halloffame[i]["id"] == int(vuln_id):
list_incident.append(halloffame[i])
#list_incident is the list of dictionnaries (based on JSON file)
return render_template('view_vuln.html', hof_vulns=list_incident)
@app.route('/update_vuln/', methods=['POST'])
def update_vuln():
halloffame = load_hof()
if request.form['action'] == 'update':
for i in range (len(halloffame)):
## for now url is used as a key - in the future, Incident_number should be uniq
if int(halloffame[i]["id"]) == int(request.form['id']):
halloffame[i]["Incident"] = request.form['incident_number']
halloffame[i]["DO"] = request.form['DO']
halloffame[i]["constituent"] = request.form['constituent']
halloffame[i]["reporter"] = request.form['reporter']
halloffame[i]["report_date"] = request.form['report_date']
halloffame[i]["type"] = request.form['vuln_type']
halloffame[i]["method"] = request.form['method']
halloffame[i]["url"] = request.form['url']
halloffame[i]["data"] = '' #request.form['data']
post_data = request.form['post_data']
if post_data != '':
data = {}
for counter in range(1,int(post_data)+1):
data[request.form['key'+str(counter)]]=request.form['value'+str(counter)]
halloffame[i]["data"]=data
halloffame[i]["check_string"] = request.form['check_string']
halloffame[i]["scanable"] = request.form['scanable']
halloffame[i]["test_type"] = request.form['test_type']
halloffame[i]["published"] = request.form['published']
elif request.form['action'] == 'test':
#print ('toto')
for i in range (len(halloffame)):
## for now url is used as a key - in the future, Incident_number should be uniq
if int(halloffame[i]["id"]) == int(request.form['id']):
halloffame[i] = checkvuln(halloffame[i])
elif request.form['action'] == 'mark as patched':
#print ('patchouli')
for i in range (len(halloffame)):
## for now url is used as a key - in the future, Incident_number should be uniq
if int(halloffame[i]["id"]) == int(request.form['id']):
halloffame[i]["patched"] = 'yes'
halloffame[i]["patched_date"] = str(datetime.datetime.now())
elif request.form['action'] == 'mark as unpatched':
#print ('patchouliii')
for i in range (len(halloffame)):
## for now url is used as a key - in the future, Incident_number should be uniq
if int(halloffame[i]["id"]) == int(request.form['id']):
halloffame[i]["patched"] = 'no'
halloffame[i]["patched_date"] = ''
elif request.form['action'] == 'delete':
#print ('deletion')
for i in range (len(halloffame)):
## for now url is used as a key - in the future, Incident_number should be uniq
if int(halloffame[i]["id"]) == int(request.form['id']):
halloffame.pop(i)
break
# writing the JSON file
with open('halloffame.json', 'w') as data:
data.write(json.dumps(halloffame, indent=4))
#return render_template('done.html')
return view_vuln(request.form['id'])
#### Start vulnerability test ####
#### TODO: modify halloffame.py to a python module ####
#@app.route('/test/<incident_number>')
#def test_vuln(incident_number):
# return ''
############################# dealing with json import ###############
def load_hof():
with open(json_file, 'r') as data:
halloffame = json.load(data)
return halloffame
def load_sorted_hof():
with open(json_file, 'r') as data:
halloffame = json.load(data)
return sorted(halloffame, key=key_is_date, reverse=True)
def key_is_date (dict):
return dict["report_date"]
############# I now have 2 list of dictionnaries:
############# halloffame : loaded json file
############# sorted_halloffame : loaded json file ordered by report_date
######################## get the bigget id from the json file ###########
def get_last_id():
halloffame = load_hof()
seq = [x['id'] for x in halloffame]
return max(seq)
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True) #for listening to all interfaces
#app.run() #for localhost only