-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiskAssessment.py
411 lines (315 loc) · 13.9 KB
/
RiskAssessment.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# -*- coding: utf-8 -*-
import sys
import MySQLdb
from flask import Flask
from flask import render_template
from flask import url_for
from flask import redirect
from flask.ext.wtf import Form
from wtforms import StringField,IntegerField
from wtforms.validators import DataRequired
app = Flask(__name__)
app.config.from_object('config')
reload(sys)
sys.setdefaultencoding('utf-8')
conn = MySQLdb.connect("10.1.3.106","root","jiexian","risk",charset='utf8')
cursor = conn.cursor()
# 项目清单
class project_form(Form):
project_name = StringField('project_name', validators=[DataRequired()])
project_client = StringField('project_client', validators=[DataRequired()])
new_project_id = StringField('new_project_id',validators=[DataRequired()])
# 系统清单
class system_form(Form):
Project_id = StringField('project_id', validators=[DataRequired()])
System_id = StringField('system_id', validators=[DataRequired()])
System_name = StringField('system_name', validators=[DataRequired()])
# 资产清单
class asset_form(Form):
Asset_id = StringField('asset_id', validators=[DataRequired()])
Asset_name = StringField('asset_name')
Asset_type = StringField('asset_type', validators=[DataRequired()])
Asset_confidentiality = IntegerField('asset_c', validators=[DataRequired()])
Asset_integrality = IntegerField('asset_i', validators=[DataRequired()])
Asset_availability = IntegerField('asset_a', validators=[DataRequired()])
class vul_form(Form):
Asset_id = StringField('asset_id', validators=[DataRequired()])
Vul_id = StringField('vul_id', validators=[DataRequired()])
Vul_content = StringField('vul_content')
Vul_level = IntegerField('vul_level', validators=[DataRequired()])
class threat_possible(Form):
THR_1 = IntegerField('thr-01', validators=[DataRequired()])
THR_2 = IntegerField('thr-02', validators=[DataRequired()])
THR_3 = IntegerField('thr-03', validators=[DataRequired()])
THR_4 = IntegerField('thr-04', validators=[DataRequired()])
THR_5 = IntegerField('thr-05', validators=[DataRequired()])
THR_6 = IntegerField('thr-06', validators=[DataRequired()])
THR_7 = IntegerField('thr-07', validators=[DataRequired()])
THR_8 = IntegerField('thr-08', validators=[DataRequired()])
THR_9 = IntegerField('thr-09', validators=[DataRequired()])
THR_10 = IntegerField('thr-10', validators=[DataRequired()])
THR_11 = IntegerField('thr-11', validators=[DataRequired()])
class threat_form(Form):
Asset_id = StringField('asset_id', validators=[DataRequired()])
THR_1 = IntegerField('thr-01', validators=[DataRequired()])
THR_2 = IntegerField('thr-02', validators=[DataRequired()])
THR_3 = IntegerField('thr-03', validators=[DataRequired()])
THR_4 = IntegerField('thr-04', validators=[DataRequired()])
THR_5 = IntegerField('thr-05', validators=[DataRequired()])
THR_6 = IntegerField('thr-06', validators=[DataRequired()])
THR_7 = IntegerField('thr-07', validators=[DataRequired()])
THR_8 = IntegerField('thr-08', validators=[DataRequired()])
THR_9 = IntegerField('thr-09', validators=[DataRequired()])
THR_10 = IntegerField('thr-10', validators=[DataRequired()])
THR_11 = IntegerField('thr-11', validators=[DataRequired()])
class mapping_form(Form):
Asset_id = StringField('asset_id', validators=[DataRequired()])
THR_id = StringField('thr_id', validators=[DataRequired()])
Vul_id = StringField('vul-id', validators=[DataRequired()])
@app.route('/')
def index():
return render_template('index.html', title='index')
@app.route('/project', methods=['GET','POST'])
def project():
form = project_form()
sql = "SELECT * FROM Project"
cursor.execute(sql)
result = cursor.fetchall()
if form.is_submitted():
sql = "INSERT INTO Project VALUES ('%s','%s','%s')" % (form.new_project_id.data,form.project_name.data,form.project_client.data)
cursor.execute(sql)
conn.commit()
return redirect('/project')
return render_template('project.html', title='Project', result=result, form=form)
@app.route('/project/<project_id>',methods=['GET','POST'])
def get_porject(project_id):
sql = "SELECT * FROM Project WHERE Project_id='%s'" % (project_id)
cursor.execute(sql)
proj=cursor.fetchall()
sql = "SELECT System_id,System_name from System WHERE Project_id='%s'" % (project_id)
cursor.execute(sql)
system=cursor.fetchall()
form = system_form()
if form.is_submitted():
sql = "INSERT INTO System VALUES ('%s','%s','%s')" % (form.System_id.data, form.System_name.data, project_id)
cursor.execute(sql)
conn.commit()
return redirect('/project/'+project_id)
return render_template('project_detail.html', title='Project', result1=proj, result2=system, form=form, id=project_id)
@app.route('/project/<project_id>/<system_id>',methods=['GET','POST'])
def get_system(project_id , system_id):
sql = "SELECT * FROM System WHERE System_id='%s'" % (system_id)
cursor.execute(sql)
system = cursor.fetchall()
sql= "SELECT * FROM Assets WHERE System_id='%s'" % (system_id)
cursor.execute(sql)
assets= cursor.fetchall()
form = asset_form()
if form.is_submitted():
asset_value = max(form.Asset_confidentiality.data, form.Asset_integrality.data, form.Asset_availability.data)
sql = "INSERT INTO Assets VALUES ('%s','%s','%s','%s',%d,%d,%d,%d)" % (form.Asset_id.data,
system_id,
form.Asset_name.data,
form.Asset_type.data,
form.Asset_confidentiality.data,
form.Asset_integrality.data,
form.Asset_availability.data,
asset_value
)
cursor.execute(sql)
conn.commit()
return redirect('/project/'+project_id+'/'+system_id)
return render_template('asstes.html',title='资产信息录入',result1=system,result2=assets ,form=form , proj=project_id, sys=system_id)
@app.route('/vul')
def vul():
sql = "SELECT * FROM System"
cursor.execute(sql)
system = cursor.fetchall()
return render_template('vul.html', title='Vul', result=system,)
@app.route('/vul/<system_id>', methods=['GET','POST'])
def get_vul(system_id):
sql = "select * from Vulnerability"
cursor.execute(sql)
assets_with_vul = cursor.fetchall()
sql = "select Assets_id,Assets_name from Assets where System_id='%s'" % (system_id)
#&&Assets_id NOT IN (select Assets_id from Vulnerability)
cursor.execute(sql)
assets = cursor.fetchall()
form = vul_form()
if form.is_submitted():
sql = "INSERT INTO Vulnerability(Vul_id,Vul_content,Vul_level,Asset_id) VALUES ('%s','%s',%d,'%s')" % (form.Vul_id.data, form.Vul_content.data, form.Vul_level.data, form.Asset_id.data)
cursor.execute(sql)
conn.commit()
return redirect('/vul/'+system_id)
return render_template('vul_import.html', title='Vul_import', result1=assets_with_vul,result2=assets, form=form, id=system_id)
@app.route('/threat_possibility', methods=['GET', 'POST'])
def threat_possibility():
form = threat_possible()
if form.is_submitted():
sql = "INSERT INTO Threat_possibility VALUES (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)" % \
(form.THR_1.data, form.THR_2.data, form.THR_3.data, form.THR_4.data, form.THR_5.data,
form.THR_6.data, form.THR_7.data, form.THR_8.data, form.THR_9.data, form.THR_10.data,
form.THR_11.data)
cursor.execute(sql)
conn.commit()
return redirect('/threat')
return render_template('threat_possibility.html', title='Threat_possibility', form=form)
@app.route('/threat')
def threat():
sql = "SELECT * FROM System"
cursor.execute(sql)
system=cursor.fetchall()
return render_template('threat.html', title='System choose',result=system)
@app.route('/threat/<system_id>',methods=['GET','POST'])
def threat_import(system_id):
sql = "SELECT Assets_id,Assets_name FROM Assets WHERE System_id = '%s'" % (system_id)
cursor.execute(sql)
result1=cursor.fetchall()
sql = "SELECT * FROM Threat"
cursor.execute(sql)
result2= cursor.fetchall()
sql = "SELECT * FROM Threat_possibility LIMIT 5"
cursor.execute(sql)
Threat_possible=cursor.fetchall()
form = threat_form()
Threat_mitrix = [[1,1,2,3,3],[1,1,2,3,4],[1,2,3,4,5],[2,3,4,5,5],[2,4,4,5,5]]
if form.is_submitted():
sql = "INSERT INTO Threat VALUES ('%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)" % (
form.Asset_id.data,
form.THR_1.data,
form.THR_2.data,
form.THR_3.data,
form.THR_4.data,
form.THR_5.data,
form.THR_6.data,
form.THR_7.data,
form.THR_8.data,
form.THR_9.data,
form.THR_10.data,
form.THR_11.data
)
cursor.execute(sql)
Threat_level=(form.THR_1.data,
form.THR_2.data,
form.THR_3.data,
form.THR_4.data,
form.THR_5.data,
form.THR_6.data,
form.THR_7.data,
form.THR_8.data,
form.THR_9.data,
form.THR_10.data,
form.THR_11.data
)
Threar_value = [0,0,0,0,0,0,0,0,0,0,0]
for i in range(0, 11):
x = int(Threat_level[i])-1
y = int(Threat_possible[0][i])-1
value = int(Threat_mitrix[y][x])
Threar_value.insert(i,value)
sql="INSERT INTO Threat_value VALUES ('%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)" % (
form.Asset_id.data,Threar_value[0],Threar_value[1],Threar_value[2],Threar_value[3],Threar_value[4],
Threar_value[5],Threar_value[6],Threar_value[7],Threar_value[8],Threar_value[9],Threar_value[10]
)
cursor.execute(sql)
conn.commit()
return redirect('/threat/'+system_id)
return render_template('threat_import.html', title='Threat_import',result1=result1,result2=result2,form=form,id=system_id)
@app.route('/mapping',methods=['GET','POST'])
def mapping():
sql = "select * from Mapping ORDER BY Asset_id,Vul_id,Thread_id"
cursor.execute(sql)
map = cursor.fetchall()
sql = "select Threat_id,Threat_type from Threat_type"
cursor.execute(sql)
threat = cursor.fetchall()
sql = "select Asset_id,Vul_id,Vul_content from Vulnerability"
cursor.execute(sql)
vul = cursor.fetchall()
form = mapping_form()
if form.is_submitted():
sql = "INSERT INTO Mapping VALUES('%s','%s','%s')" % (form.Asset_id.data,form.Vul_id.data,form.THR_id.data)
cursor.execute(sql)
conn.commit()
return redirect('/mapping')
return render_template('mapping.html',title='mapping',result=map,form=form,map=map,threat=threat,vul=vul)
@app.route('/report')
def report():
#资产赋值表生成
sql = "SELECT Assets_id,Assets_name,Assets_type,confidentiality,availability,integrality,Assets_value FROM Assets"
cursor.execute(sql)
Assets_Value = cursor.fetchall()
#威胁赋值表生成
sql = "SELECT * FROM Threat"
cursor.execute(sql)
Threat = cursor.fetchall()
sql = "SELECT * FROM Threat_possibility"
cursor.execute(sql)
Threat_possibility=cursor.fetchall()
sql = "SELECT * FROM Threat_value"
cursor.execute(sql)
Threat_value = cursor.fetchall()
#脆弱性赋值表生成
sql = "SELECT * FROM Vulnerability"
cursor.execute(sql)
Vul = cursor.fetchall()
#风险值计算总表
sql = "SELECT * FROM Mapping"
cursor.execute(sql)
map = cursor.fetchall()
for row in map:
sql = "SELECT `%s` FROM Threat WHERE Asset_id='%s'" % (row[2],row[0])
cursor.execute(sql)
Act_threat=cursor.fetchall()
sql = "SELECT Vul_id,Vul_level FROM Vulnerability WHERE Asset_id='%s'" % (row[0])
cursor.execute(sql)
Act_vul=cursor.fetchall()
sql = "SELECT Assets_value FROM Assets where Assets_id= '%s'" % (row[0])
cursor.execute(sql)
Act_Value=cursor.fetchall()
sql = "INSERT INTO RISK VALUES('%s',%d,'%s',%d,'%s','%s',%d,%d,%d)" % (
row[0],
Act_Value[0][0],
Act_vul[0][0],
Act_vul[0][1],
row[2],
Act_threat[0][0],
Act_threat[0][0] * Act_vul[0][1],
Act_Value[0][0] * Act_vul[0][1],
0
)
#(Asset_id,Asset_value,Vul_id,Vul_level,Threat_id,Threat_level,TV,AV,R)
cursor.execute(sql)
sql = "update RISK set R=TV*AV"
cursor.execute(sql)
conn.commit()
sql = "select * from RISK"
cursor.execute(sql)
R=cursor.fetchall()
return render_template('report.html',title='Report',Assets_Value=Assets_Value,Threat=Threat,Vul=Vul,Threat_possibility=Threat_possibility,Threat_value=Threat_value,Risk=R)
@app.route('/delete')
def delete():
sql = "TRUNCATE Vulnerability"
cursor.execute(sql)
sql ="TRUNCATE Project"
cursor.execute(sql)
sql ="TRUNCATE System"
cursor.execute(sql)
sql ="TRUNCATE Assets"
cursor.execute(sql)
sql ="TRUNCATE Threat"
cursor.execute(sql)
sql ="TRUNCATE Threat_value"
cursor.execute(sql)
sql ="TRUNCATE Mapping"
cursor.execute(sql)
sql ="TRUNCATE Threat_possibility"
cursor.execute(sql)
sql ="TRUNCATE RISK"
cursor.execute(sql)
return redirect('/')
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')
cursor.close()
conn.close()