-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
177 lines (158 loc) · 5.43 KB
/
dashboard.py
File metadata and controls
177 lines (158 loc) · 5.43 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
#!/usr/bin/env python3
"""Adaptive Defender Dashboard with Pagination"""
from flask import Flask, render_template_string, request
import sqlite3
from datetime import datetime
import os
import json
DB_PATH = "behavior.db"
app = Flask(__name__)
TEMPLATE = """
<!doctype html>
<html>
<head>
<title>Adaptive Defender Dashboard</title>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #1e1e2f; color: #e0e0e0; margin: 0; padding: 20px; }
h1 { color: #ff6f61; text-align: center; }
h2 { color: #ffb86c; margin-top: 40px; }
table { border-collapse: collapse; width: 100%; margin-top: 10px; }
th, td { border: 1px solid #444; padding: 8px 12px; text-align: left; vertical-align: top; }
th { background-color: #2e2e3e; }
tr:nth-child(even) { background-color: #2a2a3a; }
tr:hover { background-color: #3e3e5e; }
.stats { margin-top: 20px; font-size: 1.1em; }
.container { max-width: 1400px; margin: auto; }
pre { white-space: pre-wrap; word-wrap: break-word; }
.pagination { margin-top: 20px; text-align: center; }
.pagination a { color: #ffb86c; margin: 0 5px; text-decoration: none; }
.pagination a.current { font-weight: bold; color: #ff6f61; }
</style>
</head>
<body>
<div class="container">
<h1>Adaptive Defender Dashboard</h1>
<h2>Quarantined Processes</h2>
<table>
<tr>
<th>PID</th><th>Name</th><th>Username</th><th>Command Line</th>
<th>CWD</th><th>CPU%</th><th>Mem%</th><th>IO Read</th><th>IO Write</th>
<th>Threads</th><th>FDs</th><th>Conns</th><th>#Env Vars</th><th>#Modules</th>
<th>Reason</th><th>Timestamp</th>
</tr>
{% for q in quarantines %}
<tr>
<td>{{q['pid']}}</td>
<td>{{q['name']}}</td>
<td>{{q['username']}}</td>
<td><pre>{{q['cmdline']}}</pre></td>
<td>{{q['cwd']}}</td>
<td>{{q['cpu']}}</td>
<td>{{q['mem']}}</td>
<td>{{q['io_read']}}</td>
<td>{{q['io_write']}}</td>
<td>{{q['num_threads']}}</td>
<td>{{q['num_fds']}}</td>
<td>{{q['num_conns']}}</td>
<td>{{q['num_envvars']}}</td>
<td>{{q['num_modules']}}</td>
<td>{{q['reason']}}</td>
<td>{{q['ts']}}</td>
</tr>
{% endfor %}
</table>
<div class="pagination">
{% if page > 1 %}
<a href="/?page={{page-1}}">« Prev</a>
{% endif %}
{% for p in range(1, total_pages+1) %}
<a href="/?page={{p}}" class="{{ 'current' if p==page else '' }}">{{p}}</a>
{% endfor %}
{% if page < total_pages %}
<a href="/?page={{page+1}}">Next »</a>
{% endif %}
</div>
<h2>Model Stats</h2>
<div class="stats">
<p><strong>Total feature rows:</strong> {{feature_count}}</p>
<p><strong>Last retrain:</strong> {{last_retrain}}</p>
</div>
</div>
</body>
</html>
"""
PAGE_SIZE = 20 # rows per page
def fetch_data(page=1):
if not os.path.exists(DB_PATH):
return [], 0, 'N/A', 1
offset = (page - 1) * PAGE_SIZE
with sqlite3.connect(DB_PATH) as con:
con.row_factory = sqlite3.Row
cur = con.cursor()
# Get total count for pagination
cur.execute("SELECT COUNT(*) as cnt FROM quarantines")
total_count = cur.fetchone()['cnt']
total_pages = max(1, (total_count + PAGE_SIZE - 1) // PAGE_SIZE)
cur.execute(f"""
SELECT ts, pid,
json_extract(snapshot_json, '$.name') as name,
json_extract(snapshot_json, '$.username') as username,
json_extract(snapshot_json, '$.cmdline') as cmdline,
json_extract(snapshot_json, '$.cwd') as cwd,
json_extract(snapshot_json, '$.cpu') as cpu,
json_extract(snapshot_json, '$.mem') as mem,
json_extract(snapshot_json, '$.io_read') as io_read,
json_extract(snapshot_json, '$.io_write') as io_write,
json_extract(snapshot_json, '$.num_threads') as num_threads,
json_extract(snapshot_json, '$.num_fds') as num_fds,
json_extract(snapshot_json, '$.num_conns') as num_conns,
json_extract(snapshot_json, '$.envvars') as envvars,
json_extract(snapshot_json, '$.modules') as modules,
json_extract(snapshot_json, '$.reason') as reason
FROM quarantines
ORDER BY ts DESC
LIMIT {PAGE_SIZE} OFFSET {offset}
""")
quarantines = []
for row in cur.fetchall():
ts_readable = datetime.fromtimestamp(row['ts']).strftime("%d %b %Y %H:%M:%S")
envvars = json.loads(row['envvars']) if row['envvars'] else {}
modules = json.loads(row['modules']) if row['modules'] else []
quarantines.append({
'pid': row['pid'],
'name': row['name'],
'username': row['username'],
'cmdline': row['cmdline'],
'cwd': row['cwd'],
'cpu': row['cpu'] or 0,
'mem': row['mem'] or 0,
'io_read': row['io_read'] or 0,
'io_write': row['io_write'] or 0,
'num_threads': row['num_threads'] or 0,
'num_fds': row['num_fds'] or 0,
'num_conns': row['num_conns'] or 0,
'num_envvars': len(envvars),
'num_modules': len(modules),
'reason': row['reason'],
'ts': ts_readable
})
cur.execute("SELECT COUNT(*) as cnt FROM features")
feature_count = cur.fetchone()['cnt']
cur.execute("SELECT ts FROM features ORDER BY ts DESC LIMIT 1")
row = cur.fetchone()
last_retrain = datetime.fromtimestamp(row['ts']).strftime("%d %b %Y %H:%M:%S") if row else 'N/A'
return quarantines, feature_count, last_retrain, total_pages
@app.route('/')
def index():
page = int(request.args.get('page', 1))
quarantines, feature_count, last_retrain, total_pages = fetch_data(page)
return render_template_string(
TEMPLATE,
quarantines=quarantines,
feature_count=feature_count,
last_retrain=last_retrain,
page=page,
total_pages=total_pages
)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8888, debug=False)