Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
184 lines (154 loc) · 6.03 KB
/
Copy pathdatabase.py
File metadata and controls
184 lines (154 loc) · 6.03 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
"""SQLite database manager for SecureNET Control Panel hub."""
import sqlite3
import os
from datetime import datetime
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "control_panel.db")
def get_conn():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_conn()
conn.executescript("""
CREATE TABLE IF NOT EXISTS tool_status (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tool_name TEXT NOT NULL UNIQUE,
status TEXT NOT NULL,
port INTEGER,
last_seen TEXT,
uptime_start TEXT
);
CREATE TABLE IF NOT EXISTS health_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tool_name TEXT NOT NULL,
timestamp TEXT NOT NULL,
status TEXT NOT NULL,
response_time_ms INTEGER
);
CREATE TABLE IF NOT EXISTS alerts_unified (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tool_name TEXT NOT NULL,
severity TEXT NOT NULL,
title TEXT NOT NULL,
target TEXT,
description TEXT,
timestamp TEXT NOT NULL,
acknowledged INTEGER DEFAULT 0,
acknowledged_at TEXT
);
CREATE TABLE IF NOT EXISTS scan_log_unified (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tool_name TEXT NOT NULL,
target TEXT,
scan_type TEXT,
result_summary TEXT,
severity TEXT,
duration_ms INTEGER,
timestamp TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT,
updated_at TEXT
);
""")
conn.commit()
conn.close()
def save_tool_status(tool_name, status, port=None, last_seen=None, uptime_start=None):
now = last_seen or datetime.utcnow().isoformat()
conn = get_conn()
conn.execute(
"""INSERT INTO tool_status (tool_name, status, port, last_seen, uptime_start)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(tool_name) DO UPDATE SET
status=excluded.status,
port=excluded.port,
last_seen=excluded.last_seen,
uptime_start=excluded.uptime_start""",
(tool_name, status, port, now, uptime_start),
)
conn.commit()
conn.close()
def log_health_alert(tool_name, status, response_time_ms=None, timestamp=None):
ts = timestamp or datetime.utcnow().isoformat()
conn = get_conn()
conn.execute(
"INSERT INTO health_log (tool_name, timestamp, status, response_time_ms) VALUES (?, ?, ?, ?)",
(tool_name, ts, status, response_time_ms),
)
conn.commit()
conn.close()
def save_alert(tool_name, severity, title, target=None, description=None, timestamp=None):
ts = timestamp or datetime.utcnow().isoformat()
conn = get_conn()
conn.execute(
"""INSERT INTO alerts_unified (tool_name, severity, title, target, description, timestamp)
VALUES (?, ?, ?, ?, ?, ?)""",
(tool_name, severity, title, target, description, ts),
)
conn.commit()
conn.close()
def save_scan_log(tool_name, target=None, scan_type=None, result_summary=None, severity=None, duration_ms=None, timestamp=None):
ts = timestamp or datetime.utcnow().isoformat()
conn = get_conn()
conn.execute(
"""INSERT INTO scan_log_unified (tool_name, target, scan_type, result_summary, severity, duration_ms, timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(tool_name, target, scan_type, result_summary, severity, duration_ms, ts),
)
conn.commit()
conn.close()
def get_recent_alerts(limit=50):
conn = get_conn()
rows = conn.execute(
"SELECT * FROM alerts_unified ORDER BY id DESC LIMIT ?", (limit,)
).fetchall()
conn.close()
return [dict(r) for r in rows]
def get_scan_history(limit=50, offset=0):
conn = get_conn()
rows = conn.execute(
"SELECT * FROM scan_log_unified ORDER BY id DESC LIMIT ? OFFSET ?",
(limit, offset),
).fetchall()
conn.close()
return [dict(r) for r in rows]
def get_stats():
conn = get_conn()
tool_count = conn.execute("SELECT COUNT(*) FROM tool_status").fetchone()[0]
active_count = conn.execute("SELECT COUNT(*) FROM tool_status WHERE status='active'").fetchone()[0]
total_alerts = conn.execute("SELECT COUNT(*) FROM alerts_unified").fetchone()[0]
unacknowledged = conn.execute("SELECT COUNT(*) FROM alerts_unified WHERE acknowledged=0").fetchone()[0]
total_scans = conn.execute("SELECT COUNT(*) FROM scan_log_unified").fetchone()[0]
conn.close()
return {
"total_tools": tool_count,
"active_tools": active_count,
"total_alerts": total_alerts,
"unacknowledged_alerts": unacknowledged,
"total_scans": total_scans,
}
def update_setting(key, value):
now = datetime.utcnow().isoformat()
conn = get_conn()
conn.execute(
"INSERT INTO settings (key, value, updated_at) VALUES (?, ?, ?) ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at",
(key, value, now),
)
conn.commit()
conn.close()
def get_all_settings():
conn = get_conn()
rows = conn.execute("SELECT key, value, updated_at FROM settings").fetchall()
conn.close()
return {r["key"]: {"value": r["value"], "updated_at": r["updated_at"]} for r in rows}
if __name__ == "__main__":
init_db()
save_tool_status("scanner-alpha", "active", port=8080, uptime_start=datetime.utcnow().isoformat())
save_alert("scanner-alpha", "high", "Open port detected", target="10.0.0.1", description="Port 22 open")
save_scan_log("scanner-alpha", target="10.0.0.1", scan_type="nmap", result_summary="22/tcp open ssh", severity="high", duration_ms=3400)
log_health_alert("scanner-alpha", "ok", response_time_ms=120)
update_setting("scan_interval", "60")
assert get_stats()["total_tools"] == 1
assert get_recent_alerts(1)[0]["title"] == "Open port detected"
print("OK")