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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import json
|
| 4 |
+
import datetime
|
| 5 |
+
import random
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import torch
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
# Custom CSS for better styling
|
| 11 |
+
custom_css = """
|
| 12 |
+
.gradio-container {
|
| 13 |
+
max-width: 1200px !important;
|
| 14 |
+
}
|
| 15 |
+
.alert-box {
|
| 16 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 17 |
+
color: white;
|
| 18 |
+
padding: 20px;
|
| 19 |
+
border-radius: 10px;
|
| 20 |
+
margin: 10px 0;
|
| 21 |
+
}
|
| 22 |
+
.status-success {
|
| 23 |
+
background: #d4edda;
|
| 24 |
+
border: 1px solid #c3e6cb;
|
| 25 |
+
color: #155724;
|
| 26 |
+
padding: 10px;
|
| 27 |
+
border-radius: 5px;
|
| 28 |
+
}
|
| 29 |
+
.status-warning {
|
| 30 |
+
background: #fff3cd;
|
| 31 |
+
border: 1px solid #ffeaa7;
|
| 32 |
+
color: #856404;
|
| 33 |
+
padding: 10px;
|
| 34 |
+
border-radius: 5px;
|
| 35 |
+
}
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
# Initialize the LLM pipeline with zeroGPU support
|
| 39 |
+
@spaces.GPU
|
| 40 |
+
def initialize_llm():
|
| 41 |
+
try:
|
| 42 |
+
# Check GPU availability
|
| 43 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 44 |
+
print(f"Using device: {device}")
|
| 45 |
+
|
| 46 |
+
# Try to use a larger model with GPU acceleration
|
| 47 |
+
model_id = "microsoft/DialoGPT-medium"
|
| 48 |
+
pipe = pipeline(
|
| 49 |
+
"text-generation",
|
| 50 |
+
model=model_id,
|
| 51 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 52 |
+
device_map="auto" if device == "cuda" else "cpu",
|
| 53 |
+
max_length=512,
|
| 54 |
+
pad_token_id=50256
|
| 55 |
+
)
|
| 56 |
+
return pipe, f"✅ LLM Model loaded on {device}: {model_id}"
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return None, f"⚠️ LLM not available: {str(e)[:100]}... Using fallback analysis."
|
| 59 |
+
|
| 60 |
+
pipe, model_status = initialize_llm()
|
| 61 |
+
|
| 62 |
+
# Enhanced attack scenarios with more realistic data
|
| 63 |
+
ATTACK_SCENARIOS = {
|
| 64 |
+
"🔄 Lateral Movement": {
|
| 65 |
+
"description": "Advanced Persistent Threat (APT) - Attacker moving laterally through network after initial compromise",
|
| 66 |
+
"severity": "Critical",
|
| 67 |
+
"alerts": [
|
| 68 |
+
{
|
| 69 |
+
"id": "ALR-001",
|
| 70 |
+
"timestamp": "2025-01-15 14:30:45",
|
| 71 |
+
"source_ip": "192.168.1.100",
|
| 72 |
+
"destination_ip": "192.168.1.25",
|
| 73 |
+
"user": "corp\\john.doe",
|
| 74 |
+
"alert_type": "Suspicious Process Execution",
|
| 75 |
+
"severity": "High",
|
| 76 |
+
"description": "Unusual PowerShell execution with encoded commands detected",
|
| 77 |
+
"raw_log": "Process: powershell.exe -WindowStyle Hidden -enc ZXhlYyBjYWxjLmV4ZQ== Parent: winword.exe",
|
| 78 |
+
"threat_intel": "Base64 encoded PowerShell commonly used by APT29 (Cozy Bear) for initial access",
|
| 79 |
+
"mitre_tactic": "T1059.001 - PowerShell",
|
| 80 |
+
"confidence": 85
|
| 81 |
+
},
|
| 82 |
+
{
|
| 83 |
+
"id": "ALR-002",
|
| 84 |
+
"timestamp": "2025-01-15 14:35:12",
|
| 85 |
+
"source_ip": "192.168.1.100",
|
| 86 |
+
"destination_ip": "192.168.1.50",
|
| 87 |
+
"user": "corp\\john.doe",
|
| 88 |
+
"alert_type": "Credential Dumping Attempt",
|
| 89 |
+
"severity": "Critical",
|
| 90 |
+
"description": "LSASS memory access detected - possible credential harvesting",
|
| 91 |
+
"raw_log": "Process: rundll32.exe comsvcs.dll MiniDump [PID] lsass.dmp full",
|
| 92 |
+
"threat_intel": "LSASS dumping technique associated with credential theft operations",
|
| 93 |
+
"mitre_tactic": "T1003.001 - LSASS Memory",
|
| 94 |
+
"confidence": 92
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"id": "ALR-003",
|
| 98 |
+
"timestamp": "2025-01-15 14:42:18",
|
| 99 |
+
"source_ip": "192.168.1.100",
|
| 100 |
+
"destination_ip": "10.0.0.15",
|
| 101 |
+
"user": "SYSTEM",
|
| 102 |
+
"alert_type": "Abnormal Network Connection",
|
| 103 |
+
"severity": "Medium",
|
| 104 |
+
"description": "Connection to unusual internal subnet using stolen credentials",
|
| 105 |
+
"raw_log": "TCP connection established to 10.0.0.15:445 from 192.168.1.100:51234",
|
| 106 |
+
"threat_intel": "SMB connections to sensitive subnets often indicate lateral movement",
|
| 107 |
+
"mitre_tactic": "T1021.002 - SMB/Windows Admin Shares",
|
| 108 |
+
"confidence": 78
|
| 109 |
+
}
|
| 110 |
+
]
|
| 111 |
+
},
|
| 112 |
+
"📧 Phishing Campaign": {
|
| 113 |
+
"description": "Email-based social engineering attack leading to credential theft and data exfiltration",
|
| 114 |
+
"severity": "High",
|
| 115 |
+
"alerts": [
|
| 116 |
+
{
|
| 117 |
+
"id": "ALR-004",
|
| 118 |
+
"timestamp": "2025-01-15 09:15:30",
|
| 119 |
+
"source_ip": "203.0.113.50",
|
| 120 |
+
"destination_ip": "192.168.1.75",
|
| 121 |
+
"user": "corp\\sarah.wilson",
|
| 122 |
+
"alert_type": "Malicious Email Detected",
|
| 123 |
+
"severity": "High",
|
| 124 |
+
"description": "Suspicious email with credential harvesting link detected",
|
| 125 |
+
"raw_log": "From: [email protected] Subject: URGENT: Account Suspended - Verify Now",
|
| 126 |
+
"threat_intel": "Domain registered 48 hours ago, hosted on bulletproof hosting provider",
|
| 127 |
+
"mitre_tactic": "T1566.002 - Spearphishing Link",
|
| 128 |
+
"confidence": 88
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"id": "ALR-005",
|
| 132 |
+
"timestamp": "2025-01-15 09:45:22",
|
| 133 |
+
"source_ip": "192.168.1.75",
|
| 134 |
+
"destination_ip": "203.0.113.50",
|
| 135 |
+
"user": "corp\\sarah.wilson",
|
| 136 |
+
"alert_type": "Credential Submission",
|
| 137 |
+
"severity": "Critical",
|
| 138 |
+
"description": "User credentials submitted to suspicious external site",
|
| 139 |
+
"raw_log": "HTTPS POST to https://203.0.113.50/login.php - Credentials: username=sarah.wilson&password=[REDACTED]",
|
| 140 |
+
"threat_intel": "IP address hosting multiple phishing kits targeting financial institutions",
|
| 141 |
+
"mitre_tactic": "T1056.003 - Web Portal Capture",
|
| 142 |
+
"confidence": 95
|
| 143 |
+
}
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
"🔒 Ransomware Attack": {
|
| 147 |
+
"description": "File encryption attack with ransom demand - likely REvil/Sodinokibi variant",
|
| 148 |
+
"severity": "Critical",
|
| 149 |
+
"alerts": [
|
| 150 |
+
{
|
| 151 |
+
"id": "ALR-006",
|
| 152 |
+
"timestamp": "2025-01-15 16:20:10",
|
| 153 |
+
"source_ip": "192.168.1.85",
|
| 154 |
+
"destination_ip": "192.168.1.85",
|
| 155 |
+
"user": "corp\\admin.backup",
|
| 156 |
+
"alert_type": "Mass File Encryption",
|
| 157 |
+
"severity": "Critical",
|
| 158 |
+
"description": "Rapid file modifications detected across multiple directories",
|
| 159 |
+
"raw_log": "Files encrypted: 1,247 in C:\\Users\\Documents\\ Extensions changed to: .locked2025",
|
| 160 |
+
"threat_intel": "Encryption pattern and extension match REvil ransomware family signatures",
|
| 161 |
+
"mitre_tactic": "T1486 - Data Encrypted for Impact",
|
| 162 |
+
"confidence": 97
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"id": "ALR-007",
|
| 166 |
+
"timestamp": "2025-01-15 16:25:33",
|
| 167 |
+
"source_ip": "192.168.1.85",
|
| 168 |
+
"destination_ip": "45.33.22.11",
|
| 169 |
+
"user": "SYSTEM",
|
| 170 |
+
"alert_type": "Command and Control Communication",
|
| 171 |
+
"severity": "High",
|
| 172 |
+
"description": "Encrypted communication to known ransomware C2 infrastructure",
|
| 173 |
+
"raw_log": "TLS 1.3 connection established to 45.33.22.11:8443 - Data exchanged: 2.3KB",
|
| 174 |
+
"threat_intel": "IP address previously associated with REvil ransomware C2 operations",
|
| 175 |
+
"mitre_tactic": "T1071.001 - Web Protocols",
|
| 176 |
+
"confidence": 91
|
| 177 |
+
}
|
| 178 |
+
]
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
@spaces.GPU
|
| 183 |
+
def generate_advanced_llm_analysis(alert_data, analyst_level):
|
| 184 |
+
"""Generate comprehensive LLM-based analysis with enhanced prompting and GPU acceleration"""
|
| 185 |
+
|
| 186 |
+
# Enhanced context with more structured prompting
|
| 187 |
+
system_context = f"""You are an expert cybersecurity analyst assistant specializing in SOC operations.
|
| 188 |
+
Analyze the following security alert for a Level {analyst_level} analyst.
|
| 189 |
+
|
| 190 |
+
ALERT CONTEXT:
|
| 191 |
+
ID: {alert_data['id']}
|
| 192 |
+
Type: {alert_data['alert_type']}
|
| 193 |
+
Severity: {alert_data['severity']}
|
| 194 |
+
Timestamp: {alert_data['timestamp']}
|
| 195 |
+
Network: {alert_data['source_ip']} → {alert_data['destination_ip']}
|
| 196 |
+
User: {alert_data['user']}
|
| 197 |
+
Description: {alert_data['description']}
|
| 198 |
+
Technical Details: {alert_data['raw_log']}
|
| 199 |
+
Threat Intelligence: {alert_data['threat_intel']}
|
| 200 |
+
MITRE ATT&CK: {alert_data['mitre_tactic']}
|
| 201 |
+
Confidence: {alert_data['confidence']}%
|
| 202 |
+
|
| 203 |
+
Provide analysis appropriate for {analyst_level} level:"""
|
| 204 |
+
|
| 205 |
+
if pipe:
|
| 206 |
+
try:
|
| 207 |
+
# Use GPU acceleration for faster inference
|
| 208 |
+
device = next(pipe.model.parameters()).device
|
| 209 |
+
print(f"LLM running on device: {device}")
|
| 210 |
+
|
| 211 |
+
prompt = f"{system_context}\n\nAnalysis:"
|
| 212 |
+
response = pipe(
|
| 213 |
+
prompt,
|
| 214 |
+
max_new_tokens=300,
|
| 215 |
+
do_sample=True,
|
| 216 |
+
temperature=0.7,
|
| 217 |
+
top_p=0.9,
|
| 218 |
+
pad_token_id=pipe.tokenizer.eos_token_id
|
| 219 |
+
)
|
| 220 |
+
generated_text = response[0]['generated_text']
|
| 221 |
+
analysis = generated_text[len(prompt):].strip()
|
| 222 |
+
return analysis if analysis else get_fallback_analysis(alert_data, analyst_level)
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"LLM Error: {e}")
|
| 225 |
+
return f"LLM Processing Error: {str(e)}\n\n{get_fallback_analysis(alert_data, analyst_level)}"
|
| 226 |
+
|
| 227 |
+
return get_fallback_analysis(alert_data, analyst_level)
|
| 228 |
+
|
| 229 |
+
def get_fallback_analysis(alert_data, analyst_level):
|
| 230 |
+
"""Enhanced fallback analysis with detailed recommendations"""
|
| 231 |
+
|
| 232 |
+
base_analysis = {
|
| 233 |
+
"L1": {
|
| 234 |
+
"icon": "🚨",
|
| 235 |
+
"title": "L1 TRIAGE ANALYSIS",
|
| 236 |
+
"focus": "Initial Assessment & Escalation",
|
| 237 |
+
"template": """
|
| 238 |
+
{icon} {title}
|
| 239 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━��━━━━━━━━━━━━━━━━━━━━
|
| 240 |
+
🎯 THREAT SUMMARY: {alert_type} - {severity} severity
|
| 241 |
+
⏰ OCCURRED: {timestamp}
|
| 242 |
+
🌐 AFFECTED SYSTEM: {source_ip} (User: {user})
|
| 243 |
+
🔍 CONFIDENCE LEVEL: {confidence}%
|
| 244 |
+
|
| 245 |
+
🚀 IMMEDIATE ACTIONS:
|
| 246 |
+
• Isolate affected system: {source_ip}
|
| 247 |
+
• Verify user account status: {user}
|
| 248 |
+
• Check for similar alerts in timeframe
|
| 249 |
+
• Document incident ID: {id}
|
| 250 |
+
|
| 251 |
+
⬆️ ESCALATION CRITERIA:
|
| 252 |
+
• Severity: {severity} - Meets L2 escalation threshold
|
| 253 |
+
• MITRE Tactic: {mitre_tactic}
|
| 254 |
+
• Recommend immediate L2 review
|
| 255 |
+
|
| 256 |
+
📋 INITIAL NOTES:
|
| 257 |
+
{threat_intel}
|
| 258 |
+
"""
|
| 259 |
+
},
|
| 260 |
+
"L2": {
|
| 261 |
+
"icon": "🔍",
|
| 262 |
+
"title": "L2 INVESTIGATION ANALYSIS",
|
| 263 |
+
"focus": "Detailed Investigation & Correlation",
|
| 264 |
+
"template": """
|
| 265 |
+
{icon} {title}
|
| 266 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
| 267 |
+
🎯 ATTACK VECTOR: {description}
|
| 268 |
+
⚙️ TECHNICAL DETAILS: {raw_log}
|
| 269 |
+
🧠 THREAT CONTEXT: {threat_intel}
|
| 270 |
+
🎪 MITRE ATT&CK: {mitre_tactic}
|
| 271 |
+
|
| 272 |
+
🔬 INVESTIGATION STEPS:
|
| 273 |
+
1. Examine parent process tree for {source_ip}
|
| 274 |
+
2. Correlate network connections in ±30min window
|
| 275 |
+
3. Review authentication logs for user: {user}
|
| 276 |
+
4. Check for indicators across environment
|
| 277 |
+
5. Analyze file system changes (if applicable)
|
| 278 |
+
|
| 279 |
+
🎯 CORRELATION POINTS:
|
| 280 |
+
• Source IP timeline analysis
|
| 281 |
+
• User behavior baseline comparison
|
| 282 |
+
• Similar TTPs in recent incidents
|
| 283 |
+
• Network segmentation verification
|
| 284 |
+
|
| 285 |
+
📊 RISK ASSESSMENT:
|
| 286 |
+
• Technical Impact: {severity}
|
| 287 |
+
• Business Risk: Review asset criticality
|
| 288 |
+
• Containment Priority: High (based on {confidence}% confidence)
|
| 289 |
+
|
| 290 |
+
⬆️ L3 ESCALATION IF:
|
| 291 |
+
• Attack campaign indicators found
|
| 292 |
+
• Critical asset involvement confirmed
|
| 293 |
+
• Advanced persistent threat suspected
|
| 294 |
+
"""
|
| 295 |
+
},
|
| 296 |
+
"L3": {
|
| 297 |
+
"icon": "🎯",
|
| 298 |
+
"title": "L3 EXPERT ANALYSIS",
|
| 299 |
+
"focus": "Attribution & Strategic Response",
|
| 300 |
+
"template": """
|
| 301 |
+
{icon} {title}
|
| 302 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
| 303 |
+
🎭 ADVERSARY PROFILE: Advanced threat actor
|
| 304 |
+
🎪 CAMPAIGN ANALYSIS: {threat_intel}
|
| 305 |
+
💼 BUSINESS IMPACT: {severity} - Requires C-level awareness
|
| 306 |
+
🛡️ DEFENSIVE POSTURE: Enhanced monitoring required
|
| 307 |
+
|
| 308 |
+
🕵️ THREAT HUNTING PRIORITIES:
|
| 309 |
+
1. Memory forensics on {source_ip}
|
| 310 |
+
2. Network traffic deep packet inspection
|
| 311 |
+
3. Endpoint artifact preservation
|
| 312 |
+
4. Active Directory security log analysis
|
| 313 |
+
5. Cloud infrastructure review (if applicable)
|
| 314 |
+
|
| 315 |
+
🎯 ATTRIBUTION INDICATORS:
|
| 316 |
+
• TTPs match: {mitre_tactic}
|
| 317 |
+
• Technical sophistication: High
|
| 318 |
+
• Targeting pattern: [Analyze organizational profile]
|
| 319 |
+
• Infrastructure overlap: Review IOC databases
|
| 320 |
+
|
| 321 |
+
🛠️ MITIGATION STRATEGY:
|
| 322 |
+
• Immediate: Block C2 communications
|
| 323 |
+
• Short-term: Deploy hunting queries
|
| 324 |
+
• Medium-term: Security architecture review
|
| 325 |
+
• Long-term: Staff training and awareness
|
| 326 |
+
|
| 327 |
+
📈 EXECUTIVE BRIEFING POINTS:
|
| 328 |
+
• Sophisticated attack requiring coordinated response
|
| 329 |
+
• Potential for lateral movement and data exfiltration
|
| 330 |
+
• Recommend incident response team activation
|
| 331 |
+
• Consider external forensics support
|
| 332 |
+
|
| 333 |
+
🔮 PREDICTIVE ANALYSIS:
|
| 334 |
+
• High probability of follow-up attacks
|
| 335 |
+
• Recommend 48-72 hour enhanced monitoring
|
| 336 |
+
• Consider threat landscape implications
|
| 337 |
+
"""
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
if analyst_level in base_analysis:
|
| 342 |
+
template = base_analysis[analyst_level]["template"]
|
| 343 |
+
return template.format(
|
| 344 |
+
icon=base_analysis[analyst_level]["icon"],
|
| 345 |
+
title=base_analysis[analyst_level]["title"],
|
| 346 |
+
**alert_data
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
return "Analysis not available for specified level."
|
| 350 |
+
|
| 351 |
+
def analyze_alert_comprehensive(scenario_name, alert_index, analyst_level):
|
| 352 |
+
"""Enhanced main analysis function with timing and status updates"""
|
| 353 |
+
start_time = time.time()
|
| 354 |
+
|
| 355 |
+
# Validate inputs
|
| 356 |
+
if scenario_name not in ATTACK_SCENARIOS:
|
| 357 |
+
return "❌ Invalid scenario selected.", "", "Error: Invalid scenario"
|
| 358 |
+
|
| 359 |
+
scenario = ATTACK_SCENARIOS[scenario_name]
|
| 360 |
+
alerts = scenario["alerts"]
|
| 361 |
+
|
| 362 |
+
if alert_index >= len(alerts):
|
| 363 |
+
return "❌ Invalid alert index.", "", "Error: Invalid alert index"
|
| 364 |
+
|
| 365 |
+
selected_alert = alerts[alert_index]
|
| 366 |
+
|
| 367 |
+
# Generate comprehensive analysis
|
| 368 |
+
analysis = generate_advanced_llm_analysis(selected_alert, analyst_level)
|
| 369 |
+
|
| 370 |
+
# Enhanced alert details formatting
|
| 371 |
+
alert_details = f"""
|
| 372 |
+
🎫 ALERT ID: {selected_alert['id']} | 🕐 {selected_alert['timestamp']}
|
| 373 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
| 374 |
+
|
| 375 |
+
🌐 NETWORK FLOW:
|
| 376 |
+
Source: {selected_alert['source_ip']} → Destination: {selected_alert['destination_ip']}
|
| 377 |
+
|
| 378 |
+
👤 USER CONTEXT:
|
| 379 |
+
Account: {selected_alert['user']}
|
| 380 |
+
|
| 381 |
+
⚠️ ALERT CLASSIFICATION:
|
| 382 |
+
Type: {selected_alert['alert_type']}
|
| 383 |
+
Severity: {selected_alert['severity']}
|
| 384 |
+
Confidence: {selected_alert['confidence']}%
|
| 385 |
+
|
| 386 |
+
📝 DESCRIPTION:
|
| 387 |
+
{selected_alert['description']}
|
| 388 |
+
|
| 389 |
+
🔍 TECHNICAL EVIDENCE:
|
| 390 |
+
{selected_alert['raw_log']}
|
| 391 |
+
|
| 392 |
+
🧠 THREAT INTELLIGENCE:
|
| 393 |
+
{selected_alert['threat_intel']}
|
| 394 |
+
|
| 395 |
+
🎪 MITRE ATT&CK MAPPING:
|
| 396 |
+
{selected_alert['mitre_tactic']}
|
| 397 |
+
|
| 398 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
| 399 |
+
"""
|
| 400 |
+
|
| 401 |
+
processing_time = round(time.time() - start_time, 2)
|
| 402 |
+
status_message = f"✅ {analyst_level} analysis completed in {processing_time}s | Model: {model_status}"
|
| 403 |
+
|
| 404 |
+
return alert_details, analysis, status_message
|
| 405 |
+
|
| 406 |
+
def get_enhanced_scenario_info(scenario_name):
|
| 407 |
+
"""Enhanced scenario information with threat overview"""
|
| 408 |
+
if scenario_name in ATTACK_SCENARIOS:
|
| 409 |
+
scenario = ATTACK_SCENARIOS[scenario_name]
|
| 410 |
+
|
| 411 |
+
info = f"""
|
| 412 |
+
## 🎭 **Attack Scenario: {scenario_name}**
|
| 413 |
+
|
| 414 |
+
**📋 Description:** {scenario['description']}
|
| 415 |
+
**⚠️ Severity Level:** {scenario['severity']}
|
| 416 |
+
**📊 Total Alerts:** {len(scenario['alerts'])} security events detected
|
| 417 |
+
|
| 418 |
+
### 🔍 **Alert Timeline:**
|
| 419 |
+
"""
|
| 420 |
+
|
| 421 |
+
for i, alert in enumerate(scenario['alerts']):
|
| 422 |
+
info += f"""
|
| 423 |
+
**[{i+1}] {alert['timestamp']}** - {alert['alert_type']}
|
| 424 |
+
└─ Severity: {alert['severity']} | Confidence: {alert['confidence']}%
|
| 425 |
+
"""
|
| 426 |
+
|
| 427 |
+
info += f"""
|
| 428 |
+
### 🎯 **Analysis Capabilities:**
|
| 429 |
+
- **L1 Triage:** Initial assessment and escalation decisions
|
| 430 |
+
- **L2 Investigation:** Detailed technical analysis and correlation
|
| 431 |
+
- **L3 Expert:** Attribution, impact assessment, and strategic response
|
| 432 |
+
"""
|
| 433 |
+
|
| 434 |
+
return info
|
| 435 |
+
return "⚠️ No scenario selected. Please choose an attack scenario to begin analysis."
|
| 436 |
+
|
| 437 |
+
# Create enhanced Gradio interface
|
| 438 |
+
with gr.Blocks(title="SOC LLM Assistant - Advanced PoC", theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 439 |
+
|
| 440 |
+
# Header
|
| 441 |
+
gr.Markdown("""
|
| 442 |
+
# 🛡️ SOC LLM Assistant - Advanced Proof of Concept
|
| 443 |
+
**Intelligent Security Alert Analysis for Multi-Level SOC Operations**
|
| 444 |
+
|
| 445 |
+
*Demonstrating LLM-powered assistance for L1, L2, and L3 security analysts*
|
| 446 |
+
""")
|
| 447 |
+
|
| 448 |
+
# Model status display
|
| 449 |
+
gr.Markdown(f"🤖 **System Status:** {model_status}")
|
| 450 |
+
|
| 451 |
+
with gr.Row():
|
| 452 |
+
# Left Panel - Controls
|
| 453 |
+
with gr.Column(scale=1, min_width=300):
|
| 454 |
+
gr.Markdown("## 🎮 Attack Simulation Control")
|
| 455 |
+
|
| 456 |
+
scenario_dropdown = gr.Dropdown(
|
| 457 |
+
choices=list(ATTACK_SCENARIOS.keys()),
|
| 458 |
+
label="🎭 Select Attack Scenario",
|
| 459 |
+
value="🔄 Lateral Movement",
|
| 460 |
+
interactive=True
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
scenario_info = gr.Markdown()
|
| 464 |
+
|
| 465 |
+
gr.Markdown("---")
|
| 466 |
+
gr.Markdown("## ⚙️ Analysis Configuration")
|
| 467 |
+
|
| 468 |
+
alert_slider = gr.Slider(
|
| 469 |
+
minimum=0,
|
| 470 |
+
maximum=2,
|
| 471 |
+
step=1,
|
| 472 |
+
value=0,
|
| 473 |
+
label="📋 Alert Selection",
|
| 474 |
+
info="Choose which alert from the scenario to analyze"
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
analyst_level = gr.Radio(
|
| 478 |
+
choices=["L1", "L2", "L3"],
|
| 479 |
+
label="👤 Analyst Level",
|
| 480 |
+
value="L2",
|
| 481 |
+
info="L1: Triage | L2: Investigation | L3: Expert Analysis"
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
analyze_btn = gr.Button(
|
| 485 |
+
"🔍 Analyze Alert",
|
| 486 |
+
variant="primary",
|
| 487 |
+
size="lg"
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
gr.Markdown("---")
|
| 491 |
+
gr.Markdown("## 📊 Quick Stats")
|
| 492 |
+
gr.Markdown("""
|
| 493 |
+
**🎯 Demo Features:**
|
| 494 |
+
- 3 realistic attack scenarios
|
| 495 |
+
- Multi-level analysis (L1/L2/L3)
|
| 496 |
+
- MITRE ATT&CK mapping
|
| 497 |
+
- Threat intelligence integration
|
| 498 |
+
- Real-time LLM processing
|
| 499 |
+
""")
|
| 500 |
+
|
| 501 |
+
# Right Panel - Results
|
| 502 |
+
with gr.Column(scale=2):
|
| 503 |
+
gr.Markdown("## 📋 Security Alert Details")
|
| 504 |
+
alert_output = gr.Textbox(
|
| 505 |
+
label="🎫 Raw Alert Information",
|
| 506 |
+
lines=15,
|
| 507 |
+
interactive=False,
|
| 508 |
+
placeholder="Alert details will appear here after analysis..."
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
gr.Markdown("## 🤖 AI-Powered Analysis")
|
| 512 |
+
analysis_output = gr.Textbox(
|
| 513 |
+
label="🧠 Intelligent Analysis & Recommendations",
|
| 514 |
+
lines=20,
|
| 515 |
+
interactive=False,
|
| 516 |
+
placeholder="LLM analysis will appear here after processing..."
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
status_output = gr.Textbox(
|
| 520 |
+
label="📊 Processing Status",
|
| 521 |
+
interactive=False,
|
| 522 |
+
lines=1
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
# Footer information
|
| 526 |
+
gr.Markdown("""
|
| 527 |
+
---
|
| 528 |
+
## 📖 **Usage Instructions:**
|
| 529 |
+
|
| 530 |
+
1. **📊 Select Scenario:** Choose from realistic cybersecurity attack scenarios
|
| 531 |
+
2. **🎯 Pick Alert:** Use the slider to select which alert in the sequence to analyze
|
| 532 |
+
3. **👤 Choose Level:** Select analyst expertise level (L1/L2/L3) for tailored analysis
|
| 533 |
+
4. **🔍 Analyze:** Click the analyze button to get AI-powered insights and recommendations
|
| 534 |
+
|
| 535 |
+
## 🎯 **Key Capabilities Demonstrated:**
|
| 536 |
+
|
| 537 |
+
- **🎭 Realistic Scenarios:** Based on actual cybersecurity incidents and attack patterns
|
| 538 |
+
- **🧠 Contextual Analysis:** LLM considers all available metadata, threat intelligence, and historical patterns
|
| 539 |
+
- **👥 Role-Based Insights:** Tailored recommendations for different SOC analyst skill levels
|
| 540 |
+
- **⚡ Real-Time Processing:** Immediate analysis with actionable next steps
|
| 541 |
+
- **🎪 Industry Standards:** MITRE ATT&CK framework integration for standardized threat classification
|
| 542 |
+
|
| 543 |
+
## 🔬 **Research Value:**
|
| 544 |
+
This PoC demonstrates the feasibility of LLM integration in operational security environments, supporting research in automated threat analysis, human-AI collaboration, and intelligent SOC operations.
|
| 545 |
+
|
| 546 |
+
---
|
| 547 |
+
**👨🎓 Developed by:** Abdullah Alanazi | **🏛️ Institution:** KAUST | **👨🏫 Supervisor:** Prof. Ali Shoker
|
| 548 |
+
""")
|
| 549 |
+
|
| 550 |
+
# Event handlers with enhanced functionality
|
| 551 |
+
scenario_dropdown.change(
|
| 552 |
+
fn=get_enhanced_scenario_info,
|
| 553 |
+
inputs=[scenario_dropdown],
|
| 554 |
+
outputs=[scenario_info]
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
# Update slider maximum based on scenario
|
| 558 |
+
def update_slider_max(scenario_name):
|
| 559 |
+
if scenario_name in ATTACK_SCENARIOS:
|
| 560 |
+
max_alerts = len(ATTACK_SCENARIOS[scenario_name]["alerts"]) - 1
|
| 561 |
+
return gr.Slider(maximum=max_alerts, value=0)
|
| 562 |
+
return gr.Slider(maximum=2, value=0)
|
| 563 |
+
|
| 564 |
+
scenario_dropdown.change(
|
| 565 |
+
fn=update_slider_max,
|
| 566 |
+
inputs=[scenario_dropdown],
|
| 567 |
+
outputs=[alert_slider]
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
analyze_btn.click(
|
| 571 |
+
fn=analyze_alert_comprehensive,
|
| 572 |
+
inputs=[scenario_dropdown, alert_slider, analyst_level],
|
| 573 |
+
outputs=[alert_output, analysis_output, status_output]
|
| 574 |
+
)
|
| 575 |
+
|
| 576 |
+
# Initialize with default scenario
|
| 577 |
+
demo.load(
|
| 578 |
+
fn=get_enhanced_scenario_info,
|
| 579 |
+
inputs=[scenario_dropdown],
|
| 580 |
+
outputs=[scenario_info]
|
| 581 |
+
)
|
| 582 |
+
|
| 583 |
+
# Launch configuration
|
| 584 |
+
if __name__ == "__main__":
|
| 585 |
+
demo.launch(
|
| 586 |
+
share=True,
|
| 587 |
+
server_name="0.0.0.0",
|
| 588 |
+
server_port=7860,
|
| 589 |
+
show_error=True
|
| 590 |
+
)
|