-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
275 lines (247 loc) · 11.2 KB
/
app.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
import sqlite3
from flask import Response
from weasyprint import HTML
from flask import make_response
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required
app = Flask(__name__)
app.config['SECRET_KEY'] = 'ctir'
login_manager = LoginManager()
login_manager.init_app(app)
# User class for Flask-Login
class User(UserMixin):
def __init__(self, id):
self.id = id
# Function to create database table
def create_table():
conn = sqlite3.connect('incident_reports.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS reports
(id INTEGER PRIMARY KEY,
report_date TEXT,
incident_date TEXT,
attack_type TEXT,
threat_actor TEXT,
threat_actor_alias TEXT,
targeted_units TEXT,
target_industries TEXT,
incident_sites TEXT,
address TEXT,
severity TEXT,
digital_infrastructure TEXT,
operating_system TEXT,
attack_method TEXT,
impact TEXT,
action_taken TEXT,
source TEXT,
recommendations TEXT,
assessment TEXT,
remarks TEXT)''')
conn.commit()
conn.close()
# Function to insert data into database
def insert_data(report_date, incident_date, attack_type, threat_actor, threat_actor_alias,
targeted_units, target_industries, incident_sites, address, severity,
digital_infrastructure, operating_system, attack_method, impact,
action_taken, source, recommendations, assessment, remarks):
conn = sqlite3.connect('incident_reports.db')
c = conn.cursor()
c.execute('''INSERT INTO reports (report_date, incident_date, attack_type, threat_actor,
threat_actor_alias, targeted_units, target_industries, incident_sites,
address, severity, digital_infrastructure, operating_system, attack_method,
impact, action_taken, source, recommendations, assessment, remarks)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(report_date, incident_date, attack_type, threat_actor, threat_actor_alias,
targeted_units, target_industries, incident_sites, address, severity,
digital_infrastructure, operating_system, attack_method, impact,
action_taken, source, recommendations, assessment, remarks))
conn.commit()
conn.close()
# Route for form submission to add a new report
@app.route('/add_report', methods=['GET', 'POST'])
@login_required
def add_report():
if request.method == 'POST':
# Retrieve form data
report_date = request.form['report_date']
incident_date = request.form['incident_date']
attack_type = request.form['attack_type']
threat_actor = request.form['threat_actor']
threat_actor_alias = request.form['threat_actor_alias']
targeted_units = request.form['targeted_units']
target_industries = request.form['target_industries']
incident_sites = request.form['incident_sites']
address = request.form['address']
severity = request.form['severity']
digital_infrastructure = request.form['digital_infrastructure']
operating_system = request.form['operating_system']
attack_method = request.form['attack_method']
impact = request.form['impact']
action_taken = request.form['action_taken']
source = request.form['source']
recommendations = request.form['recommendations']
assessment = request.form['assessment']
remarks = request.form['remarks']
# Insert data into the database
insert_data(report_date, incident_date, attack_type, threat_actor, threat_actor_alias,
targeted_units, target_industries, incident_sites, address, severity,
digital_infrastructure, operating_system, attack_method, impact,
action_taken, source, recommendations, assessment, remarks)
flash('Report added successfully', 'success')
return redirect(url_for('dashboard'))
return render_template('add_report.html')
# Function to fetch single report by ID
def get_report(report_id):
conn = sqlite3.connect('incident_reports.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM reports WHERE id = ?', (report_id,))
report = c.fetchone()
conn.close()
return report
# Route for the dashboard page
@app.route('/admin_dashboard')
@login_required
def dashboard():
# Fetch data from the database
conn = sqlite3.connect('incident_reports.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM reports')
reports = c.fetchall()
conn.close()
# Render the dashboard template with the fetched data
return render_template('dashboard.html', reports=reports)
# Route for viewing a single report
@app.route('/view_report/<int:report_id>')
@login_required
def view_report(report_id):
report = get_report(report_id)
if report:
return render_template('view_report.html', report=report)
else:
flash('Report not found', 'error')
return redirect(url_for('dashboard'))
# Function to update report
def update_report(report_id, report_date, incident_date, attack_type, threat_actor, threat_actor_alias,
targeted_units, target_industries, incident_sites, address, severity,
digital_infrastructure, operating_system, attack_method, impact,
action_taken, source, recommendations, assessment, remarks):
conn = sqlite3.connect('incident_reports.db')
c = conn.cursor()
c.execute('''UPDATE reports SET report_date=?, incident_date=?, attack_type=?, threat_actor=?,
threat_actor_alias=?, targeted_units=?, target_industries=?, incident_sites=?,
address=?, severity=?, digital_infrastructure=?, operating_system=?, attack_method=?,
impact=?, action_taken=?, source=?, recommendations=?, assessment=?, remarks=?
WHERE id=?''',
(report_date, incident_date, attack_type, threat_actor, threat_actor_alias,
targeted_units, target_industries, incident_sites, address, severity,
digital_infrastructure, operating_system, attack_method, impact,
action_taken, source, recommendations, assessment, remarks, report_id))
conn.commit()
conn.close()
# Route for editing a single report
@app.route('/edit_report/<int:report_id>', methods=['GET', 'POST'])
@login_required
def edit_report(report_id):
report = get_report(report_id)
if request.method == 'POST':
if report:
# Retrieve updated data from the form
report_date = request.form['report_date']
incident_date = request.form['incident_date']
attack_type = request.form['attack_type']
threat_actor = request.form['threat_actor']
threat_actor_alias = request.form['threat_actor_alias']
targeted_units = request.form['targeted_units']
target_industries = request.form['target_industries']
incident_sites = request.form['incident_sites']
address = request.form['address']
severity = request.form['severity']
digital_infrastructure = request.form['digital_infrastructure']
operating_system = request.form['operating_system']
attack_method = request.form['attack_method']
impact = request.form['impact']
action_taken = request.form['action_taken']
source = request.form['source']
recommendations = request.form['recommendations']
assessment = request.form['assessment']
remarks = request.form['remarks']
# Perform the update operation in the database
update_report(report_id, report_date, incident_date, attack_type, threat_actor, threat_actor_alias,
targeted_units, target_industries, incident_sites, address, severity,
digital_infrastructure, operating_system, attack_method, impact,
action_taken, source, recommendations, assessment, remarks)
flash('Report updated successfully', 'success')
return redirect(url_for('dashboard'))
else:
flash('Report not found', 'error')
return redirect(url_for('dashboard'))
else:
if report:
return render_template('edit_report.html', report=report)
else:
flash('Report not found', 'error')
return redirect(url_for('dashboard'))
# Route for deleting a single report
@app.route('/delete_report/<int:report_id>', methods=['POST'])
@login_required
def delete_report(report_id):
# Delete the report from the database
flash('Report deleted successfully', 'success')
return redirect(url_for('dashboard'))
# Function to fetch single report by ID
def get_report(report_id):
conn = sqlite3.connect('incident_reports.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM reports WHERE id = ?', (report_id,))
report_row = c.fetchone()
conn.close()
# Convert the row object to a dictionary
report = dict(report_row)
return report
@app.route('/export_pdf/<int:report_id>')
@login_required
def export_pdf(report_id):
# Get the report data from your database or any other source
report = get_report(report_id)
# Render the HTML template with the report data
rendered_html = render_template('report_template.html', report=report)
# Generate PDF from the rendered HTML
pdf = HTML(string=rendered_html).write_pdf()
# Create a response with PDF as attachment
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = f'attachment; filename=CyberThreatReport_{report_id}.pdf'
return response
# User loader function for Flask-Login
@login_manager.user_loader
def load_user(user_id):
# Since we don't have a user model, we'll just return a User object with the provided user_id
return User(user_id)
# Login view
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# Your authentication logic here
if username == 'ctir' and password == 'project':
user = User(1) # Create a user object
login_user(user) # Log in the user
flash('You have been logged in', 'success')
return redirect(url_for('dashboard'))
else:
flash('Login failed. Please check your credentials', 'error')
return render_template('login.html')
# Logout view
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out', 'success')
return redirect(url_for('login'))
if __name__ == '__main__':
create_table()
app.run(debug=True)