-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_alerts.py
More file actions
379 lines (308 loc) · 12.1 KB
/
Copy pathview_alerts.py
File metadata and controls
379 lines (308 loc) · 12.1 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
#!/usr/bin/env python3
"""
IDS Alert Viewer and Analyzer
View and analyze alerts from the IDS database
"""
import sqlite3
import argparse
from datetime import datetime, timedelta
from collections import Counter
import sys
class AlertViewer:
def __init__(self, db_path='ids_alerts.db'):
self.db_path = db_path
def connect(self):
"""Connect to alerts database"""
try:
return sqlite3.connect(self.db_path)
except sqlite3.Error as e:
print(f"Error connecting to database: {e}")
sys.exit(1)
def view_recent_alerts(self, limit=10):
"""View most recent alerts"""
conn = self.connect()
cursor = conn.cursor()
cursor.execute('''
SELECT timestamp, severity, alert_type, source_ip, dest_ip,
source_port, dest_port, description
FROM alerts
ORDER BY timestamp DESC
LIMIT ?
''', (limit,))
print(f"\n{'='*80}")
print(f"RECENT ALERTS (Last {limit})")
print(f"{'='*80}\n")
results = cursor.fetchall()
if not results:
print("No alerts found.")
conn.close()
return
for row in results:
timestamp, severity, alert_type, src_ip, dst_ip, src_port, dst_port, desc = row
# Color code by severity
if severity == 'CRITICAL':
color = '\033[91m' # Red
elif severity == 'HIGH':
color = '\033[93m' # Yellow
elif severity == 'MEDIUM':
color = '\033[94m' # Blue
else:
color = '\033[92m' # Green
reset = '\033[0m'
print(f"{timestamp}")
print(f"{color}[{severity}] {alert_type}{reset}")
print(f"Source: {src_ip}:{src_port} → Destination: {dst_ip}:{dst_port}")
print(f"Description: {desc}")
print(f"{'-'*80}\n")
conn.close()
def view_alerts_by_type(self):
"""View alert statistics by type"""
conn = self.connect()
cursor = conn.cursor()
cursor.execute('''
SELECT alert_type, COUNT(*) as count
FROM alerts
GROUP BY alert_type
ORDER BY count DESC
''')
print(f"\n{'='*80}")
print("ALERTS BY TYPE")
print(f"{'='*80}\n")
results = cursor.fetchall()
if not results:
print("No alerts found.")
conn.close()
return
total = sum(count for _, count in results)
for alert_type, count in results:
percentage = (count / total) * 100
bar_length = int(percentage / 2)
bar = '█' * bar_length
print(f"{alert_type:30} {count:5} {bar} {percentage:.1f}%")
print(f"\nTotal Alerts: {total}\n")
conn.close()
def view_alerts_by_severity(self):
"""View alert statistics by severity"""
conn = self.connect()
cursor = conn.cursor()
cursor.execute('''
SELECT severity, COUNT(*) as count
FROM alerts
GROUP BY severity
ORDER BY
CASE severity
WHEN 'CRITICAL' THEN 1
WHEN 'HIGH' THEN 2
WHEN 'MEDIUM' THEN 3
WHEN 'LOW' THEN 4
END
''')
print(f"\n{'='*80}")
print("ALERTS BY SEVERITY")
print(f"{'='*80}\n")
results = cursor.fetchall()
if not results:
print("No alerts found.")
conn.close()
return
total = sum(count for _, count in results)
severity_colors = {
'CRITICAL': '\033[91m',
'HIGH': '\033[93m',
'MEDIUM': '\033[94m',
'LOW': '\033[92m'
}
reset = '\033[0m'
for severity, count in results:
percentage = (count / total) * 100
bar_length = int(percentage / 2)
bar = '█' * bar_length
color = severity_colors.get(severity, '')
print(f"{color}{severity:15}{reset} {count:5} {bar} {percentage:.1f}%")
print(f"\nTotal Alerts: {total}\n")
conn.close()
def view_top_attackers(self, limit=10):
"""View top source IPs generating alerts"""
conn = self.connect()
cursor = conn.cursor()
cursor.execute('''
SELECT source_ip, COUNT(*) as count
FROM alerts
GROUP BY source_ip
ORDER BY count DESC
LIMIT ?
''', (limit,))
print(f"\n{'='*80}")
print(f"TOP {limit} SOURCE IPs")
print(f"{'='*80}\n")
results = cursor.fetchall()
if not results:
print("No alerts found.")
conn.close()
return
total = sum(count for _, count in results)
for i, (ip, count) in enumerate(results, 1):
percentage = (count / total) * 100
bar_length = int(percentage / 2)
bar = '█' * bar_length
print(f"{i:2}. {ip:20} {count:5} {bar} {percentage:.1f}%")
print()
conn.close()
def view_timeline(self, hours=24):
"""View alert timeline for last N hours"""
conn = self.connect()
cursor = conn.cursor()
since = (datetime.now() - timedelta(hours=hours)).isoformat()
cursor.execute('''
SELECT timestamp, severity, alert_type
FROM alerts
WHERE timestamp >= ?
ORDER BY timestamp ASC
''', (since,))
print(f"\n{'='*80}")
print(f"ALERT TIMELINE (Last {hours} hours)")
print(f"{'='*80}\n")
results = cursor.fetchall()
if not results:
print(f"No alerts in the last {hours} hours.")
conn.close()
return
# Group by hour
hourly_counts = Counter()
for timestamp, _, _ in results:
dt = datetime.fromisoformat(timestamp)
hour_key = dt.strftime('%Y-%m-%d %H:00')
hourly_counts[hour_key] += 1
# Display timeline
max_count = max(hourly_counts.values())
for hour in sorted(hourly_counts.keys()):
count = hourly_counts[hour]
bar_length = int((count / max_count) * 50)
bar = '█' * bar_length
print(f"{hour} {count:4} {bar}")
print(f"\nTotal Alerts: {len(results)}\n")
conn.close()
def search_alerts(self, search_term):
"""Search alerts by IP or description"""
conn = self.connect()
cursor = conn.cursor()
cursor.execute('''
SELECT timestamp, severity, alert_type, source_ip, dest_ip, description
FROM alerts
WHERE source_ip LIKE ? OR dest_ip LIKE ? OR description LIKE ?
ORDER BY timestamp DESC
''', (f'%{search_term}%', f'%{search_term}%', f'%{search_term}%'))
print(f"\n{'='*80}")
print(f"SEARCH RESULTS: '{search_term}'")
print(f"{'='*80}\n")
results = cursor.fetchall()
if not results:
print("No matching alerts found.")
conn.close()
return
for row in results:
timestamp, severity, alert_type, src_ip, dst_ip, desc = row
print(f"{timestamp}")
print(f"[{severity}] {alert_type}")
print(f"{src_ip} → {dst_ip}")
print(f"{desc}")
print(f"{'-'*80}\n")
print(f"Found {len(results)} matching alerts\n")
conn.close()
def export_to_csv(self, output_file='alerts_export.csv'):
"""Export alerts to CSV file"""
import csv
conn = self.connect()
cursor = conn.cursor()
cursor.execute('SELECT * FROM alerts')
with open(output_file, 'w', newline='') as f:
writer = csv.writer(f)
# Write header
writer.writerow([desc[0] for desc in cursor.description])
# Write data
writer.writerows(cursor.fetchall())
conn.close()
print(f"\nAlerts exported to: {output_file}\n")
def generate_report(self):
"""Generate comprehensive report"""
conn = self.connect()
cursor = conn.cursor()
# Get statistics
cursor.execute('SELECT COUNT(*) FROM alerts')
total_alerts = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM alerts WHERE severity='CRITICAL'")
critical_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM alerts WHERE severity='HIGH'")
high_count = cursor.fetchone()[0]
cursor.execute('SELECT MIN(timestamp), MAX(timestamp) FROM alerts')
first_alert, last_alert = cursor.fetchone()
print(f"\n{'='*80}")
print("IDS ALERT REPORT")
print(f"{'='*80}\n")
print(f"Report Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"\nAlert Period:")
print(f" First Alert: {first_alert or 'N/A'}")
print(f" Last Alert: {last_alert or 'N/A'}")
print(f"\nTotal Alerts: {total_alerts}")
print(f" Critical: {critical_count}")
print(f" High: {high_count}")
print(f"\n{'-'*80}\n")
# Show top alert types
cursor.execute('''
SELECT alert_type, COUNT(*) as count
FROM alerts
GROUP BY alert_type
ORDER BY count DESC
LIMIT 5
''')
print("Top 5 Alert Types:")
for alert_type, count in cursor.fetchall():
print(f" {alert_type:30} {count}")
# Show top attackers
cursor.execute('''
SELECT source_ip, COUNT(*) as count
FROM alerts
GROUP BY source_ip
ORDER BY count DESC
LIMIT 5
''')
print("\nTop 5 Source IPs:")
for ip, count in cursor.fetchall():
print(f" {ip:20} {count}")
print(f"\n{'='*80}\n")
conn.close()
def main():
parser = argparse.ArgumentParser(description='IDS Alert Viewer')
parser.add_argument('--db', default='ids_alerts.db', help='Database file path')
parser.add_argument('--recent', type=int, metavar='N', help='Show N recent alerts')
parser.add_argument('--by-type', action='store_true', help='Show alerts by type')
parser.add_argument('--by-severity', action='store_true', help='Show alerts by severity')
parser.add_argument('--top-attackers', type=int, metavar='N', help='Show top N attackers')
parser.add_argument('--timeline', type=int, metavar='HOURS', help='Show timeline for last N hours')
parser.add_argument('--search', type=str, metavar='TERM', help='Search alerts')
parser.add_argument('--export', type=str, metavar='FILE', help='Export to CSV')
parser.add_argument('--report', action='store_true', help='Generate comprehensive report')
args = parser.parse_args()
viewer = AlertViewer(args.db)
if args.recent:
viewer.view_recent_alerts(args.recent)
elif args.by_type:
viewer.view_alerts_by_type()
elif args.by_severity:
viewer.view_alerts_by_severity()
elif args.top_attackers:
viewer.view_top_attackers(args.top_attackers)
elif args.timeline:
viewer.view_timeline(args.timeline)
elif args.search:
viewer.search_alerts(args.search)
elif args.export:
viewer.export_to_csv(args.export)
elif args.report:
viewer.generate_report()
else:
# Default: show recent alerts
viewer.view_recent_alerts(10)
print("\nUse --help for more options\n")
if __name__ == '__main__':
main()