Repository navigation
Expand file tree
/
Copy pathhub.py
More file actions
467 lines (357 loc) · 14 KB
/
Copy pathhub.py
File metadata and controls
467 lines (357 loc) · 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""SecureNET Control Panel - Main Hub
Central orchestration hub for all SecureNET tools.
Run: python hub.py
"""
from __future__ import annotations
import atexit
import os
import sys
import threading
import time
from datetime import datetime
from pathlib import Path
import yaml
from flask import Flask, jsonify, render_template, request
# Local imports
sys.path.insert(0, str(Path(__file__).parent))
from database import init_db, save_tool_status, log_health_alert, save_alert, save_scan_log
from database import get_recent_alerts, get_scan_history, get_stats, update_setting, get_all_settings
from process_manager import ProcessManager
from health_monitor import HealthMonitor
from alert_aggregator import AlertAggregator
from quick_scan import (quick_scan_headerscan, quick_scan_portmap, quick_scan_hashdetect,
quick_scan_tlscan, quick_scan_dnsaudit, quick_scan_subprobe,
quick_scan_jwtinspect, quick_scan_secretsniff, quick_scan_vulnprobe,
quick_scan_pathprobe,
quick_scan_cloudsentry,
quick_scan_imgscan,
quick_scan_techfinger,
quick_scan_apiguard,
get_result)
# Load config
CONFIG_PATH = Path(__file__).parent / "securenet.yaml"
config = {}
if CONFIG_PATH.exists():
with open(CONFIG_PATH) as f:
config = yaml.safe_load(f)
settings = config.get("settings", {})
tools_config = config.get("tools", {})
app = Flask(__name__)
app.config["SECRET_KEY"] = "securenet-hub-" + datetime.now().strftime("%Y%m%d")
# Global managers
process_manager = ProcessManager(str(Path(__file__).parent / "logs"))
health_monitor = HealthMonitor(process_manager)
alert_aggregator = AlertAggregator()
# Track start time for uptime
START_TIME = time.time()
@app.before_request
def startup():
"""Initialize subsystems on first request (idempotent)."""
if not getattr(app, '_initialized', False):
try:
init_db()
save_tool_status("hub", "running", settings.get("hub_port", 5000))
health_monitor.start()
alert_aggregator.start()
app._initialized = True
except Exception as e:
print(f"[WARN] Startup init failed: {e}")
print("[OK] Control panel subsystems initialized")
@app.teardown_appcontext
def shutdown(exception=None):
"""Cleanup on shutdown."""
pass
def cleanup_all():
"""Graceful shutdown of all subprocesses."""
health_monitor.stop()
alert_aggregator.stop()
process_manager.cleanup_all()
save_tool_status("hub", "stopped", settings.get("hub_port", 5000))
atexit.register(cleanup_all)
# === Page Routes ===
PAGE_TEMPLATES = {
"command": "command_center.html",
"analytics": "analytics.html",
"alerts": "alerts.html",
"history": "history.html",
"tools": "tools_manager.html",
"docs": "docs.html",
}
def render_page(template_name):
return render_template(template_name, settings=settings, tools=tools_config)
@app.route("/")
def index():
return render_page("command_center.html")
@app.route("/command")
def command_center():
return render_page("command_center.html")
@app.route("/analytics")
def analytics():
return render_page("analytics.html")
@app.route("/alerts")
def alerts():
return render_page("alerts.html")
@app.route("/history")
def history():
return render_page("history.html")
@app.route("/tools")
def tools_manager():
return render_page("tools_manager.html")
@app.route("/docs")
def docs():
return render_page("docs.html")
# === API Routes ===
@app.route("/api/status")
def api_status():
"""Get status of all tools."""
statuses = process_manager.get_all_statuses()
for tool_key, tool_config in tools_config.items():
if tool_key not in statuses:
port = tool_config.get("port", 0)
statuses[tool_key] = {
"name": tool_config.get("name", tool_key),
"status": "STOPPED",
"port": port,
"color": tool_config.get("color", "#64748b"),
"enabled": tool_config.get("enabled", True),
}
return jsonify({"tools": statuses, "hub_uptime": int(time.time() - START_TIME)})
@app.route("/api/status/<tool>")
def api_status_tool(tool):
"""Get status of a single tool."""
status = process_manager.get_status(tool)
if not status:
return jsonify({"error": f"Unknown tool: {tool}"}), 404
return jsonify(status)
@app.route("/api/start/<tool>", methods=["POST"])
def api_start(tool):
"""Start a tool subprocess."""
result = process_manager.start_tool(tool)
if result:
save_tool_status(tool, "running", tools_config.get(tool, {}).get("port", 0))
return jsonify({"status": "started", "tool": tool})
return jsonify({"error": "Failed to start tool"}), 500
@app.route("/api/stop/<tool>", methods=["POST"])
def api_stop(tool):
"""Stop a tool subprocess."""
result = process_manager.stop_tool(tool)
if result:
save_tool_status(tool, "stopped", tools_config.get(tool, {}).get("port", 0))
return jsonify({"status": "stopped", "tool": tool})
return jsonify({"error": "Failed to stop tool"}), 500
@app.route("/api/restart/<tool>", methods=["POST"])
def api_restart(tool):
"""Restart a tool subprocess."""
result = process_manager.restart_tool(tool)
if result:
save_tool_status(tool, "running", tools_config.get(tool, {}).get("port", 0))
return jsonify({"status": "restarted", "tool": tool})
return jsonify({"error": "Failed to restart tool"}), 500
@app.route("/api/alerts")
def api_alerts():
"""Get unified alerts."""
page = request.args.get("page", 1, type=int)
per_page = request.args.get("per_page", 50, type=int)
severity = request.args.get("severity", None)
tool = request.args.get("tool", None)
alerts = alert_aggregator.get_unified_alerts()
if severity:
alerts = [a for a in alerts if a.get("severity") == severity]
if tool:
alerts = [a for a in alerts if a.get("tool") == tool]
total = len(alerts)
start = (page - 1) * per_page
end = start + per_page
alerts_paginated = alerts[start:end]
return jsonify({"alerts": alerts_paginated, "total": total, "page": page, "per_page": per_page})
@app.route("/api/alerts/acknowledge", methods=["POST"])
def api_acknowledge_alerts():
"""Acknowledge alerts."""
data = request.get_json()
ids = data.get("ids", [])
return jsonify({"acknowledged": len(ids)})
@app.route("/api/stats")
def api_stats():
"""Get global analytics data."""
stats = get_stats()
stats["active_scans"] = 0
stats["alerts_today"] = len([a for a in alert_aggregator.get_unified_alerts()
if a.get("timestamp", "").startswith(datetime.now().strftime("%Y-%m-%d"))])
stats["running_tools"] = sum(1 for s in process_manager.get_all_statuses().values()
if s.get("status") == "running")
stats["total_tools"] = len(tools_config)
return jsonify(stats)
@app.route("/api/history")
def api_history():
"""Get unified scan history."""
page = request.args.get("page", 1, type=int)
per_page = request.args.get("per_page", 50, type=int)
history = get_scan_history(limit=per_page, offset=(page - 1) * per_page)
return jsonify({"history": history, "page": page, "per_page": per_page})
@app.route("/api/health")
def api_health():
"""Hub health check."""
return jsonify({
"status": "healthy",
"uptime_seconds": int(time.time() - START_TIME),
"python_version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
"timestamp": datetime.now().isoformat(),
})
@app.route("/api/tools")
def api_tools():
"""Get tool configuration list."""
return jsonify({"tools": tools_config, "settings": settings})
@app.route("/api/settings", methods=["POST"])
def api_settings():
"""Update settings."""
data = request.get_json()
for key, value in data.items():
update_setting(key, str(value))
return jsonify({"status": "updated", "settings": get_all_settings()})
@app.route("/api/logs/<tool>")
def api_logs(tool):
"""Get last 100 log lines for a tool."""
log_path = Path(__file__).parent / "logs" / f"{tool}.log"
if not log_path.exists():
return jsonify({"logs": []})
lines = log_path.read_text().split("\n")[-100:]
return jsonify({"logs": lines})
# === Quick Scan API ===
@app.route("/api/quickscan/headerscan", methods=["POST"])
def quickscan_headerscan():
data = request.get_json()
url = data.get("url", "")
job_id = quick_scan_headerscan(url)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/portmap", methods=["POST"])
def quickscan_portmap():
data = request.get_json()
host = data.get("host", "")
job_id = quick_scan_portmap(host)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/hashdetect", methods=["POST"])
def quickscan_hashdetect():
data = request.get_json()
hash_str = data.get("hash", "")
job_id = quick_scan_hashdetect(hash_str)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/tlscan", methods=["POST"])
def quickscan_tlscan():
data = request.get_json()
domain = data.get("domain", "")
job_id = quick_scan_tlscan(domain)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/dnsaudit", methods=["POST"])
def quickscan_dnsaudit():
data = request.get_json()
domain = data.get("domain", "")
job_id = quick_scan_dnsaudit(domain)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/subprobe", methods=["POST"])
def quickscan_subprobe():
data = request.get_json()
domain = data.get("domain", "")
job_id = quick_scan_subprobe(domain)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/jwtinspect", methods=["POST"])
def quickscan_jwtinspect():
data = request.get_json()
token = data.get("token", "")
job_id = quick_scan_jwtinspect(token)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/secretsniff", methods=["POST"])
def quickscan_secretsniff():
data = request.get_json()
path = data.get("path", "")
job_id = quick_scan_secretsniff(path)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/vulnprobe", methods=["POST"])
def quickscan_vulnprobe():
data = request.get_json()
url = data.get("url", "")
severity = data.get("severity", "HIGH,CRITICAL")
job_id = quick_scan_vulnprobe(url, severity)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/pathprobe", methods=["POST"])
def quickscan_pathprobe():
data = request.get_json()
url = data.get("url", "")
wordlist = data.get("wordlist", "common")
job_id = quick_scan_pathprobe(url, wordlist)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/cloudsentry", methods=["POST"])
def quickscan_cloudsentry():
data = request.get_json()
provider = data.get("provider", "aws")
profile = data.get("profile")
job_id = quick_scan_cloudsentry(provider, profile)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/imgscan", methods=["POST"])
def quickscan_imgscan():
data = request.get_json(silent=True) or {}
path = data.get("path", "./myproject")
job_id = quick_scan_imgscan(path)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/techfinger", methods=["POST"])
def quickscan_techfinger():
data = request.get_json()
url = data.get("url", "https://example.com")
job_id = quick_scan_techfinger(url)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/apiguard", methods=["POST"])
def quickscan_apiguard():
data = request.get_json(silent=True) or {}
url = data.get("url", "https://api.example.com")
auth = data.get("auth", "none")
spec = data.get("spec", "")
job_id = quick_scan_apiguard(url, auth, spec)
return jsonify({"job_id": job_id, "status": "pending"})
@app.route("/api/quickscan/result/<job_id>")
def quickscan_result(job_id):
result = get_result(job_id)
return jsonify(result)
# === Proxy Route ===
@app.route("/proxy/<tool>/", defaults={"path": ""})
@app.route("/proxy/<tool>/<path:path>")
def proxy(tool, path):
"""Proxy requests to tool dashboards."""
import requests as req
tool_config = tools_config.get(tool)
if not tool_config:
return jsonify({"error": "Unknown tool"}), 404
port = tool_config.get("port")
url = f"http://127.0.0.1:{port}/{path}"
try:
resp = req.request(
method=request.method,
url=url,
headers={k: v for k, v in request.headers if k.lower() != "host"},
data=request.get_data(),
cookies=request.cookies,
allow_redirects=False,
timeout=30,
)
excluded_headers = ["content-encoding", "content-length", "transfer-encoding", "connection"]
headers = [(k, v) for k, v in resp.raw.headers.items() if k.lower() not in excluded_headers]
return resp.content, resp.status_code, headers
except req.exceptions.ConnectionError:
return jsonify({"error": f"{tool} is not running on port {port}"}), 502
except Exception as e:
return jsonify({"error": str(e)}), 500
# === Main ===
def main():
"""Run the control panel."""
hub_port = settings.get("hub_port", 5000)
bind = settings.get("bind_address", "127.0.0.1")
if bind != "127.0.0.1":
print("[WARN] Binding to non-localhost is not recommended!")
print("[WARN] Tool communication is localhost-only.")
print("=" * 50)
print(" SecureNET Control Panel")
print(f" http://{bind}:{hub_port}")
print("=" * 50)
# Initialize database
init_db()
app.run(host=bind, port=hub_port, debug=False, threaded=True)
if __name__ == "__main__":
main()