-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
150 lines (125 loc) · 5.14 KB
/
app.py
File metadata and controls
150 lines (125 loc) · 5.14 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
from flask import Flask, render_template, request, redirect, url_for, flash, send_file, session
from dotenv import load_dotenv
import downloader
import requests
from validator import is_valid_cve_format, cve_exists, is_url_accessible
from fetch_exploit import fetch_exploit_db_links
import os
import webbrowser
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv('secret_key')
NIST_API_KEY = os.getenv('NIST_API_KEY')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
cve_id = request.form['cve_id'].upper()
if cve_id:
if not is_valid_cve_format(cve_id):
flash("Invalid CVE ID format", "warning")
else:
status = cve_exists(cve_id)
if status == True:
return redirect(url_for('result', cve_id=cve_id))
else:
flash(f"CVE ID status: {status}", "warning")
else:
flash("Please enter a CVE ID", "warning")
return render_template('index.html')
@app.route('/result/<cve_id>')
def result(cve_id):
# API URL
nist_api_url = f"https://services.nvd.nist.gov/rest/json/cves/2.0?cveId={cve_id}"
mitre_api_url = f"https://cveawg.mitre.org/api/cve/{cve_id}"
# Add the API key to the request headers
headers = {
'apiKey': NIST_API_KEY
}
# Fetch details from NIST API with headers
nist_response = requests.get(nist_api_url, headers=headers).json()
mitre_response = requests.get(mitre_api_url).json()
# Check if any results were found
if nist_response.get("totalResults", 0) == 0:
flash(f"No information found for CVE ID {cve_id}", "warning")
return redirect(url_for('index'))
# Extract the relevant information
cve_data = nist_response['vulnerabilities'][0]['cve']
description = cve_data['descriptions'][0]['value']
# Handle CVSS metrics for both V31 and V21
cvss_data = None
score = 'N/A'
cvss_severity = 'N/A'
cvss_vector = 'N/A'
if 'cvssMetricV31' in cve_data['metrics']:
# CVSS v3.1
cvss_data = cve_data['metrics']['cvssMetricV31'][0]['cvssData']
score = cvss_data['baseScore']
cvss_severity = cvss_data['baseSeverity']
cvss_vector = cvss_data['vectorString']
elif 'cvssMetricV30' in cve_data['metrics']:
# CVSS v2.1
cvss_data = cve_data['metrics']['cvssMetricV30'][0]['cvssData']
score = cvss_data['baseScore']
cvss_severity = cvss_data['baseSeverity']
cvss_vector = cvss_data['vectorString']
elif 'cvssMetricV21' in cve_data['metrics']:
# CVSS v2.1
cvss_data = cve_data['metrics']['cvssMetricV21'][0]['cvssData']
score = cvss_data['baseScore']
cvss_severity = cvss_data['baseSeverity']
cvss_vector = cvss_data['vectorString']
elif 'cvssMetricV2' in cve_data['metrics']:
# CVSS v2.0
cvss_data = cve_data['metrics']['cvssMetricV2'][0]['cvssData']
score = cvss_data['baseScore']
cvss_severity = cve_data['metrics']['cvssMetricV2'][0]['baseSeverity']
cvss_vector = cvss_data['vectorString']
# Fetch and filter references (excluding broken ones)
references = [
ref['url'] for ref in cve_data['references']
if 'Broken Link' not in ref.get('tags', [])
and is_url_accessible(ref['url'])
]
for url in references[:3]:
webbrowser.open(url)
# Fetch exploit links from Exploit-DB
exploit_links = fetch_exploit_db_links(cve_id)
affected_vendors = mitre_response.get("containers", {}).get("cna", {}).get("affected", [])
session['cve_data'] = {
'cve_id': cve_id,
'description': description,
'score': score,
'severity': cvss_severity,
'vector' : cvss_vector,
'exploit_links': exploit_links,
'references': references,
'vendors' : affected_vendors
}
return render_template('result.html',
cve_id=cve_id,
description=description,
score=score,
severity=cvss_severity,
vector = cvss_vector,
exploit_links=exploit_links,
references=references,
vendors = affected_vendors
)
@app.route('/download_report/<cve_id>', methods=['POST'])
def download_report(cve_id):
file_type = request.form['file_type']
# Check if CVE data exists in session
if 'cve_data' not in session or session['cve_data']['cve_id'] != cve_id:
flash(f"Session expired or no data found for CVE ID {cve_id}. Please search again.", "warning")
return redirect(url_for('index'))
# Retrieve CVE data from session
cve_data = session['cve_data']
# Generate the report using the downloader
file_path = downloader.generate_report(cve_id, cve_data, file_type)
# Send the file as a response to the client
return send_file(file_path, as_attachment=True)
if __name__ == '__main__':
import threading
url = 'http://127.0.0.1:5000/'
webbrowser.open(url)
app.run(debug=True)