-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathback-end.py
272 lines (225 loc) · 9.14 KB
/
back-end.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
import json
from urllib.parse import urlparse
from bson import ObjectId
from flask import Flask, request, jsonify
import subprocess
from flask_cors import CORS
from pymongo import MongoClient
import requests
import nmap
import threading
from pymongo.errors import AutoReconnect
import time
import socket
import os
import json
from dotenv import load_dotenv # New import to load environment variables
from openai import OpenAI
from cveGPT2 import getActionPlanFromCVEid
load_dotenv() # New line to load environment variables
# Access the API key from the environment variable
openai_api_key = os.getenv("OPEN_API_KEY")
# Check if the API key is present
if not openai_api_key:
raise ValueError(
"API Key not found. Ensure that OPENAI_API_KEY is set in the .env file.")
# Initialize OpenAI client with the API key
client_openai = OpenAI(api_key=openai_api_key)
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# MongoDB connection with retry mechanism
def connect_to_mongo():
max_retries = 5
for i in range(max_retries):
try:
client = MongoClient(
"mongodb+srv://joshipro:[email protected]/?retryWrites=true&w=majority&appName=Cluster0",
tls=True,
tlsAllowInvalidCertificates=True
)
print("Done")
return client
except AutoReconnect as e:
print(f"AutoReconnect error: {e}, retrying...")
time.sleep(5)
raise Exception("Failed to connect to MongoDB after multiple retries")
# Set up the MongoDB connection
client = connect_to_mongo()
db = client['nmap_scans'] # Database name
scan_collection = db['scans'] # Collection name
# Nmap scanner object
nm = nmap.PortScanner()
ongoing_scans = {} # Dictionary to track ongoing scans
@app.route('/scan', methods=['POST'])
def scan():
data = request.json
link = data.get('ipAddress')
print(f"Received link address: {link}") # Debugging line
if not link:
return jsonify({'error': 'ipAddress is required'}), 400
domain = urlparse(link).netloc
if not domain:
return jsonify({'error': 'Invalid link provided'}), 400
try:
# Convert domain to IP address
ip_address = socket.gethostbyname(domain)
except socket.gaierror:
return jsonify({'error': 'Failed to resolve IP address'}), 400
scan_document = {
"link":link,
"domain": domain,
"IP": ip_address,
"Completed": False
}
try:
result = scan_collection.insert_one(scan_document)
print(f"Initial scan record inserted for IP {ip_address} with ID {result.inserted_id}")
except Exception as e:
print(f"Error inserting initial scan record for IP {ip_address}: {e}")
return jsonify({'error': 'Failed to start the scan'}), 500
# Start the scan in a separate thread
thread = threading.Thread(target=scan_service_version, args=(ip_address, result.inserted_id))
thread.start()
return jsonify({'message': 'Scan started', 'ipAddress': ip_address}), 202
def scan_service_version(target_ip, document_id):
scanner = nmap.PortScanner()
print(f"Scanning {target_ip} for service/version detection...")
scanner.scan(target_ip, arguments='-sV -O --script vuln', sudo=True)
host_data = gather_important_info(scanner, target_ip)
try:
scan_collection.update_one(
{"_id": document_id},
{"$set": {"Completed": True, "ScanData": host_data}}
)
print(f"Data for IP {target_ip} updated in MongoDB.")
except Exception as e:
print(f"Error updating data for IP {target_ip} in MongoDB: {e}")
print(f"Results saved to MongoDB for IP {target_ip}")
def gather_important_info(scanner, target_ip):
host_data = {
"IP": target_ip,
"State": scanner[target_ip].state(),
"OS": {},
"Ports": []
}
if 'osmatch' in scanner[target_ip]:
os_match = scanner[target_ip]['osmatch'][0]
host_data["OS"] = {
"Name": os_match['name'],
"Version": os_match['osclass'][0]['osgen'],
"Accuracy": f"{os_match['accuracy']}%"
}
for protocol in scanner[target_ip].all_protocols():
ports = scanner[target_ip][protocol].keys()
for port in ports:
port_info = scanner[target_ip][protocol][port]
port_data = {
"Port": port,
"Protocol": protocol,
"State": port_info['state'],
"Service": port_info['name'],
"Service Version": f"{port_info.get('product', '')} {port_info.get('version', '')}".strip(),
"Vulnerabilities": []
}
if 'script' in port_info:
for script, output in port_info['script'].items():
if script == 'vulners':
vulns = parse_vulners_output(output)
port_data["Vulnerabilities"] = vulns
host_data["Ports"].append(port_data)
return host_data
def parse_vulners_output(output):
vulnerabilities = []
for line in output.split('\n'):
if line.strip() and not line.startswith('cpe:'):
parts = line.split()
if len(parts) >= 3 and parts[1].replace('.', '').isdigit():
vuln = {
"CVE": parts[0],
"Severity": float(parts[1]),
"URL": parts[2]
}
if float(vuln["Severity"]) >= 0:
if(vuln["CVE"]):
print("Getting Action plan for CVE:"+vuln["CVE"])
actionplan = getActionPlanFromCVEid(vuln["CVE"], client_openai)
else:
actionplan = {}
vuln["insights"] = actionplan
vulnerabilities.append(vuln)
return vulnerabilities
def run_scan(ip_address):
with app.app_context(): # Push the application context
try:
result = subprocess.run(
['C:\\Program Files (x86)\\Nmap\\nmap.exe', '-Pn', '-sS', '-p-', ip_address],
capture_output=True, text=True, check=True)
output = result.stdout
# Parse the output to extract open ports
open_ports = []
lines = output.split('\n')
port_section = False
for line in lines:
if 'PORT' in line and 'STATE' in line:
port_section = True
continue
if port_section:
if line.strip() == '':
break
parts = line.split()
if len(parts) >= 3:
port_info = {
'port': parts[0],
'state': parts[1],
'service': parts[2]
}
open_ports.append(port_info)
# Store scan result in MongoDB
scan_result = {
'ipAddress': ip_address,
'open_ports': open_ports,
'status': 'complete' # Indicate that the scan is complete
}
result_id = scan_collection.insert_one(scan_result).inserted_id # Get the inserted ID
ongoing_scans[ip_address] = {'id': str(result_id), 'status': 'complete'}
print({
'_id': str(result_id),
'status': 'complete'
})
except subprocess.CalledProcessError as e:
print({'error': 'An error occurred while running the scan', 'details': str(e)})
except Exception as e:
print({'error': 'An unexpected error occurred', 'details': str(e)})
@app.route('/scan-status', methods=['GET'])
def scan_status():
try:
scans = scan_collection.find({}, {"IP": 1, "Completed": 1, "_id": 1, "domain": 1})
scan_list = []
for scan in scans:
scan['_id'] = str(scan['_id'])
scan_list.append(scan)
if scan_list:
return jsonify(scan_list), 200
else:
return jsonify([]), 200
except Exception as e:
print(f"Error retrieving scan status: {e}")
return jsonify({'error': 'An error occurred while fetching scan status'}), 500
@app.route('/scan/get', methods=['POST'])
def get_scan():
data = request.json
document_id = data.get('id')
if not document_id:
return jsonify({'error': 'Document ID is required'}), 400
try:
obj_id = ObjectId(document_id)
document = scan_collection.find_one({"_id": obj_id})
if document:
document['_id'] = str(document['_id'])
return jsonify(document), 200
else:
return jsonify({'error': 'Document not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)