-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinit.py
More file actions
481 lines (399 loc) · 16.5 KB
/
Copy pathinit.py
File metadata and controls
481 lines (399 loc) · 16.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
"""
ShadowWall AI - Advanced Initialization Script
Complete setup and configuration system for the cybersecurity platform
This script provides comprehensive initialization with:
- Dependency checking and installation guidance
- Directory structure creation
- Configuration management
- Permission validation
- Multiple operational modes (debug, simulation, setup)
- Advanced logging and error handling
"""
import asyncio
import sys
import os
import argparse
import subprocess
import platform
from pathlib import Path
# Add the project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(project_root / "src"))
from src.config.settings import load_config_from_file
from src.core.application import ShadowWallApplication
from src.utils.logger import setup_logging, get_logger
# Alias for backward compatibility
def load_config(config_path: str):
"""Load configuration from file - compatibility wrapper"""
return load_config_from_file(config_path)
def parse_arguments():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(
description="ShadowWall AI - Advanced Cybersecurity Platform",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
🚀 Usage Examples:
python init.py # Start with default configuration
python init.py --config custom.yaml # Use custom config file
python init.py --debug # Enable debug logging
python init.py --setup # Run initial setup only
python init.py --simulate # Run in simulation mode
python init.py --check-dependencies # Check system dependencies
python init.py --sandbox # Start with sandbox environment
python init.py --validate-models # Validate ML models
🔧 Advanced Options:
sudo python init.py # Full network monitoring (recommended)
python init.py --no-dashboard # Run without web dashboard
python init.py --port 9090 # Custom dashboard port
"""
)
parser.add_argument(
'--config', '-c',
type=str,
default='config/config.yaml',
help='Configuration file path (default: config/config.yaml)'
)
parser.add_argument(
'--debug', '-d',
action='store_true',
help='Enable debug logging with detailed output'
)
parser.add_argument(
'--setup', '-s',
action='store_true',
help='Run initial setup only (create directories, check dependencies)'
)
parser.add_argument(
'--check-dependencies',
action='store_true',
help='Check and install missing dependencies'
)
parser.add_argument(
'--simulate',
action='store_true',
help='Run in simulation mode (no network capture, Docker optional)'
)
parser.add_argument(
'--sandbox',
action='store_true',
help='Start with sandbox environment enabled'
)
parser.add_argument(
'--no-dashboard',
action='store_true',
help='Disable web dashboard (headless mode)'
)
parser.add_argument(
'--port', '-p',
type=int,
default=8080,
help='Dashboard port (default: 8080)'
)
parser.add_argument(
'--validate-models',
action='store_true',
help='Validate ML models and training data'
)
parser.add_argument(
'--version', '-v',
action='version',
version='ShadowWall AI v1.0.0 - Advanced Cybersecurity Platform'
)
return parser.parse_args()
def get_system_info():
"""Get system information for compatibility checks"""
return {
'platform': platform.system(),
'architecture': platform.machine(),
'python_version': platform.python_version(),
'is_root': os.geteuid() == 0 if hasattr(os, 'geteuid') else False,
'has_docker': subprocess.run(['which', 'docker'], capture_output=True).returncode == 0
}
def check_dependencies():
"""Check for required dependencies"""
logger = get_logger(__name__)
# Core dependencies
required_packages = {
'fastapi': 'FastAPI web framework',
'uvicorn': 'ASGI server',
'sqlalchemy': 'Database ORM',
'scapy': 'Packet manipulation',
'sklearn': 'Machine learning', # Note: package name is sklearn, not scikit-learn
'numpy': 'Numerical computing',
'pandas': 'Data analysis',
'redis': 'In-memory database',
'psutil': 'System monitoring',
'docker': 'Container management',
'yaml': 'YAML configuration', # Note: module name is yaml, package is pyyaml
'websockets': 'WebSocket support',
'aiofiles': 'Async file operations',
'jinja2': 'Template engine',
'cryptography': 'Cryptographic operations'
}
# Optional packages
optional_packages = {
'elasticsearch': 'Log aggregation',
'prometheus_client': 'Metrics collection',
'plotly': 'Advanced visualization',
'tensorflow': 'Deep learning (optional)',
'torch': 'PyTorch ML framework (optional)'
}
missing_packages = []
optional_missing = []
logger.info("🔍 Checking core dependencies...")
for package, description in required_packages.items():
try:
# Handle special cases for package vs module names
module_name = package.replace('-', '_')
if package == 'yaml':
module_name = 'yaml' # pyyaml installs as yaml module
elif package == 'sklearn':
module_name = 'sklearn' # scikit-learn installs as sklearn module
__import__(module_name)
logger.info(f"✅ {package} - {description}")
except ImportError:
logger.warning(f"❌ {package} - {description}")
# Show the actual package name to install
package_to_install = package
if package == 'sklearn':
package_to_install = 'scikit-learn'
elif package == 'yaml':
package_to_install = 'pyyaml'
missing_packages.append(package_to_install)
logger.info("🔍 Checking optional dependencies...")
for package, description in optional_packages.items():
try:
__import__(package.replace('-', '_'))
logger.info(f"✅ {package} - {description}")
except ImportError:
logger.info(f"⚠️ {package} - {description} (optional)")
optional_missing.append(package)
if missing_packages:
logger.error(f"❌ Missing required packages: {', '.join(missing_packages)}")
logger.info("📦 Install with: pip install " + " ".join(missing_packages))
return False
if optional_missing:
logger.info(f"📋 Optional packages not installed: {', '.join(optional_missing)}")
logger.info("📦 Install optional features with: pip install " + " ".join(optional_missing))
logger.info("✅ All required dependencies are available")
return True
def setup_directories():
"""Create necessary directories"""
logger = get_logger(__name__)
directories = [
'logs',
'data',
'data/models',
'data/honeypots',
'data/captures',
'data/reports',
'data/threat_intel',
'data/sandbox',
'config',
'temp',
'backups',
'exports'
]
logger.info("📁 Creating directory structure...")
created_count = 0
for directory in directories:
dir_path = Path(directory)
if not dir_path.exists():
dir_path.mkdir(parents=True, exist_ok=True)
logger.info(f"✅ Created: {directory}")
created_count += 1
else:
logger.debug(f"📁 Exists: {directory}")
if created_count > 0:
logger.info(f"📁 Created {created_count} new directories")
else:
logger.info("📁 All directories already exist")
def check_permissions():
"""Check for required permissions"""
logger = get_logger(__name__)
system_info = get_system_info()
logger.info("🔐 Checking system permissions...")
# Check root access (for packet capture)
if not system_info['is_root']:
logger.warning("⚠️ Not running as root - network monitoring will be limited")
logger.info("💡 For full capabilities, run with: sudo python init.py")
if system_info['platform'] != 'Windows':
logger.info("🔧 Alternative: Configure capabilities with setcap")
return False
logger.info("✅ Running with root privileges")
# Check write permissions for data directories
test_dirs = ['logs', 'data', 'temp']
for test_dir in test_dirs:
dir_path = Path(test_dir)
if dir_path.exists():
if not os.access(dir_path, os.W_OK):
logger.error(f"❌ No write permission for: {test_dir}")
return False
logger.info("✅ Directory permissions verified")
return True
def validate_models():
"""Validate ML models and training data"""
logger = get_logger(__name__)
logger.info("🧠 Validating ML models...")
model_files = [
'data/models/threat_detector.pkl',
'data/models/behavioral_analyzer.pkl',
'data/models/network_classifier.pkl'
]
models_exist = True
for model_file in model_files:
if not Path(model_file).exists():
logger.warning(f"⚠️ Model not found: {model_file}")
models_exist = False
else:
logger.info(f"✅ Found: {model_file}")
if not models_exist:
logger.info("🔄 Models will be trained on first run")
else:
logger.info("✅ All ML models are available")
return True
def display_banner():
"""Display the ShadowWall banner"""
banner = """
╔═══════════════════════════════════════════════════════════════╗
║ ShadowWall AI ║
║ ║
║ 🛡️ Advanced Cybersecurity Platform with ML Integration ║
║ ║
║ 🔍 Real-time Threat Detection 🧠 Machine Learning ║
║ 📊 Behavioral Analysis 🍯 Adaptive Honeypots ║
║ 🌐 Threat Intelligence 🏗️ Security Sandbox ║
║ 📈 Live Dashboard ⚡ Sub-100ms Response ║
║ ║
║ 🚀 Predicting Tomorrow's Threats Today ║
╚═══════════════════════════════════════════════════════════════╝
"""
print(banner)
# Display system info
system_info = get_system_info()
print(f"🖥️ Platform: {system_info['platform']} ({system_info['architecture']})")
print(f"🐍 Python: {system_info['python_version']}")
print(f"🔐 Privileges: {'Root' if system_info['is_root'] else 'User'}")
print(f"🐳 Docker: {'Available' if system_info['has_docker'] else 'Not found'}")
print()
async def run_setup():
"""Run initial setup"""
logger = get_logger(__name__)
logger.info("🚀 Running ShadowWall AI setup...")
# Create directories
setup_directories()
# Check dependencies
deps_ok = check_dependencies()
# Check permissions
perms_ok = check_permissions()
# Validate models
models_ok = validate_models()
# Create default config if it doesn't exist
config_path = Path("config/config.yaml")
if not config_path.exists():
example_config = Path("config/config.example.yaml")
if example_config.exists():
import shutil
shutil.copy(example_config, config_path)
logger.info("✅ Created default configuration from example")
else:
logger.warning("⚠️ No example configuration found")
setup_success = deps_ok and models_ok
if setup_success:
if perms_ok:
logger.info("🎉 Setup completed successfully!")
logger.info("🚀 Start ShadowWall AI with: python init.py")
else:
logger.info("✅ Setup completed with limited permissions")
logger.info("🚀 Start with: sudo python init.py (recommended)")
else:
logger.warning("⚠️ Setup completed with issues - check messages above")
return setup_success
async def main():
"""Main initialization function"""
args = parse_arguments()
# Display banner
display_banner()
# Setup logging
log_level = 'DEBUG' if args.debug else 'INFO'
setup_logging() # Use defaults
logger = get_logger(__name__)
try:
# Handle specific commands
if args.setup:
await run_setup()
return
if args.check_dependencies:
success = check_dependencies()
sys.exit(0 if success else 1)
if args.validate_models:
success = validate_models()
sys.exit(0 if success else 1)
# Load configuration
logger.info(f"📝 Loading configuration from: {args.config}")
if not Path(args.config).exists():
logger.error(f"❌ Configuration file not found: {args.config}")
logger.info("💡 Run 'python init.py --setup' to create initial configuration")
sys.exit(1)
config = load_config(args.config)
# Apply command line overrides
if args.simulate:
config['network']['simulation_mode'] = True
config['sandbox']['enabled'] = False
logger.info("🔬 Running in simulation mode")
if args.sandbox:
config['sandbox']['enabled'] = True
logger.info("🏗️ Sandbox environment enabled")
if args.no_dashboard:
config['dashboard']['enabled'] = False
logger.info("📊 Dashboard disabled (headless mode)")
if args.port != 8080:
config['dashboard']['port'] = args.port
logger.info(f"🌐 Custom dashboard port: {args.port}")
if args.debug:
config['logging']['level'] = 'DEBUG'
logger.info("🔍 Debug logging enabled")
# Run initial checks
logger.info("🔍 Performing system checks...")
setup_success = await run_setup()
if not setup_success:
logger.warning("⚠️ System checks failed - some features may not work correctly")
# Initialize and start the application
logger.info("🚀 Initializing ShadowWall AI...")
app = ShadowWallApplication(config)
# Setup signal handlers
shutdown_event = asyncio.Event()
def signal_handler(signum, frame):
logger.info(f"🔄 Received signal {signum}. Initiating graceful shutdown...")
shutdown_event.set()
import signal
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
logger.info("⚡ Starting ShadowWall AI components...")
await app.start()
logger.info("🎉 ShadowWall AI is now running!")
if config.get('dashboard', {}).get('enabled', True):
port = config.get('app', {}).get('port', 8080)
logger.info(f"🌐 Dashboard: http://localhost:{port}")
logger.info("🛑 Press Ctrl+C to stop")
# Keep running until shutdown signal
await shutdown_event.wait()
except KeyboardInterrupt:
logger.info("⌨️ Keyboard interrupt received")
except FileNotFoundError as e:
logger.error(f"❌ File not found: {e}")
logger.info("💡 Run 'python init.py --setup' to initialize the project")
sys.exit(1)
except Exception as e:
logger.error(f"💥 Failed to start ShadowWall AI: {e}")
import traceback
logger.debug(traceback.format_exc())
sys.exit(1)
finally:
logger.info("🔄 Stopping ShadowWall AI...")
if 'app' in locals():
await app.stop()
logger.info("✅ ShadowWall AI stopped successfully")
if __name__ == "__main__":
asyncio.run(main())