-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
398 lines (317 loc) · 17.5 KB
/
Copy pathmain.py
File metadata and controls
398 lines (317 loc) · 17.5 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
#!/usr/bin/env python3
import os
import sys
import logging
from pathlib import Path
# Time to tell Python where our cool stuff lives! 🗺️
src_dir = Path(__file__).resolve().parent / 'src'
sys.path.insert(0, str(src_dir))
# Grabbing all our fancy modules (like grocery shopping but for code) 🛒
from src.config.config_manager import ConfigManager
from src.security.device_auth_manager import DeviceAuthManager
from src.gui.main_window import MainWindow
from src.gui.device_setup_dialog import DeviceSetupDialog
from src.gui.styles import StyleManager
from src.security.secure_memory import force_secure_memory_cleanup
from src.security.emergency_protocol import EmergencyProtocol
from src.security.intelligent_monitor import IntelligentFileMonitor, ThreatLevel
from src.security.steganographic_triggers import SteganographicTriggerSystem, TriggerType, TriggerAction
from src.security.system_health_monitor import SystemHealthMonitor, ThreatLevel as HealthThreatLevel
from src.file_manager.file_manager import FileManager
# Qt time! Because everyone loves a good GUI framework 🎨
from PySide6.QtWidgets import QApplication, QDialog, QMessageBox, QLineEdit, QInputDialog
import re
class _SensitiveDataFilter(logging.Filter):
"""Truncate hex/UUID-like tokens in log messages to prevent forensic leakage.
Any contiguous hex run of 16 or more characters is replaced with its
first 8 characters followed by '…'. This covers file IDs, device IDs,
and similar identifiers while leaving enough context for debugging.
"""
_HEX_RE = re.compile(r'\b([0-9a-fA-F]{8})([0-9a-fA-F]{8,})\b')
def filter(self, record: logging.LogRecord) -> bool:
record.msg = self._HEX_RE.sub(r'\1…', str(record.msg))
record.args = None
return True
def setup_logging():
"""Set up application logging with security considerations."""
log_dir = Path.home() / '.bar' / 'logs'
log_dir.mkdir(parents=True, exist_ok=True)
if hasattr(os, 'chmod'):
os.chmod(str(log_dir), 0o700)
log_file = log_dir / 'bar_app.log'
sensitive_filter = _SensitiveDataFilter()
file_handler = logging.FileHandler(str(log_file), encoding='utf-8')
file_handler.addFilter(sensitive_filter)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.addFilter(sensitive_filter)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[file_handler, console_handler]
)
if hasattr(os, 'chmod') and log_file.exists():
os.chmod(str(log_file), 0o600)
return log_file
def setup_application_directory():
"""Set up the application directory structure."""
app_dir = Path.home() / '.bar'
app_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
# Building a secret clubhouse that only we have the key to! 🏠🔑
if hasattr(os, 'chmod'):
os.chmod(str(app_dir), 0o700)
return app_dir
class SimpleAuthDialog(QDialog):
"""Simple authentication dialog for device password entry."""
def __init__(self, device_auth, parent=None, steg_system=None):
super().__init__(parent)
self.device_auth = device_auth
self.steg_system = steg_system # The sneaky spy system that watches for secret codes 🕵️
self.authenticated = False
self.password = None # Holding onto this password like a hot potato - won't be here long! 🥔🔥
self.setWindowTitle("BAR - Device Authentication")
self.setModal(True)
self.setMinimumWidth(400)
# Because we're hackers and hackers only use dark mode 😎🌙
StyleManager.apply_theme("dark")
def exec(self):
"""Override exec to use input dialog for simplicity."""
# Hold up! Before we let anyone in, let's make sure the door isn't already deadbolted 🚪🔒
# Testing the waters with an empty password (shh, don't tell anyone)
try:
success, lockout_message = self.device_auth.authenticate("")
if not success and ("🔒 DEVICE LOCKED" in lockout_message or "⏰ DEVICE LOCKED" in lockout_message):
# Yep, door's locked tighter than a pickle jar! Time to bail 🏃💨
QMessageBox.critical(
self.parent(),
"Device Locked",
f"{lockout_message}\n\nApplication will exit due to security lockout."
)
return QDialog.DialogCode.Rejected
except Exception:
pass # If this breaks, meh, we'll find out later anyway 🤷
max_attempts = 3 # Three strikes rule (but the real bouncer is DeviceAuthManager) ⚾
attempts = 0
while attempts < max_attempts:
password, ok = QInputDialog.getText(
self.parent(),
"Device Authentication",
f"Enter your master password (Attempt {attempts + 1}/{max_attempts}):",
QLineEdit.EchoMode.Password
)
if not ok:
# User got cold feet and pressed cancel 👣❄️
return QDialog.DialogCode.Rejected
if password:
# Is this password actually a secret code? Let's check our decoder ring! 💍🔍
if self.steg_system:
trigger_activated = self.steg_system.check_password_trigger(password)
if trigger_activated:
# ABORT ABORT! Secret code detected - self-destruct sequence initiated! 💥🚨
return QDialog.DialogCode.Rejected
success, message = self.device_auth.authenticate(password)
if success:
self.authenticated = True
# Keeping this password in our pocket just for a sec (promise we'll throw it away!) 🤞
# Don't worry - file_manager will shred it like junk mail after using it 📄✂️
self.password = password
QMessageBox.information(
self.parent(),
"Authentication Successful",
message
)
return QDialog.DialogCode.Accepted
else:
# Did we just trigger something bad? Let's scan this error message real quick 👀
if ("🔒 DEVICE LOCKED" in message or
"⏰ DEVICE LOCKED" in message or
"🔒 HIGH SECURITY LOCKOUT" in message or
"⏰ STANDARD SECURITY" in message or
"🚨 SECURITY BREACH" in message):
# Houston, we have a problem! Lockdown initiated! 🚨🔴
QMessageBox.critical(
self.parent(),
"Security Action",
f"{message}\n\nApplication will exit."
)
return QDialog.DialogCode.Rejected
# Nope, wrong password buddy! Try again 🙅
attempts += 1
if attempts < max_attempts:
QMessageBox.warning(
self.parent(),
"Authentication Failed",
f"{message}\n\nAttempts remaining: {max_attempts - attempts}"
)
else:
QMessageBox.critical(
self.parent(),
"Authentication Failed",
f"{message}\n\nMaximum attempts reached. Application will exit."
)
return QDialog.DialogCode.Rejected
else:
attempts += 1
return QDialog.DialogCode.Rejected
def is_authenticated(self):
return self.authenticated
def main():
"""Main entry point for the BAR application with enhanced security."""
# First things first: let's start taking notes on everything! 📒
log_file = setup_logging()
logger = logging.getLogger("BAR.Main")
logger.info("🔥 BAR - Burn After Reading v2.0.0 Starting...")
# Time to build our secret lair! 🦸🏰
app_dir = setup_application_directory()
# Summoning the mighty Qt framework with extra security spells! ⚡🛡️
app = QApplication(sys.argv)
app.setApplicationName("BAR - Burn After Reading")
app.setApplicationVersion("2.0.0")
app.setStyle('Fusion')
# Everything's cooler in dark mode (literally, your eyes will thank me) 🕶️
StyleManager.apply_theme("dark")
try:
logger.info("Initializing device authentication manager...")
# Waking up our security guard (he's a bit grumpy in the morning) 💂♂️☕
device_auth = DeviceAuthManager()
# Starting all the Mission Impossible self-destruct gadgets 🎬💣
logger.info("Initializing enhanced security systems...")
config_manager = ConfigManager(base_directory=str(app_dir))
emergency = EmergencyProtocol(str(app_dir), device_auth)
monitor = IntelligentFileMonitor(Path(app_dir))
steg = SteganographicTriggerSystem(Path(app_dir))
health_monitor = SystemHealthMonitor(check_interval=5.0, memory_threshold=85.0, cpu_threshold=90.0, temperature_threshold=80.0)
# Is this our first date? Let's check if you've been here before 💐
if not device_auth.is_device_initialized():
logger.info("Device not initialized - starting first-time setup")
# Welcome newbie! Let's get you all set up (no spy stuff needed yet) 🎉👋
setup_dialog = DeviceSetupDialog(device_auth)
setup_result = setup_dialog.exec()
if setup_result != QDialog.DialogCode.Accepted or not setup_dialog.was_setup_successful():
logger.info("Device setup cancelled or failed - exiting")
force_secure_memory_cleanup()
sys.exit(0)
logger.info("Device setup completed successfully")
# You're registered! Now prove it's really you (with spy system on standby) 🎭
logger.info("Device initialized - requesting authentication")
auth_dialog = SimpleAuthDialog(device_auth, steg_system=steg)
auth_result = auth_dialog.exec()
if auth_result != QDialog.DialogCode.Accepted or not auth_dialog.is_authenticated():
logger.info("Authentication failed or cancelled - exiting")
force_secure_memory_cleanup()
sys.exit(0)
logger.info("Authentication successful - configuring security systems")
# CRITICAL: FileManager goes first or everything breaks! (Don't ask me why, I don't make the rules) 🎯⚠️
file_manager = FileManager(str(app_dir), monitor=monitor)
# ⚠️ SUPER IMPORTANT: Time to encrypt all the secret metadata! 🔐🎩
# Order matters here - do this backwards and everything explodes! 💥
logger.info("Initializing encrypted metadata system...")
try:
# Borrowing your password for a hot minute to make encryption keys 🔑
# Relax - we'll forget it the moment you log out! (Like a digital goldfish) 🐠
device_password = auth_dialog.password
if device_password:
file_manager.set_metadata_key(device_password)
logger.info("✓ Encrypted metadata system initialized successfully")
# Annnnnd... the password's gone! *poof* 💨✨
auth_dialog.password = None
else:
logger.warning("⚠️ Could not initialize metadata encryption - legacy mode")
except Exception as e:
logger.error(f"Failed to initialize metadata encryption: {e}")
# Oops, encryption broke! Going old-school with plain text (it's vintage!) 📼
# Hooking up our alarm system 🚨
# HIGH threats = we're nervous but won't panic yet (false alarms are super annoying)
def handle_high_threat(data):
reason = f"High threat detected: {data.get('type', 'unknown')}"
logger.warning(f"⚠️ HIGH THREAT DETECTED: {reason} - Enhanced monitoring active")
# Just keeping an extra eye out - we're not burning the house down yet! 🏠👀
# CRITICAL threats = DEFCON 1 - hit the big red button! 🔴🚀
def handle_critical_threat(data):
reason = f"Critical threat detected: {data.get('type', 'unknown')}"
logger.critical(f"🚨 CRITICAL THREAT DETECTED: {reason} - Triggering emergency wipe")
emergency.trigger_emergency_destruction(reason=reason, level="aggressive")
monitor.register_threat_callback(ThreatLevel.HIGH, handle_high_threat)
monitor.register_threat_callback(ThreatLevel.CRITICAL, handle_critical_threat)
# Connecting our system health checkup doctor 👨⚕️💉
def handle_health_threat(metrics):
if metrics.threat_level == HealthThreatLevel.CRITICAL:
threats_summary = ", ".join(metrics.active_threats[:3]) # Top 3 worst problems (nobody wants the full list) 📋
reason = f"Critical system health threat: {threats_summary}"
emergency.trigger_emergency_destruction(reason=reason, level="aggressive")
elif metrics.threat_level == HealthThreatLevel.HIGH:
threats_summary = ", ".join(metrics.active_threats[:2]) # Top 2 problems (keeping it brief) 📝
reason = f"High system health threat: {threats_summary}"
emergency.trigger_emergency_destruction(reason=reason, level="selective")
health_monitor.add_callback(handle_health_threat)
# Setting up a tripwire for suspicious activity (you can customize this later!) 🪤
# Pro tip: Don't hardcode your actual panic passwords here, Einstein 🤓
steg.install_trigger(TriggerType.ACCESS_SEQUENCE, "count:access:20", TriggerAction.AGGRESSIVE_WIPE, sensitivity=0.9, description="Rapid access default")
# Teaching our spy system what to do when it spots secret codes 🕵️📡
def steg_callback(data):
action = data.get('action')
if action == TriggerAction.SELECTIVE_WIPE.value:
emergency.trigger_emergency_destruction(reason="Steganographic trigger", level="selective")
elif action == TriggerAction.AGGRESSIVE_WIPE.value:
emergency.trigger_emergency_destruction(reason="Steganographic trigger", level="aggressive")
elif action == TriggerAction.SCORCHED_EARTH.value:
emergency.trigger_emergency_destruction(reason="Steganographic trigger", level="scorched")
for action in TriggerAction:
steg.register_trigger_callback(action, steg_callback)
# Remember that FileManager we made earlier? Yeah, it's still there chilling 😎
# Time to boot everything up! 3... 2... 1... 🚀
monitor.start_monitoring()
health_monitor.start_monitoring()
emergency.start_dead_mans_switch()
# Showtime! Opening the curtains on our fancy GUI with ALL the security bells and whistles 🎭🔔
main_window = MainWindow(config_manager, file_manager, device_auth, emergency=emergency, monitor=monitor, steg=steg, health_monitor=health_monitor)
main_window.show()
logger.info("BAR application started successfully")
# And now we wait... (this is where the magic happens) ✨⏳
exit_code = app.exec()
logger.info(f"BAR application exiting with code: {exit_code}")
# Time to clean up our mess before we leave (mom raised us right!) 🧹🧼
try:
try:
file_manager.shutdown()
except Exception:
pass
try:
monitor.stop_monitoring()
except Exception:
pass
try:
health_monitor.stop_monitoring()
except Exception:
pass
try:
emergency.stop_dead_mans_switch()
except Exception:
pass
try:
steg.cleanup()
except Exception:
pass
device_auth.logout()
force_secure_memory_cleanup()
except Exception as e:
logger.warning(f"Error during cleanup: {e}")
sys.exit(exit_code)
except Exception as e:
logger.critical(f"Critical error during startup: {e}", exc_info=True)
# OH NO! Everything's on fire! 🔥 Quick, destroy all evidence! 🏃💨
try:
if 'device_auth' in locals():
device_auth.emergency_wipe()
force_secure_memory_cleanup()
except Exception as cleanup_error:
logger.critical(f"Emergency cleanup failed: {cleanup_error}")
QMessageBox.critical(
None,
"BAR - Critical Error",
f"A critical security error occurred during startup:\n\n{str(e)}\n\n"
"Emergency cleanup has been performed.\n"
"The application will now exit."
)
sys.exit(1)
if __name__ == "__main__":
main()