-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapp.py
230 lines (185 loc) · 9.09 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
import os
import json
import time
import uuid
import threading
import requests
from flask import Flask, request, render_template, jsonify, send_from_directory
from flask_cors import CORS
import ioc_fanger
from utils.config import get_config, BASE_DIR, SECRETS_FILE
from utils.utils import extract_observables
from utils.export import prepare_data_for_export, export_to_csv, export_to_excel
from models.analysis_result import AnalysisResult, db
from utils.stats import get_analysis_stats
from utils.analysis import perform_analysis, check_analysis_in_progress
app = Flask(__name__)
# Enable CORS, very permisive. If you want to restrict it, you can use the origins parameter (can break the GUI)
CORS(app)
# Ensure the data directory exists
DATA_DIR = os.path.join(BASE_DIR, 'data')
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
# Read the secrets from the secrets.json file
secrets = get_config()
# Define API_PREFIX
API_PREFIX = secrets.get("api_prefix", "api")
# Enable the config page - not intended for public use since authentication is not implemented
app.config['CONFIG_PAGE_ENABLED'] = secrets.get("config_page_enabled", False)
# Define GUI_ENABLED_ENGINES
GUI_ENABLED_ENGINES = secrets.get("gui_enabled_engines", [])
# Update the database URI to use the data directory
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{os.path.join(DATA_DIR, 'results.db')}"
# Disable modification tracking to save memory
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Set the size of the database connection pool
app.config['SQLALCHEMY_POOL_SIZE'] = 10
# Set the maximum overflow size of the connection pool
app.config['SQLALCHEMY_MAX_OVERFLOW'] = 20
# Set version
app.config['VERSION'] = "v0.4.3"
# Initialize the database
db.init_app(app)
# Create the database tables if they do not exist
with app.app_context():
db.create_all()
PROXIES = { "https": secrets["proxy_url"], "http": secrets["proxy_url"] }
def check_new_version(current_version):
url = "https://api.github.com/repos/stanfrbd/cyberbro/releases/latest"
cache_file = os.path.join(DATA_DIR, 'version_cache.json')
# Check if cache file exists and is not older than a day
if os.path.exists(cache_file):
with open(cache_file, 'r') as f:
cache_data = json.load(f)
last_checked = cache_data.get('last_checked')
if last_checked and time.time() - last_checked < 86400:
return cache_data.get('latest_version') != current_version
# If cache is older than a day or doesn't exist, fetch the latest version
response = requests.get(url, proxies=PROXIES, verify=False)
latest_release = response.json()
latest_version = latest_release["tag_name"]
# Update the cache
with open(cache_file, 'w') as f:
json.dump({'last_checked': time.time(), 'latest_version': latest_version}, f)
return latest_version != current_version
@app.route('/')
def index():
"""Render the index page."""
new_version_available = check_new_version(app.config['VERSION'])
return render_template('index.html', results=[], API_PREFIX=API_PREFIX, GUI_ENABLED_ENGINES=GUI_ENABLED_ENGINES, new_version_available=new_version_available)
@app.route('/analyze', methods=['POST'])
def analyze():
"""Handle the analyze request."""
form_data = ioc_fanger.fang(request.form.get("observables", ""))
observables = extract_observables(form_data)
selected_engines = request.form.getlist("engines")
analysis_id = str(uuid.uuid4())
threading.Thread(target=perform_analysis, args=(app, observables, selected_engines, analysis_id)).start()
return render_template('waiting.html', analysis_id=analysis_id, API_PREFIX=API_PREFIX), 200
@app.route('/results/<analysis_id>', methods=['GET'])
def show_results(analysis_id):
"""Show the results of the analysis."""
analysis_results = db.session.get(AnalysisResult, analysis_id)
if analysis_results:
return render_template('index.html', analysis_results=analysis_results, API_PREFIX=API_PREFIX)
else:
return render_template('404.html'), 404
@app.route(f'/{API_PREFIX}/is_analysis_complete/<analysis_id>', methods=['GET'])
def is_analysis_complete(analysis_id):
"""Check if the analysis is complete."""
complete = not check_analysis_in_progress(analysis_id)
return jsonify({'complete': complete})
@app.route('/export/<analysis_id>')
def export(analysis_id):
"""Export the analysis results."""
format = request.args.get('format')
analysis_results = db.session.get(AnalysisResult, analysis_id)
data = prepare_data_for_export(analysis_results)
timestamp = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
if format == 'csv':
return export_to_csv(data, timestamp)
elif format == 'excel':
return export_to_excel(data, timestamp)
@app.route('/favicon.ico')
def favicon():
"""Serve the favicon."""
return send_from_directory(os.path.join(app.root_path, 'images'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.errorhandler(404)
def page_not_found(e):
"""Handle 404 errors."""
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
"""Handle 500 errors."""
return render_template('500.html'), 500
@app.route('/history')
def history():
"""Render the history page."""
analysis_results = db.session.query(AnalysisResult).filter(AnalysisResult.results != []).order_by(AnalysisResult.end_time.desc()).limit(60).all()
return render_template('history.html', analysis_results=analysis_results)
@app.route('/stats')
def stats():
"""Render the stats page."""
stats = get_analysis_stats()
return render_template('stats.html', stats=stats)
@app.route('/about')
def about():
"""Render the about page."""
return render_template('about.html')
@app.route('/config')
def config():
"""Render the config page."""
if not app.config.get('CONFIG_PAGE_ENABLED', False):
return render_template('404.html'), 404
return render_template('config.html', secrets=secrets)
@app.route('/update_config', methods=['POST'])
def update_config():
"""Update config from the form data"""
if not app.config.get('CONFIG_PAGE_ENABLED', False):
return jsonify({'message': 'Configuration update is disabled.'}), 403
try:
secrets["proxy_url"] = request.form.get("proxy_url", secrets.get("proxy_url", ""))
secrets["virustotal"] = request.form.get("virustotal", secrets.get("virustotal", ""))
secrets["abuseipdb"] = request.form.get("abuseipdb", secrets.get("abuseipdb", ""))
secrets["ipinfo"] = request.form.get("ipinfo", secrets.get("ipinfo", ""))
secrets["google_safe_browsing"] = request.form.get("google_safe_browsing", secrets.get("google_safe_browsing", ""))
secrets["mde_tenant_id"] = request.form.get("mde_tenant_id", secrets.get("mde_tenant_id", ""))
secrets["mde_client_id"] = request.form.get("mde_client_id", secrets.get("mde_client_id", ""))
secrets["mde_client_secret"] = request.form.get("mde_client_secret", secrets.get("mde_client_secret", ""))
secrets["shodan"] = request.form.get("shodan", secrets.get("shodan", ""))
secrets["opencti_api_key"] = request.form.get("opencti_api_key", secrets.get("opencti_api_key", ""))
secrets["opencti_url"] = request.form.get("opencti_url", secrets.get("opencti_url", ""))
# Apply the GUI_ENABLED_ENGINES configuration directly to the GUI to avoid restarting the app
global GUI_ENABLED_ENGINES
GUI_ENABLED_ENGINES = request.form.get("gui_enabled_engines", "")
secrets["gui_enabled_engines"] = [engine.strip().lower() for engine in GUI_ENABLED_ENGINES.split(",")] if GUI_ENABLED_ENGINES else []
GUI_ENABLED_ENGINES = secrets["gui_enabled_engines"]
# Save the secrets to the secrets.json file
with open(SECRETS_FILE, 'w') as f:
json.dump(secrets, f, indent=4)
message = "Configuration updated successfully."
except Exception as e:
message = f"An error occurred while updating the configuration. {e}"
return jsonify({'message': message})
@app.route(f'/{API_PREFIX}/results/<analysis_id>', methods=['GET'])
def get_results(analysis_id):
"""Get the results of the analysis."""
analysis_results = db.session.get(AnalysisResult, analysis_id)
if analysis_results:
return jsonify(analysis_results.results)
else:
return jsonify({'error': 'Analysis not found.'}), 404
@app.route(f'/{API_PREFIX}/analyze', methods=['POST'])
def analyze_api():
"""Handle the analyze request via API. (Only JSON data is accepted)"""
data = request.get_json()
form_data = ioc_fanger.fang(data.get("text", ""))
observables = extract_observables(form_data)
# all engines
selected_engines = data.get("engines", [])
analysis_id = str(uuid.uuid4())
threading.Thread(target=perform_analysis, args=(app, observables, selected_engines, analysis_id)).start()
# return the page link to the analysis and the analysis_id
return jsonify({'analysis_id': analysis_id, 'link': f"/results/{analysis_id}"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)