-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_manager.py
More file actions
executable file
Β·377 lines (306 loc) Β· 13.7 KB
/
log_manager.py
File metadata and controls
executable file
Β·377 lines (306 loc) Β· 13.7 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
#!/usr/bin/env python3
"""
Honeypot Log Management and Filtering System
Manages log rotation, filtering, and aggregation across all detection modules
"""
import os
import gzip
import json
import time
import shutil
import logging
from datetime import datetime, timedelta
from pathlib import Path
import re
class HoneypotLogManager:
def __init__(self, log_dir="/home/burner/honeypot-minimal/logs"):
self.log_dir = Path(log_dir)
self.config_file = Path("/home/burner/honeypot-minimal/log_config.json")
# Default configuration
self.config = {
"max_log_size_mb": 50,
"max_log_files": 5,
"compression_enabled": True,
"log_levels": {
"wifi_threat": "INFO",
"usb_threat": "INFO",
"ble_threat": "INFO",
"multi_vector": "INFO",
"advanced_wireless": "INFO"
},
"noise_filters": {
"duplicate_threshold_seconds": 300,
"min_threat_score": 0.3,
"exclude_patterns": [
"RequestsDependencyWarning",
"urllib3.*doesn't match",
"charset_normalizer",
"Starting.*Detection System"
],
"whitelist_ssids": [
"eduroam",
"Guest Network",
"Visitor"
],
"rate_limit_per_hour": {
"weak_security": 5,
"hidden_ssid": 3,
"suspicious_ssid": 10
}
}
}
self.load_config()
self.threat_counts = {} # Track threat frequency
self.recent_threats = {} # Deduplication tracking
def load_config(self):
"""Load configuration from file or create default"""
try:
if self.config_file.exists():
with open(self.config_file, 'r') as f:
saved_config = json.load(f)
# Merge with defaults
self.config.update(saved_config)
print(f"π Loaded log management config from {self.config_file}")
else:
self.save_config()
print(f"π Created default log management config at {self.config_file}")
except Exception as e:
print(f"β οΈ Error loading log config: {e}, using defaults")
def save_config(self):
"""Save current configuration to file"""
try:
with open(self.config_file, 'w') as f:
json.dump(self.config, f, indent=2)
except Exception as e:
print(f"β Error saving log config: {e}")
def setup_logging(self, module_name):
"""Setup filtered logging for a detection module"""
log_level = getattr(logging, self.config["log_levels"].get(module_name, "INFO"))
# Create custom formatter that filters noise
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Setup file handler with rotation
log_file = self.log_dir / f"{module_name}_filtered.log"
handler = logging.handlers.RotatingFileHandler(
log_file,
maxBytes=self.config["max_log_size_mb"] * 1024 * 1024,
backupCount=self.config["max_log_files"]
)
handler.setFormatter(formatter)
# Create logger
logger = logging.getLogger(f"honeypot.{module_name}")
logger.setLevel(log_level)
logger.addHandler(handler)
# Add noise filter
logger.addFilter(self.create_noise_filter())
return logger
def create_noise_filter(self):
"""Create a logging filter to reduce noise"""
class NoiseFilter(logging.Filter):
def __init__(self, manager):
super().__init__()
self.manager = manager
def filter(self, record):
message = record.getMessage()
# Filter out excluded patterns
for pattern in self.manager.config["noise_filters"]["exclude_patterns"]:
if re.search(pattern, message, re.IGNORECASE):
return False
# Rate limiting for common threat types
if hasattr(record, 'threat_type'):
threat_type = record.threat_type
limit = self.manager.config["noise_filters"]["rate_limit_per_hour"].get(threat_type, float('inf'))
current_hour = datetime.now().hour
key = f"{threat_type}_{current_hour}"
if key not in self.manager.threat_counts:
self.manager.threat_counts[key] = 0
self.manager.threat_counts[key] += 1
if self.manager.threat_counts[key] > limit:
return False
return True
return NoiseFilter(self)
def rotate_logs(self):
"""Manually rotate and compress old logs"""
print("π Starting log rotation...")
for log_file in self.log_dir.glob("*.log"):
if log_file.stat().st_size > self.config["max_log_size_mb"] * 1024 * 1024:
print(f"π¦ Rotating large log file: {log_file.name}")
self.rotate_single_log(log_file)
# Clean old compressed logs
self.cleanup_old_logs()
def rotate_single_log(self, log_file):
"""Rotate a single log file"""
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
rotated_name = f"{log_file.stem}_{timestamp}.log"
rotated_path = self.log_dir / rotated_name
# Copy current log to rotated name
shutil.copy2(log_file, rotated_path)
# Compress if enabled
if self.config["compression_enabled"]:
compressed_path = rotated_path.with_suffix(".log.gz")
with open(rotated_path, 'rb') as f_in:
with gzip.open(compressed_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
rotated_path.unlink() # Remove uncompressed version
print(f"π¦ Compressed {rotated_name} -> {compressed_path.name}")
# Truncate original log
with open(log_file, 'w') as f:
f.write(f"# Log rotated at {datetime.now()}\n")
except Exception as e:
print(f"β Error rotating {log_file}: {e}")
def cleanup_old_logs(self):
"""Remove old compressed logs beyond retention limit"""
compressed_logs = list(self.log_dir.glob("*.log.gz"))
# Group by base name
log_groups = {}
for log_file in compressed_logs:
base_name = log_file.name.split('_')[0]
if base_name not in log_groups:
log_groups[base_name] = []
log_groups[base_name].append(log_file)
# Keep only the newest files for each log type
for base_name, files in log_groups.items():
files.sort(key=lambda x: x.stat().st_mtime, reverse=True)
to_remove = files[self.config["max_log_files"]:]
for old_file in to_remove:
print(f"ποΈ Removing old log: {old_file.name}")
old_file.unlink()
def filter_elasticsearch_logs(self):
"""Filter and deduplicate threats going to Elasticsearch"""
print("π Filtering Elasticsearch logs...")
# This would integrate with the data forwarder to filter before sending
# For now, we'll create a filtered version of recent logs
try:
import requests
# Query recent logs from Elasticsearch
query = {
"query": {
"range": {
"timestamp": {
"gte": "now-1h"
}
}
},
"size": 1000,
"sort": [{"timestamp": {"order": "desc"}}]
}
response = requests.post(
'http://localhost:9200/honeypot-logs/_search',
json=query,
timeout=10
)
if response.status_code == 200:
data = response.json()
hits = data.get('hits', {}).get('hits', [])
filtered_count = 0
duplicate_count = 0
for hit in hits:
source = hit['_source']
# Apply threat score filter
threat_score = source.get('threat_score', 0)
if threat_score < self.config["noise_filters"]["min_threat_score"]:
filtered_count += 1
continue
# Check for duplicates
threat_key = f"{source.get('source', '')}:{source.get('message', '')}"
if self.is_duplicate_threat(threat_key):
duplicate_count += 1
continue
print(f"π Elasticsearch filter results:")
print(f" - {filtered_count} low-score threats filtered")
print(f" - {duplicate_count} duplicate threats filtered")
print(f" - {len(hits) - filtered_count - duplicate_count} legitimate threats")
except Exception as e:
print(f"β Error filtering Elasticsearch logs: {e}")
def is_duplicate_threat(self, threat_key):
"""Check if threat is a recent duplicate"""
now = datetime.now()
threshold = timedelta(seconds=self.config["noise_filters"]["duplicate_threshold_seconds"])
if threat_key in self.recent_threats:
last_seen = self.recent_threats[threat_key]
if now - last_seen < threshold:
return True
self.recent_threats[threat_key] = now
return False
def generate_log_report(self):
"""Generate a summary report of log activity"""
print("\nπ Honeypot Log Summary Report")
print("=" * 50)
# Analyze log files
total_size = 0
file_count = 0
for log_file in self.log_dir.glob("*.log"):
size_mb = log_file.stat().st_size / (1024 * 1024)
total_size += size_mb
file_count += 1
print(f"π {log_file.name}: {size_mb:.2f} MB")
# Count compressed logs
compressed_count = len(list(self.log_dir.glob("*.log.gz")))
print(f"\nπ Summary:")
print(f" - Active log files: {file_count}")
print(f" - Compressed archives: {compressed_count}")
print(f" - Total size: {total_size:.2f} MB")
# Threat frequency analysis
if self.threat_counts:
print(f"\nβ‘ Threat Frequency (current hour):")
for threat_type, count in sorted(self.threat_counts.items()):
print(f" - {threat_type}: {count} alerts")
def run_maintenance(self):
"""Run log maintenance tasks"""
print("π§ Running log maintenance...")
# Rotate large logs
self.rotate_logs()
# Filter recent threats
self.filter_elasticsearch_logs()
# Clean up threat tracking
self.cleanup_threat_tracking()
# Generate report
self.generate_log_report()
print("β
Log maintenance completed")
def cleanup_threat_tracking(self):
"""Clean up old threat tracking data"""
cutoff = datetime.now() - timedelta(hours=1)
# Clean recent threats
old_keys = [k for k, v in self.recent_threats.items() if v < cutoff]
for key in old_keys:
del self.recent_threats[key]
# Clean hourly threat counts for previous hours
current_hour = datetime.now().hour
old_hour_keys = [k for k in self.threat_counts.keys()
if not k.endswith(f"_{current_hour}")]
for key in old_hour_keys:
del self.threat_counts[key]
def start_monitoring(self):
"""Start continuous log monitoring"""
print("π Starting log monitoring...")
print("π‘ Monitoring log sizes and performing maintenance")
print("π Press Ctrl+C to stop")
try:
while True:
# Run maintenance every 10 minutes
self.run_maintenance()
# Wait
time.sleep(600) # 10 minutes
except KeyboardInterrupt:
print("\nπ Log monitoring stopped")
if __name__ == "__main__":
manager = HoneypotLogManager()
import sys
if len(sys.argv) > 1:
command = sys.argv[1]
if command == "rotate":
manager.rotate_logs()
elif command == "filter":
manager.filter_elasticsearch_logs()
elif command == "report":
manager.generate_log_report()
elif command == "maintenance":
manager.run_maintenance()
elif command == "monitor":
manager.start_monitoring()
else:
print("Usage: log_manager.py [rotate|filter|report|maintenance|monitor]")
else:
manager.run_maintenance()